Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

follower.go 6.9KB

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