summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>2022-01-21 18:59:26 +0100
committerGitHub <noreply@github.com>2022-01-21 18:59:26 +0100
commit1f40933d38b58b60938a77cbea1c847fc8f8c3e5 (patch)
treef817b372123ab31dbbfa54a39087b6e5560eee8e /models
parent108f1aab5cc4e667288a507acadb53a7b6ea4dbe (diff)
downloadgitea-1f40933d38b58b60938a77cbea1c847fc8f8c3e5.tar.gz
gitea-1f40933d38b58b60938a77cbea1c847fc8f8c3e5.zip
Add config options to hide issue events (#17414)
* Add config option to hide issue events Adds a config option `HIDE_ISSUE_EVENTS` to hide most issue events (changed labels, milestones, projects...) on the issue detail page. If this is true, only the following events (comment types) are shown: * plain comments * closed/reopned/merged * reviews * Make configurable using a list * Add docs * Add missing newline * Fix merge issues * Allow changes per user settings * Fix lint * Rm old docs * Apply suggestions from code review * Use bitsets * Rm comment * fmt * Fix lint * Use variable/constant to provide key * fmt * fix lint * refactor * Add a prefix for user setting key * Add license comment * Add license comment * Update services/forms/user_form_hidden_comments.go Co-authored-by: Gusted <williamzijl7@hotmail.com> * check len == 0 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'models')
-rw-r--r--models/issue_comment.go6
-rw-r--r--models/user/setting.go54
-rw-r--r--models/user/setting_keys.go10
-rw-r--r--models/user/setting_test.go18
4 files changed, 69 insertions, 19 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go
index bf36881a00..34541dc3cd 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -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
@@ -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
}
@@ -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,
}
diff --git a/models/user/setting.go b/models/user/setting.go
index 5ff18f8265..fbb6fbab30 100644
--- a/models/user/setting.go
+++ b/models/user/setting.go
@@ -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).
@@ -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)
diff --git a/models/user/setting_keys.go b/models/user/setting_keys.go
new file mode 100644
index 0000000000..458b78e357
--- /dev/null
+++ b/models/user/setting_keys.go
@@ -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"
+)
diff --git a/models/user/setting_test.go b/models/user/setting_test.go
index 81445a9f6f..0b42e0fe2a 100644
--- a/models/user/setting_test.go
+++ b/models/user/setting_test.go
@@ -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
@@ -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)