Skip to content

Commit 56eccb4

Browse files
Add Notifications section in User Settings (#35008)
Related: #34982 --------- Signed-off-by: NorthRealm <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent b46623f commit 56eccb4

File tree

6 files changed

+109
-58
lines changed

6 files changed

+109
-58
lines changed

routers/web/user/setting/account.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ const (
3535

3636
// Account renders change user's password, user's email and user suicide page
3737
func Account(ctx *context.Context) {
38-
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageCredentials, setting.UserFeatureDeletion) && !setting.Service.EnableNotifyMail {
38+
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageCredentials, setting.UserFeatureDeletion) {
3939
ctx.NotFound(errors.New("account setting are not allowed to be changed"))
4040
return
4141
}
4242

4343
ctx.Data["Title"] = ctx.Tr("settings.account")
4444
ctx.Data["PageIsSettingsAccount"] = true
4545
ctx.Data["Email"] = ctx.Doer.Email
46-
ctx.Data["EnableNotifyMail"] = setting.Service.EnableNotifyMail
4746

4847
loadAccountData(ctx)
4948

@@ -61,7 +60,6 @@ func AccountPost(ctx *context.Context) {
6160
ctx.Data["Title"] = ctx.Tr("settings")
6261
ctx.Data["PageIsSettingsAccount"] = true
6362
ctx.Data["Email"] = ctx.Doer.Email
64-
ctx.Data["EnableNotifyMail"] = setting.Service.EnableNotifyMail
6563

6664
if ctx.HasError() {
6765
loadAccountData(ctx)
@@ -112,7 +110,6 @@ func EmailPost(ctx *context.Context) {
112110
ctx.Data["Title"] = ctx.Tr("settings")
113111
ctx.Data["PageIsSettingsAccount"] = true
114112
ctx.Data["Email"] = ctx.Doer.Email
115-
ctx.Data["EnableNotifyMail"] = setting.Service.EnableNotifyMail
116113

117114
// Make email address primary.
118115
if ctx.FormString("_method") == "PRIMARY" {
@@ -172,30 +169,6 @@ func EmailPost(ctx *context.Context) {
172169
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
173170
return
174171
}
175-
// Set Email Notification Preference
176-
if ctx.FormString("_method") == "NOTIFICATION" {
177-
preference := ctx.FormString("preference")
178-
if !(preference == user_model.EmailNotificationsEnabled ||
179-
preference == user_model.EmailNotificationsOnMention ||
180-
preference == user_model.EmailNotificationsDisabled ||
181-
preference == user_model.EmailNotificationsAndYourOwn) {
182-
log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.Doer.Name)
183-
ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
184-
return
185-
}
186-
opts := &user.UpdateOptions{
187-
EmailNotificationsPreference: optional.Some(preference),
188-
}
189-
if err := user.UpdateUser(ctx, ctx.Doer, opts); err != nil {
190-
log.Error("Set Email Notifications failed: %v", err)
191-
ctx.ServerError("UpdateUser", err)
192-
return
193-
}
194-
log.Trace("Email notifications preference made %s: %s", preference, ctx.Doer.Name)
195-
ctx.Flash.Success(ctx.Tr("settings.email_preference_set_success"))
196-
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
197-
return
198-
}
199172

200173
if ctx.HasError() {
201174
loadAccountData(ctx)
@@ -267,7 +240,6 @@ func DeleteAccount(ctx *context.Context) {
267240
ctx.Data["Title"] = ctx.Tr("settings")
268241
ctx.Data["PageIsSettingsAccount"] = true
269242
ctx.Data["Email"] = ctx.Doer.Email
270-
ctx.Data["EnableNotifyMail"] = setting.Service.EnableNotifyMail
271243

272244
if _, _, err := auth.UserSignIn(ctx, ctx.Doer.Name, ctx.FormString("password")); err != nil {
273245
switch {
@@ -342,7 +314,6 @@ func loadAccountData(ctx *context.Context) {
342314
emails[i] = &email
343315
}
344316
ctx.Data["Emails"] = emails
345-
ctx.Data["EmailNotificationsPreference"] = ctx.Doer.EmailNotificationsPreference
346317
ctx.Data["ActivationsPending"] = pendingActivation
347318
ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm
348319
ctx.Data["UserDisabledFeatures"] = user_model.DisabledFeaturesWithLoginType(ctx.Doer)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package setting
5+
6+
import (
7+
"errors"
8+
"net/http"
9+
10+
user_model "code.gitea.io/gitea/models/user"
11+
"code.gitea.io/gitea/modules/log"
12+
"code.gitea.io/gitea/modules/optional"
13+
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/templates"
15+
"code.gitea.io/gitea/services/context"
16+
"code.gitea.io/gitea/services/user"
17+
)
18+
19+
const tplSettingsNotifications templates.TplName = "user/settings/notifications"
20+
21+
// Notifications render user's notifications settings
22+
func Notifications(ctx *context.Context) {
23+
if !setting.Service.EnableNotifyMail {
24+
ctx.NotFound(nil)
25+
return
26+
}
27+
28+
ctx.Data["Title"] = ctx.Tr("notifications")
29+
ctx.Data["PageIsSettingsNotifications"] = true
30+
ctx.Data["EmailNotificationsPreference"] = ctx.Doer.EmailNotificationsPreference
31+
32+
ctx.HTML(http.StatusOK, tplSettingsNotifications)
33+
}
34+
35+
// NotificationsEmailPost set user's email notification preference
36+
func NotificationsEmailPost(ctx *context.Context) {
37+
if !setting.Service.EnableNotifyMail {
38+
ctx.NotFound(nil)
39+
return
40+
}
41+
42+
preference := ctx.FormString("preference")
43+
if !(preference == user_model.EmailNotificationsEnabled ||
44+
preference == user_model.EmailNotificationsOnMention ||
45+
preference == user_model.EmailNotificationsDisabled ||
46+
preference == user_model.EmailNotificationsAndYourOwn) {
47+
log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.Doer.Name)
48+
ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
49+
return
50+
}
51+
opts := &user.UpdateOptions{
52+
EmailNotificationsPreference: optional.Some(preference),
53+
}
54+
if err := user.UpdateUser(ctx, ctx.Doer, opts); err != nil {
55+
log.Error("Set Email Notifications failed: %v", err)
56+
ctx.ServerError("UpdateUser", err)
57+
return
58+
}
59+
log.Trace("Email notifications preference made %s: %s", preference, ctx.Doer.Name)
60+
ctx.Flash.Success(ctx.Tr("settings.email_preference_set_success"))
61+
ctx.Redirect(setting.AppSubURL + "/user/settings/notifications")
62+
}

routers/web/web.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,10 @@ func registerWebRoutes(m *web.Router) {
595595
m.Post("/hidden_comments", user_setting.UpdateUserHiddenComments)
596596
m.Post("/theme", web.Bind(forms.UpdateThemeForm{}), user_setting.UpdateUIThemePost)
597597
})
598+
m.Group("/notifications", func() {
599+
m.Get("", user_setting.Notifications)
600+
m.Post("/email", user_setting.NotificationsEmailPost)
601+
})
598602
m.Group("/security", func() {
599603
m.Get("", security.Security)
600604
m.Group("/two_factor", func() {
@@ -682,7 +686,7 @@ func registerWebRoutes(m *web.Router) {
682686
m.Get("", user_setting.BlockedUsers)
683687
m.Post("", web.Bind(forms.BlockUserForm{}), user_setting.BlockedUsersPost)
684688
})
685-
}, reqSignIn, ctxDataSet("PageIsUserSettings", true, "EnablePackages", setting.Packages.Enabled))
689+
}, reqSignIn, ctxDataSet("PageIsUserSettings", true, "EnablePackages", setting.Packages.Enabled, "EnableNotifyMail", setting.Service.EnableNotifyMail))
686690

687691
m.Group("/user", func() {
688692
m.Get("/activate", auth.Activate)

templates/user/settings/account.tmpl

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,12 @@
3535
{{end}}
3636
</div>
3737

38-
{{if not (and ($.UserDisabledFeatures.Contains "manage_credentials") (not $.EnableNotifyMail))}}
38+
{{if not ($.UserDisabledFeatures.Contains "manage_credentials")}}
3939
<h4 class="ui top attached header">
4040
{{ctx.Locale.Tr "settings.manage_emails"}}
4141
</h4>
4242
<div class="ui attached segment">
4343
<div class="ui list flex-items-block">
44-
{{if $.EnableNotifyMail}}
45-
<div class="item">
46-
<form class="ui form tw-w-full" action="{{AppSubUrl}}/user/settings/account/email" method="post">
47-
{{$.CsrfTokenHtml}}
48-
<input name="_method" type="hidden" value="NOTIFICATION">
49-
<div class="field">
50-
<label>{{ctx.Locale.Tr "settings.email_desc"}}</label>
51-
<div class="ui selection dropdown">
52-
<input name="preference" type="hidden" value="{{.EmailNotificationsPreference}}">
53-
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
54-
<div class="text"></div>
55-
<div class="menu">
56-
<div data-value="enabled" class="{{if eq .EmailNotificationsPreference "enabled"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.enable"}}</div>
57-
<div data-value="andyourown" class="{{if eq .EmailNotificationsPreference "andyourown"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.andyourown"}}</div>
58-
<div data-value="onmention" class="{{if eq .EmailNotificationsPreference "onmention"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.onmention"}}</div>
59-
<div data-value="disabled" class="{{if eq .EmailNotificationsPreference "disabled"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.disable"}}</div>
60-
</div>
61-
</div>
62-
</div>
63-
<div class="field">
64-
<button class="ui primary button">{{ctx.Locale.Tr "settings.email_notifications.submit"}}</button>
65-
</div>
66-
</form>
67-
</div>
68-
{{end}}
6944
{{if not ($.UserDisabledFeatures.Contains "manage_credentials")}}
7045
{{range .Emails}}
7146
<div class="item tw-flex-wrap">

