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

ui.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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) {
  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. }
  55. func (ns *notificationService) NotifyNewIssue(issue *models.Issue) {
  56. _ = ns.issueQueue.Push(issueNotificationOpts{
  57. IssueID: issue.ID,
  58. NotificationAuthorID: issue.Poster.ID,
  59. })
  60. }
  61. func (ns *notificationService) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
  62. _ = ns.issueQueue.Push(issueNotificationOpts{
  63. IssueID: issue.ID,
  64. NotificationAuthorID: doer.ID,
  65. })
  66. }
  67. func (ns *notificationService) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
  68. _ = ns.issueQueue.Push(issueNotificationOpts{
  69. IssueID: pr.Issue.ID,
  70. NotificationAuthorID: doer.ID,
  71. })
  72. }
  73. func (ns *notificationService) NotifyNewPullRequest(pr *models.PullRequest) {
  74. if err := pr.LoadIssue(); err != nil {
  75. log.Error("Unable to load issue: %d for pr: %d: Error: %v", pr.IssueID, pr.ID, err)
  76. return
  77. }
  78. _ = ns.issueQueue.Push(issueNotificationOpts{
  79. IssueID: pr.Issue.ID,
  80. NotificationAuthorID: pr.Issue.PosterID,
  81. })
  82. }
  83. func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, c *models.Comment) {
  84. var opts = issueNotificationOpts{
  85. IssueID: pr.Issue.ID,
  86. NotificationAuthorID: r.Reviewer.ID,
  87. }
  88. if c != nil {
  89. opts.CommentID = c.ID
  90. }
  91. _ = ns.issueQueue.Push(opts)
  92. }
  93. func (ns *notificationService) NotifyPullRequestPushCommits(doer *models.User, pr *models.PullRequest, comment *models.Comment) {
  94. var opts = issueNotificationOpts{
  95. IssueID: pr.IssueID,
  96. NotificationAuthorID: doer.ID,
  97. CommentID: comment.ID,
  98. }
  99. _ = ns.issueQueue.Push(opts)
  100. }
  101. func (ns *notificationService) NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, assignee *models.User, removed bool, comment *models.Comment) {
  102. if !removed {
  103. var opts = issueNotificationOpts{
  104. IssueID: issue.ID,
  105. NotificationAuthorID: doer.ID,
  106. ReceiverID: assignee.ID,
  107. }
  108. if comment != nil {
  109. opts.CommentID = comment.ID
  110. }
  111. _ = ns.issueQueue.Push(opts)
  112. }
  113. }
  114. func (ns *notificationService) NotifyPullReviewRequest(doer *models.User, issue *models.Issue, reviewer *models.User, isRequest bool, comment *models.Comment) {
  115. if isRequest {
  116. var opts = issueNotificationOpts{
  117. IssueID: issue.ID,
  118. NotificationAuthorID: doer.ID,
  119. ReceiverID: reviewer.ID,
  120. }
  121. if comment != nil {
  122. opts.CommentID = comment.ID
  123. }
  124. _ = ns.issueQueue.Push(opts)
  125. }
  126. }