You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui.go 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2018 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. package ui
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/graceful"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/notification/base"
  10. "code.gitea.io/gitea/modules/queue"
  11. )
  12. type (
  13. notificationService struct {
  14. base.NullNotifier
  15. issueQueue queue.Queue
  16. }
  17. issueNotificationOpts struct {
  18. IssueID int64
  19. CommentID int64
  20. NotificationAuthorID int64
  21. ReceiverID int64 // 0 -- ALL Watcher
  22. }
  23. )
  24. var (
  25. _ base.Notifier = &notificationService{}
  26. )
  27. // NewNotifier create a new notificationService notifier
  28. func NewNotifier() base.Notifier {
  29. ns := &notificationService{}
  30. ns.issueQueue = queue.CreateQueue("notification-service", ns.handle, issueNotificationOpts{})
  31. return ns
  32. }
  33. func (ns *notificationService) handle(data ...queue.Data) {
  34. for _, datum := range data {
  35. opts := datum.(issueNotificationOpts)
  36. if err := models.CreateOrUpdateIssueNotifications(opts.IssueID, opts.CommentID, opts.NotificationAuthorID, opts.ReceiverID); err != nil {
  37. log.Error("Was unable to create issue notification: %v", err)
  38. }
  39. }
  40. }
  41. func (ns *notificationService) Run() {
  42. graceful.GetManager().RunWithShutdownFns(ns.issueQueue.Run)
  43. }
  44. func (ns *notificationService) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
  45. issue *models.Issue, comment *models.Comment, mentions []*models.User) {
  46. var opts = issueNotificationOpts{
  47. IssueID: issue.ID,
  48. NotificationAuthorID: doer.ID,
  49. }
  50. if comment != nil {
  51. opts.CommentID = comment.ID
  52. }
  53. _ = ns.issueQueue.Push(opts)
  54. for _, mention := range mentions {
  55. var opts = issueNotificationOpts{
  56. IssueID: issue.ID,
  57. NotificationAuthorID: doer.ID,
  58. ReceiverID: mention.ID,
  59. }
  60. if comment != nil {
  61. opts.CommentID = comment.ID
  62. }
  63. _ = ns.issueQueue.Push(opts)
  64. }
  65. }
  66. func (ns *notificationService) NotifyNewIssue(issue *models.Issue, mentions []*models.User) {
  67. _ = ns.issueQueue.Push(issueNotificationOpts{
  68. IssueID: issue.ID,
  69. NotificationAuthorID: issue.Poster.ID,
  70. })
  71. for _, mention := range mentions {
  72. _ = ns.issueQueue.Push(issueNotificationOpts{
  73. IssueID: issue.ID,
  74. NotificationAuthorID: issue.Poster.ID,
  75. ReceiverID: mention.ID,
  76. })
  77. }
  78. }
  79. func (ns *notificationService) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
  80. _ = ns.issueQueue.Push(issueNotificationOpts{
  81. IssueID: issue.ID,
  82. NotificationAuthorID: doer.ID,
  83. })
  84. }
  85. func (ns *notificationService) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
  86. _ = ns.issueQueue.Push(issueNotificationOpts{
  87. IssueID: pr.Issue.ID,
  88. NotificationAuthorID: doer.ID,
  89. })
  90. }
  91. func (ns *notificationService) NotifyNewPullRequest(pr *models.PullRequest, mentions []*models.User) {
  92. if err := pr.LoadIssue(); err != nil {
  93. log.Error("Unable to load issue: %d for pr: %d: Error: %v", pr.IssueID, pr.ID, err)
  94. return
  95. }
  96. _ = ns.issueQueue.Push(issueNotificationOpts{
  97. IssueID: pr.Issue.ID,
  98. NotificationAuthorID: pr.Issue.PosterID,
  99. })
  100. for _, mention := range mentions {
  101. _ = ns.issueQueue.Push(issueNotificationOpts{
  102. IssueID: pr.Issue.ID,
  103. NotificationAuthorID: pr.Issue.PosterID,
  104. ReceiverID: mention.ID,
  105. })
  106. }
  107. }
  108. func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, c *models.Comment, mentions []*models.User) {
  109. var opts = issueNotificationOpts{
  110. IssueID: pr.Issue.ID,
  111. NotificationAuthorID: r.Reviewer.ID,
  112. }
  113. if c != nil {
  114. opts.CommentID = c.ID
  115. }
  116. _ = ns.issueQueue.Push(opts)
  117. for _, mention := range mentions {
  118. var opts = issueNotificationOpts{
  119. IssueID: pr.Issue.ID,
  120. NotificationAuthorID: r.Reviewer.ID,
  121. ReceiverID: mention.ID,
  122. }
  123. if c != nil {
  124. opts.CommentID = c.ID
  125. }
  126. _ = ns.issueQueue.Push(opts)
  127. }
  128. }
  129. func (ns *notificationService) NotifyPullRequestCodeComment(pr *models.PullRequest, c *models.Comment, mentions []*models.User) {
  130. for _, mention := range mentions {
  131. _ = ns.issueQueue.Push(issueNotificationOpts{
  132. IssueID: pr.Issue.ID,
  133. NotificationAuthorID: c.Poster.ID,
  134. CommentID: c.ID,
  135. ReceiverID: mention.ID,
  136. })
  137. }
  138. }
  139. func (ns *notificationService) NotifyPullRequestPushCommits(doer *models.User, pr *models.PullRequest, comment *models.Comment) {
  140. var opts = issueNotificationOpts{
  141. IssueID: pr.IssueID,
  142. NotificationAuthorID: doer.ID,
  143. CommentID: comment.ID,
  144. }
  145. _ = ns.issueQueue.Push(opts)
  146. }
  147. func (ns *notificationService) NotifyPullRevieweDismiss(doer *models.User, review *models.Review, comment *models.Comment) {
  148. var opts = issueNotificationOpts{
  149. IssueID: review.IssueID,
  150. NotificationAuthorID: doer.ID,
  151. CommentID: comment.ID,
  152. }
  153. _ = ns.issueQueue.Push(opts)
  154. }
  155. func (ns *notificationService) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) {
  156. if !removed {
  157. var opts = issueNotificationOpts{
  158. IssueID: issue.ID,
  159. NotificationAuthorID: doer.ID,
  160. ReceiverID: assignee.ID,
  161. }
  162. if comment != nil {
  163. opts.CommentID = comment.ID
  164. }
  165. _ = ns.issueQueue.Push(opts)
  166. }
  167. }
  168. func (ns *notificationService) NotifyPullReviewRequest(doer *models.User, issue *models.Issue, reviewer *models.User, isRequest bool, comment *models.Comment) {
  169. if isRequest {
  170. var opts = issueNotificationOpts{
  171. IssueID: issue.ID,
  172. NotificationAuthorID: doer.ID,
  173. ReceiverID: reviewer.ID,
  174. }
  175. if comment != nil {
  176. opts.CommentID = comment.ID
  177. }
  178. _ = ns.issueQueue.Push(opts)
  179. }
  180. }
  181. func (ns *notificationService) NotifyRepoPendingTransfer(doer, newOwner *models.User, repo *models.Repository) {
  182. if err := models.CreateRepoTransferNotification(doer, newOwner, repo); err != nil {
  183. log.Error("NotifyRepoPendingTransfer: %v", err)
  184. }
  185. }