Files

221 lines
7.7 KiB
Go
Raw Permalink Normal View History

// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package mailer
import (
"context"
"fmt"
activities_model "code.gitea.io/gitea/models/activities"
issues_model "code.gitea.io/gitea/models/issues"
access_model "code.gitea.io/gitea/models/perm/access"
2021-12-12 23:48:20 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-10 03:57:58 +08:00
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
2022-10-12 07:18:26 +02:00
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)
2025-03-03 11:31:47 +08:00
const MailBatchSize = 100 // batch size used in mailIssueCommentBatch
2021-04-02 12:25:13 +02:00
// mailIssueCommentToParticipants can be used for both new issue creation and comment.
// This function sends two list of emails:
// 1. Repository watchers (except for WIP pull requests) and users who are participated in comments.
// 2. Users who are not in 1. but get mentioned in current issue/comment.
2025-03-06 21:57:00 +01:00
func mailIssueCommentToParticipants(ctx context.Context, comment *mailComment, mentions []*user_model.User) error {
// Required by the mail composer; make sure to load these before calling the async function
2025-03-06 21:57:00 +01:00
if err := comment.Issue.LoadRepo(ctx); err != nil {
return fmt.Errorf("LoadRepo: %w", err)
}
2025-03-06 21:57:00 +01:00
if err := comment.Issue.LoadPoster(ctx); err != nil {
return fmt.Errorf("LoadPoster: %w", err)
}
2025-03-06 21:57:00 +01:00
if err := comment.Issue.LoadPullRequest(ctx); err != nil {
return fmt.Errorf("LoadPullRequest: %w", err)
}
// Enough room to avoid reallocations
unfiltered := make([]int64, 1, 64)
// =========== Original poster ===========
2025-03-06 21:57:00 +01:00
unfiltered[0] = comment.Issue.PosterID
// =========== Assignees ===========
2025-03-06 21:57:00 +01:00
ids, err := issues_model.GetAssigneeIDsByIssue(ctx, comment.Issue.ID)
if err != nil {
2025-03-06 21:57:00 +01:00
return fmt.Errorf("GetAssigneeIDsByIssue(%d): %w", comment.Issue.ID, err)
}
unfiltered = append(unfiltered, ids...)
// =========== Participants (i.e. commenters, reviewers) ===========
2025-03-06 21:57:00 +01:00
ids, err = issues_model.GetParticipantsIDsByIssueID(ctx, comment.Issue.ID)
2018-05-09 18:29:04 +02:00
if err != nil {
2025-03-06 21:57:00 +01:00
return fmt.Errorf("GetParticipantsIDsByIssueID(%d): %w", comment.Issue.ID, err)
2018-05-09 18:29:04 +02:00
}
unfiltered = append(unfiltered, ids...)
2018-05-09 18:29:04 +02:00
// =========== Issue watchers ===========
2025-03-06 21:57:00 +01:00
ids, err = issues_model.GetIssueWatchersIDs(ctx, comment.Issue.ID, true)
if err != nil {
2025-03-06 21:57:00 +01:00
return fmt.Errorf("GetIssueWatchersIDs(%d): %w", comment.Issue.ID, err)
2017-06-23 15:43:37 +02:00
}
unfiltered = append(unfiltered, ids...)
2017-06-23 15:43:37 +02:00
// =========== Repo watchers ===========
// Make repo watchers last, since it's likely the list with the most users
2025-03-06 21:57:00 +01:00
if !(comment.Issue.IsPull && comment.Issue.PullRequest.IsWorkInProgress(ctx) && comment.ActionType != activities_model.ActionCreatePullRequest) {
ids, err = repo_model.GetRepoWatchersIDs(ctx, comment.Issue.RepoID)
if err != nil {
2025-03-06 21:57:00 +01:00
return fmt.Errorf("GetRepoWatchersIDs(%d): %w", comment.Issue.RepoID, err)
}
unfiltered = append(ids, unfiltered...)
}
2022-10-12 07:18:26 +02:00
visited := make(container.Set[int64], len(unfiltered)+len(mentions)+1)
// Avoid mailing the doer
2025-03-06 21:57:00 +01:00
if comment.Doer.EmailNotificationsPreference != user_model.EmailNotificationsAndYourOwn && !comment.ForceDoerNotification {
visited.Add(comment.Doer.ID)
}
// =========== Mentions ===========
2025-03-06 21:57:00 +01:00
if err = mailIssueCommentBatch(ctx, comment, mentions, visited, true); err != nil {
return fmt.Errorf("mailIssueCommentBatch() mentions: %w", err)
}
// Avoid mailing explicit unwatched
2025-03-06 21:57:00 +01:00
ids, err = issues_model.GetIssueWatchersIDs(ctx, comment.Issue.ID, false)
if err != nil {
2025-03-06 21:57:00 +01:00
return fmt.Errorf("GetIssueWatchersIDs(%d): %w", comment.Issue.ID, err)
}
2022-10-12 07:18:26 +02:00
visited.AddMultiple(ids...)
2019-01-29 22:43:40 +00:00
2025-01-30 07:33:50 +08:00
unfilteredUsers, err := user_model.GetMailableUsersByIDs(ctx, unfiltered, false)
2021-04-02 12:25:13 +02:00
if err != nil {
return err
}
2025-03-06 21:57:00 +01:00
if err = mailIssueCommentBatch(ctx, comment, unfilteredUsers, visited, false); err != nil {
return fmt.Errorf("mailIssueCommentBatch(): %w", err)
}
return nil
}
2025-03-06 21:57:00 +01:00
func mailIssueCommentBatch(ctx context.Context, comment *mailComment, users []*user_model.User, visited container.Set[int64], fromMention bool) error {
2021-11-10 03:57:58 +08:00
checkUnit := unit.TypeIssues
2025-03-06 21:57:00 +01:00
if comment.Issue.IsPull {
2021-11-10 03:57:58 +08:00
checkUnit = unit.TypePullRequests
2021-04-02 12:25:13 +02:00
}
langMap := make(map[string][]*user_model.User)
2021-04-02 12:25:13 +02:00
for _, user := range users {
if !user.IsActive {
// Exclude deactivated users
continue
}
2021-04-02 12:25:13 +02: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 ...
if !(user.EmailNotificationsPreference == user_model.EmailNotificationsEnabled ||
user.EmailNotificationsPreference == user_model.EmailNotificationsAndYourOwn ||
fromMention && user.EmailNotificationsPreference == user_model.EmailNotificationsOnMention) {
2021-04-02 12:25:13 +02:00
continue
}
2021-04-02 12:25:13 +02:00
// if we have already visited this user we exclude them
2022-10-12 07:18:26 +02:00
if !visited.Add(user.ID) {
2021-04-02 12:25:13 +02:00
continue
}
2021-04-02 12:25:13 +02:00
// test if this user is allowed to see the issue/pull
2025-03-06 21:57:00 +01:00
if !access_model.CheckRepoUnitUser(ctx, comment.Issue.Repo, user, checkUnit) {
2021-04-02 12:25:13 +02:00
continue
}
2021-05-22 08:47:16 +02:00
langMap[user.Language] = append(langMap[user.Language], user)
2021-04-02 12:25:13 +02: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 {
2025-03-06 21:57:00 +01:00
msgs, err := composeIssueCommentMessages(ctx, comment, lang, receivers[i:], fromMention, "issue comments")
2021-04-20 06:25:08 +08:00
if err != nil {
return err
}
2023-10-31 22:11:48 +08:00
SendAsync(msgs...)
2021-04-02 12:25:13 +02:00
receivers = receivers[:i]
}
}
2021-04-02 12:25:13 +02:00
return nil
}
// MailParticipants sends new issue thread created emails to repository watchers
// and mentioned people.
func MailParticipants(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, opType activities_model.ActionType, mentions []*user_model.User) error {
if setting.MailService == nil {
// No mail service configured
return nil
}
content := issue.Content
if opType == activities_model.ActionCloseIssue || opType == activities_model.ActionClosePullRequest ||
opType == activities_model.ActionReopenIssue || opType == activities_model.ActionReopenPullRequest ||
opType == activities_model.ActionMergePullRequest || opType == activities_model.ActionAutoMergePullRequest {
content = ""
}
forceDoerNotification := opType == activities_model.ActionAutoMergePullRequest
2025-03-06 21:57:00 +01:00
if err := mailIssueCommentToParticipants(ctx,
&mailComment{
Issue: issue,
Doer: doer,
ActionType: opType,
Content: content,
Comment: nil,
ForceDoerNotification: forceDoerNotification,
2021-04-02 12:25:13 +02:00
}, mentions); err != nil {
log.Error("mailIssueCommentToParticipants: %v", err)
}
return nil
}
2025-03-03 11:31:47 +08:00
// SendIssueAssignedMail composes and sends issue assigned email
func SendIssueAssignedMail(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, content string, comment *issues_model.Comment, recipients []*user_model.User) error {
if setting.MailService == nil {
// No mail service configured
return nil
}
if err := issue.LoadRepo(ctx); err != nil {
log.Error("Unable to load repo [%d] for issue #%d [%d]. Error: %v", issue.RepoID, issue.Index, issue.ID, err)
return err
}
langMap := make(map[string][]*user_model.User)
for _, user := range recipients {
if !user.IsActive {
// don't send emails to inactive users
continue
}
langMap[user.Language] = append(langMap[user.Language], user)
}
for lang, tos := range langMap {
2025-03-06 21:57:00 +01:00
msgs, err := composeIssueCommentMessages(ctx, &mailComment{
2025-03-03 11:31:47 +08:00
Issue: issue,
Doer: doer,
ActionType: activities_model.ActionType(0),
Content: content,
Comment: comment,
}, lang, tos, false, "issue assigned")
if err != nil {
return err
}
SendAsync(msgs...)
}
return nil
}