diff options
author | Jimmy Praet <jimmy.praet@telenet.be> | 2021-01-02 18:04:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-02 18:04:02 +0100 |
commit | e6acce649b348cc497b999100a170866a90c87b8 (patch) | |
tree | 2a1bb838fe8b36f016c638190651e61e6b27acd3 /services/mailer | |
parent | ac88b0ee839bfbfae5759e211d0f9a69abe4d8f7 (diff) | |
download | gitea-e6acce649b348cc497b999100a170866a90c87b8.tar.gz gitea-e6acce649b348cc497b999100a170866a90c87b8.zip |
Send notifications for mentions in pulls, issues, (code-)comments (#14218)
Fixes #14187: mention handling extracted from email notification code
Fixes #14013: add notification for mentions in pull request code comments
Fixes #13450: Not receiving any emails with setting "Only Email on Mention"
Diffstat (limited to 'services/mailer')
-rw-r--r-- | services/mailer/mail_comment.go | 46 | ||||
-rw-r--r-- | services/mailer/mail_issue.go | 34 |
2 files changed, 41 insertions, 39 deletions
diff --git a/services/mailer/mail_comment.go b/services/mailer/mail_comment.go index 9edbfabd48..2f166720db 100644 --- a/services/mailer/mail_comment.go +++ b/services/mailer/mail_comment.go @@ -5,31 +5,20 @@ package mailer import ( - "fmt" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/references" ) // MailParticipantsComment sends new comment emails to repository watchers // and mentioned people. -func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue) error { - return mailParticipantsComment(models.DefaultDBContext(), c, opType, issue) +func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) error { + return mailParticipantsComment(c, opType, issue, mentions) } -func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType models.ActionType, issue *models.Issue) (err error) { - rawMentions := references.FindAllMentionsMarkdown(c.Content) - userMentions, err := issue.ResolveMentionsByVisibility(ctx, c.Poster, rawMentions) - if err != nil { - return fmt.Errorf("ResolveMentionsByVisibility [%d]: %v", c.IssueID, err) - } - if err = models.UpdateIssueMentions(ctx, c.IssueID, userMentions); err != nil { - return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err) - } - mentions := make([]int64, len(userMentions)) - for i, u := range userMentions { - mentions[i] = u.ID +func mailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) (err error) { + mentionedIDs := make([]int64, len(mentions)) + for i, u := range mentions { + mentionedIDs[i] = u.ID } if err = mailIssueCommentToParticipants( &mailCommentContext{ @@ -38,8 +27,29 @@ func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType mod ActionType: opType, Content: c.Content, Comment: c, - }, mentions); err != nil { + }, mentionedIDs); err != nil { log.Error("mailIssueCommentToParticipants: %v", err) } return nil } + +// MailMentionsComment sends email to users mentioned in a code comment +func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []*models.User) (err error) { + mentionedIDs := make([]int64, len(mentions)) + for i, u := range mentions { + mentionedIDs[i] = u.ID + } + visited := make(map[int64]bool, len(mentions)+1) + visited[c.Poster.ID] = true + if err = mailIssueCommentBatch( + &mailCommentContext{ + Issue: pr.Issue, + Doer: c.Poster, + ActionType: models.ActionCommentPull, + Content: c.Content, + Comment: c, + }, mentionedIDs, visited, true); err != nil { + log.Error("mailIssueCommentBatch: %v", err) + } + return nil +} diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index 30b54eb6cb..ffe2a4e471 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -9,7 +9,6 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/references" ) func fallbackMailSubject(issue *models.Issue) string { @@ -80,6 +79,12 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e // Avoid mailing the doer visited[ctx.Doer.ID] = true + + // =========== Mentions =========== + if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil { + return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err) + } + // Avoid mailing explicit unwatched ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, false) if err != nil { @@ -93,11 +98,6 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e return fmt.Errorf("mailIssueCommentBatch(): %v", err) } - // =========== Mentions =========== - if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil { - return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err) - } - return nil } @@ -145,22 +145,14 @@ func mailIssueCommentBatch(ctx *mailCommentContext, ids []int64, visited map[int // MailParticipants sends new issue thread created emails to repository watchers // and mentioned people. -func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType) error { - return mailParticipants(models.DefaultDBContext(), issue, doer, opType) +func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) error { + return mailParticipants(issue, doer, opType, mentions) } -func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.User, opType models.ActionType) (err error) { - rawMentions := references.FindAllMentionsMarkdown(issue.Content) - userMentions, err := issue.ResolveMentionsByVisibility(ctx, doer, rawMentions) - if err != nil { - return fmt.Errorf("ResolveMentionsByVisibility [%d]: %v", issue.ID, err) - } - if err = models.UpdateIssueMentions(ctx, issue.ID, userMentions); err != nil { - return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err) - } - mentions := make([]int64, len(userMentions)) - for i, u := range userMentions { - mentions[i] = u.ID +func mailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) (err error) { + mentionedIDs := make([]int64, len(mentions)) + for i, u := range mentions { + mentionedIDs[i] = u.ID } if err = mailIssueCommentToParticipants( &mailCommentContext{ @@ -169,7 +161,7 @@ func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.Us ActionType: opType, Content: issue.Content, Comment: nil, - }, mentions); err != nil { + }, mentionedIDs); err != nil { log.Error("mailIssueCommentToParticipants: %v", err) } return nil |