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.

151 lines
3.9 KiB

  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. }