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