follower.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package user
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. api "gitlab.com/gitote/go-gitote-client"
  11. )
  12. func responseAPIUsers(c *context.APIContext, users []*models.User) {
  13. apiUsers := make([]*api.User, len(users))
  14. for i := range users {
  15. apiUsers[i] = users[i].APIFormat()
  16. }
  17. c.JSON(200, &apiUsers)
  18. }
  19. func listUserFollowers(c *context.APIContext, u *models.User) {
  20. users, err := u.GetFollowers(c.QueryInt("page"))
  21. if err != nil {
  22. c.Error(500, "GetUserFollowers", err)
  23. return
  24. }
  25. responseAPIUsers(c, users)
  26. }
  27. // ListMyFollowers list the authenticated user's followers
  28. func ListMyFollowers(c *context.APIContext) {
  29. listUserFollowers(c, c.User)
  30. }
  31. // ListFollowers list the given user's followers
  32. func ListFollowers(c *context.APIContext) {
  33. u := GetUserByParams(c)
  34. if c.Written() {
  35. return
  36. }
  37. listUserFollowers(c, u)
  38. }
  39. func listUserFollowing(c *context.APIContext, u *models.User) {
  40. users, err := u.GetFollowing(c.QueryInt("page"))
  41. if err != nil {
  42. c.Error(500, "GetFollowing", err)
  43. return
  44. }
  45. responseAPIUsers(c, users)
  46. }
  47. // ListMyFollowing list the users that the authenticated user is following
  48. func ListMyFollowing(c *context.APIContext) {
  49. listUserFollowing(c, c.User)
  50. }
  51. // ListFollowing list the users that the given user is following
  52. func ListFollowing(c *context.APIContext) {
  53. u := GetUserByParams(c)
  54. if c.Written() {
  55. return
  56. }
  57. listUserFollowing(c, u)
  58. }
  59. func checkUserFollowing(c *context.APIContext, u *models.User, followID int64) {
  60. if u.IsFollowing(followID) {
  61. c.Status(204)
  62. } else {
  63. c.Status(404)
  64. }
  65. }
  66. // CheckMyFollowing whether the given user is followed by the authenticated user
  67. func CheckMyFollowing(c *context.APIContext) {
  68. target := GetUserByParams(c)
  69. if c.Written() {
  70. return
  71. }
  72. checkUserFollowing(c, c.User, target.ID)
  73. }
  74. // CheckFollowing check if one user is following another user
  75. func CheckFollowing(c *context.APIContext) {
  76. u := GetUserByParams(c)
  77. if c.Written() {
  78. return
  79. }
  80. target := GetUserByParamsName(c, ":target")
  81. if c.Written() {
  82. return
  83. }
  84. checkUserFollowing(c, u, target.ID)
  85. }
  86. // Follow follow a user
  87. func Follow(c *context.APIContext) {
  88. target := GetUserByParams(c)
  89. if c.Written() {
  90. return
  91. }
  92. if err := models.FollowUser(c.User.ID, target.ID); err != nil {
  93. c.Error(500, "FollowUser", err)
  94. return
  95. }
  96. c.Status(204)
  97. }
  98. // Unfollow unfollow a user
  99. func Unfollow(c *context.APIContext) {
  100. target := GetUserByParams(c)
  101. if c.Written() {
  102. return
  103. }
  104. if err := models.UnfollowUser(c.User.ID, target.ID); err != nil {
  105. c.Error(500, "UnfollowUser", err)
  106. return
  107. }
  108. c.Status(204)
  109. }