Skip to content

Commit beab2df

Browse files
lunnylafriks
authored andcommitted
Refactor mail notification (#5110)
* mail notification implement interface * fix file comment year * use NullNotifier as parent struct of notifiers
1 parent e5228b8 commit beab2df

File tree

8 files changed

+190
-55
lines changed

8 files changed

+190
-55
lines changed

models/issue.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,9 +1121,6 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
11211121
}); err != nil {
11221122
log.Error(4, "NotifyWatchers: %v", err)
11231123
}
1124-
if err = issue.MailParticipants(); err != nil {
1125-
log.Error(4, "MailParticipants: %v", err)
1126-
}
11271124

11281125
mode, _ := AccessLevel(issue.Poster, issue.Repo)
11291126
if err = PrepareWebhooks(repo, HookEventIssues, &api.IssuePayload{

models/issue_comment.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,11 @@ func (c *Comment) LoadDepIssueDetails() (err error) {
361361

362362
// MailParticipants sends new comment emails to repository watchers
363363
// and mentioned people.
364-
func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
364+
func (c *Comment) MailParticipants(opType ActionType, issue *Issue) (err error) {
365+
return c.mailParticipants(x, opType, issue)
366+
}
367+
368+
func (c *Comment) mailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
365369
mentions := markup.FindAllMentions(c.Content)
366370
if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil {
367371
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
@@ -632,9 +636,6 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
632636
if err = notifyWatchers(e, act); err != nil {
633637
log.Error(4, "notifyWatchers: %v", err)
634638
}
635-
if err = comment.MailParticipants(e, act.OpType, opts.Issue); err != nil {
636-
log.Error(4, "MailParticipants: %v", err)
637-
}
638639
}
639640
return nil
640641
}

models/pull.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
845845
IsPrivate: repo.IsPrivate,
846846
}); err != nil {
847847
log.Error(4, "NotifyWatchers: %v", err)
848-
} else if err = pull.MailParticipants(); err != nil {
849-
log.Error(4, "MailParticipants: %v", err)
850848
}
851849

852850
pr.Issue = pull

modules/notification/base/null.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package base
6+
7+
import (
8+
"code.gitea.io/git"
9+
"code.gitea.io/gitea/models"
10+
)
11+
12+
// NullNotifier implements a blank notifier
13+
type NullNotifier struct {
14+
}
15+
16+
var (
17+
_ Notifier = &NullNotifier{}
18+
)
19+
20+
// Run places a place holder function
21+
func (*NullNotifier) Run() {
22+
}
23+
24+
// NotifyCreateIssueComment places a place holder function
25+
func (*NullNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
26+
issue *models.Issue, comment *models.Comment) {
27+
}
28+
29+
// NotifyNewIssue places a place holder function
30+
func (*NullNotifier) NotifyNewIssue(issue *models.Issue) {
31+
}
32+
33+
// NotifyIssueChangeStatus places a place holder function
34+
func (*NullNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, isClosed bool) {
35+
}
36+
37+
// NotifyNewPullRequest places a place holder function
38+
func (*NullNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
39+
}
40+
41+
// NotifyPullRequestReview places a place holder function
42+
func (*NullNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, comment *models.Comment) {
43+
}
44+
45+
// NotifyMergePullRequest places a place holder function
46+
func (*NullNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
47+
}
48+
49+
// NotifyUpdateComment places a place holder function
50+
func (*NullNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
51+
}
52+
53+
// NotifyDeleteComment places a place holder function
54+
func (*NullNotifier) NotifyDeleteComment(doer *models.User, c *models.Comment) {
55+
}
56+
57+
// NotifyDeleteRepository places a place holder function
58+
func (*NullNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
59+
}
60+
61+
// NotifyForkRepository places a place holder function
62+
func (*NullNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
63+
}
64+
65+
// NotifyNewRelease places a place holder function
66+
func (*NullNotifier) NotifyNewRelease(rel *models.Release) {
67+
}
68+
69+
// NotifyUpdateRelease places a place holder function
70+
func (*NullNotifier) NotifyUpdateRelease(doer *models.User, rel *models.Release) {
71+
}
72+
73+
// NotifyDeleteRelease places a place holder function
74+
func (*NullNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Release) {
75+
}
76+
77+
// NotifyIssueChangeMilestone places a place holder function
78+
func (*NullNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue) {
79+
}
80+
81+
// NotifyIssueChangeContent places a place holder function
82+
func (*NullNotifier) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
83+
}
84+
85+
// NotifyIssueChangeAssignee places a place holder function
86+
func (*NullNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, removed bool) {
87+
}
88+
89+
// NotifyIssueClearLabels places a place holder function
90+
func (*NullNotifier) NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
91+
}
92+
93+
// NotifyIssueChangeTitle places a place holder function
94+
func (*NullNotifier) NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
95+
}
96+
97+
// NotifyIssueChangeLabels places a place holder function
98+
func (*NullNotifier) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
99+
addedLabels []*models.Label, removedLabels []*models.Label) {
100+
}
101+
102+
// NotifyCreateRepository places a place holder function
103+
func (*NullNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
104+
}
105+
106+
// NotifyMigrateRepository places a place holder function
107+
func (*NullNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
108+
}

