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 2 commits
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
gary-kim marked this conversation as resolved.
Show resolved Hide resolved
type User struct {
EmailNotificationsEnabled bool `xorm:"DEFAULT true"`
gary-kim marked this conversation as resolved.
Show resolved Hide resolved
}

return x.Sync2(new(User))
}
24 changes: 19 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:"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,19 @@ func (u *User) IsMailable() bool {
return u.IsActive
}

// EnabledEmailNotifications checks if the user has
// enabled receiving notifications by email
func (u *User) EnabledEmailNotifications() bool {
gary-kim marked this conversation as resolved.
Show resolved Hide resolved
return u.EmailNotificationsEnabled
}

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

func isUserExist(e Engine, uid int64, name string) (bool, error) {
if len(name) == 0 {
return false, nil
Expand Down Expand Up @@ -1265,7 +1279,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
5 changes: 5 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ confirm_delete_account = Confirm Deletion
delete_account_title = Delete User Account
delete_account_desc = Are you sure you want to permanently delete this user account?

email_notifications.enable = Enable Email Notifications
email_notifications.disable = Disable Email Notifications

[repo]
owner = Owner
repo_name = Repository Name
Expand Down Expand Up @@ -1126,6 +1129,8 @@ settings.basic_settings = Basic Settings
settings.mirror_settings = Mirror Settings
settings.sync_mirror = Synchronize Now
settings.mirror_sync_in_progress = Mirror synchronization is in progress. Check back in a minute.
settings.email_notifications.enable = Enable Email Notifications
settings.email_notifications.disable = Disable Email Notifications
settings.site = Website
settings.update_settings = Update Settings
settings.advanced_settings = Advanced Settings
Expand Down
8 changes: 8 additions & 0 deletions routers/user/setting/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Account(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsAccount"] = true
ctx.Data["Email"] = ctx.User.Email
ctx.Data["NotificationsEnabled"] = ctx.User.EnabledEmailNotifications()

loadAccountData(ctx)

Expand Down Expand Up @@ -81,6 +82,13 @@ 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"))
log.Trace("Email notifications enabled made %s: %s", ctx.QueryBool("enable"), ctx.User.Name)
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
return
}

if ctx.HasError() {
loadAccountData(ctx)
Expand Down
13 changes: 13 additions & 0 deletions templates/user/settings/account.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@
<div class="ui email list">
<div class="item">
{{.i18n.Tr "settings.email_desc"}}
<div class="right floated content">
<form action="{{AppSubUrl}}/user/settings/account/email" method="post">
{{$.CsrfTokenHtml}}
<input name="_method" type="hidden" value="NOTIFICATION">
{{if .NotificationsEnabled}}
<input name="enable" type="hidden" value="false">
<button class="ui blue tiny button">{{$.i18n.Tr "settings.email_notifications.disable"}}</button>
{{else}}
<input name="enable" type="hidden" value="true">
<button class="ui blue tiny button">{{$.i18n.Tr "settings.email_notifications.enable"}}</button>
{{end}}
</form>
</div>
</div>
{{range .Emails}}
<div class="item">
Expand Down