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.

114 lines
2.7 KiB

  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.GetNotificationUnreadCount(c.User)
  25. if err != nil {
  26. c.Handle(500, "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 = 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.Handle(500, "ErrNotificationsForUser", err)
  55. return
  56. }
  57. total, err := models.GetNotificationCount(c.User, status)
  58. if err != nil {
  59. c.Handle(500, "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.Handle(500, "InvalidNotificationStatus", errors.New("Invalid notification status"))
  89. return
  90. }
  91. if err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
  92. c.Handle(500, "SetNotificationStatus", err)
  93. return
  94. }
  95. url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
  96. c.Redirect(url, 303)
  97. }