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 Ability for User to Customize Email Notification Frequency #7813

Merged
merged 15 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
8 changes: 4 additions & 4 deletions models/issue_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content
if err != nil {
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
}
if to.IsOrganization() {
if to.IsOrganization() || !to.EnabledEmailNotifications() {
continue
}

tos = append(tos, to.Email)
names = append(names, to.Name)
}
for i := range participants {
if participants[i].ID == doer.ID {
continue
} else if com.IsSliceContainsStr(names, participants[i].Name) {
if participants[i].ID == doer.ID ||
com.IsSliceContainsStr(names, participants[i].Name) ||
!participants[i].EnabledEmailNotifications() {
continue
}

Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ var migrations = []Migration{
NewMigration("add index on owner_id of repository and type, review_id of comment", addIndexOnRepositoryAndComment),
// v92 -> v93
NewMigration("remove orphaned repository index statuses", removeLingeringIndexStatus),
// v93 -> v94
NewMigration("add email notification enabled boolean to user", addEmailNotificationEnabledToUser),
}

// Migrate database to current version
Expand Down
16 changes: 16 additions & 0 deletions models/migrations/v93.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2019 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 migrations

import "github.com/go-xorm/xorm"

func addEmailNotificationEnabledToUser(x *xorm.Engine) error {
// Issue see models/user.go
type User struct {
EmailNotificationsEnabled bool `xorm:"NOT NULL DEFAULT true"`
}

return x.Sync2(new(User))
}
23 changes: 18 additions & 5 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ type User struct {
Name string `xorm:"UNIQUE NOT NULL"`
FullName string
// Email is the primary email address (to be used for communication)
Email string `xorm:"NOT NULL"`
KeepEmailPrivate bool
Passwd string `xorm:"NOT NULL"`
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'pbkdf2'"`
Email string `xorm:"NOT NULL"`
KeepEmailPrivate bool
EmailNotificationsEnabled bool `xorm:"NOT NULL DEFAULT true"`
Passwd string `xorm:"NOT NULL"`
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'pbkdf2'"`

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

// EnabledEmailNotifications checks if the user has
// enabled receiving notifications by email
func (u *User) EnabledEmailNotifications() bool {
return u.EmailNotificationsEnabled
}

// SetEmailNotifications sets whether the user
// would like to receive notifications by email
func (u *User) SetEmailNotifications(set bool) {
u.EmailNotificationsEnabled = set
}

func isUserExist(e Engine, uid int64, name string) (bool, error) {
if len(name) == 0 {
return false, nil
Expand Down Expand Up @@ -1265,7 +1278,7 @@ func getUserEmailsByNames(e Engine, names []string) []string {
if err != nil {
continue
}
if u.IsMailable() {
if u.IsMailable() && u.EnabledEmailNotifications() {
mails = append(mails, u.Email)
}
}
Expand Down
6 changes: 6 additions & 0 deletions routers/user/setting/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
return
}
// Set Email Notification Preference
if ctx.Query("_method") == "NOTIFICATION" {
ctx.User.SetEmailNotifications(ctx.QueryBool("enable"))
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
return
}

if ctx.HasError() {
loadAccountData(ctx)
Expand Down