Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

notification.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2016 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 models
  5. import (
  6. "time"
  7. )
  8. type (
  9. // NotificationStatus is the status of the notification (read or unread)
  10. NotificationStatus uint8
  11. // NotificationSource is the source of the notification (issue, PR, commit, etc)
  12. NotificationSource uint8
  13. )
  14. const (
  15. // NotificationStatusUnread represents an unread notification
  16. NotificationStatusUnread NotificationStatus = iota + 1
  17. // NotificationStatusRead represents a read notification
  18. NotificationStatusRead
  19. )
  20. const (
  21. // NotificationSourceIssue is a notification of an issue
  22. NotificationSourceIssue NotificationSource = iota + 1
  23. // NotificationSourcePullRequest is a notification of a pull request
  24. NotificationSourcePullRequest
  25. // NotificationSourceCommit is a notification of a commit
  26. NotificationSourceCommit
  27. )
  28. // Notification represents a notification
  29. type Notification struct {
  30. ID int64 `xorm:"pk autoincr"`
  31. UserID int64 `xorm:"INDEX NOT NULL"`
  32. RepoID int64 `xorm:"INDEX NOT NULL"`
  33. Status NotificationStatus `xorm:"SMALLINT INDEX NOT NULL"`
  34. Source NotificationSource `xorm:"SMALLINT INDEX NOT NULL"`
  35. IssueID int64 `xorm:"INDEX NOT NULL"`
  36. CommitID string `xorm:"INDEX"`
  37. UpdatedBy int64 `xorm:"INDEX NOT NULL"`
  38. Issue *Issue `xorm:"-"`
  39. Repository *Repository `xorm:"-"`
  40. Created time.Time `xorm:"-"`
  41. CreatedUnix int64 `xorm:"INDEX NOT NULL"`
  42. Updated time.Time `xorm:"-"`
  43. UpdatedUnix int64 `xorm:"INDEX NOT NULL"`
  44. }
  45. // BeforeInsert runs while inserting a record
  46. func (n *Notification) BeforeInsert() {
  47. var (
  48. now = time.Now()
  49. nowUnix = now.Unix()
  50. )
  51. n.Created = now
  52. n.CreatedUnix = nowUnix
  53. n.Updated = now
  54. n.UpdatedUnix = nowUnix
  55. }
  56. // BeforeUpdate runs while updateing a record
  57. func (n *Notification) BeforeUpdate() {
  58. var (
  59. now = time.Now()
  60. nowUnix = now.Unix()
  61. )
  62. n.Updated = now
  63. n.UpdatedUnix = nowUnix
  64. }
  65. // CreateOrUpdateIssueNotifications creates an issue notification
  66. // for each watcher, or updates it if already exists
  67. func CreateOrUpdateIssueNotifications(issue *Issue, notificationAuthorID int64) error {
  68. sess := x.NewSession()
  69. defer sess.Close()
  70. if err := sess.Begin(); err != nil {
  71. return err
  72. }
  73. if err := createOrUpdateIssueNotifications(sess, issue, notificationAuthorID); err != nil {
  74. return err
  75. }
  76. return sess.Commit()
  77. }
  78. func createOrUpdateIssueNotifications(e Engine, issue *Issue, notificationAuthorID int64) error {
  79. watches, err := getWatchers(e, issue.RepoID)
  80. if err != nil {
  81. return err
  82. }
  83. notifications, err := getNotificationsByIssueID(e, issue.ID)
  84. if err != nil {
  85. return err
  86. }
  87. for _, watch := range watches {
  88. // do not send notification for the own issuer/commenter
  89. if watch.UserID == notificationAuthorID {
  90. continue
  91. }
  92. if notificationExists(notifications, issue.ID, watch.UserID) {
  93. err = updateIssueNotification(e, watch.UserID, issue.ID, notificationAuthorID)
  94. } else {
  95. err = createIssueNotification(e, watch.UserID, issue, notificationAuthorID)
  96. }
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }
  103. func getNotificationsByIssueID(e Engine, issueID int64) (notifications []*Notification, err error) {
  104. err = e.
  105. Where("issue_id = ?", issueID).
  106. Find(&notifications)
  107. return
  108. }
  109. func notificationExists(notifications []*Notification, issueID, userID int64) bool {
  110. for _, notification := range notifications {
  111. if notification.IssueID == issueID && notification.UserID == userID {
  112. return true
  113. }
  114. }
  115. return false
  116. }
  117. func createIssueNotification(e Engine, userID int64, issue *Issue, updatedByID int64) error {
  118. notification := &Notification{
  119. UserID: userID,
  120. RepoID: issue.RepoID,
  121. Status: NotificationStatusUnread,
  122. IssueID: issue.ID,
  123. UpdatedBy: updatedByID,
  124. }
  125. if issue.IsPull {
  126. notification.Source = NotificationSourcePullRequest
  127. } else {
  128. notification.Source = NotificationSourceIssue
  129. }
  130. _, err := e.Insert(notification)
  131. return err
  132. }
  133. func updateIssueNotification(e Engine, userID, issueID, updatedByID int64) error {
  134. notification, err := getIssueNotification(e, userID, issueID)
  135. if err != nil {
  136. return err
  137. }
  138. notification.Status = NotificationStatusUnread
  139. notification.UpdatedBy = updatedByID
  140. _, err = e.Id(notification.ID).Update(notification)
  141. return err
  142. }
  143. func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
  144. notification := new(Notification)
  145. _, err := e.
  146. Where("user_id = ?", userID).
  147. And("issue_id = ?", issueID).
  148. Get(notification)
  149. return notification, err
  150. }
  151. // NotificationsForUser returns notifications for a given user and status
  152. func NotificationsForUser(user *User, status NotificationStatus) ([]*Notification, error) {
  153. return notificationsForUser(x, user, status)
  154. }
  155. func notificationsForUser(e Engine, user *User, status NotificationStatus) (notifications []*Notification, err error) {
  156. err = e.
  157. Where("user_id = ?", user.ID).
  158. And("status = ?", status).
  159. OrderBy("updated_unix DESC").
  160. Find(&notifications)
  161. return
  162. }
  163. // GetRepo returns the repo of the notification
  164. func (n *Notification) GetRepo() (*Repository, error) {
  165. n.Repository = new(Repository)
  166. _, err := x.
  167. Where("id = ?", n.RepoID).
  168. Get(n.Repository)
  169. return n.Repository, err
  170. }
  171. // GetIssue returns the issue of the notification
  172. func (n *Notification) GetIssue() (*Issue, error) {
  173. n.Issue = new(Issue)
  174. _, err := x.
  175. Where("id = ?", n.IssueID).
  176. Get(n.Issue)
  177. return n.Issue, err
  178. }
  179. // GetNotificationReadCount returns the notification read count for user
  180. func GetNotificationReadCount(user *User) (int64, error) {
  181. return GetNotificationCount(user, NotificationStatusRead)
  182. }
  183. // GetNotificationUnreadCount returns the notification unread count for user
  184. func GetNotificationUnreadCount(user *User) (int64, error) {
  185. return GetNotificationCount(user, NotificationStatusUnread)
  186. }
  187. // GetNotificationCount returns the notification count for user
  188. func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
  189. return getNotificationCount(x, user, status)
  190. }
  191. func getNotificationCount(e Engine, user *User, status NotificationStatus) (count int64, err error) {
  192. count, err = e.
  193. Where("user_id = ?", user.ID).
  194. And("status = ?", status).
  195. Count(&Notification{})
  196. return
  197. }
  198. func setNotificationStatusRead(e Engine, userID, issueID int64) error {
  199. notification, err := getIssueNotification(e, userID, issueID)
  200. // ignore if not exists
  201. if err != nil {
  202. return nil
  203. }
  204. notification.Status = NotificationStatusRead
  205. _, err = e.Id(notification.ID).Update(notification)
  206. return err
  207. }