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

notification.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if len(statuses) == 0 {
  160. return
  161. }
  162. sess := e.
  163. Where("user_id = ?", user.ID).
  164. In("status", statuses).
  165. OrderBy("updated_unix DESC")
  166. if page > 0 && perPage > 0 {
  167. sess.Limit(perPage, (page-1)*perPage)
  168. }
  169. err = sess.Find(&notifications)
  170. return
  171. }
  172. // GetRepo returns the repo of the notification
  173. func (n *Notification) GetRepo() (*Repository, error) {
  174. n.Repository = new(Repository)
  175. _, err := x.
  176. Where("id = ?", n.RepoID).
  177. Get(n.Repository)
  178. return n.Repository, err
  179. }
  180. // GetIssue returns the issue of the notification
  181. func (n *Notification) GetIssue() (*Issue, error) {
  182. n.Issue = new(Issue)
  183. _, err := x.
  184. Where("id = ?", n.IssueID).
  185. Get(n.Issue)
  186. return n.Issue, err
  187. }
  188. // GetNotificationCount returns the notification count for user
  189. func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
  190. return getNotificationCount(x, user, status)
  191. }
  192. func getNotificationCount(e Engine, user *User, status NotificationStatus) (count int64, err error) {
  193. count, err = e.
  194. Where("user_id = ?", user.ID).
  195. And("status = ?", status).
  196. Count(&Notification{})
  197. return
  198. }
  199. func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
  200. notification, err := getIssueNotification(e, userID, issueID)
  201. // ignore if not exists
  202. if err != nil {
  203. return nil
  204. }
  205. if notification.Status != NotificationStatusUnread {
  206. return nil
  207. }
  208. notification.Status = NotificationStatusRead
  209. _, err = e.Id(notification.ID).Update(notification)
  210. return err
  211. }
  212. // SetNotificationStatus change the notification status
  213. func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
  214. notification, err := getNotificationByID(notificationID)
  215. if err != nil {
  216. return err
  217. }
  218. if notification.UserID != user.ID {
  219. return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
  220. }
  221. notification.Status = status
  222. _, err = x.Id(notificationID).Update(notification)
  223. return err
  224. }
  225. func getNotificationByID(notificationID int64) (*Notification, error) {
  226. notification := new(Notification)
  227. ok, err := x.
  228. Where("id = ?", notificationID).
  229. Get(notification)
  230. if err != nil {
  231. return nil, err
  232. }
  233. if !ok {
  234. return nil, fmt.Errorf("Notification %d does not exists", notificationID)
  235. }
  236. return notification, nil
  237. }