You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 4.9 kB

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>
5 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>
5 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>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/routers/api/v1/utils"
  13. )
  14. // ListNotifications list users's notification threads
  15. func ListNotifications(ctx *context.APIContext) {
  16. // swagger:operation GET /notifications notification notifyGetList
  17. // ---
  18. // summary: List users's notification threads
  19. // consumes:
  20. // - application/json
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: all
  25. // in: query
  26. // description: If true, show notifications marked as read. Default value is false
  27. // type: string
  28. // required: false
  29. // - name: status-types
  30. // in: query
  31. // description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned."
  32. // type: array
  33. // collectionFormat: multi
  34. // items:
  35. // type: string
  36. // required: false
  37. // - name: since
  38. // in: query
  39. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  40. // type: string
  41. // format: date-time
  42. // required: false
  43. // - name: before
  44. // in: query
  45. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  46. // type: string
  47. // format: date-time
  48. // required: false
  49. // - name: page
  50. // in: query
  51. // description: page number of results to return (1-based)
  52. // type: integer
  53. // - name: limit
  54. // in: query
  55. // description: page size of results
  56. // type: integer
  57. // responses:
  58. // "200":
  59. // "$ref": "#/responses/NotificationThreadList"
  60. before, since, err := utils.GetQueryBeforeSince(ctx)
  61. if err != nil {
  62. ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
  63. return
  64. }
  65. opts := models.FindNotificationOptions{
  66. ListOptions: utils.GetListOptions(ctx),
  67. UserID: ctx.User.ID,
  68. UpdatedBeforeUnix: before,
  69. UpdatedAfterUnix: since,
  70. }
  71. if !ctx.QueryBool("all") {
  72. statuses := ctx.QueryStrings("status-types")
  73. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
  74. }
  75. nl, err := models.GetNotifications(opts)
  76. if err != nil {
  77. ctx.InternalServerError(err)
  78. return
  79. }
  80. err = nl.LoadAttributes()
  81. if err != nil {
  82. ctx.InternalServerError(err)
  83. return
  84. }
  85. ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
  86. }
  87. // ReadNotifications mark notification threads as read, unread, or pinned
  88. func ReadNotifications(ctx *context.APIContext) {
  89. // swagger:operation PUT /notifications notification notifyReadList
  90. // ---
  91. // summary: Mark notification threads as read, pinned or unread
  92. // consumes:
  93. // - application/json
  94. // produces:
  95. // - application/json
  96. // parameters:
  97. // - name: last_read_at
  98. // in: query
  99. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  100. // type: string
  101. // format: date-time
  102. // required: false
  103. // - name: all
  104. // in: query
  105. // description: If true, mark all notifications on this repo. Default value is false
  106. // type: string
  107. // required: false
  108. // - name: status-types
  109. // in: query
  110. // description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
  111. // type: array
  112. // collectionFormat: multi
  113. // items:
  114. // type: string
  115. // required: false
  116. // - name: to-status
  117. // in: query
  118. // description: Status to mark notifications as, Defaults to read.
  119. // type: string
  120. // required: false
  121. // responses:
  122. // "205":
  123. // "$ref": "#/responses/empty"
  124. lastRead := int64(0)
  125. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  126. if len(qLastRead) > 0 {
  127. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  128. if err != nil {
  129. ctx.InternalServerError(err)
  130. return
  131. }
  132. if !tmpLastRead.IsZero() {
  133. lastRead = tmpLastRead.Unix()
  134. }
  135. }
  136. opts := models.FindNotificationOptions{
  137. UserID: ctx.User.ID,
  138. UpdatedBeforeUnix: lastRead,
  139. }
  140. if !ctx.QueryBool("all") {
  141. statuses := ctx.QueryStrings("status-types")
  142. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
  143. }
  144. nl, err := models.GetNotifications(opts)
  145. if err != nil {
  146. ctx.InternalServerError(err)
  147. return
  148. }
  149. targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
  150. if targetStatus == 0 {
  151. targetStatus = models.NotificationStatusRead
  152. }
  153. for _, n := range nl {
  154. err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
  155. if err != nil {
  156. ctx.InternalServerError(err)
  157. return
  158. }
  159. ctx.Status(http.StatusResetContent)
  160. }
  161. ctx.Status(http.StatusResetContent)
  162. }