From 23da90e25d610e94a50dd27223baaa8834f61607 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 10 Mar 2017 18:47:02 -0500 Subject: [PATCH] issue_mail: send notifications to participants in comments (#2929) --- gogs.go | 2 +- models/issue.go | 17 +++++++++++++++++ models/issue_mail.go | 20 ++++++++++++++++++-- templates/.VERSION | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/gogs.go b/gogs.go index 50cccff57..c5a9e484e 100644 --- a/gogs.go +++ b/gogs.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.10.12.0310" +const APP_VER = "0.10.13.0310" func init() { setting.AppVer = APP_VER diff --git a/models/issue.go b/models/issue.go index 90d577638..89c72f487 100644 --- a/models/issue.go +++ b/models/issue.go @@ -980,6 +980,23 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { return issues, nil } +// GetParticipantsByIssueID returns all users who are participated in comments of an issue. +func GetParticipantsByIssueID(issueID int64) ([]*User, error) { + userIDs := make([]int64, 0, 5) + if err := x.Table("comment").Cols("poster_id"). + Where("issue_id = ?", issueID). + Distinct("poster_id"). + Find(&userIDs); err != nil { + return nil, fmt.Errorf("get poster IDs: %v", err) + } + if len(userIDs) == 0 { + return nil, nil + } + + users := make([]*User, 0, len(userIDs)) + return users, x.In("id", userIDs).Find(&users) +} + // .___ ____ ___ // | | ______ ________ __ ____ | | \______ ___________ // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \ diff --git a/models/issue_mail.go b/models/issue_mail.go index f46291237..dc05496ee 100644 --- a/models/issue_mail.go +++ b/models/issue_mail.go @@ -91,15 +91,21 @@ func NewMailerIssue(issue *Issue) mailer.Issue { } // mailIssueCommentToParticipants can be used for both new issue creation and comment. +// This functions sends two list of emails: +// 1. Repository watchers and users who are participated in comments. +// 2. Users who are not in 1. but get mentioned in current issue/comment. func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) error { if !setting.Service.EnableNotifyMail { return nil } - // Mail wahtcers. watchers, err := GetWatchers(issue.RepoID) if err != nil { - return fmt.Errorf("GetWatchers [%d]: %v", issue.RepoID, err) + return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err) + } + participants, err := GetParticipantsByIssueID(issue.ID) + if err != nil { + return fmt.Errorf("GetParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err) } tos := make([]string, 0, len(watchers)) // List of email addresses. @@ -120,6 +126,16 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) 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) { + continue + } + + tos = append(tos, participants[i].Email) + names = append(names, participants[i].Name) + } mailer.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos) // Mail mentioned people and exclude watchers. diff --git a/templates/.VERSION b/templates/.VERSION index 2ed58e6a1..425113627 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.10.12.0310 \ No newline at end of file +0.10.13.0310 \ No newline at end of file