本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

133 lines
3.3KB

  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. total, err := models.GetNotificationCount(c.User, status)
  60. if err != nil {
  61. c.ServerError("ErrGetNotificationCount", err)
  62. return
  63. }
  64. title := c.Tr("notifications")
  65. if status == models.NotificationStatusUnread && total > 0 {
  66. title = fmt.Sprintf("(%d) %s", total, title)
  67. }
  68. c.Data["Title"] = title
  69. c.Data["Keyword"] = keyword
  70. c.Data["Status"] = status
  71. c.Data["Notifications"] = notifications
  72. pager := context.NewPagination(int(total), perPage, page, 5)
  73. pager.SetDefaultParams(c)
  74. c.Data["Page"] = pager
  75. c.HTML(200, tplNotification)
  76. }
  77. // NotificationStatusPost is a route for changing the status of a notification
  78. func NotificationStatusPost(c *context.Context) {
  79. var (
  80. notificationID, _ = strconv.ParseInt(c.Req.PostFormValue("notification_id"), 10, 64)
  81. statusStr = c.Req.PostFormValue("status")
  82. status models.NotificationStatus
  83. )
  84. switch statusStr {
  85. case "read":
  86. status = models.NotificationStatusRead
  87. case "unread":
  88. status = models.NotificationStatusUnread
  89. case "pinned":
  90. status = models.NotificationStatusPinned
  91. default:
  92. c.ServerError("InvalidNotificationStatus", errors.New("Invalid notification status"))
  93. return
  94. }
  95. if err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
  96. c.ServerError("SetNotificationStatus", err)
  97. return
  98. }
  99. url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
  100. c.Redirect(url, 303)
  101. }
  102. // NotificationPurgePost is a route for 'purging' the list of notifications - marking all unread as read
  103. func NotificationPurgePost(c *context.Context) {
  104. err := models.UpdateNotificationStatuses(c.User, models.NotificationStatusUnread, models.NotificationStatusRead)
  105. if err != nil {
  106. c.ServerError("ErrUpdateNotificationStatuses", err)
  107. return
  108. }
  109. url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
  110. c.Redirect(url, 303)
  111. }
上海开阖软件有限公司 沪ICP备12045867号-1