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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  22. )
  23. var (
  24. _ base.Notifier = &notificationService{}
  25. )
  26. // NewNotifier create a new notificationService notifier
  27. func NewNotifier() base.Notifier {
  28. ns := &notificationService{}
  29. ns.issueQueue = queue.CreateQueue("notification-service", ns.handle, issueNotificationOpts{})
  30. return ns
  31. }
  32. func (ns *notificationService) handle(data ...queue.Data) {
  33. for _, datum := range data {
  34. opts := datum.(issueNotificationOpts)
  35. if err := models.CreateOrUpdateIssueNotifications(opts.IssueID, opts.CommentID, opts.NotificationAuthorID); err != nil {
  36. log.Error("Was unable to create issue notification: %v", err)
  37. }
  38. }
  39. }
  40. func (ns *notificationService) Run() {
  41. graceful.GetManager().RunWithShutdownFns(ns.issueQueue.Run)
  42. }
  43. func (ns *notificationService) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
  44. issue *models.Issue, comment *models.Comment) {
  45. var opts = issueNotificationOpts{
  46. IssueID: issue.ID,
  47. NotificationAuthorID: doer.ID,
  48. }
  49. if comment != nil {
  50. opts.CommentID = comment.ID
  51. }
  52. _ = ns.issueQueue.Push(opts)
  53. }
  54. func (ns *notificationService) NotifyNewIssue(issue *models.Issue) {
  55. _ = ns.issueQueue.Push(issueNotificationOpts{
  56. IssueID: issue.ID,
  57. NotificationAuthorID: issue.Poster.ID,
  58. })
  59. }
  60. func (ns *notificationService) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
  61. _ = ns.issueQueue.Push(issueNotificationOpts{
  62. IssueID: issue.ID,
  63. NotificationAuthorID: doer.ID,
  64. })
  65. }
  66. func (ns *notificationService) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
  67. _ = ns.issueQueue.Push(issueNotificationOpts{
  68. IssueID: pr.Issue.ID,
  69. NotificationAuthorID: doer.ID,
  70. })
  71. }
  72. func (ns *notificationService) NotifyNewPullRequest(pr *models.PullRequest) {
  73. if err := pr.LoadIssue(); err != nil {
  74. log.Error("Unable to load issue: %d for pr: %d: Error: %v", pr.IssueID, pr.ID, err)
  75. return
  76. }
  77. _ = ns.issueQueue.Push(issueNotificationOpts{
  78. IssueID: pr.Issue.ID,
  79. NotificationAuthorID: pr.Issue.PosterID,
  80. })
  81. }
  82. func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, c *models.Comment) {
  83. var opts = issueNotificationOpts{
  84. IssueID: pr.Issue.ID,
  85. NotificationAuthorID: r.Reviewer.ID,
  86. }
  87. if c != nil {
  88. opts.CommentID = c.ID
  89. }
  90. _ = ns.issueQueue.Push(opts)
  91. }