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 19KB

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