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

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