Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config options to hide issue events #17414

Merged
merged 52 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
c31588d
Add config option to hide issue events
qwerty287 Oct 23, 2021
b2ac2a5
Make configurable using a list
qwerty287 Oct 24, 2021
e499016
Add docs
qwerty287 Oct 24, 2021
d14beda
Add missing newline
qwerty287 Oct 25, 2021
3d8abdc
Merge branch 'main' into hide-issue-events
qwerty287 Oct 25, 2021
7e94f54
Merge branch 'main' into hide-issue-events
qwerty287 Oct 26, 2021
141611e
Merge branch 'main' into hide-issue-events
qwerty287 Oct 28, 2021
c3f40ae
Merge branch 'main' into hide-issue-events
qwerty287 Oct 29, 2021
d17579d
Merge branch 'main' into hide-issue-events
qwerty287 Nov 1, 2021
26270f1
Merge branch 'main' into hide-issue-events
qwerty287 Nov 12, 2021
7eabacf
Fix merge issues
qwerty287 Nov 12, 2021
20e9fc9
Merge branch 'main' into hide-issue-events
qwerty287 Nov 15, 2021
3891745
Merge branch 'main' into hide-issue-events
qwerty287 Nov 16, 2021
8fa5fe4
Merge branch 'main' into hide-issue-events
qwerty287 Nov 21, 2021
2463c85
Merge branch 'main' into hide-issue-events
qwerty287 Nov 22, 2021
2f49cab
Allow changes per user settings
qwerty287 Nov 22, 2021
f0e6e02
Fix lint
qwerty287 Nov 22, 2021
395a714
Merge branch 'main' into hide-issue-events
qwerty287 Nov 23, 2021
20b6975
Merge branch 'main' into hide-issue-events
qwerty287 Nov 26, 2021
15a5ab6
Rm old docs
qwerty287 Nov 27, 2021
02ba951
Apply suggestions from code review
qwerty287 Nov 27, 2021
9afedcf
Merge branch 'main' into hide-issue-events
qwerty287 Nov 28, 2021
3cd7818
Merge branch 'main' into hide-issue-events
qwerty287 Nov 29, 2021
afc675c
Use bitsets
qwerty287 Dec 2, 2021
d2b84b5
Rm comment
qwerty287 Dec 2, 2021
e16472b
fmt
qwerty287 Dec 2, 2021
1e09a6d
Merge branch 'main' into hide-issue-events
qwerty287 Dec 2, 2021
3a76a14
Fix lint
qwerty287 Dec 2, 2021
2b22ebe
Merge branch 'main' into hide-issue-events
qwerty287 Dec 6, 2021
33276f9
Merge branch 'main' into hide-issue-events
qwerty287 Dec 8, 2021
debb758
Merge branch 'main' into hide-issue-events
qwerty287 Dec 12, 2021
ff70bdb
Use variable/constant to provide key
qwerty287 Dec 14, 2021
ec2c9de
Merge branch 'hide-issue-events' of github.com:qwerty287/gitea into h…
qwerty287 Dec 14, 2021
9f61e51
fmt
qwerty287 Dec 14, 2021
050af8b
fix lint
qwerty287 Dec 14, 2021
d5a02bc
Merge branch 'main' into hide-issue-events
qwerty287 Dec 21, 2021
b2a77c1
Merge branch 'main' into hide-issue-events
qwerty287 Dec 24, 2021
241def9
refactor
wxiaoguang Dec 25, 2021
7a48acd
Add a prefix for user setting key
wxiaoguang Dec 25, 2021
b134912
Merge pull request #2 from wxiaoguang/hide-issue-events
qwerty287 Dec 26, 2021
5a92210
Merge branch 'main' into hide-issue-events
qwerty287 Dec 26, 2021
e05fb4b
Add license comment
wxiaoguang Dec 27, 2021
110300e
Merge branch 'main' into hide-issue-events
wxiaoguang Dec 27, 2021
a9666b1
Add license comment
wxiaoguang Dec 27, 2021
8723518
Merge branch 'main' into hide-issue-events
qwerty287 Jan 7, 2022
e2de406
Merge branch 'main' into hide-issue-events
zeripath Jan 7, 2022
e6b31ad
Update services/forms/user_form_hidden_comments.go
wxiaoguang Jan 8, 2022
b8c2785
Merge branch 'main' into hide-issue-events
zeripath Jan 8, 2022
24a927a
Merge branch 'main' into hide-issue-events
qwerty287 Jan 12, 2022
eb8caac
Merge branch 'main' into hide-issue-events
qwerty287 Jan 21, 2022
dcd7041
Merge branch 'main' into hide-issue-events
6543 Jan 21, 2022
c3933ba
check len == 0
6543 Jan 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
6543 marked this conversation as resolved.
Show resolved Hide resolved
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"
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
)
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
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved
}

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