You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

notification.go 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. "path"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "xorm.io/builder"
  13. "xorm.io/xorm"
  14. )
  15. type (
  16. // NotificationStatus is the status of the notification (read or unread)
  17. NotificationStatus uint8
  18. // NotificationSource is the source of the notification (issue, PR, commit, etc)
  19. NotificationSource uint8
  20. )
  21. const (
  22. // NotificationStatusUnread represents an unread notification
  23. NotificationStatusUnread NotificationStatus = iota + 1
  24. // NotificationStatusRead represents a read notification
  25. NotificationStatusRead
  26. // NotificationStatusPinned represents a pinned notification
  27. NotificationStatusPinned
  28. )
  29. const (
  30. // NotificationSourceIssue is a notification of an issue
  31. NotificationSourceIssue NotificationSource = iota + 1
  32. // NotificationSourcePullRequest is a notification of a pull request
  33. NotificationSourcePullRequest
  34. // NotificationSourceCommit is a notification of a commit
  35. NotificationSourceCommit
  36. )
  37. // Notification represents a notification
  38. type Notification struct {
  39. ID int64 `xorm:"pk autoincr"`
  40. UserID int64 `xorm:"INDEX NOT NULL"`
  41. RepoID int64 `xorm:"INDEX NOT NULL"`
  42. Status NotificationStatus `xorm:"SMALLINT INDEX NOT NULL"`
  43. Source NotificationSource `xorm:"SMALLINT INDEX NOT NULL"`
  44. IssueID int64 `xorm:"INDEX NOT NULL"`
  45. CommitID string `xorm:"INDEX"`
  46. CommentID int64
  47. UpdatedBy int64 `xorm:"INDEX NOT NULL"`
  48. Issue *Issue `xorm:"-"`
  49. Repository *Repository `xorm:"-"`
  50. Comment *Comment `xorm:"-"`
  51. User *User `xorm:"-"`
  52. CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"`
  53. UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
  54. }
  55. // FindNotificationOptions represent the filters for notifications. If an ID is 0 it will be ignored.
  56. type FindNotificationOptions struct {
  57. UserID int64
  58. RepoID int64
  59. IssueID int64
  60. Status NotificationStatus
  61. UpdatedAfterUnix int64
  62. UpdatedBeforeUnix int64
  63. }
  64. // ToCond will convert each condition into a xorm-Cond
  65. func (opts *FindNotificationOptions) ToCond() builder.Cond {
  66. cond := builder.NewCond()
  67. if opts.UserID != 0 {
  68. cond = cond.And(builder.Eq{"notification.user_id": opts.UserID})
  69. }
  70. if opts.RepoID != 0 {
  71. cond = cond.And(builder.Eq{"notification.repo_id": opts.RepoID})
  72. }
  73. if opts.IssueID != 0 {
  74. cond = cond.And(builder.Eq{"notification.issue_id": opts.IssueID})
  75. }
  76. if opts.Status != 0 {
  77. cond = cond.And(builder.Eq{"notification.status": opts.Status})
  78. }
  79. if opts.UpdatedAfterUnix != 0 {
  80. cond = cond.And(builder.Gte{"notification.updated_unix": opts.UpdatedAfterUnix})
  81. }
  82. if opts.UpdatedBeforeUnix != 0 {
  83. cond = cond.And(builder.Lte{"notification.updated_unix": opts.UpdatedBeforeUnix})
  84. }
  85. return cond
  86. }
  87. // ToSession will convert the given options to a xorm Session by using the conditions from ToCond and joining with issue table if required
  88. func (opts *FindNotificationOptions) ToSession(e Engine) *xorm.Session {
  89. return e.Where(opts.ToCond())
  90. }
  91. func getNotifications(e Engine, options FindNotificationOptions) (nl NotificationList, err error) {
  92. err = options.ToSession(e).OrderBy("notification.updated_unix DESC").Find(&nl)
  93. return
  94. }
  95. // GetNotifications returns all notifications that fit to the given options.
  96. func GetNotifications(opts FindNotificationOptions) (NotificationList, error) {
  97. return getNotifications(x, opts)
  98. }
  99. // CreateOrUpdateIssueNotifications creates an issue notification
  100. // for each watcher, or updates it if already exists
  101. func CreateOrUpdateIssueNotifications(issueID, commentID int64, notificationAuthorID int64) error {
  102. sess := x.NewSession()
  103. defer sess.Close()
  104. if err := sess.Begin(); err != nil {
  105. return err
  106. }
  107. if err := createOrUpdateIssueNotifications(sess, issueID, commentID, notificationAuthorID); err != nil {
  108. return err
  109. }
  110. return sess.Commit()
  111. }
  112. func createOrUpdateIssueNotifications(e Engine, issueID, commentID int64, notificationAuthorID int64) error {
  113. issueWatches, err := getIssueWatchers(e, issueID)
  114. if err != nil {
  115. return err
  116. }
  117. issue, err := getIssueByID(e, issueID)
  118. if err != nil {
  119. return err
  120. }
  121. watches, err := getWatchers(e, issue.RepoID)
  122. if err != nil {
  123. return err
  124. }
  125. notifications, err := getNotificationsByIssueID(e, issueID)
  126. if err != nil {
  127. return err
  128. }
  129. alreadyNotified := make(map[int64]struct{}, len(issueWatches)+len(watches))
  130. notifyUser := func(userID int64) error {
  131. // do not send notification for the own issuer/commenter
  132. if userID == notificationAuthorID {
  133. return nil
  134. }
  135. if _, ok := alreadyNotified[userID]; ok {
  136. return nil
  137. }
  138. alreadyNotified[userID] = struct{}{}
  139. if notificationExists(notifications, issue.ID, userID) {
  140. return updateIssueNotification(e, userID, issue.ID, commentID, notificationAuthorID)
  141. }
  142. return createIssueNotification(e, userID, issue, commentID, notificationAuthorID)
  143. }
  144. for _, issueWatch := range issueWatches {
  145. // ignore if user unwatched the issue
  146. if !issueWatch.IsWatching {
  147. alreadyNotified[issueWatch.UserID] = struct{}{}
  148. continue
  149. }
  150. if err := notifyUser(issueWatch.UserID); err != nil {
  151. return err
  152. }
  153. }
  154. err = issue.loadRepo(e)
  155. if err != nil {
  156. return err
  157. }
  158. for _, watch := range watches {
  159. issue.Repo.Units = nil
  160. if issue.IsPull && !issue.Repo.checkUnitUser(e, watch.UserID, false, UnitTypePullRequests) {
  161. continue
  162. }
  163. if !issue.IsPull && !issue.Repo.checkUnitUser(e, watch.UserID, false, UnitTypeIssues) {
  164. continue
  165. }
  166. if err := notifyUser(watch.UserID); err != nil {
  167. return err
  168. }
  169. }
  170. return nil
  171. }
  172. func getNotificationsByIssueID(e Engine, issueID int64) (notifications []*Notification, err error) {
  173. err = e.
  174. Where("issue_id = ?", issueID).
  175. Find(&notifications)
  176. return
  177. }
  178. func notificationExists(notifications []*Notification, issueID, userID int64) bool {
  179. for _, notification := range notifications {
  180. if notification.IssueID == issueID && notification.UserID == userID {
  181. return true
  182. }
  183. }
  184. return false
  185. }
  186. func createIssueNotification(e Engine, userID int64, issue *Issue, commentID, updatedByID int64) error {
  187. notification := &Notification{
  188. UserID: userID,
  189. RepoID: issue.RepoID,
  190. Status: NotificationStatusUnread,
  191. IssueID: issue.ID,
  192. CommentID: commentID,
  193. UpdatedBy: updatedByID,
  194. }
  195. if issue.IsPull {
  196. notification.Source = NotificationSourcePullRequest
  197. } else {
  198. notification.Source = NotificationSourceIssue
  199. }
  200. _, err := e.Insert(notification)
  201. return err
  202. }
  203. func updateIssueNotification(e Engine, userID, issueID, commentID, updatedByID int64) error {
  204. notification, err := getIssueNotification(e, userID, issueID)
  205. if err != nil {
  206. return err
  207. }
  208. // NOTICE: Only update comment id when the before notification on this issue is read, otherwise you may miss some old comments.
  209. // But we need update update_by so that the notification will be reorder
  210. var cols []string
  211. if notification.Status == NotificationStatusRead {
  212. notification.Status = NotificationStatusUnread
  213. notification.CommentID = commentID
  214. cols = []string{"status", "update_by", "comment_id"}
  215. } else {
  216. notification.UpdatedBy = updatedByID
  217. cols = []string{"update_by"}
  218. }
  219. _, err = e.ID(notification.ID).Cols(cols...).Update(notification)
  220. return err
  221. }
  222. func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error) {
  223. notification := new(Notification)
  224. _, err := e.
  225. Where("user_id = ?", userID).
  226. And("issue_id = ?", issueID).
  227. Get(notification)
  228. return notification, err
  229. }
  230. // NotificationsForUser returns notifications for a given user and status
  231. func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPage int) (NotificationList, error) {
  232. return notificationsForUser(x, user, statuses, page, perPage)
  233. }
  234. func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
  235. if len(statuses) == 0 {
  236. return
  237. }
  238. sess := e.
  239. Where("user_id = ?", user.ID).
  240. In("status", statuses).
  241. OrderBy("updated_unix DESC")
  242. if page > 0 && perPage > 0 {
  243. sess.Limit(perPage, (page-1)*perPage)
  244. }
  245. err = sess.Find(&notifications)
  246. return
  247. }
  248. // CountUnread count unread notifications for a user
  249. func CountUnread(user *User) int64 {
  250. return countUnread(x, user.ID)
  251. }
  252. func countUnread(e Engine, userID int64) int64 {
  253. exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Count(new(Notification))
  254. if err != nil {
  255. log.Error("countUnread", err)
  256. return 0
  257. }
  258. return exist
  259. }
  260. // APIFormat converts a Notification to api.NotificationThread
  261. func (n *Notification) APIFormat() *api.NotificationThread {
  262. result := &api.NotificationThread{
  263. ID: n.ID,
  264. Unread: !(n.Status == NotificationStatusRead || n.Status == NotificationStatusPinned),
  265. Pinned: n.Status == NotificationStatusPinned,
  266. UpdatedAt: n.UpdatedUnix.AsTime(),
  267. URL: n.APIURL(),
  268. }
  269. //since user only get notifications when he has access to use minimal access mode
  270. if n.Repository != nil {
  271. result.Repository = n.Repository.APIFormat(AccessModeRead)
  272. }
  273. //handle Subject
  274. switch n.Source {
  275. case NotificationSourceIssue:
  276. result.Subject = &api.NotificationSubject{Type: "Issue"}
  277. if n.Issue != nil {
  278. result.Subject.Title = n.Issue.Title
  279. result.Subject.URL = n.Issue.APIURL()
  280. comment, err := n.Issue.GetLastComment()
  281. if err == nil && comment != nil {
  282. result.Subject.LatestCommentURL = comment.APIURL()
  283. }
  284. }
  285. case NotificationSourcePullRequest:
  286. result.Subject = &api.NotificationSubject{Type: "Pull"}
  287. if n.Issue != nil {
  288. result.Subject.Title = n.Issue.Title
  289. result.Subject.URL = n.Issue.APIURL()
  290. comment, err := n.Issue.GetLastComment()
  291. if err == nil && comment != nil {
  292. result.Subject.LatestCommentURL = comment.APIURL()
  293. }
  294. }
  295. case NotificationSourceCommit:
  296. result.Subject = &api.NotificationSubject{
  297. Type: "Commit",
  298. Title: n.CommitID,
  299. }
  300. //unused until now
  301. }
  302. return result
  303. }
  304. // LoadAttributes load Repo Issue User and Comment if not loaded
  305. func (n *Notification) LoadAttributes() (err error) {
  306. return n.loadAttributes(x)
  307. }
  308. func (n *Notification) loadAttributes(e Engine) (err error) {
  309. if err = n.loadRepo(e); err != nil {
  310. return
  311. }
  312. if err = n.loadIssue(e); err != nil {
  313. return
  314. }
  315. if err = n.loadUser(e); err != nil {
  316. return
  317. }
  318. if err = n.loadComment(e); err != nil {
  319. return
  320. }
  321. return
  322. }
  323. func (n *Notification) loadRepo(e Engine) (err error) {
  324. if n.Repository == nil {
  325. n.Repository, err = getRepositoryByID(e, n.RepoID)
  326. if err != nil {
  327. return fmt.Errorf("getRepositoryByID [%d]: %v", n.RepoID, err)
  328. }
  329. }
  330. return nil
  331. }
  332. func (n *Notification) loadIssue(e Engine) (err error) {
  333. if n.Issue == nil {
  334. n.Issue, err = getIssueByID(e, n.IssueID)
  335. if err != nil {
  336. return fmt.Errorf("getIssueByID [%d]: %v", n.IssueID, err)
  337. }
  338. return n.Issue.loadAttributes(e)
  339. }
  340. return nil
  341. }
  342. func (n *Notification) loadComment(e Engine) (err error) {
  343. if n.Comment == nil && n.CommentID > 0 {
  344. n.Comment, err = GetCommentByID(n.CommentID)
  345. if err != nil {
  346. return fmt.Errorf("GetCommentByID [%d] for issue ID [%d]: %v", n.CommentID, n.IssueID, err)
  347. }
  348. }
  349. return nil
  350. }
  351. func (n *Notification) loadUser(e Engine) (err error) {
  352. if n.User == nil {
  353. n.User, err = getUserByID(e, n.UserID)
  354. if err != nil {
  355. return fmt.Errorf("getUserByID [%d]: %v", n.UserID, err)
  356. }
  357. }
  358. return nil
  359. }
  360. // GetRepo returns the repo of the notification
  361. func (n *Notification) GetRepo() (*Repository, error) {
  362. return n.Repository, n.loadRepo(x)
  363. }
  364. // GetIssue returns the issue of the notification
  365. func (n *Notification) GetIssue() (*Issue, error) {
  366. return n.Issue, n.loadIssue(x)
  367. }
  368. // HTMLURL formats a URL-string to the notification
  369. func (n *Notification) HTMLURL() string {
  370. if n.Comment != nil {
  371. return n.Comment.HTMLURL()
  372. }
  373. return n.Issue.HTMLURL()
  374. }
  375. // APIURL formats a URL-string to the notification
  376. func (n *Notification) APIURL() string {
  377. return setting.AppURL + path.Join("api/v1/notifications/threads", fmt.Sprintf("%d", n.ID))
  378. }
  379. // NotificationList contains a list of notifications
  380. type NotificationList []*Notification
  381. // APIFormat converts a NotificationList to api.NotificationThread list
  382. func (nl NotificationList) APIFormat() []*api.NotificationThread {
  383. var result = make([]*api.NotificationThread, 0, len(nl))
  384. for _, n := range nl {
  385. result = append(result, n.APIFormat())
  386. }
  387. return result
  388. }
  389. // LoadAttributes load Repo Issue User and Comment if not loaded
  390. func (nl NotificationList) LoadAttributes() (err error) {
  391. for i := 0; i < len(nl); i++ {
  392. err = nl[i].LoadAttributes()
  393. if err != nil {
  394. return
  395. }
  396. }
  397. return
  398. }
  399. func (nl NotificationList) getPendingRepoIDs() []int64 {
  400. var ids = make(map[int64]struct{}, len(nl))
  401. for _, notification := range nl {
  402. if notification.Repository != nil {
  403. continue
  404. }
  405. if _, ok := ids[notification.RepoID]; !ok {
  406. ids[notification.RepoID] = struct{}{}
  407. }
  408. }
  409. return keysInt64(ids)
  410. }
  411. // LoadRepos loads repositories from database
  412. func (nl NotificationList) LoadRepos() (RepositoryList, error) {
  413. if len(nl) == 0 {
  414. return RepositoryList{}, nil
  415. }
  416. var repoIDs = nl.getPendingRepoIDs()
  417. var repos = make(map[int64]*Repository, len(repoIDs))
  418. var left = len(repoIDs)
  419. for left > 0 {
  420. var limit = defaultMaxInSize
  421. if left < limit {
  422. limit = left
  423. }
  424. rows, err := x.
  425. In("id", repoIDs[:limit]).
  426. Rows(new(Repository))
  427. if err != nil {
  428. return nil, err
  429. }
  430. for rows.Next() {
  431. var repo Repository
  432. err = rows.Scan(&repo)
  433. if err != nil {
  434. rows.Close()
  435. return nil, err
  436. }
  437. repos[repo.ID] = &repo
  438. }
  439. _ = rows.Close()
  440. left -= limit
  441. repoIDs = repoIDs[limit:]
  442. }
  443. var reposList = make(RepositoryList, 0, len(repoIDs))
  444. for _, notification := range nl {
  445. if notification.Repository == nil {
  446. notification.Repository = repos[notification.RepoID]
  447. }
  448. var found bool
  449. for _, r := range reposList {
  450. if r.ID == notification.Repository.ID {
  451. found = true
  452. break
  453. }
  454. }
  455. if !found {
  456. reposList = append(reposList, notification.Repository)
  457. }
  458. }
  459. return reposList, nil
  460. }
  461. func (nl NotificationList) getPendingIssueIDs() []int64 {
  462. var ids = make(map[int64]struct{}, len(nl))
  463. for _, notification := range nl {
  464. if notification.Issue != nil {
  465. continue
  466. }
  467. if _, ok := ids[notification.IssueID]; !ok {
  468. ids[notification.IssueID] = struct{}{}
  469. }
  470. }
  471. return keysInt64(ids)
  472. }
  473. // LoadIssues loads issues from database
  474. func (nl NotificationList) LoadIssues() error {
  475. if len(nl) == 0 {
  476. return nil
  477. }
  478. var issueIDs = nl.getPendingIssueIDs()
  479. var issues = make(map[int64]*Issue, len(issueIDs))
  480. var left = len(issueIDs)
  481. for left > 0 {
  482. var limit = defaultMaxInSize
  483. if left < limit {
  484. limit = left
  485. }
  486. rows, err := x.
  487. In("id", issueIDs[:limit]).
  488. Rows(new(Issue))
  489. if err != nil {
  490. return err
  491. }
  492. for rows.Next() {
  493. var issue Issue
  494. err = rows.Scan(&issue)
  495. if err != nil {
  496. rows.Close()
  497. return err
  498. }
  499. issues[issue.ID] = &issue
  500. }
  501. _ = rows.Close()
  502. left -= limit
  503. issueIDs = issueIDs[limit:]
  504. }
  505. for _, notification := range nl {
  506. if notification.Issue == nil {
  507. notification.Issue = issues[notification.IssueID]
  508. notification.Issue.Repo = notification.Repository
  509. }
  510. }
  511. return nil
  512. }
  513. func (nl NotificationList) getPendingCommentIDs() []int64 {
  514. var ids = make(map[int64]struct{}, len(nl))
  515. for _, notification := range nl {
  516. if notification.CommentID == 0 || notification.Comment != nil {
  517. continue
  518. }
  519. if _, ok := ids[notification.CommentID]; !ok {
  520. ids[notification.CommentID] = struct{}{}
  521. }
  522. }
  523. return keysInt64(ids)
  524. }
  525. // LoadComments loads comments from database
  526. func (nl NotificationList) LoadComments() error {
  527. if len(nl) == 0 {
  528. return nil
  529. }
  530. var commentIDs = nl.getPendingCommentIDs()
  531. var comments = make(map[int64]*Comment, len(commentIDs))
  532. var left = len(commentIDs)
  533. for left > 0 {
  534. var limit = defaultMaxInSize
  535. if left < limit {
  536. limit = left
  537. }
  538. rows, err := x.
  539. In("id", commentIDs[:limit]).
  540. Rows(new(Comment))
  541. if err != nil {
  542. return err
  543. }
  544. for rows.Next() {
  545. var comment Comment
  546. err = rows.Scan(&comment)
  547. if err != nil {
  548. rows.Close()
  549. return err
  550. }
  551. comments[comment.ID] = &comment
  552. }
  553. _ = rows.Close()
  554. left -= limit
  555. commentIDs = commentIDs[limit:]
  556. }
  557. for _, notification := range nl {
  558. if notification.CommentID > 0 && notification.Comment == nil && comments[notification.CommentID] != nil {
  559. notification.Comment = comments[notification.CommentID]
  560. notification.Comment.Issue = notification.Issue
  561. }
  562. }
  563. return nil
  564. }
  565. // GetNotificationCount returns the notification count for user
  566. func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
  567. return getNotificationCount(x, user, status)
  568. }
  569. func getNotificationCount(e Engine, user *User, status NotificationStatus) (count int64, err error) {
  570. count, err = e.
  571. Where("user_id = ?", user.ID).
  572. And("status = ?", status).
  573. Count(&Notification{})
  574. return
  575. }
  576. func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
  577. notification, err := getIssueNotification(e, userID, issueID)
  578. // ignore if not exists
  579. if err != nil {
  580. return nil
  581. }
  582. if notification.Status != NotificationStatusUnread {
  583. return nil
  584. }
  585. notification.Status = NotificationStatusRead
  586. _, err = e.ID(notification.ID).Update(notification)
  587. return err
  588. }
  589. // SetNotificationStatus change the notification status
  590. func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
  591. notification, err := getNotificationByID(x, notificationID)
  592. if err != nil {
  593. return err
  594. }
  595. if notification.UserID != user.ID {
  596. return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
  597. }
  598. notification.Status = status
  599. _, err = x.ID(notificationID).Update(notification)
  600. return err
  601. }
  602. // GetNotificationByID return notification by ID
  603. func GetNotificationByID(notificationID int64) (*Notification, error) {
  604. return getNotificationByID(x, notificationID)
  605. }
  606. func getNotificationByID(e Engine, notificationID int64) (*Notification, error) {
  607. notification := new(Notification)
  608. ok, err := e.
  609. Where("id = ?", notificationID).
  610. Get(notification)
  611. if err != nil {
  612. return nil, err
  613. }
  614. if !ok {
  615. return nil, ErrNotExist{ID: notificationID}
  616. }
  617. return notification, nil
  618. }
  619. // UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
  620. func UpdateNotificationStatuses(user *User, currentStatus NotificationStatus, desiredStatus NotificationStatus) error {
  621. n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
  622. _, err := x.
  623. Where("user_id = ? AND status = ?", user.ID, currentStatus).
  624. Cols("status", "updated_by", "updated_unix").
  625. Update(n)
  626. return err
  627. }