Skip to content

Commit fd51209

Browse files
committed
Add Backend Logic for Toggling Email Notification
This commit adds the backend logic for allowing users to enable or disable email notifications. The implementation ensures that only issue notification emails get disabled and important emails are still sent regardless of the setting. The UI to toggle this setting has not yet been implemented.
1 parent 0470b16 commit fd51209

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

models/issue_mail.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
7070
if err != nil {
7171
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
7272
}
73-
if to.IsOrganization() {
73+
if to.IsOrganization() || !to.EnabledEmailNotifications() {
7474
continue
7575
}
7676

7777
tos = append(tos, to.Email)
7878
names = append(names, to.Name)
7979
}
8080
for i := range participants {
81-
if participants[i].ID == doer.ID {
82-
continue
83-
} else if com.IsSliceContainsStr(names, participants[i].Name) {
81+
if participants[i].ID == doer.ID ||
82+
com.IsSliceContainsStr(names, participants[i].Name) ||
83+
!participants[i].EnabledEmailNotifications() {
8484
continue
8585
}
8686

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ var migrations = []Migration{
240240
NewMigration("add index on owner_id of repository and type, review_id of comment", addIndexOnRepositoryAndComment),
241241
// v92 -> v93
242242
NewMigration("remove orphaned repository index statuses", removeLingeringIndexStatus),
243+
// v93 -> v94
244+
NewMigration("add email notification enabled boolean to user", addEmailNotificationEnabledToUser),
243245
}
244246

245247
// Migrate database to current version

models/migrations/v93.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import "github.com/go-xorm/xorm"
8+
9+
func addEmailNotificationEnabledToUser(x *xorm.Engine) error {
10+
// Issue see models/user.go
11+
type User struct {
12+
EmailNotificationsEnabled bool `xorm:"NOT NULL DEFAULT true"`
13+
}
14+
15+
return x.Sync2(new(User))
16+
}

models/user.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ type User struct {
8787
Name string `xorm:"UNIQUE NOT NULL"`
8888
FullName string
8989
// Email is the primary email address (to be used for communication)
90-
Email string `xorm:"NOT NULL"`
91-
KeepEmailPrivate bool
92-
Passwd string `xorm:"NOT NULL"`
93-
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'pbkdf2'"`
90+
Email string `xorm:"NOT NULL"`
91+
KeepEmailPrivate bool
92+
EmailNotificationsEnabled bool `xorm:"NOT NULL DEFAULT true"`
93+
Passwd string `xorm:"NOT NULL"`
94+
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'pbkdf2'"`
9495

9596
// MustChangePassword is an attribute that determines if a user
9697
// is to change his/her password after registration.
@@ -719,6 +720,18 @@ func (u *User) IsMailable() bool {
719720
return u.IsActive
720721
}
721722

723+
// EnabledEmailNotifications checks if the user has
724+
// enabled receiving notifications by email
725+
func (u *User) EnabledEmailNotifications() bool {
726+
return u.EmailNotificationsEnabled
727+
}
728+
729+
// SetEmailNotifications sets whether the user
730+
// would like to receive notifications by email
731+
func (u *User) SetEmailNotifications(set bool) {
732+
u.EmailNotificationsEnabled = set
733+
}
734+
722735
func isUserExist(e Engine, uid int64, name string) (bool, error) {
723736
if len(name) == 0 {
724737
return false, nil
@@ -1265,7 +1278,7 @@ func getUserEmailsByNames(e Engine, names []string) []string {
12651278
if err != nil {
12661279
continue
12671280
}
1268-
if u.IsMailable() {
1281+
if u.IsMailable() && u.EnabledEmailNotifications() {
12691282
mails = append(mails, u.Email)
12701283
}
12711284
}

routers/user/setting/account.go

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
8181
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
8282
return
8383
}
84+
// Set Email Notification Preference
85+
if ctx.Query("_method") == "NOTIFICATION" {
86+
ctx.User.SetEmailNotifications(ctx.QueryBool("enable"))
87+
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
88+
return
89+
}
8490

8591
if ctx.HasError() {
8692
loadAccountData(ctx)

0 commit comments

Comments
 (0)