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.

follower.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.User)
  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. ctx.SetTotalCountHeader(int64(u.NumFollowers))
  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.User)
  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. u := GetUserByParams(ctx)
  76. if ctx.Written() {
  77. return
  78. }
  79. listUserFollowers(ctx, u)
  80. }
  81. func listUserFollowing(ctx *context.APIContext, u *models.User) {
  82. users, err := u.GetFollowing(utils.GetListOptions(ctx))
  83. if err != nil {
  84. ctx.Error(http.StatusInternalServerError, "GetFollowing", err)
  85. return
  86. }
  87. ctx.SetTotalCountHeader(int64(u.NumFollowing))
  88. responseAPIUsers(ctx, users)
  89. }
  90. // ListMyFollowing list the users that the authenticated user is following
  91. func ListMyFollowing(ctx *context.APIContext) {
  92. // swagger:operation GET /user/following user userCurrentListFollowing
  93. // ---
  94. // summary: List the users that the authenticated user is following
  95. // parameters:
  96. // - name: page
  97. // in: query
  98. // description: page number of results to return (1-based)
  99. // type: integer
  100. // - name: limit
  101. // in: query
  102. // description: page size of results
  103. // type: integer
  104. // produces:
  105. // - application/json
  106. // responses:
  107. // "200":
  108. // "$ref": "#/responses/UserList"
  109. listUserFollowing(ctx, ctx.User)
  110. }
  111. // ListFollowing list the users that the given user is following
  112. func ListFollowing(ctx *context.APIContext) {
  113. // swagger:operation GET /users/{username}/following user userListFollowing
  114. // ---
  115. // summary: List the users that the given user is following
  116. // produces:
  117. // - application/json
  118. // parameters:
  119. // - name: username
  120. // in: path
  121. // description: username of user
  122. // type: string
  123. // required: true
  124. // - name: page
  125. // in: query
  126. // description: page number of results to return (1-based)
  127. // type: integer
  128. // - name: limit
  129. // in: query
  130. // description: page size of results
  131. // type: integer
  132. // responses:
  133. // "200":
  134. // "$ref": "#/responses/UserList"
  135. u := GetUserByParams(ctx)
  136. if ctx.Written() {
  137. return
  138. }
  139. listUserFollowing(ctx, u)
  140. }
  141. func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) {
  142. if u.IsFollowing(followID) {
  143. ctx.Status(http.StatusNoContent)
  144. } else {
  145. ctx.NotFound()
  146. }
  147. }
  148. // CheckMyFollowing whether the given user is followed by the authenticated user
  149. func CheckMyFollowing(ctx *context.APIContext) {
  150. // swagger:operation GET /user/following/{username} user userCurrentCheckFollowing
  151. // ---
  152. // summary: Check whether a user is followed by the authenticated user
  153. // parameters:
  154. // - name: username
  155. // in: path
  156. // description: username of followed user
  157. // type: string
  158. // required: true
  159. // responses:
  160. // "204":
  161. // "$ref": "#/responses/empty"
  162. // "404":
  163. // "$ref": "#/responses/notFound"
  164. target := GetUserByParams(ctx)
  165. if ctx.Written() {
  166. return
  167. }
  168. checkUserFollowing(ctx, ctx.User, target.ID)
  169. }
  170. // CheckFollowing check if one user is following another user
  171. func CheckFollowing(ctx *context.APIContext) {
  172. // swagger:operation GET /users/{follower}/following/{followee} user userCheckFollowing
  173. // ---
  174. // summary: Check if one user is following another user
  175. // parameters:
  176. // - name: follower
  177. // in: path
  178. // description: username of following user
  179. // type: string
  180. // required: true
  181. // - name: followee
  182. // in: path
  183. // description: username of followed user
  184. // type: string
  185. // required: true
  186. // responses:
  187. // "204":
  188. // "$ref": "#/responses/empty"
  189. // "404":
  190. // "$ref": "#/responses/notFound"
  191. u := GetUserByParams(ctx)
  192. if ctx.Written() {
  193. return
  194. }
  195. target := GetUserByParamsName(ctx, ":target")
  196. if ctx.Written() {
  197. return
  198. }
  199. checkUserFollowing(ctx, u, target.ID)
  200. }
  201. // Follow follow a user
  202. func Follow(ctx *context.APIContext) {
  203. // swagger:operation PUT /user/following/{username} user userCurrentPutFollow
  204. // ---
  205. // summary: Follow a user
  206. // parameters:
  207. // - name: username
  208. // in: path
  209. // description: username of user to follow
  210. // type: string
  211. // required: true
  212. // responses:
  213. // "204":
  214. // "$ref": "#/responses/empty"
  215. target := GetUserByParams(ctx)
  216. if ctx.Written() {
  217. return
  218. }
  219. if err := models.FollowUser(ctx.User.ID, target.ID); err != nil {
  220. ctx.Error(http.StatusInternalServerError, "FollowUser", err)
  221. return
  222. }
  223. ctx.Status(http.StatusNoContent)
  224. }
  225. // Unfollow unfollow a user
  226. func Unfollow(ctx *context.APIContext) {
  227. // swagger:operation DELETE /user/following/{username} user userCurrentDeleteFollow
  228. // ---
  229. // summary: Unfollow a user
  230. // parameters:
  231. // - name: username
  232. // in: path
  233. // description: username of user to unfollow
  234. // type: string
  235. // required: true
  236. // responses:
  237. // "204":
  238. // "$ref": "#/responses/empty"
  239. target := GetUserByParams(ctx)
  240. if ctx.Written() {
  241. return
  242. }
  243. if err := models.UnfollowUser(ctx.User.ID, target.ID); err != nil {
  244. ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
  245. return
  246. }
  247. ctx.Status(http.StatusNoContent)
  248. }