From 50f2e90b76acf2ebda225b682c08051846fe594b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 27 Feb 2020 18:42:51 +0100 Subject: [PATCH] [BugFix] Avoid mailing explicit unwatched (#10475) (#10500) * [BugFix] Avoid mailing explicit unwatched (#10475) --- models/issue_watch.go | 12 ++++++++---- services/mailer/mail_issue.go | 10 +++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/models/issue_watch.go b/models/issue_watch.go index 6144d6cafe..5b87f3599f 100644 --- a/models/issue_watch.go +++ b/models/issue_watch.go @@ -64,14 +64,18 @@ func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool return } -// GetIssueWatchersIDs returns IDs of subscribers to a given issue id +// GetIssueWatchersIDs returns IDs of subscribers or explicit unsubscribers to a given issue id // but avoids joining with `user` for performance reasons // User permissions must be verified elsewhere if required -func GetIssueWatchersIDs(issueID int64) ([]int64, error) { +func GetIssueWatchersIDs(issueID int64, watching bool) ([]int64, error) { + return getIssueWatchersIDs(x, issueID, watching) +} + +func getIssueWatchersIDs(e Engine, issueID int64, watching bool) ([]int64, error) { ids := make([]int64, 0, 64) - return ids, x.Table("issue_watch"). + return ids, e.Table("issue_watch"). Where("issue_id=?", issueID). - And("is_watching = ?", true). + And("is_watching = ?", watching). Select("user_id"). Find(&ids) } diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index 696adfadda..1030a9548e 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -62,7 +62,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e unfiltered = append(unfiltered, ids...) // =========== Issue watchers =========== - ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID) + ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, true) if err != nil { return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err) } @@ -80,6 +80,14 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e // Avoid mailing the doer visited[ctx.Doer.ID] = true + // Avoid mailing explicit unwatched + ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, false) + if err != nil { + return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err) + } + for _, i := range ids { + visited[i] = true + } if err = mailIssueCommentBatch(ctx, unfiltered, visited, false); err != nil { return fmt.Errorf("mailIssueCommentBatch(): %v", err)