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.

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