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.

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