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.

269 lines
6.9 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
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 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package user
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. func responseAPIUsers(ctx *context.APIContext, users []*models.User) {
  15. apiUsers := make([]*api.User, len(users))
  16. for i := range users {
  17. apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  18. }
  19. ctx.JSON(http.StatusOK, &apiUsers)
  20. }
  21. func listUserFollowers(ctx *context.APIContext, u *models.User) {
  22. users, err := u.GetFollowers(utils.GetListOptions(ctx))
  23. if err != nil {
  24. ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
  25. return
  26. }
  27. responseAPIUsers(ctx, users)
  28. }
  29. // ListMyFollowers list the authenticated user's followers
  30. func ListMyFollowers(ctx *context.APIContext) {
  31. // swagger:operation GET /user/followers user userCurrentListFollowers
  32. // ---
  33. // summary: List the authenticated user's followers
  34. // parameters:
  35. // - name: page
  36. // in: query
  37. // description: page number of results to return (1-based)
  38. // type: integer
  39. // - name: limit
  40. // in: query
  41. // description: page size of results, maximum page size is 50
  42. // type: integer
  43. // produces:
  44. // - application/json
  45. // responses:
  46. // "200":
  47. // "$ref": "#/responses/UserList"
  48. listUserFollowers(ctx, ctx.User)
  49. }
  50. // ListFollowers list the given user's followers
  51. func ListFollowers(ctx *context.APIContext) {
  52. // swagger:operation GET /users/{username}/followers user userListFollowers
  53. // ---
  54. // summary: List the given user's followers
  55. // produces:
  56. // - application/json
  57. // parameters:
  58. // - name: username
  59. // in: path
  60. // description: username of user
  61. // type: string
  62. // required: true
  63. // - name: page
  64. // in: query
  65. // description: page number of results to return (1-based)
  66. // type: integer
  67. // - name: limit
  68. // in: query
  69. // description: page size of results, maximum page size is 50
  70. // type: integer
  71. // responses:
  72. // "200":
  73. // "$ref": "#/responses/UserList"
  74. u := GetUserByParams(ctx)
  75. if ctx.Written() {
  76. return
  77. }
  78. listUserFollowers(ctx, u)
  79. }
  80. func listUserFollowing(ctx *context.APIContext, u *models.User) {
  81. users, err := u.GetFollowing(utils.GetListOptions(ctx))
  82. if err != nil {
  83. ctx.Error(http.StatusInternalServerError, "GetFollowing", err)
  84. return
  85. }
  86. responseAPIUsers(ctx, users)
  87. }
  88. // ListMyFollowing list the users that the authenticated user is following
  89. func ListMyFollowing(ctx *context.APIContext) {
  90. // swagger:operation GET /user/following user userCurrentListFollowing
  91. // ---
  92. // summary: List the users that the authenticated user is following
  93. // parameters:
  94. // - name: page
  95. // in: query
  96. // description: page number of results to return (1-based)
  97. // type: integer
  98. // - name: limit
  99. // in: query
  100. // description: page size of results, maximum page size is 50
  101. // type: integer
  102. // produces:
  103. // - application/json
  104. // responses:
  105. // "200":
  106. // "$ref": "#/responses/UserList"
  107. listUserFollowing(ctx, ctx.User)
  108. }
  109. // ListFollowing list the users that the given user is following
  110. func ListFollowing(ctx *context.APIContext) {
  111. // swagger:operation GET /users/{username}/following user userListFollowing
  112. // ---
  113. // summary: List the users that the given user is following
  114. // produces:
  115. // - application/json
  116. // parameters:
  117. // - name: username
  118. // in: path
  119. // description: username of user
  120. // type: string
  121. // required: true
  122. // - name: page
  123. // in: query
  124. // description: page number of results to return (1-based)
  125. // type: integer
  126. // - name: limit
  127. // in: query
  128. // description: page size of results, maximum page size is 50
  129. // type: integer
  130. // responses:
  131. // "200":
  132. // "$ref": "#/responses/UserList"
  133. u := GetUserByParams(ctx)
  134. if ctx.Written() {
  135. return
  136. }
  137. listUserFollowing(ctx, u)
  138. }
  139. func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) {
  140. if u.IsFollowing(followID) {
  141. ctx.Status(http.StatusNoContent)
  142. } else {
  143. ctx.NotFound()
  144. }
  145. }
  146. // CheckMyFollowing whether the given user is followed by the authenticated user
  147. func CheckMyFollowing(ctx *context.APIContext) {
  148. // swagger:operation GET /user/following/{username} user userCurrentCheckFollowing
  149. // ---
  150. // summary: Check whether a user is followed by the authenticated user
  151. // parameters:
  152. // - name: username
  153. // in: path
  154. // description: username of followed user
  155. // type: string
  156. // required: true
  157. // responses:
  158. // "204":
  159. // "$ref": "#/responses/empty"
  160. // "404":
  161. // "$ref": "#/responses/notFound"
  162. target := GetUserByParams(ctx)
  163. if ctx.Written() {
  164. return
  165. }
  166. checkUserFollowing(ctx, ctx.User, target.ID)
  167. }
  168. // CheckFollowing check if one user is following another user
  169. func CheckFollowing(ctx *context.APIContext) {
  170. // swagger:operation GET /users/{follower}/following/{followee} user userCheckFollowing
  171. // ---
  172. // summary: Check if one user is following another user
  173. // parameters:
  174. // - name: follower
  175. // in: path
  176. // description: username of following user
  177. // type: string
  178. // required: true
  179. // - name: followee
  180. // in: path
  181. // description: username of followed user
  182. // type: string
  183. // required: true
  184. // responses:
  185. // "204":
  186. // "$ref": "#/responses/empty"
  187. // "404":
  188. // "$ref": "#/responses/notFound"
  189. u := GetUserByParams(ctx)
  190. if ctx.Written() {
  191. return
  192. }
  193. target := GetUserByParamsName(ctx, ":target")
  194. if ctx.Written() {
  195. return
  196. }
  197. checkUserFollowing(ctx, u, target.ID)
  198. }
  199. // Follow follow a user
  200. func Follow(ctx *context.APIContext) {
  201. // swagger:operation PUT /user/following/{username} user userCurrentPutFollow
  202. // ---
  203. // summary: Follow a user
  204. // parameters:
  205. // - name: username
  206. // in: path
  207. // description: username of user to follow
  208. // type: string
  209. // required: true
  210. // responses:
  211. // "204":
  212. // "$ref": "#/responses/empty"
  213. target := GetUserByParams(ctx)
  214. if ctx.Written() {
  215. return
  216. }
  217. if err := models.FollowUser(ctx.User.ID, target.ID); err != nil {
  218. ctx.Error(http.StatusInternalServerError, "FollowUser", err)
  219. return
  220. }
  221. ctx.Status(http.StatusNoContent)
  222. }
  223. // Unfollow unfollow a user
  224. func Unfollow(ctx *context.APIContext) {
  225. // swagger:operation DELETE /user/following/{username} user userCurrentDeleteFollow
  226. // ---
  227. // summary: Unfollow a user
  228. // parameters:
  229. // - name: username
  230. // in: path
  231. // description: username of user to unfollow
  232. // type: string
  233. // required: true
  234. // responses:
  235. // "204":
  236. // "$ref": "#/responses/empty"
  237. target := GetUserByParams(ctx)
  238. if ctx.Written() {
  239. return
  240. }
  241. if err := models.UnfollowUser(ctx.User.ID, target.ID); err != nil {
  242. ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
  243. return
  244. }
  245. ctx.Status(http.StatusNoContent)
  246. }