aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorJimmy Praet <jimmy.praet@telenet.be>2021-01-02 18:04:02 +0100
committerGitHub <noreply@github.com>2021-01-02 18:04:02 +0100
commite6acce649b348cc497b999100a170866a90c87b8 (patch)
tree2a1bb838fe8b36f016c638190651e61e6b27acd3 /services
parentac88b0ee839bfbfae5759e211d0f9a69abe4d8f7 (diff)
downloadgitea-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')
-rw-r--r--services/comments/comments.go7
-rw-r--r--services/issue/issue.go7
-rw-r--r--services/mailer/mail_comment.go46
-rw-r--r--services/mailer/mail_issue.go34
-rw-r--r--services/pull/pull.go7
-rw-r--r--services/pull/review.go27
6 files changed, 83 insertions, 45 deletions
diff --git a/services/comments/comments.go b/services/comments/comments.go
index ed479e7110..ad79eec4fb 100644
--- a/services/comments/comments.go
+++ b/services/comments/comments.go
@@ -22,8 +22,11 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
if err != nil {
return nil, err
}
-
- notification.NotifyCreateIssueComment(doer, repo, issue, comment)
+ mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), doer, comment.Content)
+ if err != nil {
+ return nil, err
+ }
+ notification.NotifyCreateIssueComment(doer, repo, issue, comment, mentions)
return comment, nil
}
diff --git a/services/issue/issue.go b/services/issue/issue.go
index 0f90a2bcd0..14de0290a1 100644
--- a/services/issue/issue.go
+++ b/services/issue/issue.go
@@ -23,7 +23,12 @@ func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, uu
}
}
- notification.NotifyNewIssue(issue)
+ mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), issue.Poster, issue.Content)
+ if err != nil {
+ return err
+ }
+
+ notification.NotifyNewIssue(issue, mentions)
return nil
}
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
diff --git a/services/pull/pull.go b/services/pull/pull.go
index 476c5dad54..35dcbf9604 100644
--- a/services/pull/pull.go
+++ b/services/pull/pull.go
@@ -53,7 +53,12 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6
return err
}
- notification.NotifyNewPullRequest(pr)
+ mentions, err := pull.FindAndUpdateIssueMentions(models.DefaultDBContext(), pull.Poster, pull.Content)
+ if err != nil {
+ return err
+ }
+
+ notification.NotifyNewPullRequest(pr, mentions)
// add first push codes comment
baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
diff --git a/services/pull/review.go b/services/pull/review.go
index 6781136061..8994a9e78a 100644
--- a/services/pull/review.go
+++ b/services/pull/review.go
@@ -57,7 +57,12 @@ func CreateCodeComment(doer *models.User, gitRepo *git.Repository, issue *models
return nil, err
}
- notification.NotifyCreateIssueComment(doer, issue.Repo, issue, comment)
+ mentions, err := issue.FindAndUpdateIssueMentions(models.DefaultDBContext(), doer, comment.Content)
+ if err != nil {
+ return nil, err
+ }
+
+ notification.NotifyCreateIssueComment(doer, issue.Repo, issue, comment, mentions)
return comment, nil
}
@@ -226,7 +231,25 @@ func SubmitReview(doer *models.User, gitRepo *git.Repository, issue *models.Issu
return nil, nil, err
}
- notification.NotifyPullRequestReview(pr, review, comm)
+ ctx := models.DefaultDBContext()
+ mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, comm.Content)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ notification.NotifyPullRequestReview(pr, review, comm, mentions)
+
+ for _, lines := range review.CodeComments {
+ for _, comments := range lines {
+ for _, codeComment := range comments {
+ mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, codeComment.Content)
+ if err != nil {
+ return nil, nil, err
+ }
+ notification.NotifyPullRequestCodeComment(pr, codeComment, mentions)
+ }
+ }
+ }
return review, comm, nil
}