follower.go 2.9 KB

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