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.

notification.go 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. err = issue.loadRepo(e)
  98. if err != nil {
  99. return err
  100. }
  101. for _, watch := range watches {
  102. issue.Repo.Units = nil
  103. if issue.IsPull && !issue.Repo.checkUnitUser(e, watch.UserID, false, UnitTypePullRequests) {
  104. continue
  105. }
  106. if !issue.IsPull && !issue.Repo.checkUnitUser(e, watch.UserID, false, UnitTypeIssues) {
  107. continue
  108. }
  109. if err := notifyUser(watch.UserID); err != nil {
  110. return err
  111. }
  112. }
  113. return nil
  114. }
  115. func getNotificationsByIssueID(e Engine, issueID int64) (notifications []*Notification, err error) {
  116. err = e.
  117. Where("issue_id = ?", issueID).
  118. Find(&notifications)
  119. return
  120. }
  121. func notificationExists(notifications []*Notification, issueID, userID int64) bool {
  122. for _, notification := range notifications {
  123. if notification.IssueID == issueID && notification.UserID == userID {
  124. return true
  125. }
  126. }
  127. return false
  128. }
  129. func createIssueNotification(e Engine, userID int64, issue *Issue, updatedByID int64) error {
  130. notification := &Notification{
  131. UserID: userID,
  132. RepoID: issue.RepoID,
  133. Status: NotificationStatusUnread,
  134. IssueID: issue.ID,
  135. UpdatedBy: updatedByID,
  136. }
  137. if issue.IsPull {
  138. notification.Source = NotificationSourcePullRequest
  139. } else {
  140. notification.Source = NotificationSourceIssue
  141. }
  142. _, err := e.Insert(notification)
  143. return err
  144. }
  145. func updateIssueNotification(e Engine, userID, issueID, updatedByID int64) error {
  146. notification, err := getIssueNotification(e, userID, issueID)
  147. if err != nil {
  148. return err
  149. }
  150. notification.Status = NotificationStatusUnread
  151. notification.UpdatedBy = updatedByID
  152. _, err = e.ID(notification.ID).Update(notification)
  153. return err
  154. }
  155. func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
  156. notification := new(Notification)
  157. _, err := e.
  158. Where("user_id = ?", userID).
  159. And("issue_id = ?", issueID).
  160. Get(notification)
  161. return notification, err
  162. }
  163. // NotificationsForUser returns notifications for a given user and status
  164. func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPage int) ([]*Notification, error) {
  165. return notificationsForUser(x, user, statuses, page, perPage)
  166. }
  167. func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
  168. if len(statuses) == 0 {
  169. return
  170. }
  171. sess := e.
  172. Where("user_id = ?", user.ID).
  173. In("status", statuses).
  174. OrderBy("updated_unix DESC")
  175. if page > 0 && perPage > 0 {
  176. sess.Limit(perPage, (page-1)*perPage)
  177. }
  178. err = sess.Find(&notifications)
  179. return
  180. }
  181. // GetRepo returns the repo of the notification
  182. func (n *Notification) GetRepo() (*Repository, error) {
  183. n.Repository = new(Repository)
  184. _, err := x.
  185. Where("id = ?", n.RepoID).
  186. Get(n.Repository)
  187. return n.Repository, err
  188. }
  189. // GetIssue returns the issue of the notification
  190. func (n *Notification) GetIssue() (*Issue, error) {
  191. n.Issue = new(Issue)
  192. _, err := x.
  193. Where("id = ?", n.IssueID).
  194. Get(n.Issue)
  195. return n.Issue, err
  196. }
  197. // GetNotificationCount returns the notification count for user
  198. func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
  199. return getNotificationCount(x, user, status)
  200. }
  201. func getNotificationCount(e Engine, user *User, status NotificationStatus) (count int64, err error) {
  202. count, err = e.
  203. Where("user_id = ?", user.ID).
  204. And("status = ?", status).
  205. Count(&Notification{})
  206. return
  207. }
  208. func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
  209. notification, err := getIssueNotification(e, userID, issueID)
  210. // ignore if not exists
  211. if err != nil {
  212. return nil
  213. }
  214. if notification.Status != NotificationStatusUnread {
  215. return nil
  216. }
  217. notification.Status = NotificationStatusRead
  218. _, err = e.ID(notification.ID).Update(notification)
  219. return err
  220. }
  221. // SetNotificationStatus change the notification status
  222. func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
  223. notification, err := getNotificationByID(notificationID)
  224. if err != nil {
  225. return err
  226. }
  227. if notification.UserID != user.ID {
  228. return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
  229. }
  230. notification.Status = status
  231. _, err = x.ID(notificationID).Update(notification)
  232. return err
  233. }
  234. func getNotificationByID(notificationID int64) (*Notification, error) {
  235. notification := new(Notification)
  236. ok, err := x.
  237. Where("id = ?", notificationID).
  238. Get(notification)
  239. if err != nil {
  240. return nil, err
  241. }
  242. if !ok {
  243. return nil, fmt.Errorf("Notification %d does not exists", notificationID)
  244. }
  245. return notification, nil
  246. }
  247. // UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
  248. func UpdateNotificationStatuses(user *User, currentStatus NotificationStatus, desiredStatus NotificationStatus) error {
  249. n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
  250. _, err := x.
  251. Where("user_id = ? AND status = ?", user.ID, currentStatus).
  252. Cols("status", "updated_by", "updated_unix").
  253. Update(n)
  254. return err
  255. }