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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2019 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 user
  5. import (
  6. "errors"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. tplNotification base.TplName = "user/notification/notification"
  17. )
  18. // GetNotificationCount is the middleware that sets the notification count in the context
  19. func GetNotificationCount(c *context.Context) {
  20. if strings.HasPrefix(c.Req.URL.Path, "/api") {
  21. return
  22. }
  23. if !c.IsSigned {
  24. return
  25. }
  26. count, err := models.GetNotificationCount(c.User, models.NotificationStatusUnread)
  27. if err != nil {
  28. c.ServerError("GetNotificationCount", err)
  29. return
  30. }
  31. c.Data["NotificationUnreadCount"] = count
  32. }
  33. // Notifications is the notifications page
  34. func Notifications(c *context.Context) {
  35. var (
  36. keyword = strings.Trim(c.Query("q"), " ")
  37. status models.NotificationStatus
  38. page = c.QueryInt("page")
  39. perPage = c.QueryInt("perPage")
  40. )
  41. if page < 1 {
  42. page = 1
  43. }
  44. if perPage < 1 {
  45. perPage = 20
  46. }
  47. switch keyword {
  48. case "read":
  49. status = models.NotificationStatusRead
  50. default:
  51. status = models.NotificationStatusUnread
  52. }
  53. statuses := []models.NotificationStatus{status, models.NotificationStatusPinned}
  54. notifications, err := models.NotificationsForUser(c.User, statuses, page, perPage)
  55. if err != nil {
  56. c.ServerError("ErrNotificationsForUser", err)
  57. return
  58. }
  59. repos, err := notifications.LoadRepos()
  60. if err != nil {
  61. c.ServerError("LoadRepos", err)
  62. return
  63. }
  64. if err := repos.LoadAttributes(); err != nil {
  65. c.ServerError("LoadAttributes", err)
  66. return
  67. }
  68. if err := notifications.LoadIssues(); err != nil {
  69. c.ServerError("LoadIssues", err)
  70. return
  71. }
  72. if err := notifications.LoadComments(); err != nil {
  73. c.ServerError("LoadComments", err)
  74. return
  75. }
  76. total, err := models.GetNotificationCount(c.User, status)
  77. if err != nil {
  78. c.ServerError("ErrGetNotificationCount", err)
  79. return
  80. }
  81. title := c.Tr("notifications")
  82. if status == models.NotificationStatusUnread && total > 0 {
  83. title = fmt.Sprintf("(%d) %s", total, title)
  84. }
  85. c.Data["Title"] = title
  86. c.Data["Keyword"] = keyword
  87. c.Data["Status"] = status
  88. c.Data["Notifications"] = notifications
  89. pager := context.NewPagination(int(total), perPage, page, 5)
  90. pager.SetDefaultParams(c)
  91. c.Data["Page"] = pager
  92. c.HTML(200, tplNotification)
  93. }
  94. // NotificationStatusPost is a route for changing the status of a notification
  95. func NotificationStatusPost(c *context.Context) {
  96. var (
  97. notificationID, _ = strconv.ParseInt(c.Req.PostFormValue("notification_id"), 10, 64)
  98. statusStr = c.Req.PostFormValue("status")
  99. status models.NotificationStatus
  100. )
  101. switch statusStr {
  102. case "read":
  103. status = models.NotificationStatusRead
  104. case "unread":
  105. status = models.NotificationStatusUnread
  106. case "pinned":
  107. status = models.NotificationStatusPinned
  108. default:
  109. c.ServerError("InvalidNotificationStatus", errors.New("Invalid notification status"))
  110. return
  111. }
  112. if err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
  113. c.ServerError("SetNotificationStatus", err)
  114. return
  115. }
  116. url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
  117. c.Redirect(url, 303)
  118. }
  119. // NotificationPurgePost is a route for 'purging' the list of notifications - marking all unread as read
  120. func NotificationPurgePost(c *context.Context) {
  121. err := models.UpdateNotificationStatuses(c.User, models.NotificationStatusUnread, models.NotificationStatusRead)
  122. if err != nil {
  123. c.ServerError("ErrUpdateNotificationStatuses", err)
  124. return
  125. }
  126. url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
  127. c.Redirect(url, 303)
  128. }