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.

200 lines
5.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
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
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 2016 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 user
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/routers/api/v1/utils"
  11. )
  12. // getWatchedRepos returns the repos that the user with the specified userID is
  13. // watching
  14. func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, error) {
  15. watchedRepos, err := models.GetWatchedRepos(user.ID, private, listOptions)
  16. if err != nil {
  17. return nil, err
  18. }
  19. repos := make([]*api.Repository, len(watchedRepos))
  20. for i, watched := range watchedRepos {
  21. access, err := models.AccessLevel(user, watched)
  22. if err != nil {
  23. return nil, err
  24. }
  25. repos[i] = watched.APIFormat(access)
  26. }
  27. return repos, nil
  28. }
  29. // GetWatchedRepos returns the repos that the user specified in ctx is watching
  30. func GetWatchedRepos(ctx *context.APIContext) {
  31. // swagger:operation GET /users/{username}/subscriptions user userListSubscriptions
  32. // ---
  33. // summary: List the repositories watched by a user
  34. // produces:
  35. // - application/json
  36. // parameters:
  37. // - name: username
  38. // type: string
  39. // in: path
  40. // description: username of the user
  41. // required: true
  42. // - name: page
  43. // in: query
  44. // description: page number of results to return (1-based)
  45. // type: integer
  46. // - name: limit
  47. // in: query
  48. // description: page size of results, maximum page size is 50
  49. // type: integer
  50. // responses:
  51. // "200":
  52. // "$ref": "#/responses/RepositoryList"
  53. user := GetUserByParams(ctx)
  54. private := user.ID == ctx.User.ID
  55. repos, err := getWatchedRepos(user, private, utils.GetListOptions(ctx))
  56. if err != nil {
  57. ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
  58. }
  59. ctx.JSON(http.StatusOK, &repos)
  60. }
  61. // GetMyWatchedRepos returns the repos that the authenticated user is watching
  62. func GetMyWatchedRepos(ctx *context.APIContext) {
  63. // swagger:operation GET /user/subscriptions user userCurrentListSubscriptions
  64. // ---
  65. // summary: List repositories watched by the authenticated user
  66. // produces:
  67. // - application/json
  68. // parameters:
  69. // - name: page
  70. // in: query
  71. // description: page number of results to return (1-based)
  72. // type: integer
  73. // - name: limit
  74. // in: query
  75. // description: page size of results, maximum page size is 50
  76. // type: integer
  77. // responses:
  78. // "200":
  79. // "$ref": "#/responses/RepositoryList"
  80. repos, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx))
  81. if err != nil {
  82. ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
  83. }
  84. ctx.JSON(http.StatusOK, &repos)
  85. }
  86. // IsWatching returns whether the authenticated user is watching the repo
  87. // specified in ctx
  88. func IsWatching(ctx *context.APIContext) {
  89. // swagger:operation GET /repos/{owner}/{repo}/subscription repository userCurrentCheckSubscription
  90. // ---
  91. // summary: Check if the current user is watching a repo
  92. // parameters:
  93. // - name: owner
  94. // in: path
  95. // description: owner of the repo
  96. // type: string
  97. // required: true
  98. // - name: repo
  99. // in: path
  100. // description: name of the repo
  101. // type: string
  102. // required: true
  103. // responses:
  104. // "200":
  105. // "$ref": "#/responses/WatchInfo"
  106. // "404":
  107. // description: User is not watching this repo or repo do not exist
  108. if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
  109. ctx.JSON(http.StatusOK, api.WatchInfo{
  110. Subscribed: true,
  111. Ignored: false,
  112. Reason: nil,
  113. CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
  114. URL: subscriptionURL(ctx.Repo.Repository),
  115. RepositoryURL: ctx.Repo.Repository.APIURL(),
  116. })
  117. } else {
  118. ctx.NotFound()
  119. }
  120. }
  121. // Watch the repo specified in ctx, as the authenticated user
  122. func Watch(ctx *context.APIContext) {
  123. // swagger:operation PUT /repos/{owner}/{repo}/subscription repository userCurrentPutSubscription
  124. // ---
  125. // summary: Watch a repo
  126. // parameters:
  127. // - name: owner
  128. // in: path
  129. // description: owner of the repo
  130. // type: string
  131. // required: true
  132. // - name: repo
  133. // in: path
  134. // description: name of the repo
  135. // type: string
  136. // required: true
  137. // responses:
  138. // "200":
  139. // "$ref": "#/responses/WatchInfo"
  140. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  141. if err != nil {
  142. ctx.Error(http.StatusInternalServerError, "WatchRepo", err)
  143. return
  144. }
  145. ctx.JSON(http.StatusOK, api.WatchInfo{
  146. Subscribed: true,
  147. Ignored: false,
  148. Reason: nil,
  149. CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
  150. URL: subscriptionURL(ctx.Repo.Repository),
  151. RepositoryURL: ctx.Repo.Repository.APIURL(),
  152. })
  153. }
  154. // Unwatch the repo specified in ctx, as the authenticated user
  155. func Unwatch(ctx *context.APIContext) {
  156. // swagger:operation DELETE /repos/{owner}/{repo}/subscription repository userCurrentDeleteSubscription
  157. // ---
  158. // summary: Unwatch a repo
  159. // parameters:
  160. // - name: owner
  161. // in: path
  162. // description: owner of the repo
  163. // type: string
  164. // required: true
  165. // - name: repo
  166. // in: path
  167. // description: name of the repo
  168. // type: string
  169. // required: true
  170. // responses:
  171. // "204":
  172. // "$ref": "#/responses/empty"
  173. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  174. if err != nil {
  175. ctx.Error(http.StatusInternalServerError, "UnwatchRepo", err)
  176. return
  177. }
  178. ctx.Status(http.StatusNoContent)
  179. }
  180. // subscriptionURL returns the URL of the subscription API endpoint of a repo
  181. func subscriptionURL(repo *models.Repository) string {
  182. return repo.APIURL() + "/subscription"
  183. }