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.

repo.go 6.0KB

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/convert"
  12. "code.gitea.io/gitea/modules/log"
  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: boolean
  63. // - name: status-types
  64. // in: query
  65. // description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned"
  66. // type: array
  67. // collectionFormat: multi
  68. // items:
  69. // type: string
  70. // - name: subject-type
  71. // in: query
  72. // description: "filter notifications by subject type"
  73. // type: array
  74. // collectionFormat: multi
  75. // items:
  76. // type: string
  77. // enum: [issue,pull,commit,repository]
  78. // - name: since
  79. // in: query
  80. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  81. // type: string
  82. // format: date-time
  83. // - name: before
  84. // in: query
  85. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  86. // type: string
  87. // format: date-time
  88. // - name: page
  89. // in: query
  90. // description: page number of results to return (1-based)
  91. // type: integer
  92. // - name: limit
  93. // in: query
  94. // description: page size of results
  95. // type: integer
  96. // responses:
  97. // "200":
  98. // "$ref": "#/responses/NotificationThreadList"
  99. opts := getFindNotificationOptions(ctx)
  100. if ctx.Written() {
  101. return
  102. }
  103. opts.RepoID = ctx.Repo.Repository.ID
  104. totalCount, err := models.CountNotifications(opts)
  105. if err != nil {
  106. ctx.InternalServerError(err)
  107. return
  108. }
  109. nl, err := models.GetNotifications(opts)
  110. if err != nil {
  111. ctx.InternalServerError(err)
  112. return
  113. }
  114. err = nl.LoadAttributes()
  115. if err != nil {
  116. ctx.InternalServerError(err)
  117. return
  118. }
  119. ctx.SetTotalCountHeader(totalCount)
  120. ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
  121. }
  122. // ReadRepoNotifications mark notification threads as read on a specific repo
  123. func ReadRepoNotifications(ctx *context.APIContext) {
  124. // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
  125. // ---
  126. // summary: Mark notification threads as read, pinned or unread on a specific repo
  127. // consumes:
  128. // - application/json
  129. // produces:
  130. // - application/json
  131. // parameters:
  132. // - name: owner
  133. // in: path
  134. // description: owner of the repo
  135. // type: string
  136. // required: true
  137. // - name: repo
  138. // in: path
  139. // description: name of the repo
  140. // type: string
  141. // required: true
  142. // - name: all
  143. // in: query
  144. // description: If true, mark all notifications on this repo. Default value is false
  145. // type: string
  146. // required: false
  147. // - name: status-types
  148. // in: query
  149. // description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
  150. // type: array
  151. // collectionFormat: multi
  152. // items:
  153. // type: string
  154. // required: false
  155. // - name: to-status
  156. // in: query
  157. // description: Status to mark notifications as. Defaults to read.
  158. // type: string
  159. // required: false
  160. // - name: last_read_at
  161. // in: query
  162. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  163. // type: string
  164. // format: date-time
  165. // required: false
  166. // responses:
  167. // "205":
  168. // "$ref": "#/responses/empty"
  169. lastRead := int64(0)
  170. qLastRead := ctx.FormTrim("last_read_at")
  171. if len(qLastRead) > 0 {
  172. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  173. if err != nil {
  174. ctx.InternalServerError(err)
  175. return
  176. }
  177. if !tmpLastRead.IsZero() {
  178. lastRead = tmpLastRead.Unix()
  179. }
  180. }
  181. opts := &models.FindNotificationOptions{
  182. UserID: ctx.User.ID,
  183. RepoID: ctx.Repo.Repository.ID,
  184. UpdatedBeforeUnix: lastRead,
  185. }
  186. if !ctx.FormBool("all") {
  187. statuses := ctx.FormStrings("status-types")
  188. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
  189. log.Error("%v", opts.Status)
  190. }
  191. nl, err := models.GetNotifications(opts)
  192. if err != nil {
  193. ctx.InternalServerError(err)
  194. return
  195. }
  196. targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
  197. if targetStatus == 0 {
  198. targetStatus = models.NotificationStatusRead
  199. }
  200. for _, n := range nl {
  201. err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
  202. if err != nil {
  203. ctx.InternalServerError(err)
  204. return
  205. }
  206. ctx.Status(http.StatusResetContent)
  207. }
  208. ctx.Status(http.StatusResetContent)
  209. }