followers.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/middleware"
  9. "github.com/gogits/gogs/routers/api/v1/convert"
  10. )
  11. func responseApiUsers(ctx *middleware.Context, users []*models.User) {
  12. apiUsers := make([]*api.User, len(users))
  13. for i := range users {
  14. apiUsers[i] = convert.ToApiUser(users[i])
  15. }
  16. ctx.JSON(200, &apiUsers)
  17. }
  18. func listUserFollowers(ctx *middleware.Context, u *models.User) {
  19. users, err := u.GetFollowers(ctx.QueryInt("page"))
  20. if err != nil {
  21. ctx.APIError(500, "GetUserFollowers", err)
  22. return
  23. }
  24. responseApiUsers(ctx, users)
  25. }
  26. func ListMyFollowers(ctx *middleware.Context) {
  27. listUserFollowers(ctx, ctx.User)
  28. }
  29. // https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-followers-of-a-user
  30. func ListFollowers(ctx *middleware.Context) {
  31. u := GetUserByParams(ctx)
  32. if ctx.Written() {
  33. return
  34. }
  35. listUserFollowers(ctx, u)
  36. }
  37. func listUserFollowing(ctx *middleware.Context, u *models.User) {
  38. users, err := u.GetFollowing(ctx.QueryInt("page"))
  39. if err != nil {
  40. ctx.APIError(500, "GetFollowing", err)
  41. return
  42. }
  43. responseApiUsers(ctx, users)
  44. }
  45. func ListMyFollowing(ctx *middleware.Context) {
  46. listUserFollowing(ctx, ctx.User)
  47. }
  48. // https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-users-followed-by-another-user
  49. func ListFollowing(ctx *middleware.Context) {
  50. u := GetUserByParams(ctx)
  51. if ctx.Written() {
  52. return
  53. }
  54. listUserFollowing(ctx, u)
  55. }
  56. func checkUserFollowing(ctx *middleware.Context, u *models.User, followID int64) {
  57. if u.IsFollowing(followID) {
  58. ctx.Status(204)
  59. } else {
  60. ctx.Error(404)
  61. }
  62. }
  63. // https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-you-are-following-a-user
  64. func CheckMyFollowing(ctx *middleware.Context) {
  65. target := GetUserByParams(ctx)
  66. if ctx.Written() {
  67. return
  68. }
  69. checkUserFollowing(ctx, ctx.User, target.Id)
  70. }
  71. // https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-one-user-follows-another
  72. func CheckFollowing(ctx *middleware.Context) {
  73. u := GetUserByParams(ctx)
  74. if ctx.Written() {
  75. return
  76. }
  77. target := GetUserByParamsName(ctx, ":target")
  78. if ctx.Written() {
  79. return
  80. }
  81. checkUserFollowing(ctx, u, target.Id)
  82. }
  83. // https://github.com/gogits/go-gogs-client/wiki/Users-Followers#follow-a-user
  84. func Follow(ctx *middleware.Context) {
  85. target := GetUserByParams(ctx)
  86. if ctx.Written() {
  87. return
  88. }
  89. if err := models.FollowUser(ctx.User.Id, target.Id); err != nil {
  90. ctx.APIError(500, "FollowUser", err)
  91. return
  92. }
  93. ctx.Status(204)
  94. }
  95. // https://github.com/gogits/go-gogs-client/wiki/Users-Followers#unfollow-a-user
  96. func Unfollow(ctx *middleware.Context) {
  97. target := GetUserByParams(ctx)
  98. if ctx.Written() {
  99. return
  100. }
  101. if err := models.UnfollowUser(ctx.User.Id, target.Id); err != nil {
  102. ctx.APIError(500, "UnfollowUser", err)
  103. return
  104. }
  105. ctx.Status(204)
  106. }