diff options
author | Andrey Nering <andrey.nering@gmail.com> | 2017-03-29 20:54:57 -0300 |
---|---|---|
committer | Andrey Nering <andrey.nering@gmail.com> | 2017-03-29 20:54:57 -0300 |
commit | aa6e949b3de11e675311e54b59e027cd82a230f4 (patch) | |
tree | b56c680bdcef1ea367742a437d114c422394aa84 | |
parent | b6744607484008826d18f129326664105b9d7bfc (diff) | |
download | gitea-aa6e949b3de11e675311e54b59e027cd82a230f4.tar.gz gitea-aa6e949b3de11e675311e54b59e027cd82a230f4.zip |
Consider issue_watchers while sending notifications
-rw-r--r-- | models/issue_watch.go | 10 | ||||
-rw-r--r-- | models/notification.go | 40 |
2 files changed, 42 insertions, 8 deletions
diff --git a/models/issue_watch.go b/models/issue_watch.go index d082211c77..03a677a3a0 100644 --- a/models/issue_watch.go +++ b/models/issue_watch.go @@ -58,3 +58,13 @@ func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool Get(iw) return } + +func GetIssueWatchers(issueID int64) ([]*IssueWatch, error) { + return getIssueWatchers(x, issueID) +} +func getIssueWatchers(e Engine, issueID int64) (watches []*IssueWatch, err error) { + err = e. + Where("issue_id = ?", issueID). + Find(&watches) + return +} diff --git a/models/notification.go b/models/notification.go index bba662c06c..a59c6f1401 100644 --- a/models/notification.go +++ b/models/notification.go @@ -96,6 +96,11 @@ func CreateOrUpdateIssueNotifications(issue *Issue, notificationAuthorID int64) } func createOrUpdateIssueNotifications(e Engine, issue *Issue, notificationAuthorID int64) error { + issueWatches, err := getIssueWatchers(e, issue.ID) + if err != nil { + return err + } + watches, err := getWatchers(e, issue.RepoID) if err != nil { return err @@ -106,23 +111,42 @@ func createOrUpdateIssueNotifications(e Engine, issue *Issue, notificationAuthor return err } - for _, watch := range watches { + alreadyNotified := make(map[int64]struct{}, len(issueWatches)+len(watches)) + + notifyUser := func(userID int64) error { // do not send notification for the own issuer/commenter - if watch.UserID == notificationAuthorID { - continue + if userID == notificationAuthorID { + return nil } - if notificationExists(notifications, issue.ID, watch.UserID) { - err = updateIssueNotification(e, watch.UserID, issue.ID, notificationAuthorID) - } else { - err = createIssueNotification(e, watch.UserID, issue, notificationAuthorID) + if _, ok := alreadyNotified[userID]; ok { + return nil } + alreadyNotified[userID] = struct{}{} - if err != nil { + if notificationExists(notifications, issue.ID, userID) { + return updateIssueNotification(e, userID, issue.ID, notificationAuthorID) + } + return createIssueNotification(e, userID, issue, notificationAuthorID) + } + + for _, issueWatch := range issueWatches { + // ignore if user unwatched the issue + if !issueWatch.IsWatching { + alreadyNotified[issueWatch.UserID] = struct{}{} + continue + } + + if err := notifyUser(issueWatch.UserID); err != nil { return err } } + for _, watch := range watches { + if err := notifyUser(watch.UserID); err != nil { + return err + } + } return nil } |