users_followers.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // ListFollowers lists the followers for a user. Passing the empty string will
  11. // fetch followers for the authenticated user.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/users/followers/#list-followers-of-a-user
  14. func (s *UsersService) ListFollowers(ctx context.Context, user string, opt *ListOptions) ([]*User, *Response, error) {
  15. var u string
  16. if user != "" {
  17. u = fmt.Sprintf("users/%v/followers", user)
  18. } else {
  19. u = "user/followers"
  20. }
  21. u, err := addOptions(u, opt)
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. req, err := s.client.NewRequest("GET", u, nil)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. var users []*User
  30. resp, err := s.client.Do(ctx, req, &users)
  31. if err != nil {
  32. return nil, resp, err
  33. }
  34. return users, resp, nil
  35. }
  36. // ListFollowing lists the people that a user is following. Passing the empty
  37. // string will list people the authenticated user is following.
  38. //
  39. // GitHub API docs: https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user
  40. func (s *UsersService) ListFollowing(ctx context.Context, user string, opt *ListOptions) ([]*User, *Response, error) {
  41. var u string
  42. if user != "" {
  43. u = fmt.Sprintf("users/%v/following", user)
  44. } else {
  45. u = "user/following"
  46. }
  47. u, err := addOptions(u, opt)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. req, err := s.client.NewRequest("GET", u, nil)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. var users []*User
  56. resp, err := s.client.Do(ctx, req, &users)
  57. if err != nil {
  58. return nil, resp, err
  59. }
  60. return users, resp, nil
  61. }
  62. // IsFollowing checks if "user" is following "target". Passing the empty
  63. // string for "user" will check if the authenticated user is following "target".
  64. //
  65. // GitHub API docs: https://developer.github.com/v3/users/followers/#check-if-you-are-following-a-user
  66. func (s *UsersService) IsFollowing(ctx context.Context, user, target string) (bool, *Response, error) {
  67. var u string
  68. if user != "" {
  69. u = fmt.Sprintf("users/%v/following/%v", user, target)
  70. } else {
  71. u = fmt.Sprintf("user/following/%v", target)
  72. }
  73. req, err := s.client.NewRequest("GET", u, nil)
  74. if err != nil {
  75. return false, nil, err
  76. }
  77. resp, err := s.client.Do(ctx, req, nil)
  78. following, err := parseBoolResponse(err)
  79. return following, resp, err
  80. }
  81. // Follow will cause the authenticated user to follow the specified user.
  82. //
  83. // GitHub API docs: https://developer.github.com/v3/users/followers/#follow-a-user
  84. func (s *UsersService) Follow(ctx context.Context, user string) (*Response, error) {
  85. u := fmt.Sprintf("user/following/%v", user)
  86. req, err := s.client.NewRequest("PUT", u, nil)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return s.client.Do(ctx, req, nil)
  91. }
  92. // Unfollow will cause the authenticated user to unfollow the specified user.
  93. //
  94. // GitHub API docs: https://developer.github.com/v3/users/followers/#unfollow-a-user
  95. func (s *UsersService) Unfollow(ctx context.Context, user string) (*Response, error) {
  96. u := fmt.Sprintf("user/following/%v", user)
  97. req, err := s.client.NewRequest("DELETE", u, nil)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return s.client.Do(ctx, req, nil)
  102. }