Skip to content

Commit 9faf2e3

Browse files
committed
utilize map
1 parent 084a2b0 commit 9faf2e3

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

services/mailer/mail_issue.go

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,75 +42,80 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []int64) e
4242
}
4343

4444
// Enough room to avoid reallocations
45-
unfiltered := make([]int64, 1, 64)
45+
toNotify := make(map[int64]struct{}, 64)
4646

4747
// =========== Original poster ===========
48-
unfiltered[0] = ctx.Issue.PosterID
48+
toNotify[ctx.Issue.PosterID] = struct{}{}
4949

5050
// =========== Assignees ===========
5151
ids, err := models.GetAssigneeIDsByIssue(ctx.Issue.ID)
5252
if err != nil {
5353
return fmt.Errorf("GetAssigneeIDsByIssue(%d): %v", ctx.Issue.ID, err)
5454
}
55-
unfiltered = append(unfiltered, ids...)
55+
for _, id := range ids {
56+
toNotify[id] = struct{}{}
57+
}
5658

5759
// =========== Participants (i.e. commenters, reviewers) ===========
5860
ids, err = models.GetParticipantsIDsByIssueID(ctx.Issue.ID)
5961
if err != nil {
6062
return fmt.Errorf("GetParticipantsIDsByIssueID(%d): %v", ctx.Issue.ID, err)
6163
}
62-
unfiltered = append(unfiltered, ids...)
64+
for _, id := range ids {
65+
toNotify[id] = struct{}{}
66+
}
6367

6468
// =========== Issue watchers ===========
6569
ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID)
6670
if err != nil {
6771
return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err)
6872
}
69-
unfiltered = append(unfiltered, ids...)
73+
for _, id := range ids {
74+
toNotify[id] = struct{}{}
75+
}
7076

7177
// =========== Repo watchers ===========
7278
// Make repo watchers last, since it's likely the list with the most users
7379
ids, err = models.GetRepoWatchersIDs(ctx.Issue.RepoID)
7480
if err != nil {
7581
return fmt.Errorf("GetRepoWatchersIDs(%d): %v", ctx.Issue.RepoID, err)
7682
}
77-
unfiltered = append(ids, unfiltered...)
78-
79-
visited := make(map[int64]bool, len(unfiltered)+len(mentions)+1)
83+
for _, id := range ids {
84+
toNotify[id] = struct{}{}
85+
}
8086

8187
// Avoid mailing the doer
82-
visited[ctx.Doer.ID] = true
83-
84-
if err = mailIssueCommentBatch(ctx, unfiltered, visited, false); err != nil {
85-
return fmt.Errorf("mailIssueCommentBatch(): %v", err)
86-
}
88+
delete(toNotify, ctx.Doer.ID)
8789

8890
// =========== Mentions ===========
89-
if err = mailIssueCommentBatch(ctx, mentions, visited, true); err != nil {
91+
for _, m := range mentions {
92+
delete(toNotify, m)
93+
}
94+
if err = mailIssueCommentBatch(ctx, mentions, true); err != nil {
9095
return fmt.Errorf("mailIssueCommentBatch() mentions: %v", err)
9196
}
9297

98+
notify := make([]int64, len(toNotify))
99+
for id := range toNotify {
100+
notify = append(notify, id)
101+
}
102+
if err = mailIssueCommentBatch(ctx, notify, false); err != nil {
103+
return fmt.Errorf("mailIssueCommentBatch(): %v", err)
104+
}
93105
return nil
94106
}
95107

96-
func mailIssueCommentBatch(ctx *mailCommentContext, ids []int64, visited map[int64]bool, fromMention bool) error {
108+
func mailIssueCommentBatch(ctx *mailCommentContext, ids []int64, fromMention bool) error {
97109
const batchSize = 100
98110
for i := 0; i < len(ids); i += batchSize {
99-
var last int
111+
job := make([]int64, batchSize)
100112
if i+batchSize < len(ids) {
101-
last = i + batchSize
113+
job = ids[i : i+batchSize]
102114
} else {
103-
last = len(ids)
104-
}
105-
unique := make([]int64, 0, last-i)
106-
for j := i; j < last; j++ {
107-
id := ids[j]
108-
if _, ok := visited[id]; !ok {
109-
unique = append(unique, id)
110-
visited[id] = true
111-
}
115+
job = ids[i:]
112116
}
113-
recipients, err := models.GetMaileableUsersByIDs(unique)
117+
118+
recipients, err := models.GetMaileableUsersByIDs(job)
114119
if err != nil {
115120
return err
116121
}

0 commit comments

Comments
 (0)