follower.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.JSONSuccess(&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.ServerError("GetUserFollowers", err)
  21. return
  22. }
  23. responseApiUsers(c, users)
  24. }
  25. func ListMyFollowers(c *context.APIContext) {
  26. listUserFollowers(c, c.User)
  27. }
  28. func ListFollowers(c *context.APIContext) {
  29. u := GetUserByParams(c)
  30. if c.Written() {
  31. return
  32. }
  33. listUserFollowers(c, u)
  34. }
  35. func listUserFollowing(c *context.APIContext, u *models.User) {
  36. users, err := u.GetFollowing(c.QueryInt("page"))
  37. if err != nil {
  38. c.ServerError("GetFollowing", err)
  39. return
  40. }
  41. responseApiUsers(c, users)
  42. }
  43. func ListMyFollowing(c *context.APIContext) {
  44. listUserFollowing(c, c.User)
  45. }
  46. func ListFollowing(c *context.APIContext) {
  47. u := GetUserByParams(c)
  48. if c.Written() {
  49. return
  50. }
  51. listUserFollowing(c, u)
  52. }
  53. func checkUserFollowing(c *context.APIContext, u *models.User, followID int64) {
  54. if u.IsFollowing(followID) {
  55. c.NotFound()
  56. } else {
  57. c.NotFound()
  58. }
  59. }
  60. func CheckMyFollowing(c *context.APIContext) {
  61. target := GetUserByParams(c)
  62. if c.Written() {
  63. return
  64. }
  65. checkUserFollowing(c, c.User, target.ID)
  66. }
  67. func CheckFollowing(c *context.APIContext) {
  68. u := GetUserByParams(c)
  69. if c.Written() {
  70. return
  71. }
  72. target := GetUserByParamsName(c, ":target")
  73. if c.Written() {
  74. return
  75. }
  76. checkUserFollowing(c, u, target.ID)
  77. }
  78. func Follow(c *context.APIContext) {
  79. target := GetUserByParams(c)
  80. if c.Written() {
  81. return
  82. }
  83. if err := models.FollowUser(c.User.ID, target.ID); err != nil {
  84. c.ServerError("FollowUser", err)
  85. return
  86. }
  87. c.NoContent()
  88. }
  89. func Unfollow(c *context.APIContext) {
  90. target := GetUserByParams(c)
  91. if c.Written() {
  92. return
  93. }
  94. if err := models.UnfollowUser(c.User.ID, target.ID); err != nil {
  95. c.ServerError("UnfollowUser", err)
  96. return
  97. }
  98. c.NoContent()
  99. }