Skip to content

Commit

Permalink
Add config options to hide issue events (#17414)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
5 people committed Jan 21, 2022
1 parent 108f1aa commit 1f40933
Show file tree
Hide file tree
Showing 16 changed files with 1,086 additions and 769 deletions.
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
17 changes: 16 additions & 1 deletion routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -1465,7 +1466,7 @@ func ViewIssue(ctx *context.Context) {
ctx.ServerError("LoadResolveDoer", err)
return
}
} else if comment.Type == models.CommentTypePullPush {
} else if comment.Type == models.CommentTypePullRequestPush {
participants = addParticipant(comment.Poster, participants)
if err = comment.LoadPushCommits(ctx); err != nil {
ctx.ServerError("LoadPushCommits", err)
Expand Down Expand Up @@ -1650,6 +1651,20 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["IsRepoAdmin"] = ctx.IsSigned && (ctx.Repo.IsAdmin() || ctx.User.IsAdmin)
ctx.Data["LockReasons"] = setting.Repository.Issue.LockReasons
ctx.Data["RefEndName"] = git.RefEndName(issue.Ref)

var hiddenCommentTypes *big.Int
if ctx.IsSigned {
val, err := user_model.GetUserSetting(ctx.User.ID, user_model.SettingsKeyHiddenCommentTypes)
if err != nil {
ctx.ServerError("GetUserSetting", err)
return
}
hiddenCommentTypes, _ = new(big.Int).SetString(val, 10) // we can safely ignore the failed conversion here
}
ctx.Data["ShouldShowCommentType"] = func(commentType models.CommentType) bool {
return hiddenCommentTypes == nil || hiddenCommentTypes.Bit(int(commentType)) == 0
}

ctx.HTML(http.StatusOK, tplIssueView)
}

Expand Down
26 changes: 26 additions & 0 deletions routers/web/user/setting/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"math/big"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -358,6 +359,18 @@ func Appearance(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsAppearance"] = true

var hiddenCommentTypes *big.Int
val, err := user_model.GetUserSetting(ctx.User.ID, user_model.SettingsKeyHiddenCommentTypes)
if err != nil {
ctx.ServerError("GetUserSetting", err)
return
}
hiddenCommentTypes, _ = new(big.Int).SetString(val, 10) // we can safely ignore the failed conversion here

ctx.Data["IsCommentTypeGroupChecked"] = func(commentTypeGroup string) bool {
return forms.IsUserHiddenCommentTypeGroupChecked(commentTypeGroup, hiddenCommentTypes)
}

ctx.HTML(http.StatusOK, tplSettingsAppearance)
}

Expand Down Expand Up @@ -416,3 +429,16 @@ func UpdateUserLang(ctx *context.Context) {
ctx.Flash.Success(i18n.Tr(ctx.User.Language, "settings.update_language_success"))
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
}

// UpdateUserHiddenComments update a user's shown comment types
func UpdateUserHiddenComments(ctx *context.Context) {
err := user_model.SetUserSetting(ctx.User.ID, user_model.SettingsKeyHiddenCommentTypes, forms.UserHiddenCommentTypesFromRequest(ctx).String())
if err != nil {
ctx.ServerError("SetUserSetting", err)
return
}

log.Trace("User settings updated: %s", ctx.User.Name)
ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
}
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func RegisterRoutes(m *web.Route) {
m.Group("/appearance", func() {
m.Get("", user_setting.Appearance)
m.Post("/language", bindIgnErr(forms.UpdateLanguageForm{}), user_setting.UpdateUserLang)
m.Post("/hidden_comments", user_setting.UpdateUserHiddenComments)
m.Post("/theme", bindIgnErr(forms.UpdateThemeForm{}), user_setting.UpdateUIThemePost)
})
m.Group("/security", func() {
Expand Down
Loading

0 comments on commit 1f40933

Please sign in to comment.