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.

user.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 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 notify
  5. import (
  6. "net/http"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/routers/api/v1/utils"
  12. )
  13. // ListNotifications list users's notification threads
  14. func ListNotifications(ctx *context.APIContext) {
  15. // swagger:operation GET /notifications notification notifyGetList
  16. // ---
  17. // summary: List users's notification threads
  18. // consumes:
  19. // - application/json
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: all
  24. // in: query
  25. // description: If true, show notifications marked as read. Default value is false
  26. // type: string
  27. // required: false
  28. // - name: since
  29. // in: query
  30. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  31. // type: string
  32. // format: date-time
  33. // required: false
  34. // - name: before
  35. // in: query
  36. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  37. // type: string
  38. // format: date-time
  39. // required: false
  40. // responses:
  41. // "200":
  42. // "$ref": "#/responses/NotificationThreadList"
  43. before, since, err := utils.GetQueryBeforeSince(ctx)
  44. if err != nil {
  45. ctx.InternalServerError(err)
  46. return
  47. }
  48. opts := models.FindNotificationOptions{
  49. UserID: ctx.User.ID,
  50. UpdatedBeforeUnix: before,
  51. UpdatedAfterUnix: since,
  52. }
  53. qAll := strings.Trim(ctx.Query("all"), " ")
  54. if qAll != "true" {
  55. opts.Status = models.NotificationStatusUnread
  56. }
  57. nl, err := models.GetNotifications(opts)
  58. if err != nil {
  59. ctx.InternalServerError(err)
  60. return
  61. }
  62. err = nl.LoadAttributes()
  63. if err != nil {
  64. ctx.InternalServerError(err)
  65. return
  66. }
  67. ctx.JSON(http.StatusOK, nl.APIFormat())
  68. }
  69. // ReadNotifications mark notification threads as read
  70. func ReadNotifications(ctx *context.APIContext) {
  71. // swagger:operation PUT /notifications notification notifyReadList
  72. // ---
  73. // summary: Mark notification threads as read
  74. // consumes:
  75. // - application/json
  76. // produces:
  77. // - application/json
  78. // parameters:
  79. // - name: last_read_at
  80. // in: query
  81. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  82. // type: string
  83. // format: date-time
  84. // required: false
  85. // responses:
  86. // "205":
  87. // "$ref": "#/responses/empty"
  88. lastRead := int64(0)
  89. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  90. if len(qLastRead) > 0 {
  91. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  92. if err != nil {
  93. ctx.InternalServerError(err)
  94. return
  95. }
  96. if !tmpLastRead.IsZero() {
  97. lastRead = tmpLastRead.Unix()
  98. }
  99. }
  100. opts := models.FindNotificationOptions{
  101. UserID: ctx.User.ID,
  102. UpdatedBeforeUnix: lastRead,
  103. Status: models.NotificationStatusUnread,
  104. }
  105. nl, err := models.GetNotifications(opts)
  106. if err != nil {
  107. ctx.InternalServerError(err)
  108. return
  109. }
  110. for _, n := range nl {
  111. err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
  112. if err != nil {
  113. ctx.InternalServerError(err)
  114. return
  115. }
  116. ctx.Status(http.StatusResetContent)
  117. }
  118. ctx.Status(http.StatusResetContent)
  119. }