From d160d8cc10284c16230af93a529a15683b3690bf Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 19 Mar 2022 12:45:44 +0000 Subject: [PATCH 1/2] Do not send notification emails to inactive users (#19131) Backport #19131 Emails should not be sent to inactive users except for Activate and ResetPassword messages. Fix #18950 Signed-off-by: Andrew Thornton --- routers/private/mail.go | 2 +- services/mailer/mail.go | 12 ++++++++---- services/mailer/mail_repo.go | 4 ++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/routers/private/mail.go b/routers/private/mail.go index 8b69c380930ae..853b58b09d4dc 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -60,7 +60,7 @@ func SendEmail(ctx *context.PrivateContext) { } } else { err := user_model.IterateUser(func(user *user_model.User) error { - if len(user.Email) > 0 { + if len(user.Email) > 0 && user.IsActive { emails = append(emails, user.Email) } return nil diff --git a/services/mailer/mail.go b/services/mailer/mail.go index 3d93f1bd69d90..e0a454d4983bb 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -146,8 +146,8 @@ func SendActivateEmailMail(u *user_model.User, email *user_model.EmailAddress) { // SendRegisterNotifyMail triggers a notify e-mail by admin created a account. func SendRegisterNotifyMail(u *user_model.User) { - if setting.MailService == nil { - // No mail service configured + if setting.MailService == nil || !u.IsActive { + // No mail service configured OR user is inactive return } locale := translation.NewLocale(u.Language) @@ -176,8 +176,8 @@ func SendRegisterNotifyMail(u *user_model.User) { // SendCollaboratorMail sends mail notification to new collaborator. func SendCollaboratorMail(u, doer *user_model.User, repo *repo_model.Repository) { - if setting.MailService == nil { - // No mail service configured + if setting.MailService == nil || !u.IsActive { + // No mail service configured OR the user is inactive return } locale := translation.NewLocale(u.Language) @@ -404,6 +404,10 @@ func SendIssueAssignedMail(issue *models.Issue, doer *user_model.User, content s langMap := make(map[string][]*user_model.User) for _, user := range recipients { + if !user.IsActive { + // don't send emails to inactive users + continue + } langMap[user.Language] = append(langMap[user.Language], user) } diff --git a/services/mailer/mail_repo.go b/services/mailer/mail_repo.go index a5343f81286e4..24e6d671f4882 100644 --- a/services/mailer/mail_repo.go +++ b/services/mailer/mail_repo.go @@ -31,6 +31,10 @@ func SendRepoTransferNotifyMail(doer, newOwner *user_model.User, repo *repo_mode langMap := make(map[string][]string) for _, user := range users { + if !user.IsActive { + // don't send emails to inactive users + continue + } langMap[user.Language] = append(langMap[user.Language], user.Email) } From 3552a764a4e58176c571b196c864bae9ab28dbfa Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 19 Mar 2022 14:45:26 +0000 Subject: [PATCH 2/2] not send notification emails to inactive users (part 2) Unfortunately fixing changes to `mail_issue.go` did not get included in #19131. We also need to not send issue comment mails to deactivated users. Fix #18950 Signed-off-by: Andrew Thornton --- services/mailer/mail_issue.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index c9cc2e015abe8..c37414df05f47 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -125,6 +125,10 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, vi langMap := make(map[string][]*user_model.User) for _, user := range users { + if !user.IsActive { + // Exclude deactivated users + continue + } // At this point we exclude: // user that don't have all mails enabled or users only get mail on mention and this is one ... if !(user.EmailNotificationsPreference == user_model.EmailNotificationsEnabled ||