Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

repo.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/modules/log"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. func statusStringToNotificationStatus(status string) models.NotificationStatus {
  15. switch strings.ToLower(strings.TrimSpace(status)) {
  16. case "unread":
  17. return models.NotificationStatusUnread
  18. case "read":
  19. return models.NotificationStatusRead
  20. case "pinned":
  21. return models.NotificationStatusPinned
  22. default:
  23. return 0
  24. }
  25. }
  26. func statusStringsToNotificationStatuses(statuses []string, defaultStatuses []string) []models.NotificationStatus {
  27. if len(statuses) == 0 {
  28. statuses = defaultStatuses
  29. }
  30. results := make([]models.NotificationStatus, 0, len(statuses))
  31. for _, status := range statuses {
  32. notificationStatus := statusStringToNotificationStatus(status)
  33. if notificationStatus > 0 {
  34. results = append(results, notificationStatus)
  35. }
  36. }
  37. return results
  38. }
  39. // ListRepoNotifications list users's notification threads on a specific repo
  40. func ListRepoNotifications(ctx *context.APIContext) {
  41. // swagger:operation GET /repos/{owner}/{repo}/notifications notification notifyGetRepoList
  42. // ---
  43. // summary: List users's notification threads on a specific repo
  44. // consumes:
  45. // - application/json
  46. // produces:
  47. // - application/json
  48. // parameters:
  49. // - name: owner
  50. // in: path
  51. // description: owner of the repo
  52. // type: string
  53. // required: true
  54. // - name: repo
  55. // in: path
  56. // description: name of the repo
  57. // type: string
  58. // required: true
  59. // - name: all
  60. // in: query
  61. // description: If true, show notifications marked as read. Default value is false
  62. // type: string
  63. // required: false
  64. // - name: status-types
  65. // in: query
  66. // description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned"
  67. // type: array
  68. // collectionFormat: multi
  69. // items:
  70. // type: string
  71. // required: false
  72. // - name: since
  73. // in: query
  74. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  75. // type: string
  76. // format: date-time
  77. // required: false
  78. // - name: before
  79. // in: query
  80. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  81. // type: string
  82. // format: date-time
  83. // required: false
  84. // - name: page
  85. // in: query
  86. // description: page number of results to return (1-based)
  87. // type: integer
  88. // - name: limit
  89. // in: query
  90. // description: page size of results
  91. // type: integer
  92. // responses:
  93. // "200":
  94. // "$ref": "#/responses/NotificationThreadList"
  95. before, since, err := utils.GetQueryBeforeSince(ctx)
  96. if err != nil {
  97. ctx.InternalServerError(err)
  98. return
  99. }
  100. opts := models.FindNotificationOptions{
  101. ListOptions: utils.GetListOptions(ctx),
  102. UserID: ctx.User.ID,
  103. RepoID: ctx.Repo.Repository.ID,
  104. UpdatedBeforeUnix: before,
  105. UpdatedAfterUnix: since,
  106. }
  107. if !ctx.QueryBool("all") {
  108. statuses := ctx.QueryStrings("status-types")
  109. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
  110. }
  111. nl, err := models.GetNotifications(opts)
  112. if err != nil {
  113. ctx.InternalServerError(err)
  114. return
  115. }
  116. err = nl.LoadAttributes()
  117. if err != nil {
  118. ctx.InternalServerError(err)
  119. return
  120. }
  121. ctx.JSON(http.StatusOK, nl.APIFormat())
  122. }
  123. // ReadRepoNotifications mark notification threads as read on a specific repo
  124. func ReadRepoNotifications(ctx *context.APIContext) {
  125. // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
  126. // ---
  127. // summary: Mark notification threads as read, pinned or unread on a specific repo
  128. // consumes:
  129. // - application/json
  130. // produces:
  131. // - application/json
  132. // parameters:
  133. // - name: owner
  134. // in: path
  135. // description: owner of the repo
  136. // type: string
  137. // required: true
  138. // - name: repo
  139. // in: path
  140. // description: name of the repo
  141. // type: string
  142. // required: true
  143. // - name: all
  144. // in: query
  145. // description: If true, mark all notifications on this repo. Default value is false
  146. // type: string
  147. // required: false
  148. // - name: status-types
  149. // in: query
  150. // description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
  151. // type: array
  152. // collectionFormat: multi
  153. // items:
  154. // type: string
  155. // required: false
  156. // - name: to-status
  157. // in: query
  158. // description: Status to mark notifications as. Defaults to read.
  159. // type: string
  160. // required: false
  161. // - name: last_read_at
  162. // in: query
  163. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  164. // type: string
  165. // format: date-time
  166. // required: false
  167. // responses:
  168. // "205":
  169. // "$ref": "#/responses/empty"
  170. lastRead := int64(0)
  171. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  172. if len(qLastRead) > 0 {
  173. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  174. if err != nil {
  175. ctx.InternalServerError(err)
  176. return
  177. }
  178. if !tmpLastRead.IsZero() {
  179. lastRead = tmpLastRead.Unix()
  180. }
  181. }
  182. opts := models.FindNotificationOptions{
  183. UserID: ctx.User.ID,
  184. RepoID: ctx.Repo.Repository.ID,
  185. UpdatedBeforeUnix: lastRead,
  186. }
  187. if !ctx.QueryBool("all") {
  188. statuses := ctx.QueryStrings("status-types")
  189. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
  190. log.Error("%v", opts.Status)
  191. }
  192. nl, err := models.GetNotifications(opts)
  193. if err != nil {
  194. ctx.InternalServerError(err)
  195. return
  196. }
  197. targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
  198. if targetStatus == 0 {
  199. targetStatus = models.NotificationStatusRead
  200. }
  201. for _, n := range nl {
  202. err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
  203. if err != nil {
  204. ctx.InternalServerError(err)
  205. return
  206. }
  207. ctx.Status(http.StatusResetContent)
  208. }
  209. ctx.Status(http.StatusResetContent)
  210. }