2019-09-24 10:32:49 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-07-15 22:06:39 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-09-24 10:32:49 +05:30
|
|
|
package mailer
|
2016-07-15 22:06:39 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-09-24 10:32:49 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-07-15 22:06:39 +05:30
|
|
|
)
|
|
|
|
|
2019-11-07 19:04:28 +05:30
|
|
|
func fallbackMailSubject(issue *models.Issue) string {
|
2019-05-28 15:11:48 +05:30
|
|
|
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
|
2016-07-15 22:06:39 +05:30
|
|
|
}
|
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
type mailCommentContext struct {
|
|
|
|
Issue *models.Issue
|
|
|
|
Doer *models.User
|
|
|
|
ActionType models.ActionType
|
|
|
|
Content string
|
|
|
|
Comment *models.Comment
|
|
|
|
}
|
|
|
|
|
2021-04-02 15:55:13 +05:30
|
|
|
const (
|
|
|
|
// MailBatchSize set the batch size used in mailIssueCommentBatch
|
|
|
|
MailBatchSize = 100
|
|
|
|
)
|
|
|
|
|
2016-07-15 22:06:39 +05:30
|
|
|
// mailIssueCommentToParticipants can be used for both new issue creation and comment.
|
2017-03-16 07:04:24 +05:30
|
|
|
// This function sends two list of emails:
|
2021-06-23 09:44:22 +05:30
|
|
|
// 1. Repository watchers (except for WIP pull requests) and users who are participated in comments.
|
2017-03-16 07:04:24 +05:30
|
|
|
// 2. Users who are not in 1. but get mentioned in current issue/comment.
|
2021-04-02 15:55:13 +05:30
|
|
|
func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*models.User) error {
|
2016-07-15 22:06:39 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
// Required by the mail composer; make sure to load these before calling the async function
|
|
|
|
if err := ctx.Issue.LoadRepo(); err != nil {
|
|
|
|
return fmt.Errorf("LoadRepo(): %v", err)
|
2017-03-16 07:04:24 +05:30
|
|
|
}
|
2019-11-18 13:38:20 +05:30
|
|
|
if err := ctx.Issue.LoadPoster(); err != nil {
|
|
|
|
return fmt.Errorf("LoadPoster(): %v", err)
|
|
|
|
}
|
|
|
|
if err := ctx.Issue.LoadPullRequest(); err != nil {
|
|
|
|
return fmt.Errorf("LoadPullRequest(): %v", err)
|
2017-03-16 07:04:24 +05:30
|
|
|
}
|
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
// Enough room to avoid reallocations
|
|
|
|
unfiltered := make([]int64, 1, 64)
|
|
|
|
|
|
|
|
// =========== Original poster ===========
|
|
|
|
unfiltered[0] = ctx.Issue.PosterID
|
|
|
|
|
|
|
|
// =========== Assignees ===========
|
|
|
|
ids, err := models.GetAssigneeIDsByIssue(ctx.Issue.ID)
|
2017-09-16 05:48:25 +05:30
|
|
|
if err != nil {
|
2019-11-18 13:38:20 +05:30
|
|
|
return fmt.Errorf("GetAssigneeIDsByIssue(%d): %v", ctx.Issue.ID, err)
|
2016-07-15 22:06:39 +05:30
|
|
|
}
|
2019-11-18 13:38:20 +05:30
|
|
|
unfiltered = append(unfiltered, ids...)
|
2016-07-15 22:06:39 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
// =========== Participants (i.e. commenters, reviewers) ===========
|
|
|
|
ids, err = models.GetParticipantsIDsByIssueID(ctx.Issue.ID)
|
2018-05-09 21:59:04 +05:30
|
|
|
if err != nil {
|
2019-11-18 13:38:20 +05:30
|
|
|
return fmt.Errorf("GetParticipantsIDsByIssueID(%d): %v", ctx.Issue.ID, err)
|
2018-05-09 21:59:04 +05:30
|
|
|
}
|
2019-11-18 13:38:20 +05:30
|
|
|
unfiltered = append(unfiltered, ids...)
|
2018-05-09 21:59:04 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
// =========== Issue watchers ===========
|
2020-02-27 15:37:05 +05:30
|
|
|
ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, true)
|
2019-11-18 13:38:20 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err)
|
2017-06-23 19:13:37 +05:30
|
|
|
}
|
2019-11-18 13:38:20 +05:30
|
|
|
unfiltered = append(unfiltered, ids...)
|
2017-06-23 19:13:37 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
// =========== Repo watchers ===========
|
|
|
|
// Make repo watchers last, since it's likely the list with the most users
|
2021-06-23 09:44:22 +05:30
|
|
|
if !(ctx.Issue.IsPull && ctx.Issue.PullRequest.IsWorkInProgress() && ctx.ActionType != models.ActionCreatePullRequest) {
|
|
|
|
ids, err = models.GetRepoWatchersIDs(ctx.Issue.RepoID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepoWatchersIDs(%d): %v", ctx.Issue.RepoID, err)
|
|
|
|
}
|
|
|
|
unfiltered = append(ids, unfiltered...)
|
2016-07-15 22:06:39 +05:30
|
|
|
}
|
2017-03-16 07:04:24 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
visited := make(map[int64]bool, len(unfiltered)+len(mentions)+1)
|
2017-05-25 08:08:56 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
// Avoid mailing the doer
|
|
|
|
visited[ctx.Doer.ID] = true
|
2021-01-02 22:34:02 +05:30
|
|
|
|
|
|
|
// =========== Mentions ===========
|
|
|
|
if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil {
|
|
|
|
return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-27 15:37:05 +05:30
|
|
|
// 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
|
|
|
|
}
|
2019-01-30 04:13:40 +05:30
|
|
|
|
2021-04-02 15:55:13 +05:30
|
|
|
unfilteredUsers, err := models.GetMaileableUsersByIDs(unfiltered, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = mailIssueCommentBatch(ctx, unfilteredUsers, visited, false); err != nil {
|
2019-11-18 13:38:20 +05:30
|
|
|
return fmt.Errorf("mailIssueCommentBatch(): %v", err)
|
2018-08-24 10:11:26 +05:30
|
|
|
}
|
2016-07-15 22:06:39 +05:30
|
|
|
|
2019-11-18 13:38:20 +05:30
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 10:11:26 +05:30
|
|
|
|
2021-04-02 15:55:13 +05:30
|
|
|
func mailIssueCommentBatch(ctx *mailCommentContext, users []*models.User, visited map[int64]bool, fromMention bool) error {
|
|
|
|
checkUnit := models.UnitTypeIssues
|
|
|
|
if ctx.Issue.IsPull {
|
|
|
|
checkUnit = models.UnitTypePullRequests
|
|
|
|
}
|
|
|
|
|
2021-05-22 12:17:16 +05:30
|
|
|
langMap := make(map[string][]*models.User)
|
2021-04-02 15:55:13 +05:30
|
|
|
for _, user := range users {
|
|
|
|
// At this point we exclude:
|
|
|
|
// user that don't have all mails enabled or users only get mail on mention and this is one ...
|
|
|
|
if !(user.EmailNotificationsPreference == models.EmailNotificationsEnabled ||
|
|
|
|
fromMention && user.EmailNotificationsPreference == models.EmailNotificationsOnMention) {
|
|
|
|
continue
|
2019-11-18 13:38:20 +05:30
|
|
|
}
|
2020-12-04 01:55:49 +05:30
|
|
|
|
2021-04-02 15:55:13 +05:30
|
|
|
// if we have already visited this user we exclude them
|
|
|
|
if _, ok := visited[user.ID]; ok {
|
|
|
|
continue
|
2021-01-09 23:04:08 +05:30
|
|
|
}
|
2021-04-02 15:55:13 +05:30
|
|
|
|
|
|
|
// now mark them as visited
|
|
|
|
visited[user.ID] = true
|
|
|
|
|
|
|
|
// test if this user is allowed to see the issue/pull
|
|
|
|
if !ctx.Issue.Repo.CheckUnitUser(user, checkUnit) {
|
|
|
|
continue
|
2020-12-04 01:55:49 +05:30
|
|
|
}
|
|
|
|
|
2021-05-22 12:17:16 +05:30
|
|
|
langMap[user.Language] = append(langMap[user.Language], user)
|
2021-04-02 15:55:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for lang, receivers := range langMap {
|
|
|
|
// because we know that the len(receivers) > 0 and we don't care about the order particularly
|
|
|
|
// working backwards from the last (possibly) incomplete batch. If len(receivers) can be 0 this
|
|
|
|
// starting condition will need to be changed slightly
|
|
|
|
for i := ((len(receivers) - 1) / MailBatchSize) * MailBatchSize; i >= 0; i -= MailBatchSize {
|
2021-04-20 03:55:08 +05:30
|
|
|
msgs, err := composeIssueCommentMessages(ctx, lang, receivers[i:], fromMention, "issue comments")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
SendAsyncs(msgs)
|
2021-04-02 15:55:13 +05:30
|
|
|
receivers = receivers[:i]
|
2019-11-18 13:38:20 +05:30
|
|
|
}
|
2018-08-24 10:11:26 +05:30
|
|
|
}
|
2021-04-02 15:55:13 +05:30
|
|
|
|
2016-07-15 22:06:39 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MailParticipants sends new issue thread created emails to repository watchers
|
|
|
|
// and mentioned people.
|
2021-01-02 22:34:02 +05:30
|
|
|
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) error {
|
2021-05-30 15:08:38 +05:30
|
|
|
content := issue.Content
|
|
|
|
if opType == models.ActionCloseIssue || opType == models.ActionClosePullRequest ||
|
|
|
|
opType == models.ActionReopenIssue || opType == models.ActionReopenPullRequest ||
|
|
|
|
opType == models.ActionMergePullRequest {
|
|
|
|
content = ""
|
|
|
|
}
|
2021-04-02 15:55:13 +05:30
|
|
|
if err := mailIssueCommentToParticipants(
|
2019-11-18 13:38:20 +05:30
|
|
|
&mailCommentContext{
|
|
|
|
Issue: issue,
|
|
|
|
Doer: doer,
|
|
|
|
ActionType: opType,
|
2021-05-30 15:08:38 +05:30
|
|
|
Content: content,
|
2019-11-18 13:38:20 +05:30
|
|
|
Comment: nil,
|
2021-04-02 15:55:13 +05:30
|
|
|
}, mentions); err != nil {
|
2019-11-07 19:04:28 +05:30
|
|
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
2016-07-15 22:06:39 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|