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.

301 lines
7.9 KiB

  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. "fmt"
  7. "code.gitea.io/gitea/modules/util"
  8. )
  9. type (
  10. // NotificationStatus is the status of the notification (read or unread)
  11. NotificationStatus uint8
  12. // NotificationSource is the source of the notification (issue, PR, commit, etc)
  13. NotificationSource uint8
  14. )
  15. const (
  16. // NotificationStatusUnread represents an unread notification
  17. NotificationStatusUnread NotificationStatus = iota + 1
  18. // NotificationStatusRead represents a read notification
  19. NotificationStatusRead
  20. // NotificationStatusPinned represents a pinned notification
  21. NotificationStatusPinned
  22. )
  23. const (
  24. // NotificationSourceIssue is a notification of an issue
  25. NotificationSourceIssue NotificationSource = iota + 1
  26. // NotificationSourcePullRequest is a notification of a pull request
  27. NotificationSourcePullRequest
  28. // NotificationSourceCommit is a notification of a commit
  29. NotificationSourceCommit
  30. )
  31. // Notification represents a notification
  32. type Notification struct {
  33. ID int64 `xorm:"pk autoincr"`
  34. UserID int64 `xorm:"INDEX NOT NULL"`
  35. RepoID int64 `xorm:"INDEX NOT NULL"`
  36. Status NotificationStatus `xorm:"SMALLINT INDEX NOT NULL"`
  37. Source NotificationSource `xorm:"SMALLINT INDEX NOT NULL"`
  38. IssueID int64 `xorm:"INDEX NOT NULL"`
  39. CommitID string `xorm:"INDEX"`
  40. UpdatedBy int64 `xorm:"INDEX NOT NULL"`
  41. Issue *Issue `xorm:"-"`
  42. Repository *Repository `xorm:"-"`
  43. CreatedUnix util.TimeStamp `xorm:"created INDEX NOT NULL"`
  44. UpdatedUnix util.TimeStamp `xorm:"updated INDEX NOT NULL"`
  45. }
  46. // CreateOrUpdateIssueNotifications creates an issue notification
  47. // for each watcher, or updates it if already exists
  48. func CreateOrUpdateIssueNotifications(issue *Issue, notificationAuthorID int64) error {
  49. sess := x.NewSession()
  50. defer sess.Close()
  51. if err := sess.Begin(); err != nil {
  52. return err
  53. }
  54. if err := createOrUpdateIssueNotifications(sess, issue, notificationAuthorID); err != nil {
  55. return err
  56. }
  57. return sess.Commit()
  58. }
  59. func createOrUpdateIssueNotifications(e Engine, issue *Issue, notificationAuthorID int64) error {
  60. issueWatches, err := getIssueWatchers(e, issue.ID)
  61. if err != nil {
  62. return err
  63. }
  64. watches, err := getWatchers(e, issue.RepoID)
  65. if err != nil {
  66. return err
  67. }
  68. notifications, err := getNotificationsByIssueID(e, issue.ID)
  69. if err != nil {
  70. return err
  71. }
  72. alreadyNotified := make(map[int64]struct{}, len(issueWatches)+len(watches))
  73. notifyUser := func(userID int64) error {
  74. // do not send notification for the own issuer/commenter
  75. if userID == notificationAuthorID {
  76. return nil
  77. }
  78. if _, ok := alreadyNotified[userID]; ok {
  79. return nil
  80. }
  81. alreadyNotified[userID] = struct{}{}
  82. if notificationExists(notifications, issue.ID, userID) {
  83. return updateIssueNotification(e, userID, issue.ID, notificationAuthorID)
  84. }
  85. return createIssueNotification(e, userID, issue, notificationAuthorID)
  86. }
  87. for _, issueWatch := range issueWatches {
  88. // ignore if user unwatched the issue
  89. if !issueWatch.IsWatching {
  90. alreadyNotified[issueWatch.UserID] = struct{}{}
  91. continue
  92. }
  93. if err := notifyUser(issueWatch.UserID); err != nil {
  94. return err
  95. }
  96. }
  97. for _, watch := range watches {
  98. if err := notifyUser(watch.UserID); err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }
  104. func getNotificationsByIssueID(e Engine, issueID int64) (notifications []*Notification, err error) {
  105. err = e.
  106. Where("issue_id = ?", issueID).
  107. Find(&notifications)
  108. return
  109. }
  110. func notificationExists(notifications []*Notification, issueID, userID int64) bool {
  111. for _, notification := range notifications {
  112. if notification.IssueID == issueID && notification.UserID == userID {
  113. return true
  114. }
  115. }
  116. return false
  117. }
  118. func createIssueNotification(e Engine, userID int64, issue *Issue, updatedByID int64) error {
  119. notification := &Notification{
  120. UserID: userID,
  121. RepoID: issue.RepoID,
  122. Status: NotificationStatusUnread,
  123. IssueID: issue.ID,
  124. UpdatedBy: updatedByID,
  125. }
  126. if issue.IsPull {
  127. notification.Source = NotificationSourcePullRequest
  128. } else {
  129. notification.Source = NotificationSourceIssue
  130. }
  131. _, err := e.Insert(notification)
  132. return err
  133. }
  134. func updateIssueNotification(e Engine, userID, issueID, updatedByID int64) error {
  135. notification, err := getIssueNotification(e, userID, issueID)
  136. if err != nil {
  137. return err
  138. }
  139. notification.Status = NotificationStatusUnread
  140. notification.UpdatedBy = updatedByID
  141. _, err = e.ID(notification.ID).Update(notification)
  142. return err
  143. }
  144. func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
  145. notification := new(Notification)
  146. _, err := e.
  147. Where("user_id = ?", userID).
  148. And("issue_id = ?", issueID).
  149. Get(notification)
  150. return notification, err
  151. }
  152. // NotificationsForUser returns notifications for a given user and status
  153. func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPage int) ([]*Notification, error) {
  154. return notificationsForUser(x, user, statuses, page, perPage)
  155. }
  156. func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
  157. if len(statuses) == 0 {
  158. return
  159. }
  160. sess := e.
  161. Where("user_id = ?", user.ID).
  162. In("status", statuses).
  163. OrderBy("updated_unix DESC")
  164. if page > 0 && perPage > 0 {
  165. sess.Limit(perPage, (page-1)*perPage)
  166. }
  167. err = sess.Find(&notifications)
  168. return
  169. }
  170. // GetRepo returns the repo of the notification
  171. func (n *Notification) GetRepo() (*Repository, error) {
  172. n.Repository = new(Repository)
  173. _, err := x.
  174. Where("id = ?", n.RepoID).
  175. Get(n.Repository)
  176. return n.Repository, err
  177. }
  178. // GetIssue returns the issue of the notification
  179. func (n *Notification) GetIssue() (*Issue, error) {
  180. n.Issue = new(Issue)
  181. _, err := x.
  182. Where("id = ?", n.IssueID).
  183. Get(n.Issue)
  184. return n.Issue, err
  185. }
  186. // GetNotificationCount returns the notification count for user
  187. func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
  188. return getNotificationCount(x, user, status)
  189. }
  190. func getNotificationCount(e Engine, user *User, status NotificationStatus) (count int64, err error) {
  191. count, err = e.
  192. Where("user_id = ?", user.ID).
  193. And("status = ?", status).
  194. Count(&Notification{})
  195. return
  196. }
  197. func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
  198. notification, err := getIssueNotification(e, userID, issueID)
  199. // ignore if not exists
  200. if err != nil {
  201. return nil
  202. }
  203. if notification.Status != NotificationStatusUnread {
  204. return nil
  205. }
  206. notification.Status = NotificationStatusRead
  207. _, err = e.ID(notification.ID).Update(notification)
  208. return err
  209. }
  210. // SetNotificationStatus change the notification status
  211. func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
  212. notification, err := getNotificationByID(notificationID)
  213. if err != nil {
  214. return err
  215. }
  216. if notification.UserID != user.ID {
  217. return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
  218. }
  219. notification.Status = status
  220. _, err = x.ID(notificationID).Update(notification)
  221. return err
  222. }
  223. func getNotificationByID(notificationID int64) (*Notification, error) {
  224. notification := new(Notification)
  225. ok, err := x.
  226. Where("id = ?", notificationID).
  227. Get(notification)
  228. if err != nil {
  229. return nil, err
  230. }
  231. if !ok {
  232. return nil, fmt.Errorf("Notification %d does not exists", notificationID)
  233. }
  234. return notification, nil
  235. }
  236. // UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
  237. func UpdateNotificationStatuses(user *User, currentStatus NotificationStatus, desiredStatus NotificationStatus) error {
  238. n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
  239. _, err := x.
  240. Where("user_id = ? AND status = ?", user.ID, currentStatus).
  241. Cols("status", "updated_by", "updated_unix").
  242. Update(n)
  243. return err
  244. }