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.1KB

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