You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

follower.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. listUserFollowers(ctx, ctx.User)
  28. }
  29. // ListFollowers list user's followers
  30. // see https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-followers-of-a-user
  31. func ListFollowers(ctx *context.APIContext) {
  32. u := GetUserByParams(ctx)
  33. if ctx.Written() {
  34. return
  35. }
  36. listUserFollowers(ctx, u)
  37. }
  38. func listUserFollowing(ctx *context.APIContext, u *models.User) {
  39. users, err := u.GetFollowing(ctx.QueryInt("page"))
  40. if err != nil {
  41. ctx.Error(500, "GetFollowing", err)
  42. return
  43. }
  44. responseAPIUsers(ctx, users)
  45. }
  46. // ListMyFollowing list all my followings
  47. func ListMyFollowing(ctx *context.APIContext) {
  48. listUserFollowing(ctx, ctx.User)
  49. }
  50. // ListFollowing list user's followings
  51. // see https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-users-followed-by-another-user
  52. func ListFollowing(ctx *context.APIContext) {
  53. u := GetUserByParams(ctx)
  54. if ctx.Written() {
  55. return
  56. }
  57. listUserFollowing(ctx, u)
  58. }
  59. func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) {
  60. if u.IsFollowing(followID) {
  61. ctx.Status(204)
  62. } else {
  63. ctx.Status(404)
  64. }
  65. }
  66. // CheckMyFollowing check if the repo is followed by me
  67. // see https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-you-are-following-a-user
  68. func CheckMyFollowing(ctx *context.APIContext) {
  69. target := GetUserByParams(ctx)
  70. if ctx.Written() {
  71. return
  72. }
  73. checkUserFollowing(ctx, ctx.User, target.ID)
  74. }
  75. // CheckFollowing check if the repo is followed by user
  76. // see https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-one-user-follows-another
  77. func CheckFollowing(ctx *context.APIContext) {
  78. u := GetUserByParams(ctx)
  79. if ctx.Written() {
  80. return
  81. }
  82. target := GetUserByParamsName(ctx, ":target")
  83. if ctx.Written() {
  84. return
  85. }
  86. checkUserFollowing(ctx, u, target.ID)
  87. }
  88. // Follow follow one repository
  89. // see https://github.com/gogits/go-gogs-client/wiki/Users-Followers#follow-a-user
  90. func Follow(ctx *context.APIContext) {
  91. target := GetUserByParams(ctx)
  92. if ctx.Written() {
  93. return
  94. }
  95. if err := models.FollowUser(ctx.User.ID, target.ID); err != nil {
  96. ctx.Error(500, "FollowUser", err)
  97. return
  98. }
  99. ctx.Status(204)
  100. }
  101. // Unfollow unfollow one repository
  102. // see https://github.com/gogits/go-gogs-client/wiki/Users-Followers#unfollow-a-user
  103. func Unfollow(ctx *context.APIContext) {
  104. target := GetUserByParams(ctx)
  105. if ctx.Written() {
  106. return
  107. }
  108. if err := models.UnfollowUser(ctx.User.ID, target.ID); err != nil {
  109. ctx.Error(500, "UnfollowUser", err)
  110. return
  111. }
  112. ctx.Status(204)
  113. }