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

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