modules/notification/mail/mail.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mail
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/notification/base"
11+
)
12+
13+
type mailNotifier struct {
14+
base.NullNotifier
15+
}
16+
17+
var (
18+
_ base.Notifier = &mailNotifier{}
19+
)
20+
21+
// NewNotifier create a new mailNotifier notifier
22+
func NewNotifier() base.Notifier {
23+
return &mailNotifier{}
24+
}
25+
26+
func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
27+
issue *models.Issue, comment *models.Comment) {
28+
var act models.ActionType
29+
if comment.Type == models.CommentTypeClose {
30+
act = models.ActionCloseIssue
31+
} else if comment.Type == models.CommentTypeReopen {
32+
act = models.ActionReopenIssue
33+
} else if comment.Type == models.CommentTypeComment {
34+
act = models.ActionCommentIssue
35+
} else if comment.Type == models.CommentTypeCode {
36+
act = models.ActionCommentIssue
37+
}
38+
39+
if err := comment.MailParticipants(act, issue); err != nil {
40+
log.Error(4, "MailParticipants: %v", err)
41+
}
42+
}
43+
44+
func (m *mailNotifier) NotifyNewIssue(issue *models.Issue) {
45+
if err := issue.MailParticipants(); err != nil {
46+
log.Error(4, "MailParticipants: %v", err)
47+
}
48+
}
49+
50+
func (m *mailNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, isClosed bool) {
51+
if err := issue.MailParticipants(); err != nil {
52+
log.Error(4, "MailParticipants: %v", err)
53+
}
54+
}
55+
56+
func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
57+
if err := pr.Issue.MailParticipants(); err != nil {
58+
log.Error(4, "MailParticipants: %v", err)
59+
}
60+
}
61+
62+
func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, comment *models.Comment) {
63+
var act models.ActionType
64+
if comment.Type == models.CommentTypeClose {
65+
act = models.ActionCloseIssue
66+
} else if comment.Type == models.CommentTypeReopen {
67+
act = models.ActionReopenIssue
68+
} else if comment.Type == models.CommentTypeComment {
69+
act = models.ActionCommentIssue
70+
}
71+
if err := comment.MailParticipants(act, pr.Issue); err != nil {
72+
log.Error(4, "MailParticipants: %v", err)
73+
}
74+
}

modules/notification/notification.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/git"
99
"code.gitea.io/gitea/models"
1010
"code.gitea.io/gitea/modules/notification/base"
11+
"code.gitea.io/gitea/modules/notification/mail"
1112
"code.gitea.io/gitea/modules/notification/ui"
1213
)
1314

@@ -23,6 +24,7 @@ func RegisterNotifier(notifier base.Notifier) {
2324

2425
func init() {
2526
RegisterNotifier(ui.NewNotifier())
27+
RegisterNotifier(mail.NewNotifier())
2628
}
2729

2830
// NotifyCreateIssueComment notifies issue comment related message to notifiers

modules/notification/ui/ui.go

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
type (
1515
notificationService struct {
16+
base.NullNotifier
1617
issueQueue chan issueNotificationOpts
1718
}
1819

@@ -86,49 +87,3 @@ func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r
8687
r.Reviewer.ID,
8788
}
8889
}
89-
90-
func (ns *notificationService) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
91-
}
92-
93-
func (ns *notificationService) NotifyDeleteComment(doer *models.User, c *models.Comment) {
94-
}
95-
96-
func (ns *notificationService) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
97-
}
98-
99-
func (ns *notificationService) NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
100-
}
101-
102-
func (ns *notificationService) NotifyNewRelease(rel *models.Release) {
103-
}
104-
105-
func (ns *notificationService) NotifyUpdateRelease(doer *models.User, rel *models.Release) {
106-
}
107-
108-
func (ns *notificationService) NotifyDeleteRelease(doer *models.User, rel *models.Release) {
109-
}
110-
111-
func (ns *notificationService) NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue) {
112-
}
113-
114-
func (ns *notificationService) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
115-
}
116-
117-
func (ns *notificationService) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, removed bool) {
118-
}
119-
120-
func (ns *notificationService) NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
121-
}
122-
123-
func (ns *notificationService) NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
124-
}
125-
126-
func (ns *notificationService) NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
127-
addedLabels []*models.Label, removedLabels []*models.Label) {
128-
}
129-
130-
func (ns *notificationService) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
131-
}
132-
133-
func (ns *notificationService) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
134-
}

0 commit comments

Comments
 (0)