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.

182 lines
4.0 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 all my followers
  26. func ListMyFollowers(ctx *context.APIContext) {
  27. // swagger:route GET /user/followers userCurrentListFollowers
  28. //
  29. // Produces:
  30. // - application/json
  31. //
  32. // Responses:
  33. // 200: UserList
  34. // 500: error
  35. listUserFollowers(ctx, ctx.User)
  36. }
  37. // ListFollowers list user's followers
  38. func ListFollowers(ctx *context.APIContext) {
  39. // swagger:route GET /users/:username/followers userListFollowers
  40. //
  41. // Produces:
  42. // - application/json
  43. //
  44. // Responses:
  45. // 200: UserList
  46. // 500: error
  47. u := GetUserByParams(ctx)
  48. if ctx.Written() {
  49. return
  50. }
  51. listUserFollowers(ctx, u)
  52. }
  53. func listUserFollowing(ctx *context.APIContext, u *models.User) {
  54. users, err := u.GetFollowing(ctx.QueryInt("page"))
  55. if err != nil {
  56. ctx.Error(500, "GetFollowing", err)
  57. return
  58. }
  59. responseAPIUsers(ctx, users)
  60. }
  61. // ListMyFollowing list all my followings
  62. func ListMyFollowing(ctx *context.APIContext) {
  63. // swagger:route GET /user/following userCurrentListFollowing
  64. //
  65. // Produces:
  66. // - application/json
  67. //
  68. // Responses:
  69. // 200: UserList
  70. // 500: error
  71. listUserFollowing(ctx, ctx.User)
  72. }
  73. // ListFollowing list user's followings
  74. func ListFollowing(ctx *context.APIContext) {
  75. // swagger:route GET /users/{username}/following userListFollowing
  76. //
  77. // Produces:
  78. // - application/json
  79. //
  80. // Responses:
  81. // 200: UserList
  82. // 500: error
  83. u := GetUserByParams(ctx)
  84. if ctx.Written() {
  85. return
  86. }
  87. listUserFollowing(ctx, u)
  88. }
  89. func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) {
  90. if u.IsFollowing(followID) {
  91. ctx.Status(204)
  92. } else {
  93. ctx.Status(404)
  94. }
  95. }
  96. // CheckMyFollowing check if the repo is followed by me
  97. func CheckMyFollowing(ctx *context.APIContext) {
  98. // swagger:route GET /user/following/{username} userCurrentCheckFollowing
  99. //
  100. // Responses:
  101. // 204: empty
  102. // 404: notFound
  103. target := GetUserByParams(ctx)
  104. if ctx.Written() {
  105. return
  106. }
  107. checkUserFollowing(ctx, ctx.User, target.ID)
  108. }
  109. // CheckFollowing check if the repo is followed by user
  110. func CheckFollowing(ctx *context.APIContext) {
  111. // swagger:route GET /users/{username}/following/:target userCheckFollowing
  112. //
  113. // Responses:
  114. // 204: empty
  115. // 404: notFound
  116. u := GetUserByParams(ctx)
  117. if ctx.Written() {
  118. return
  119. }
  120. target := GetUserByParamsName(ctx, ":target")
  121. if ctx.Written() {
  122. return
  123. }
  124. checkUserFollowing(ctx, u, target.ID)
  125. }
  126. // Follow follow one repository
  127. func Follow(ctx *context.APIContext) {
  128. // swagger:route PUT /user/following/{username} userCurrentPutFollow
  129. //
  130. // Responses:
  131. // 204: empty
  132. // 500: error
  133. target := GetUserByParams(ctx)
  134. if ctx.Written() {
  135. return
  136. }
  137. if err := models.FollowUser(ctx.User.ID, target.ID); err != nil {
  138. ctx.Error(500, "FollowUser", err)
  139. return
  140. }
  141. ctx.Status(204)
  142. }
  143. // Unfollow unfollow one repository
  144. func Unfollow(ctx *context.APIContext) {
  145. // swagger:route DELETE /user/following/{username} userCurrentDeleteFollow
  146. //
  147. // Responses:
  148. // 204: empty
  149. // 500: error
  150. target := GetUserByParams(ctx)
  151. if ctx.Written() {
  152. return
  153. }
  154. if err := models.UnfollowUser(ctx.User.ID, target.ID); err != nil {
  155. ctx.Error(500, "UnfollowUser", err)
  156. return
  157. }
  158. ctx.Status(204)
  159. }