|
1 |
| -// Copyright 2016 The Gitea Authors. All rights reserved. |
| 1 | +// Copyright 2018 The Gitea Authors. All rights reserved. |
2 | 2 | // Use of this source code is governed by a MIT-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 | 5 | package notification
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "code.gitea.io/git" |
8 | 9 | "code.gitea.io/gitea/models"
|
9 |
| - "code.gitea.io/gitea/modules/log" |
| 10 | + "code.gitea.io/gitea/modules/notification/base" |
| 11 | + "code.gitea.io/gitea/modules/notification/ui" |
10 | 12 | )
|
11 | 13 |
|
12 |
| -type ( |
13 |
| - notificationService struct { |
14 |
| - issueQueue chan issueNotificationOpts |
| 14 | +var ( |
| 15 | + notifiers []base.Notifier |
| 16 | +) |
| 17 | + |
| 18 | +// RegisterNotifier providers method to receive notify messages |
| 19 | +func RegisterNotifier(notifier base.Notifier) { |
| 20 | + go notifier.Run() |
| 21 | + notifiers = append(notifiers, notifier) |
| 22 | +} |
| 23 | + |
| 24 | +func init() { |
| 25 | + RegisterNotifier(ui.NewNotifier()) |
| 26 | +} |
| 27 | + |
| 28 | +// NotifyCreateIssueComment notifies issue comment related message to notifiers |
| 29 | +func NotifyCreateIssueComment(doer *models.User, repo *models.Repository, |
| 30 | + issue *models.Issue, comment *models.Comment) { |
| 31 | + for _, notifier := range notifiers { |
| 32 | + notifier.NotifyCreateIssueComment(doer, repo, issue, comment) |
15 | 33 | }
|
| 34 | +} |
16 | 35 |
|
17 |
| - issueNotificationOpts struct { |
18 |
| - issue *models.Issue |
19 |
| - notificationAuthorID int64 |
| 36 | +// NotifyNewIssue notifies new issue to notifiers |
| 37 | +func NotifyNewIssue(issue *models.Issue) { |
| 38 | + for _, notifier := range notifiers { |
| 39 | + notifier.NotifyNewIssue(issue) |
20 | 40 | }
|
21 |
| -) |
| 41 | +} |
22 | 42 |
|
23 |
| -var ( |
24 |
| - // Service is the notification service |
25 |
| - Service = ¬ificationService{ |
26 |
| - issueQueue: make(chan issueNotificationOpts, 100), |
| 43 | +// NotifyIssueChangeStatus notifies close or reopen issue to notifiers |
| 44 | +func NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, closeOrReopen bool) { |
| 45 | + for _, notifier := range notifiers { |
| 46 | + notifier.NotifyIssueChangeStatus(doer, issue, closeOrReopen) |
27 | 47 | }
|
28 |
| -) |
| 48 | +} |
29 | 49 |
|
30 |
| -func init() { |
31 |
| - go Service.Run() |
| 50 | +// NotifyMergePullRequest notifies merge pull request to notifiers |
| 51 | +func NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository) { |
| 52 | + for _, notifier := range notifiers { |
| 53 | + notifier.NotifyMergePullRequest(pr, doer, baseGitRepo) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// NotifyNewPullRequest notifies new pull request to notifiers |
| 58 | +func NotifyNewPullRequest(pr *models.PullRequest) { |
| 59 | + for _, notifier := range notifiers { |
| 60 | + notifier.NotifyNewPullRequest(pr) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// NotifyPullRequestReview notifies new pull request review |
| 65 | +func NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) { |
| 66 | + for _, notifier := range notifiers { |
| 67 | + notifier.NotifyPullRequestReview(pr, review, comment) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// NotifyUpdateComment notifies update comment to notifiers |
| 72 | +func NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) { |
| 73 | + for _, notifier := range notifiers { |
| 74 | + notifier.NotifyUpdateComment(doer, c, oldContent) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// NotifyDeleteComment notifies delete comment to notifiers |
| 79 | +func NotifyDeleteComment(doer *models.User, c *models.Comment) { |
| 80 | + for _, notifier := range notifiers { |
| 81 | + notifier.NotifyDeleteComment(doer, c) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// NotifyDeleteRepository notifies delete repository to notifiers |
| 86 | +func NotifyDeleteRepository(doer *models.User, repo *models.Repository) { |
| 87 | + for _, notifier := range notifiers { |
| 88 | + notifier.NotifyDeleteRepository(doer, repo) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// NotifyForkRepository notifies fork repository to notifiers |
| 93 | +func NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) { |
| 94 | + for _, notifier := range notifiers { |
| 95 | + notifier.NotifyForkRepository(doer, oldRepo, repo) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// NotifyNewRelease notifies new release to notifiers |
| 100 | +func NotifyNewRelease(rel *models.Release) { |
| 101 | + for _, notifier := range notifiers { |
| 102 | + notifier.NotifyNewRelease(rel) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// NotifyUpdateRelease notifies update release to notifiers |
| 107 | +func NotifyUpdateRelease(doer *models.User, rel *models.Release) { |
| 108 | + for _, notifier := range notifiers { |
| 109 | + notifier.NotifyUpdateRelease(doer, rel) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// NotifyDeleteRelease notifies delete release to notifiers |
| 114 | +func NotifyDeleteRelease(doer *models.User, rel *models.Release) { |
| 115 | + for _, notifier := range notifiers { |
| 116 | + notifier.NotifyDeleteRelease(doer, rel) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// NotifyIssueChangeMilestone notifies change milestone to notifiers |
| 121 | +func NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue) { |
| 122 | + for _, notifier := range notifiers { |
| 123 | + notifier.NotifyIssueChangeMilestone(doer, issue) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// NotifyIssueChangeContent notifies change content to notifiers |
| 128 | +func NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) { |
| 129 | + for _, notifier := range notifiers { |
| 130 | + notifier.NotifyIssueChangeContent(doer, issue, oldContent) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// NotifyIssueChangeAssignee notifies change content to notifiers |
| 135 | +func NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, removed bool) { |
| 136 | + for _, notifier := range notifiers { |
| 137 | + notifier.NotifyIssueChangeAssignee(doer, issue, removed) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// NotifyIssueClearLabels notifies clear labels to notifiers |
| 142 | +func NotifyIssueClearLabels(doer *models.User, issue *models.Issue) { |
| 143 | + for _, notifier := range notifiers { |
| 144 | + notifier.NotifyIssueClearLabels(doer, issue) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// NotifyIssueChangeTitle notifies change title to notifiers |
| 149 | +func NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) { |
| 150 | + for _, notifier := range notifiers { |
| 151 | + notifier.NotifyIssueChangeTitle(doer, issue, oldTitle) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// NotifyIssueChangeLabels notifies change labels to notifiers |
| 156 | +func NotifyIssueChangeLabels(doer *models.User, issue *models.Issue, |
| 157 | + addedLabels []*models.Label, removedLabels []*models.Label) { |
| 158 | + for _, notifier := range notifiers { |
| 159 | + notifier.NotifyIssueChangeLabels(doer, issue, addedLabels, removedLabels) |
| 160 | + } |
32 | 161 | }
|
33 | 162 |
|
34 |
| -func (ns *notificationService) Run() { |
35 |
| - for { |
36 |
| - select { |
37 |
| - case opts := <-ns.issueQueue: |
38 |
| - if err := models.CreateOrUpdateIssueNotifications(opts.issue, opts.notificationAuthorID); err != nil { |
39 |
| - log.Error(4, "Was unable to create issue notification: %v", err) |
40 |
| - } |
41 |
| - } |
| 163 | +// NotifyCreateRepository notifies create repository to notifiers |
| 164 | +func NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) { |
| 165 | + for _, notifier := range notifiers { |
| 166 | + notifier.NotifyCreateRepository(doer, u, repo) |
42 | 167 | }
|
43 | 168 | }
|
44 | 169 |
|
45 |
| -func (ns *notificationService) NotifyIssue(issue *models.Issue, notificationAuthorID int64) { |
46 |
| - ns.issueQueue <- issueNotificationOpts{ |
47 |
| - issue, |
48 |
| - notificationAuthorID, |
| 170 | +// NotifyMigrateRepository notifies create repository to notifiers |
| 171 | +func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) { |
| 172 | + for _, notifier := range notifiers { |
| 173 | + notifier.NotifyMigrateRepository(doer, u, repo) |
49 | 174 | }
|
50 | 175 | }
|
0 commit comments