Skip to content

Commit ffa12bd

Browse files
Fix "only mail on mention" bug (#12775)
* fix mail mention bug fix #12774 Signed-off-by: a1012112796 <1012112796@qq.com> * fix test Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 0cd49aa commit ffa12bd

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

models/user.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,11 +1419,21 @@ func getUserEmailsByNames(e Engine, names []string) []string {
14191419
}
14201420

14211421
// GetMaileableUsersByIDs gets users from ids, but only if they can receive mails
1422-
func GetMaileableUsersByIDs(ids []int64) ([]*User, error) {
1422+
func GetMaileableUsersByIDs(ids []int64, isMention bool) ([]*User, error) {
14231423
if len(ids) == 0 {
14241424
return nil, nil
14251425
}
14261426
ous := make([]*User, 0, len(ids))
1427+
1428+
if isMention {
1429+
return ous, x.In("id", ids).
1430+
Where("`type` = ?", UserTypeIndividual).
1431+
And("`prohibit_login` = ?", false).
1432+
And("`is_active` = ?", true).
1433+
And("`email_notifications_preference` IN ( ?, ?)", EmailNotificationsEnabled, EmailNotificationsOnMention).
1434+
Find(&ous)
1435+
}
1436+
14271437
return ous, x.In("id", ids).
14281438
Where("`type` = ?", UserTypeIndividual).
14291439
And("`prohibit_login` = ?", false).

models/user_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,20 @@ func TestGetUserIDsByNames(t *testing.T) {
389389
assert.Error(t, err)
390390
assert.Equal(t, []int64(nil), IDs)
391391
}
392+
393+
func TestGetMaileableUsersByIDs(t *testing.T) {
394+
results, err := GetMaileableUsersByIDs([]int64{1, 4}, false)
395+
assert.NoError(t, err)
396+
assert.Equal(t, 1, len(results))
397+
if len(results) > 1 {
398+
assert.Equal(t, results[0].ID, 1)
399+
}
400+
401+
results, err = GetMaileableUsersByIDs([]int64{1, 4}, true)
402+
assert.NoError(t, err)
403+
assert.Equal(t, 2, len(results))
404+
if len(results) > 2 {
405+
assert.Equal(t, results[0].ID, 1)
406+
assert.Equal(t, results[1].ID, 4)
407+
}
408+
}

services/mailer/mail_issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func mailIssueCommentBatch(ctx *mailCommentContext, ids []int64, visited map[int
118118
visited[id] = true
119119
}
120120
}
121-
recipients, err := models.GetMaileableUsersByIDs(unique)
121+
recipients, err := models.GetMaileableUsersByIDs(unique, fromMention)
122122
if err != nil {
123123
return err
124124
}

services/mailer/mail_release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func MailNewRelease(rel *models.Release) {
2727
return
2828
}
2929

30-
recipients, err := models.GetMaileableUsersByIDs(watcherIDList)
30+
recipients, err := models.GetMaileableUsersByIDs(watcherIDList, false)
3131
if err != nil {
3232
log.Error("models.GetMaileableUsersByIDs: %v", err)
3333
return

0 commit comments

Comments
 (0)