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.

223 lines
5.5 KiB

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