templates/user/settings/navbar.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
<a class="{{if .PageIsSettingsProfile}}active {{end}}item" href="{{AppSubUrl}}/user/settings">
55
{{ctx.Locale.Tr "settings.profile"}}
66
</a>
7-
{{if not (and ($.UserDisabledFeatures.Contains "manage_credentials" "deletion") (not $.EnableNotifyMail))}}
7+
{{if not ($.UserDisabledFeatures.Contains "manage_credentials" "deletion")}}
88
<a class="{{if .PageIsSettingsAccount}}active {{end}}item" href="{{AppSubUrl}}/user/settings/account">
99
{{ctx.Locale.Tr "settings.account"}}
1010
</a>
1111
{{end}}
12+
{{if $.EnableNotifyMail}}
13+
<a class="{{if .PageIsSettingsNotifications}}active {{end}}item" href="{{AppSubUrl}}/user/settings/notifications">
14+
{{ctx.Locale.Tr "notifications"}}
15+
</a>
16+
{{end}}
1217
<a class="{{if .PageIsSettingsAppearance}}active {{end}}item" href="{{AppSubUrl}}/user/settings/appearance">
1318
{{ctx.Locale.Tr "settings.appearance"}}
1419
</a>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{{template "user/settings/layout_head" (dict "ctxData" . "pageClass" "user settings")}}
2+
<div class="user-setting-content">
3+
<h4 class="ui top attached header">
4+
{{ctx.Locale.Tr "notifications"}}
5+
</h4>
6+
<div class="ui attached segment">
7+
<div class="ui list flex-items-block">
8+
<div class="item">
9+
<form class="ui form tw-w-full" action="{{AppSubUrl}}/user/settings/notifications/email" method="post">
10+
{{$.CsrfTokenHtml}}
11+
<div class="field">
12+
<label>{{ctx.Locale.Tr "settings.email_desc"}}</label>
13+
<div class="ui selection dropdown">
14+
<input name="preference" type="hidden" value="{{.EmailNotificationsPreference}}">
15+
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
16+
<div class="text"></div>
17+
<div class="menu">
18+
<div data-value="enabled" class="{{if eq .EmailNotificationsPreference "enabled"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.enable"}}</div>
19+
<div data-value="andyourown" class="{{if eq .EmailNotificationsPreference "andyourown"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.andyourown"}}</div>
20+
<div data-value="onmention" class="{{if eq .EmailNotificationsPreference "onmention"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.onmention"}}</div>
21+
<div data-value="disabled" class="{{if eq .EmailNotificationsPreference "disabled"}}active selected {{end}}item">{{ctx.Locale.Tr "settings.email_notifications.disable"}}</div>
22+
</div>
23+
</div>
24+
</div>
25+
<div class="field">
26+
<button class="ui primary button">{{ctx.Locale.Tr "settings.email_notifications.submit"}}</button>
27+
</div>
28+
</form>
29+
</div>
30+
</div>
31+
</div>
32+
</div>
33+
34+
{{template "user/settings/layout_footer" .}}

0 commit comments

Comments
 (0)