Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

repo.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // ListRepoNotifications list users's notification threads on a specific repo
  14. func ListRepoNotifications(ctx *context.APIContext) {
  15. // swagger:operation GET /repos/{owner}/{repo}/notifications notification notifyGetRepoList
  16. // ---
  17. // summary: List users's notification threads on a specific repo
  18. // consumes:
  19. // - application/json
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: owner
  24. // in: path
  25. // description: owner of the repo
  26. // type: string
  27. // required: true
  28. // - name: repo
  29. // in: path
  30. // description: name of the repo
  31. // type: string
  32. // required: true
  33. // - name: all
  34. // in: query
  35. // description: If true, show notifications marked as read. Default value is false
  36. // type: string
  37. // required: false
  38. // - name: since
  39. // in: query
  40. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  41. // type: string
  42. // format: date-time
  43. // required: false
  44. // - name: before
  45. // in: query
  46. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  47. // type: string
  48. // format: date-time
  49. // required: false
  50. // responses:
  51. // "200":
  52. // "$ref": "#/responses/NotificationThreadList"
  53. before, since, err := utils.GetQueryBeforeSince(ctx)
  54. if err != nil {
  55. ctx.InternalServerError(err)
  56. return
  57. }
  58. opts := models.FindNotificationOptions{
  59. UserID: ctx.User.ID,
  60. RepoID: ctx.Repo.Repository.ID,
  61. UpdatedBeforeUnix: before,
  62. UpdatedAfterUnix: since,
  63. }
  64. qAll := strings.Trim(ctx.Query("all"), " ")
  65. if qAll != "true" {
  66. opts.Status = models.NotificationStatusUnread
  67. }
  68. nl, err := models.GetNotifications(opts)
  69. if err != nil {
  70. ctx.InternalServerError(err)
  71. return
  72. }
  73. err = nl.LoadAttributes()
  74. if err != nil {
  75. ctx.InternalServerError(err)
  76. return
  77. }
  78. ctx.JSON(http.StatusOK, nl.APIFormat())
  79. }
  80. // ReadRepoNotifications mark notification threads as read on a specific repo
  81. func ReadRepoNotifications(ctx *context.APIContext) {
  82. // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
  83. // ---
  84. // summary: Mark notification threads as read on a specific repo
  85. // consumes:
  86. // - application/json
  87. // produces:
  88. // - application/json
  89. // parameters:
  90. // - name: owner
  91. // in: path
  92. // description: owner of the repo
  93. // type: string
  94. // required: true
  95. // - name: repo
  96. // in: path
  97. // description: name of the repo
  98. // type: string
  99. // required: true
  100. // - name: last_read_at
  101. // in: query
  102. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  103. // type: string
  104. // format: date-time
  105. // required: false
  106. // responses:
  107. // "205":
  108. // "$ref": "#/responses/empty"
  109. lastRead := int64(0)
  110. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  111. if len(qLastRead) > 0 {
  112. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  113. if err != nil {
  114. ctx.InternalServerError(err)
  115. return
  116. }
  117. if !tmpLastRead.IsZero() {
  118. lastRead = tmpLastRead.Unix()
  119. }
  120. }
  121. opts := models.FindNotificationOptions{
  122. UserID: ctx.User.ID,
  123. RepoID: ctx.Repo.Repository.ID,
  124. UpdatedBeforeUnix: lastRead,
  125. Status: models.NotificationStatusUnread,
  126. }
  127. nl, err := models.GetNotifications(opts)
  128. if err != nil {
  129. ctx.InternalServerError(err)
  130. return
  131. }
  132. for _, n := range nl {
  133. err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
  134. if err != nil {
  135. ctx.InternalServerError(err)
  136. return
  137. }
  138. ctx.Status(http.StatusResetContent)
  139. }
  140. ctx.Status(http.StatusResetContent)
  141. }