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.

repo.go 6.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/modules/log"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. )
  15. func statusStringToNotificationStatus(status string) models.NotificationStatus {
  16. switch strings.ToLower(strings.TrimSpace(status)) {
  17. case "unread":
  18. return models.NotificationStatusUnread
  19. case "read":
  20. return models.NotificationStatusRead
  21. case "pinned":
  22. return models.NotificationStatusPinned
  23. default:
  24. return 0
  25. }
  26. }
  27. func statusStringsToNotificationStatuses(statuses []string, defaultStatuses []string) []models.NotificationStatus {
  28. if len(statuses) == 0 {
  29. statuses = defaultStatuses
  30. }
  31. results := make([]models.NotificationStatus, 0, len(statuses))
  32. for _, status := range statuses {
  33. notificationStatus := statusStringToNotificationStatus(status)
  34. if notificationStatus > 0 {
  35. results = append(results, notificationStatus)
  36. }
  37. }
  38. return results
  39. }
  40. // ListRepoNotifications list users's notification threads on a specific repo
  41. func ListRepoNotifications(ctx *context.APIContext) {
  42. // swagger:operation GET /repos/{owner}/{repo}/notifications notification notifyGetRepoList
  43. // ---
  44. // summary: List users's notification threads on a specific repo
  45. // consumes:
  46. // - application/json
  47. // produces:
  48. // - application/json
  49. // parameters:
  50. // - name: owner
  51. // in: path
  52. // description: owner of the repo
  53. // type: string
  54. // required: true
  55. // - name: repo
  56. // in: path
  57. // description: name of the repo
  58. // type: string
  59. // required: true
  60. // - name: all
  61. // in: query
  62. // description: If true, show notifications marked as read. Default value is false
  63. // type: string
  64. // required: false
  65. // - name: status-types
  66. // in: query
  67. // description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned"
  68. // type: array
  69. // collectionFormat: multi
  70. // items:
  71. // type: string
  72. // required: false
  73. // - name: since
  74. // in: query
  75. // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
  76. // type: string
  77. // format: date-time
  78. // required: false
  79. // - name: before
  80. // in: query
  81. // description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
  82. // type: string
  83. // format: date-time
  84. // required: false
  85. // - name: page
  86. // in: query
  87. // description: page number of results to return (1-based)
  88. // type: integer
  89. // - name: limit
  90. // in: query
  91. // description: page size of results
  92. // type: integer
  93. // responses:
  94. // "200":
  95. // "$ref": "#/responses/NotificationThreadList"
  96. before, since, err := utils.GetQueryBeforeSince(ctx)
  97. if err != nil {
  98. ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
  99. return
  100. }
  101. opts := models.FindNotificationOptions{
  102. ListOptions: utils.GetListOptions(ctx),
  103. UserID: ctx.User.ID,
  104. RepoID: ctx.Repo.Repository.ID,
  105. UpdatedBeforeUnix: before,
  106. UpdatedAfterUnix: since,
  107. }
  108. if !ctx.QueryBool("all") {
  109. statuses := ctx.QueryStrings("status-types")
  110. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
  111. }
  112. nl, err := models.GetNotifications(opts)
  113. if err != nil {
  114. ctx.InternalServerError(err)
  115. return
  116. }
  117. err = nl.LoadAttributes()
  118. if err != nil {
  119. ctx.InternalServerError(err)
  120. return
  121. }
  122. ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
  123. }
  124. // ReadRepoNotifications mark notification threads as read on a specific repo
  125. func ReadRepoNotifications(ctx *context.APIContext) {
  126. // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
  127. // ---
  128. // summary: Mark notification threads as read, pinned or unread on a specific repo
  129. // consumes:
  130. // - application/json
  131. // produces:
  132. // - application/json
  133. // parameters:
  134. // - name: owner
  135. // in: path
  136. // description: owner of the repo
  137. // type: string
  138. // required: true
  139. // - name: repo
  140. // in: path
  141. // description: name of the repo
  142. // type: string
  143. // required: true
  144. // - name: all
  145. // in: query
  146. // description: If true, mark all notifications on this repo. Default value is false
  147. // type: string
  148. // required: false
  149. // - name: status-types
  150. // in: query
  151. // description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
  152. // type: array
  153. // collectionFormat: multi
  154. // items:
  155. // type: string
  156. // required: false
  157. // - name: to-status
  158. // in: query
  159. // description: Status to mark notifications as. Defaults to read.
  160. // type: string
  161. // required: false
  162. // - name: last_read_at
  163. // in: query
  164. // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
  165. // type: string
  166. // format: date-time
  167. // required: false
  168. // responses:
  169. // "205":
  170. // "$ref": "#/responses/empty"
  171. lastRead := int64(0)
  172. qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
  173. if len(qLastRead) > 0 {
  174. tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
  175. if err != nil {
  176. ctx.InternalServerError(err)
  177. return
  178. }
  179. if !tmpLastRead.IsZero() {
  180. lastRead = tmpLastRead.Unix()
  181. }
  182. }
  183. opts := models.FindNotificationOptions{
  184. UserID: ctx.User.ID,
  185. RepoID: ctx.Repo.Repository.ID,
  186. UpdatedBeforeUnix: lastRead,
  187. }
  188. if !ctx.QueryBool("all") {
  189. statuses := ctx.QueryStrings("status-types")
  190. opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
  191. log.Error("%v", opts.Status)
  192. }
  193. nl, err := models.GetNotifications(opts)
  194. if err != nil {
  195. ctx.InternalServerError(err)
  196. return
  197. }
  198. targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
  199. if targetStatus == 0 {
  200. targetStatus = models.NotificationStatusRead
  201. }
  202. for _, n := range nl {
  203. err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
  204. if err != nil {
  205. ctx.InternalServerError(err)
  206. return
  207. }
  208. ctx.Status(http.StatusResetContent)
  209. }
  210. ctx.Status(http.StatusResetContent)
  211. }