| 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 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
// ListFollowers lists the followers for a user. Passing the empty string will |
| 14 |
// fetch followers for the authenticated user. |
| 15 |
// |
| 16 |
// GitHub API docs: https://docs.github.com/rest/users/followers#list-followers-of-a-user |
| 17 |
// GitHub API docs: https://docs.github.com/rest/users/followers#list-followers-of-the-authenticated-user |
| 18 |
// |
| 19 |
//meta:operation GET /user/followers |
| 20 |
//meta:operation GET /users/{username}/followers |
| 21 |
func (s *UsersService) ListFollowers(ctx context.Context, user string, opts *ListOptions) ([]*User, *Response, error) { |
| 22 |
var u string |
| 23 |
if user != "" { |
| 24 |
u = fmt.Sprintf("users/%v/followers", user) |
| 25 |
} else { |
| 26 |
u = "user/followers" |
| 27 |
} |
| 28 |
u, err := addOptions(u, opts) |
| 29 |
if err != nil { |
| 30 |
return nil, nil, err |
| 31 |
} |
| 32 |
|
| 33 |
req, err := s.client.NewRequest("GET", u, nil) |
| 34 |
if err != nil { |
| 35 |
return nil, nil, err |
| 36 |
} |
| 37 |
|
| 38 |
var users []*User |
| 39 |
resp, err := s.client.Do(ctx, req, &users) |
| 40 |
if err != nil { |
| 41 |
return nil, resp, err |
| 42 |
} |
| 43 |
|
| 44 |
return users, resp, nil |
| 45 |
} |
| 46 |
|
| 47 |
// ListFollowing lists the people that a user is following. Passing the empty |
| 48 |
// string will list people the authenticated user is following. |
| 49 |
// |
| 50 |
// GitHub API docs: https://docs.github.com/rest/users/followers#list-the-people-a-user-follows |
| 51 |
// GitHub API docs: https://docs.github.com/rest/users/followers#list-the-people-the-authenticated-user-follows |
| 52 |
// |
| 53 |
//meta:operation GET /user/following |
| 54 |
//meta:operation GET /users/{username}/following |
| 55 |
func (s *UsersService) ListFollowing(ctx context.Context, user string, opts *ListOptions) ([]*User, *Response, error) { |
| 56 |
var u string |
| 57 |
if user != "" { |
| 58 |
u = fmt.Sprintf("users/%v/following", user) |
| 59 |
} else { |
| 60 |
u = "user/following" |
| 61 |
} |
| 62 |
u, err := addOptions(u, opts) |
| 63 |
if err != nil { |
| 64 |
return nil, nil, err |
| 65 |
} |
| 66 |
|
| 67 |
req, err := s.client.NewRequest("GET", u, nil) |
| 68 |
if err != nil { |
| 69 |
return nil, nil, err |
| 70 |
} |
| 71 |
|
| 72 |
var users []*User |
| 73 |
resp, err := s.client.Do(ctx, req, &users) |
| 74 |
if err != nil { |
| 75 |
return nil, resp, err |
| 76 |
} |
| 77 |
|
| 78 |
return users, resp, nil |
| 79 |
} |
| 80 |
|
| 81 |
// IsFollowing checks if "user" is following "target". Passing the empty |
| 82 |
// string for "user" will check if the authenticated user is following "target". |
| 83 |
// |
| 84 |
// GitHub API docs: https://docs.github.com/rest/users/followers#check-if-a-person-is-followed-by-the-authenticated-user |
| 85 |
// GitHub API docs: https://docs.github.com/rest/users/followers#check-if-a-user-follows-another-user |
| 86 |
// |
| 87 |
//meta:operation GET /user/following/{username} |
| 88 |
//meta:operation GET /users/{username}/following/{target_user} |
| 89 |
func (s *UsersService) IsFollowing(ctx context.Context, user, target string) (bool, *Response, error) { |
| 90 |
var u string |
| 91 |
if user != "" { |
| 92 |
u = fmt.Sprintf("users/%v/following/%v", user, target) |
| 93 |
} else { |
| 94 |
u = fmt.Sprintf("user/following/%v", target) |
| 95 |
} |
| 96 |
|
| 97 |
req, err := s.client.NewRequest("GET", u, nil) |
| 98 |
if err != nil { |
| 99 |
return false, nil, err |
| 100 |
} |
| 101 |
|
| 102 |
resp, err := s.client.Do(ctx, req, nil) |
| 103 |
following, err := parseBoolResponse(err) |
| 104 |
return following, resp, err |
| 105 |
} |
| 106 |
|
| 107 |
// Follow will cause the authenticated user to follow the specified user. |
| 108 |
// |
| 109 |
// GitHub API docs: https://docs.github.com/rest/users/followers#follow-a-user |
| 110 |
// |
| 111 |
//meta:operation PUT /user/following/{username} |
| 112 |
func (s *UsersService) Follow(ctx context.Context, user string) (*Response, error) { |
| 113 |
u := fmt.Sprintf("user/following/%v", user) |
| 114 |
req, err := s.client.NewRequest("PUT", u, nil) |
| 115 |
if err != nil { |
| 116 |
return nil, err |
| 117 |
} |
| 118 |
|
| 119 |
return s.client.Do(ctx, req, nil) |
| 120 |
} |
| 121 |
|
| 122 |
// Unfollow will cause the authenticated user to unfollow the specified user. |
| 123 |
// |
| 124 |
// GitHub API docs: https://docs.github.com/rest/users/followers#unfollow-a-user |
| 125 |
// |
| 126 |
//meta:operation DELETE /user/following/{username} |
| 127 |
func (s *UsersService) Unfollow(ctx context.Context, user string) (*Response, error) { |
| 128 |
u := fmt.Sprintf("user/following/%v", user) |
| 129 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
|
| 134 |
return s.client.Do(ctx, req, nil) |
| 135 |
} |