2019-09-24 01:02:49 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-07-15 12:36:39 -04:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-09-24 01:02:49 -04:00
|
|
|
package mailer
|
2016-07-15 12:36:39 -04:00
|
|
|
|
|
|
|
import (
|
2022-01-19 18:26:57 -05:00
|
|
|
"context"
|
2016-07-15 12:36:39 -04:00
|
|
|
"fmt"
|
|
|
|
|
2019-09-24 01:02:49 -04:00
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-12 10:48:20 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 14:57:58 -05:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-08-12 03:26:33 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2016-07-15 12:36:39 -04:00
|
|
|
)
|
|
|
|
|
2019-11-07 08:34:28 -05:00
|
|
|
func fallbackMailSubject(issue *models.Issue) string {
|
2019-05-28 05:41:48 -04:00
|
|
|
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
|
2016-07-15 12:36:39 -04:00
|
|
|
}
|
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
type mailCommentContext struct {
|
2022-01-19 18:26:57 -05:00
|
|
|
context.Context
|
2019-11-18 03:08:20 -05:00
|
|
|
Issue *models.Issue
|
2021-11-24 04:49:20 -05:00
|
|
|
Doer *user_model.User
|
2019-11-18 03:08:20 -05:00
|
|
|
ActionType models.ActionType
|
|
|
|
Content string
|
|
|
|
Comment *models.Comment
|
|
|
|
}
|
|
|
|
|
2021-04-02 06:25:13 -04:00
|
|
|
const (
|
|
|
|
// MailBatchSize set the batch size used in mailIssueCommentBatch
|
|
|
|
MailBatchSize = 100
|
|
|
|
)
|
|
|
|
|
2016-07-15 12:36:39 -04:00
|
|
|
// mailIssueCommentToParticipants can be used for both new issue creation and comment.
|
2017-03-15 21:34:24 -04:00
|
|
|
// This function sends two list of emails:
|
2021-06-23 00:14:22 -04:00
|
|
|
// 1. Repository watchers (except for WIP pull requests) and users who are participated in comments.
|
2017-03-15 21:34:24 -04:00
|
|
|
// 2. Users who are not in 1. but get mentioned in current issue/comment.
|
2021-11-24 04:49:20 -05:00
|
|
|
func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_model.User) error {
|
2019-11-18 03:08:20 -05:00
|
|
|
// Required by the mail composer; make sure to load these before calling the async function
|
2022-04-08 05:11:15 -04:00
|
|
|
if err := ctx.Issue.LoadRepo(ctx); err != nil {
|
2019-11-18 03:08:20 -05:00
|
|
|
return fmt.Errorf("LoadRepo(): %v", err)
|
2017-03-15 21:34:24 -04:00
|
|
|
}
|
2019-11-18 03:08:20 -05:00
|
|
|
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-15 21:34:24 -04:00
|
|
|
}
|
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
// 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-15 20:18:25 -04:00
|
|
|
if err != nil {
|
2019-11-18 03:08:20 -05:00
|
|
|
return fmt.Errorf("GetAssigneeIDsByIssue(%d): %v", ctx.Issue.ID, err)
|
2016-07-15 12:36:39 -04:00
|
|
|
}
|
2019-11-18 03:08:20 -05:00
|
|
|
unfiltered = append(unfiltered, ids...)
|
2016-07-15 12:36:39 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
// =========== Participants (i.e. commenters, reviewers) ===========
|
|
|
|
ids, err = models.GetParticipantsIDsByIssueID(ctx.Issue.ID)
|
2018-05-09 12:29:04 -04:00
|
|
|
if err != nil {
|
2019-11-18 03:08:20 -05:00
|
|
|
return fmt.Errorf("GetParticipantsIDsByIssueID(%d): %v", ctx.Issue.ID, err)
|
2018-05-09 12:29:04 -04:00
|
|
|
}
|
2019-11-18 03:08:20 -05:00
|
|
|
unfiltered = append(unfiltered, ids...)
|
2018-05-09 12:29:04 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
// =========== Issue watchers ===========
|
2022-05-20 10:08:52 -04:00
|
|
|
ids, err = models.GetIssueWatchersIDs(ctx, ctx.Issue.ID, true)
|
2019-11-18 03:08:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err)
|
2017-06-23 09:43:37 -04:00
|
|
|
}
|
2019-11-18 03:08:20 -05:00
|
|
|
unfiltered = append(unfiltered, ids...)
|
2017-06-23 09:43:37 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
// =========== Repo watchers ===========
|
|
|
|
// Make repo watchers last, since it's likely the list with the most users
|
2021-06-23 00:14:22 -04:00
|
|
|
if !(ctx.Issue.IsPull && ctx.Issue.PullRequest.IsWorkInProgress() && ctx.ActionType != models.ActionCreatePullRequest) {
|
2022-03-22 11:22:54 -04:00
|
|
|
ids, err = repo_model.GetRepoWatchersIDs(ctx, ctx.Issue.RepoID)
|
2021-06-23 00:14:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepoWatchersIDs(%d): %v", ctx.Issue.RepoID, err)
|
|
|
|
}
|
|
|
|
unfiltered = append(ids, unfiltered...)
|
2016-07-15 12:36:39 -04:00
|
|
|
}
|
2017-03-15 21:34:24 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
visited := make(map[int64]bool, len(unfiltered)+len(mentions)+1)
|
2017-05-24 22:38:56 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
// Avoid mailing the doer
|
|
|
|
visited[ctx.Doer.ID] = true
|
2021-01-02 12:04:02 -05:00
|
|
|
|
|
|
|
// =========== Mentions ===========
|
|
|
|
if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil {
|
|
|
|
return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-27 05:07:05 -05:00
|
|
|
// Avoid mailing explicit unwatched
|
2022-05-20 10:08:52 -04:00
|
|
|
ids, err = models.GetIssueWatchersIDs(ctx, ctx.Issue.ID, false)
|
2020-02-27 05:07:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err)
|
|
|
|
}
|
|
|
|
for _, i := range ids {
|
|
|
|
visited[i] = true
|
|
|
|
}
|
2019-01-29 17:43:40 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
unfilteredUsers, err := user_model.GetMaileableUsersByIDs(unfiltered, false)
|
2021-04-02 06:25:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = mailIssueCommentBatch(ctx, unfilteredUsers, visited, false); err != nil {
|
2019-11-18 03:08:20 -05:00
|
|
|
return fmt.Errorf("mailIssueCommentBatch(): %v", err)
|
2018-08-24 00:41:26 -04:00
|
|
|
}
|
2016-07-15 12:36:39 -04:00
|
|
|
|
2019-11-18 03:08:20 -05:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 00:41:26 -04:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, visited map[int64]bool, fromMention bool) error {
|
2021-11-09 14:57:58 -05:00
|
|
|
checkUnit := unit.TypeIssues
|
2021-04-02 06:25:13 -04:00
|
|
|
if ctx.Issue.IsPull {
|
2021-11-09 14:57:58 -05:00
|
|
|
checkUnit = unit.TypePullRequests
|
2021-04-02 06:25:13 -04:00
|
|
|
}
|
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
langMap := make(map[string][]*user_model.User)
|
2021-04-02 06:25:13 -04:00
|
|
|
for _, user := range users {
|
2022-03-19 12:20:03 -04:00
|
|
|
if !user.IsActive {
|
|
|
|
// Exclude deactivated users
|
|
|
|
continue
|
|
|
|
}
|
2021-04-02 06:25:13 -04:00
|
|
|
// At this point we exclude:
|
|
|
|
// user that don't have all mails enabled or users only get mail on mention and this is one ...
|
2021-11-24 04:49:20 -05:00
|
|
|
if !(user.EmailNotificationsPreference == user_model.EmailNotificationsEnabled ||
|
|
|
|
fromMention && user.EmailNotificationsPreference == user_model.EmailNotificationsOnMention) {
|
2021-04-02 06:25:13 -04:00
|
|
|
continue
|
2019-11-18 03:08:20 -05:00
|
|
|
}
|
2020-12-03 15:25:49 -05:00
|
|
|
|
2021-04-02 06:25:13 -04:00
|
|
|
// if we have already visited this user we exclude them
|
|
|
|
if _, ok := visited[user.ID]; ok {
|
|
|
|
continue
|
2021-01-09 12:34:08 -05:00
|
|
|
}
|
2021-04-02 06:25:13 -04:00
|
|
|
|
|
|
|
// now mark them as visited
|
|
|
|
visited[user.ID] = true
|
|
|
|
|
|
|
|
// test if this user is allowed to see the issue/pull
|
2021-12-09 20:27:50 -05:00
|
|
|
if !models.CheckRepoUnitUser(ctx.Issue.Repo, user, checkUnit) {
|
2021-04-02 06:25:13 -04:00
|
|
|
continue
|
2020-12-03 15:25:49 -05:00
|
|
|
}
|
|
|
|
|
2021-05-22 02:47:16 -04:00
|
|
|
langMap[user.Language] = append(langMap[user.Language], user)
|
2021-04-02 06:25:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-19 18:25:08 -04:00
|
|
|
msgs, err := composeIssueCommentMessages(ctx, lang, receivers[i:], fromMention, "issue comments")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
SendAsyncs(msgs)
|
2021-04-02 06:25:13 -04:00
|
|
|
receivers = receivers[:i]
|
2019-11-18 03:08:20 -05:00
|
|
|
}
|
2018-08-24 00:41:26 -04:00
|
|
|
}
|
2021-04-02 06:25:13 -04:00
|
|
|
|
2016-07-15 12:36:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MailParticipants sends new issue thread created emails to repository watchers
|
|
|
|
// and mentioned people.
|
2021-11-24 04:49:20 -05:00
|
|
|
func MailParticipants(issue *models.Issue, doer *user_model.User, opType models.ActionType, mentions []*user_model.User) error {
|
2021-08-12 03:26:33 -04:00
|
|
|
if setting.MailService == nil {
|
|
|
|
// No mail service configured
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-30 05:38:38 -04:00
|
|
|
content := issue.Content
|
|
|
|
if opType == models.ActionCloseIssue || opType == models.ActionClosePullRequest ||
|
|
|
|
opType == models.ActionReopenIssue || opType == models.ActionReopenPullRequest ||
|
|
|
|
opType == models.ActionMergePullRequest {
|
|
|
|
content = ""
|
|
|
|
}
|
2021-04-02 06:25:13 -04:00
|
|
|
if err := mailIssueCommentToParticipants(
|
2019-11-18 03:08:20 -05:00
|
|
|
&mailCommentContext{
|
2022-03-22 11:22:54 -04:00
|
|
|
Context: context.TODO(), // TODO: use a correct context
|
2019-11-18 03:08:20 -05:00
|
|
|
Issue: issue,
|
|
|
|
Doer: doer,
|
|
|
|
ActionType: opType,
|
2021-05-30 05:38:38 -04:00
|
|
|
Content: content,
|
2019-11-18 03:08:20 -05:00
|
|
|
Comment: nil,
|
2021-04-02 06:25:13 -04:00
|
|
|
}, mentions); err != nil {
|
2019-11-07 08:34:28 -05:00
|
|
|
log.Error("mailIssueCommentToParticipants: %v", err)
|
2016-07-15 12:36:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|