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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: status-types
  29. // in: query
  30. // description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned."
  31. // type: array
  32. // collectionFormat: multi
  33. // items:
  34. // type: string
  35. // required: false
  36. // - name: since
  37. // in: query
  38. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  39. // type: string
  40. // format: date-time
  41. // required: false
  42. // - name: before
  43. // in: query
  44. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  45. // type: string
  46. // format: date-time
  47. // required: false
  48. // - name: page
  49. // in: query
  50. // description: page number of results to return (1-based)
  51. // type: integer
  52. // - name: limit
  53. // in: query
  54. // description: page size of results
  55. // type: integer
  56. // responses:
  57. // "200":
  58. // "$ref": "#/responses/NotificationThreadList"
  59. before, since, err := utils.GetQueryBeforeSince(ctx)
  60. if err != nil {
  61. ctx.InternalServerError(err)
  62. return
  63. }
  64. opts := models.FindNotificationOptions{
  65. ListOptions: utils.GetListOptions(ctx),
  66. UserID: ctx.User.ID,
  67. UpdatedBeforeUnix: before,
  68. UpdatedAfterUnix: since,
  69. }
  70. if !ctx.QueryBool("all") {
  71. statuses := ctx.QueryStrings("status-types")
  72. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
  73. }
  74. nl, err := models.GetNotifications(opts)
  75. if err != nil {
  76. ctx.InternalServerError(err)
  77. return
  78. }
  79. err = nl.LoadAttributes()
  80. if err != nil {
  81. ctx.InternalServerError(err)
  82. return
  83. }
  84. ctx.JSON(http.StatusOK, nl.APIFormat())
  85. }
  86. // ReadNotifications mark notification threads as read, unread, or pinned
  87. func ReadNotifications(ctx *context.APIContext) {
  88. // swagger:operation PUT /notifications notification notifyReadList
  89. // ---
  90. // summary: Mark notification threads as read, pinned or unread
  91. // consumes:
  92. // - application/json
  93. // produces:
  94. // - application/json
  95. // parameters:
  96. // - name: last_read_at
  97. // in: query
  98. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  99. // type: string
  100. // format: date-time
  101. // required: false
  102. // - name: all
  103. // in: query
  104. // description: If true, mark all notifications on this repo. Default value is false
  105. // type: string
  106. // required: false
  107. // - name: status-types
  108. // in: query
  109. // description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
  110. // type: array
  111. // collectionFormat: multi
  112. // items:
  113. // type: string
  114. // required: false
  115. // - name: to-status
  116. // in: query
  117. // description: Status to mark notifications as, Defaults to read.
  118. // type: string
  119. // required: false
  120. // responses:
  121. // "205":
  122. // "$ref": "#/responses/empty"
  123. lastRead := int64(0)
  124. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  125. if len(qLastRead) > 0 {
  126. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  127. if err != nil {
  128. ctx.InternalServerError(err)
  129. return
  130. }
  131. if !tmpLastRead.IsZero() {
  132. lastRead = tmpLastRead.Unix()
  133. }
  134. }
  135. opts := models.FindNotificationOptions{
  136. UserID: ctx.User.ID,
  137. UpdatedBeforeUnix: lastRead,
  138. }
  139. if !ctx.QueryBool("all") {
  140. statuses := ctx.QueryStrings("status-types")
  141. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
  142. }
  143. nl, err := models.GetNotifications(opts)
  144. if err != nil {
  145. ctx.InternalServerError(err)
  146. return
  147. }
  148. targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
  149. if targetStatus == 0 {
  150. targetStatus = models.NotificationStatusRead
  151. }
  152. for _, n := range nl {
  153. err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
  154. if err != nil {
  155. ctx.InternalServerError(err)
  156. return
  157. }
  158. ctx.Status(http.StatusResetContent)
  159. }
  160. ctx.Status(http.StatusResetContent)
  161. }