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