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.

160 lines
4.2 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  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. // - name: page
  51. // in: query
  52. // description: page number of results to return (1-based)
  53. // type: integer
  54. // - name: limit
  55. // in: query
  56. // description: page size of results, maximum page size is 50
  57. // type: integer
  58. // responses:
  59. // "200":
  60. // "$ref": "#/responses/NotificationThreadList"
  61. before, since, err := utils.GetQueryBeforeSince(ctx)
  62. if err != nil {
  63. ctx.InternalServerError(err)
  64. return
  65. }
  66. opts := models.FindNotificationOptions{
  67. ListOptions: utils.GetListOptions(ctx),
  68. UserID: ctx.User.ID,
  69. RepoID: ctx.Repo.Repository.ID,
  70. UpdatedBeforeUnix: before,
  71. UpdatedAfterUnix: since,
  72. }
  73. qAll := strings.Trim(ctx.Query("all"), " ")
  74. if qAll != "true" {
  75. opts.Status = models.NotificationStatusUnread
  76. }
  77. nl, err := models.GetNotifications(opts)
  78. if err != nil {
  79. ctx.InternalServerError(err)
  80. return
  81. }
  82. err = nl.LoadAttributes()
  83. if err != nil {
  84. ctx.InternalServerError(err)
  85. return
  86. }
  87. ctx.JSON(http.StatusOK, nl.APIFormat())
  88. }
  89. // ReadRepoNotifications mark notification threads as read on a specific repo
  90. func ReadRepoNotifications(ctx *context.APIContext) {
  91. // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
  92. // ---
  93. // summary: Mark notification threads as read on a specific repo
  94. // consumes:
  95. // - application/json
  96. // produces:
  97. // - application/json
  98. // parameters:
  99. // - name: owner
  100. // in: path
  101. // description: owner of the repo
  102. // type: string
  103. // required: true
  104. // - name: repo
  105. // in: path
  106. // description: name of the repo
  107. // type: string
  108. // required: true
  109. // - name: last_read_at
  110. // in: query
  111. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  112. // type: string
  113. // format: date-time
  114. // required: false
  115. // responses:
  116. // "205":
  117. // "$ref": "#/responses/empty"
  118. lastRead := int64(0)
  119. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  120. if len(qLastRead) > 0 {
  121. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  122. if err != nil {
  123. ctx.InternalServerError(err)
  124. return
  125. }
  126. if !tmpLastRead.IsZero() {
  127. lastRead = tmpLastRead.Unix()
  128. }
  129. }
  130. opts := models.FindNotificationOptions{
  131. UserID: ctx.User.ID,
  132. RepoID: ctx.Repo.Repository.ID,
  133. UpdatedBeforeUnix: lastRead,
  134. Status: models.NotificationStatusUnread,
  135. }
  136. nl, err := models.GetNotifications(opts)
  137. if err != nil {
  138. ctx.InternalServerError(err)
  139. return
  140. }
  141. for _, n := range nl {
  142. err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
  143. if err != nil {
  144. ctx.InternalServerError(err)
  145. return
  146. }
  147. ctx.Status(http.StatusResetContent)
  148. }
  149. ctx.Status(http.StatusResetContent)
  150. }