Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaoffical/main'
Browse files Browse the repository at this point in the history
* giteaoffical/main:
  [skip ci] Updated translations via Crowdin
  Add config options to hide issue events (go-gitea#17414)
  Add js vendor directory to .gitattributes (go-gitea#18350)
  • Loading branch information
zjjhot committed Jan 22, 2022
2 parents 0e80795 + f910924 commit 66a56f9
Show file tree
Hide file tree
Showing 19 changed files with 1,161 additions and 1,573 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* text=auto eol=lf
/vendor/** -text -eol linguist-vendored
/public/vendor/** -text -eol linguist-vendored
/web_src/js/vendor/** -text -eol linguist-vendored
/templates/**/*.tmpl linguist-language=Handlebars
/.eslintrc linguist-language=YAML
/.stylelintrc linguist-language=YAML
Expand Down
6 changes: 3 additions & 3 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const (
// 28 merge pull request
CommentTypeMergePull
// 29 push to PR head branch
CommentTypePullPush
CommentTypePullRequestPush
// 30 Project changed
CommentTypeProject
// 31 Project board changed
Expand Down Expand Up @@ -725,7 +725,7 @@ func (c *Comment) CodeCommentURL() string {

// LoadPushCommits Load push commits
func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
if c.Content == "" || c.Commits != nil || c.Type != CommentTypePullPush {
if c.Content == "" || c.Commits != nil || c.Type != CommentTypePullRequestPush {
return nil
}

Expand Down Expand Up @@ -1325,7 +1325,7 @@ func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *Pul
}

ops := &CreateCommentOptions{
Type: CommentTypePullPush,
Type: CommentTypePullRequestPush,
Doer: pusher,
Repo: pr.BaseRepo,
}
Expand Down
54 changes: 43 additions & 11 deletions models/user/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func init() {
db.RegisterModel(new(Setting))
}

// GetSettings returns specific settings from user
func GetSettings(uid int64, keys []string) (map[string]*Setting, error) {
// GetUserSettings returns specific settings from user
func GetUserSettings(uid int64, keys []string) (map[string]*Setting, error) {
settings := make([]*Setting, 0, len(keys))
if err := db.GetEngine(db.DefaultContext).
Where("user_id=?", uid).
Expand Down Expand Up @@ -62,21 +62,53 @@ func GetUserAllSettings(uid int64) (map[string]*Setting, error) {
return settingsMap, nil
}

// DeleteSetting deletes a specific setting for a user
func DeleteSetting(setting *Setting) error {
_, err := db.GetEngine(db.DefaultContext).Delete(setting)
func validateUserSettingKey(key string) error {
if len(key) == 0 {
return fmt.Errorf("setting key must be set")
}
if strings.ToLower(key) != key {
return fmt.Errorf("setting key should be lowercase")
}
return nil
}

// GetUserSetting gets a specific setting for a user
func GetUserSetting(userID int64, key string, def ...string) (string, error) {
if err := validateUserSettingKey(key); err != nil {
return "", err
}
setting := &Setting{UserID: userID, SettingKey: key}
has, err := db.GetEngine(db.DefaultContext).Get(setting)
if err != nil {
return "", err
}
if !has {
if len(def) == 1 {
return def[0], nil
}
return "", nil
}
return setting.SettingValue, nil
}

// DeleteUserSetting deletes a specific setting for a user
func DeleteUserSetting(userID int64, key string) error {
if err := validateUserSettingKey(key); err != nil {
return err
}
_, err := db.GetEngine(db.DefaultContext).Delete(&Setting{UserID: userID, SettingKey: key})
return err
}

// SetSetting updates a users' setting for a specific key
func SetSetting(setting *Setting) error {
if strings.ToLower(setting.SettingKey) != setting.SettingKey {
return fmt.Errorf("setting key should be lowercase")
// SetUserSetting updates a users' setting for a specific key
func SetUserSetting(userID int64, key, value string) error {
if err := validateUserSettingKey(key); err != nil {
return err
}
return upsertSettingValue(setting.UserID, setting.SettingKey, setting.SettingValue)
return upsertUserSettingValue(userID, key, value)
}

func upsertSettingValue(userID int64, key, value string) error {
func upsertUserSettingValue(userID int64, key, value string) error {
return db.WithTx(func(ctx context.Context) error {
e := db.GetEngine(ctx)

Expand Down
10 changes: 10 additions & 0 deletions models/user/setting_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package user

const (
// SettingsKeyHiddenCommentTypes is the settings key for hidden comment types
SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types"
)
18 changes: 13 additions & 5 deletions models/user/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@ func TestSettings(t *testing.T) {
newSetting := &Setting{UserID: 99, SettingKey: keyName, SettingValue: "Gitea User Setting Test"}

// create setting
err := SetSetting(newSetting)
err := SetUserSetting(newSetting.UserID, newSetting.SettingKey, newSetting.SettingValue)
assert.NoError(t, err)
// test about saving unchanged values
err = SetSetting(newSetting)
err = SetUserSetting(newSetting.UserID, newSetting.SettingKey, newSetting.SettingValue)
assert.NoError(t, err)

// get specific setting
settings, err := GetSettings(99, []string{keyName})
settings, err := GetUserSettings(99, []string{keyName})
assert.NoError(t, err)
assert.Len(t, settings, 1)
assert.EqualValues(t, newSetting.SettingValue, settings[keyName].SettingValue)

settingValue, err := GetUserSetting(99, keyName)
assert.NoError(t, err)
assert.EqualValues(t, newSetting.SettingValue, settingValue)

settingValue, err = GetUserSetting(99, "no_such")
assert.NoError(t, err)
assert.EqualValues(t, "", settingValue)

// updated setting
updatedSetting := &Setting{UserID: 99, SettingKey: keyName, SettingValue: "Updated"}
err = SetSetting(updatedSetting)
err = SetUserSetting(updatedSetting.UserID, updatedSetting.SettingKey, updatedSetting.SettingValue)
assert.NoError(t, err)

// get all settings
Expand All @@ -43,7 +51,7 @@ func TestSettings(t *testing.T) {
assert.EqualValues(t, updatedSetting.SettingValue, settings[updatedSetting.SettingKey].SettingValue)

// delete setting
err = DeleteSetting(&Setting{UserID: 99, SettingKey: keyName})
err = DeleteUserSetting(99, keyName)
assert.NoError(t, err)
settings, err = GetUserAllSettings(99)
assert.NoError(t, err)
Expand Down
10 changes: 7 additions & 3 deletions modules/context/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ func (ctx *Context) FormInt64(key string) int64 {
return v
}

// FormBool returns true if the value for the provided key in the form is "1" or "true"
// FormBool returns true if the value for the provided key in the form is "1", "true" or "on"
func (ctx *Context) FormBool(key string) bool {
v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
s := ctx.Req.FormValue(key)
v, _ := strconv.ParseBool(s)
v = v || strings.EqualFold(s, "on")
return v
}

Expand All @@ -59,6 +61,8 @@ func (ctx *Context) FormOptionalBool(key string) util.OptionalBool {
if len(value) == 0 {
return util.OptionalBoolNone
}
v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
s := ctx.Req.FormValue(key)
v, _ := strconv.ParseBool(s)
v = v || strings.EqualFold(s, "on")
return util.OptionalBoolOf(v)
}
2 changes: 1 addition & 1 deletion modules/notification/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *rep
act = models.ActionCommentIssue
} else if comment.Type == models.CommentTypeCode {
act = models.ActionCommentIssue
} else if comment.Type == models.CommentTypePullPush {
} else if comment.Type == models.CommentTypePullRequestPush {
act = 0
}

Expand Down
16 changes: 16 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ continue = Continue
cancel = Cancel
language = Language
ui = Theme
hidden_comment_types = Hidden comment types
comment_type_group_reference = Reference
comment_type_group_label = Label
comment_type_group_milestone = Milestone
comment_type_group_assignee = Assignee
comment_type_group_title = Title
comment_type_group_branch = Branch
comment_type_group_time_tracking = Time Tracking
comment_type_group_deadline = Deadline
comment_type_group_dependency = Dependency
comment_type_group_lock = Lock Status
comment_type_group_review_request = Review request
comment_type_group_pull_request_push = Added commits
comment_type_group_project = Project
comment_type_group_issue_ref = Issue reference
saved_successfully = Your settings were saved successfully.
privacy = Privacy
keep_activity_private = Hide the activity from the profile page
keep_activity_private_popup = Makes the activity visible only for you and the admins
Expand Down
Loading

0 comments on commit 66a56f9

Please sign in to comment.