本站源代码
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.

224 lines
5.6KB

  1. // Copyright 2015 The Gogs 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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. )
  11. func responseAPIUsers(ctx *context.APIContext, users []*models.User) {
  12. apiUsers := make([]*api.User, len(users))
  13. for i := range users {
  14. apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  15. }
  16. ctx.JSON(200, &apiUsers)
  17. }
  18. func listUserFollowers(ctx *context.APIContext, u *models.User) {
  19. users, err := u.GetFollowers(ctx.QueryInt("page"))
  20. if err != nil {
  21. ctx.Error(500, "GetUserFollowers", err)
  22. return
  23. }
  24. responseAPIUsers(ctx, users)
  25. }
  26. // ListMyFollowers list the authenticated user's followers
  27. func ListMyFollowers(ctx *context.APIContext) {
  28. // swagger:operation GET /user/followers user userCurrentListFollowers
  29. // ---
  30. // summary: List the authenticated user's followers
  31. // produces:
  32. // - application/json
  33. // responses:
  34. // "200":
  35. // "$ref": "#/responses/UserList"
  36. listUserFollowers(ctx, ctx.User)
  37. }
  38. // ListFollowers list the given user's followers
  39. func ListFollowers(ctx *context.APIContext) {
  40. // swagger:operation GET /users/{username}/followers user userListFollowers
  41. // ---
  42. // summary: List the given user's followers
  43. // produces:
  44. // - application/json
  45. // parameters:
  46. // - name: username
  47. // in: path
  48. // description: username of user
  49. // type: string
  50. // required: true
  51. // responses:
  52. // "200":
  53. // "$ref": "#/responses/UserList"
  54. u := GetUserByParams(ctx)
  55. if ctx.Written() {
  56. return
  57. }
  58. listUserFollowers(ctx, u)
  59. }
  60. func listUserFollowing(ctx *context.APIContext, u *models.User) {
  61. users, err := u.GetFollowing(ctx.QueryInt("page"))
  62. if err != nil {
  63. ctx.Error(500, "GetFollowing", err)
  64. return
  65. }
  66. responseAPIUsers(ctx, users)
  67. }
  68. // ListMyFollowing list the users that the authenticated user is following
  69. func ListMyFollowing(ctx *context.APIContext) {
  70. // swagger:operation GET /user/following user userCurrentListFollowing
  71. // ---
  72. // summary: List the users that the authenticated user is following
  73. // produces:
  74. // - application/json
  75. // responses:
  76. // "200":
  77. // "$ref": "#/responses/UserList"
  78. listUserFollowing(ctx, ctx.User)
  79. }
  80. // ListFollowing list the users that the given user is following
  81. func ListFollowing(ctx *context.APIContext) {
  82. // swagger:operation GET /users/{username}/following user userListFollowing
  83. // ---
  84. // summary: List the users that the given user is following
  85. // produces:
  86. // - application/json
  87. // parameters:
  88. // - name: username
  89. // in: path
  90. // description: username of user
  91. // type: string
  92. // required: true
  93. // responses:
  94. // "200":
  95. // "$ref": "#/responses/UserList"
  96. u := GetUserByParams(ctx)
  97. if ctx.Written() {
  98. return
  99. }
  100. listUserFollowing(ctx, u)
  101. }
  102. func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) {
  103. if u.IsFollowing(followID) {
  104. ctx.Status(204)
  105. } else {
  106. ctx.NotFound()
  107. }
  108. }
  109. // CheckMyFollowing whether the given user is followed by the authenticated user
  110. func CheckMyFollowing(ctx *context.APIContext) {
  111. // swagger:operation GET /user/following/{username} user userCurrentCheckFollowing
  112. // ---
  113. // summary: Check whether a user is followed by the authenticated user
  114. // parameters:
  115. // - name: username
  116. // in: path
  117. // description: username of followed user
  118. // type: string
  119. // required: true
  120. // responses:
  121. // "204":
  122. // "$ref": "#/responses/empty"
  123. // "404":
  124. // "$ref": "#/responses/notFound"
  125. target := GetUserByParams(ctx)
  126. if ctx.Written() {
  127. return
  128. }
  129. checkUserFollowing(ctx, ctx.User, target.ID)
  130. }
  131. // CheckFollowing check if one user is following another user
  132. func CheckFollowing(ctx *context.APIContext) {
  133. // swagger:operation GET /users/{follower}/following/{followee} user userCheckFollowing
  134. // ---
  135. // summary: Check if one user is following another user
  136. // parameters:
  137. // - name: follower
  138. // in: path
  139. // description: username of following user
  140. // type: string
  141. // required: true
  142. // - name: followee
  143. // in: path
  144. // description: username of followed user
  145. // type: string
  146. // required: true
  147. // responses:
  148. // "204":
  149. // "$ref": "#/responses/empty"
  150. // "404":
  151. // "$ref": "#/responses/notFound"
  152. u := GetUserByParams(ctx)
  153. if ctx.Written() {
  154. return
  155. }
  156. target := GetUserByParamsName(ctx, ":target")
  157. if ctx.Written() {
  158. return
  159. }
  160. checkUserFollowing(ctx, u, target.ID)
  161. }
  162. // Follow follow a user
  163. func Follow(ctx *context.APIContext) {
  164. // swagger:operation PUT /user/following/{username} user userCurrentPutFollow
  165. // ---
  166. // summary: Follow a user
  167. // parameters:
  168. // - name: username
  169. // in: path
  170. // description: username of user to follow
  171. // type: string
  172. // required: true
  173. // responses:
  174. // "204":
  175. // "$ref": "#/responses/empty"
  176. target := GetUserByParams(ctx)
  177. if ctx.Written() {
  178. return
  179. }
  180. if err := models.FollowUser(ctx.User.ID, target.ID); err != nil {
  181. ctx.Error(500, "FollowUser", err)
  182. return
  183. }
  184. ctx.Status(204)
  185. }
  186. // Unfollow unfollow a user
  187. func Unfollow(ctx *context.APIContext) {
  188. // swagger:operation DELETE /user/following/{username} user userCurrentDeleteFollow
  189. // ---
  190. // summary: Unfollow a user
  191. // parameters:
  192. // - name: username
  193. // in: path
  194. // description: username of user to unfollow
  195. // type: string
  196. // required: true
  197. // responses:
  198. // "204":
  199. // "$ref": "#/responses/empty"
  200. target := GetUserByParams(ctx)
  201. if ctx.Written() {
  202. return
  203. }
  204. if err := models.UnfollowUser(ctx.User.ID, target.ID); err != nil {
  205. ctx.Error(500, "UnfollowUser", err)
  206. return
  207. }
  208. ctx.Status(204)
  209. }
上海开阖软件有限公司 沪ICP备12045867号-1