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.

302 lines
7.8 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. "time"
  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. Created time.Time `xorm:"-"`
  44. CreatedUnix int64 `xorm:"INDEX NOT NULL"`
  45. Updated time.Time `xorm:"-"`
  46. UpdatedUnix int64 `xorm:"INDEX NOT NULL"`
  47. }
  48. // BeforeInsert runs while inserting a record
  49. func (n *Notification) BeforeInsert() {
  50. var (
  51. now = time.Now()
  52. nowUnix = now.Unix()
  53. )
  54. n.Created = now
  55. n.CreatedUnix = nowUnix
  56. n.Updated = now
  57. n.UpdatedUnix = nowUnix
  58. }
  59. // BeforeUpdate runs while updating a record
  60. func (n *Notification) BeforeUpdate() {
  61. var (
  62. now = time.Now()
  63. nowUnix = now.Unix()
  64. )
  65. n.Updated = now
  66. n.UpdatedUnix = nowUnix
  67. }
  68. // CreateOrUpdateIssueNotifications creates an issue notification
  69. // for each watcher, or updates it if already exists
  70. func CreateOrUpdateIssueNotifications(issue *Issue, notificationAuthorID int64) error {
  71. sess := x.NewSession()
  72. defer sess.Close()
  73. if err := sess.Begin(); err != nil {
  74. return err
  75. }
  76. if err := createOrUpdateIssueNotifications(sess, issue, notificationAuthorID); err != nil {
  77. return err
  78. }
  79. return sess.Commit()
  80. }
  81. func createOrUpdateIssueNotifications(e Engine, issue *Issue, notificationAuthorID int64) error {
  82. watches, err := getWatchers(e, issue.RepoID)
  83. if err != nil {
  84. return err
  85. }
  86. notifications, err := getNotificationsByIssueID(e, issue.ID)
  87. if err != nil {
  88. return err
  89. }
  90. for _, watch := range watches {
  91. // do not send notification for the own issuer/commenter
  92. if watch.UserID == notificationAuthorID {
  93. continue
  94. }
  95. if notificationExists(notifications, issue.ID, watch.UserID) {
  96. err = updateIssueNotification(e, watch.UserID, issue.ID, notificationAuthorID)
  97. } else {
  98. err = createIssueNotification(e, watch.UserID, issue, notificationAuthorID)
  99. }
  100. if err != nil {
  101. return err
  102. }
  103. }
  104. return nil
  105. }
  106. func getNotificationsByIssueID(e Engine, issueID int64) (notifications []*Notification, err error) {
  107. err = e.
  108. Where("issue_id = ?", issueID).
  109. Find(&notifications)
  110. return
  111. }
  112. func notificationExists(notifications []*Notification, issueID, userID int64) bool {
  113. for _, notification := range notifications {
  114. if notification.IssueID == issueID && notification.UserID == userID {
  115. return true
  116. }
  117. }
  118. return false
  119. }
  120. func createIssueNotification(e Engine, userID int64, issue *Issue, updatedByID int64) error {
  121. notification := &Notification{
  122. UserID: userID,
  123. RepoID: issue.RepoID,
  124. Status: NotificationStatusUnread,
  125. IssueID: issue.ID,
  126. UpdatedBy: updatedByID,
  127. }
  128. if issue.IsPull {
  129. notification.Source = NotificationSourcePullRequest
  130. } else {
  131. notification.Source = NotificationSourceIssue
  132. }
  133. _, err := e.Insert(notification)
  134. return err
  135. }
  136. func updateIssueNotification(e Engine, userID, issueID, updatedByID int64) error {
  137. notification, err := getIssueNotification(e, userID, issueID)
  138. if err != nil {
  139. return err
  140. }
  141. notification.Status = NotificationStatusUnread
  142. notification.UpdatedBy = updatedByID
  143. _, err = e.Id(notification.ID).Update(notification)
  144. return err
  145. }
  146. func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
  147. notification := new(Notification)
  148. _, err := e.
  149. Where("user_id = ?", userID).
  150. And("issue_id = ?", issueID).
  151. Get(notification)
  152. return notification, err
  153. }
  154. // NotificationsForUser returns notifications for a given user and status
  155. func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPage int) ([]*Notification, error) {
  156. return notificationsForUser(x, user, statuses, page, perPage)
  157. }
  158. func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
  159. // FIXME: Xorm does not support aliases types (like NotificationStatus) on In() method
  160. s := make([]uint8, len(statuses))
  161. for i, status := range statuses {
  162. s[i] = uint8(status)
  163. }
  164. sess := e.
  165. Where("user_id = ?", user.ID).
  166. In("status", s).
  167. OrderBy("updated_unix DESC")
  168. if page > 0 && perPage > 0 {
  169. sess.Limit(perPage, (page-1)*perPage)
  170. }
  171. err = sess.
  172. Find(&notifications)
  173. return
  174. }
  175. // GetRepo returns the repo of the notification
  176. func (n *Notification) GetRepo() (*Repository, error) {
  177. n.Repository = new(Repository)
  178. _, err := x.
  179. Where("id = ?", n.RepoID).
  180. Get(n.Repository)
  181. return n.Repository, err
  182. }
  183. // GetIssue returns the issue of the notification
  184. func (n *Notification) GetIssue() (*Issue, error) {
  185. n.Issue = new(Issue)
  186. _, err := x.
  187. Where("id = ?", n.IssueID).
  188. Get(n.Issue)
  189. return n.Issue, err
  190. }
  191. // GetNotificationReadCount returns the notification read count for user
  192. func GetNotificationReadCount(user *User) (int64, error) {
  193. return GetNotificationCount(user, NotificationStatusRead)
  194. }
  195. // GetNotificationUnreadCount returns the notification unread count for user
  196. func GetNotificationUnreadCount(user *User) (int64, error) {
  197. return GetNotificationCount(user, NotificationStatusUnread)
  198. }
  199. // GetNotificationCount returns the notification count for user
  200. func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
  201. return getNotificationCount(x, user, status)
  202. }
  203. func getNotificationCount(e Engine, user *User, status NotificationStatus) (count int64, err error) {
  204. count, err = e.
  205. Where("user_id = ?", user.ID).
  206. And("status = ?", status).
  207. Count(&Notification{})
  208. return
  209. }
  210. func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
  211. notification, err := getIssueNotification(e, userID, issueID)
  212. // ignore if not exists
  213. if err != nil {
  214. return nil
  215. }
  216. if notification.Status != NotificationStatusUnread {
  217. return nil
  218. }
  219. notification.Status = NotificationStatusRead
  220. _, err = e.Id(notification.ID).Update(notification)
  221. return err
  222. }
  223. // SetNotificationStatus change the notification status
  224. func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
  225. notification, err := getNotificationByID(notificationID)
  226. if err != nil {
  227. return err
  228. }
  229. if notification.UserID != user.ID {
  230. return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
  231. }
  232. notification.Status = status
  233. _, err = x.Id(notificationID).Update(notification)
  234. return err
  235. }
  236. func getNotificationByID(notificationID int64) (*Notification, error) {
  237. notification := new(Notification)
  238. ok, err := x.
  239. Where("id = ?", notificationID).
  240. Get(notification)
  241. if err != nil {
  242. return nil, err
  243. }
  244. if !ok {
  245. return nil, fmt.Errorf("Notification %d does not exists", notificationID)
  246. }
  247. return notification, nil
  248. }