2017-02-07 21:33:23 +00:00
|
|
|
// 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
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
// ListFollowers lists the followers for a user. Passing the empty string will
|
2017-02-07 21:33:23 +00:00
|
|
|
// fetch followers for the authenticated user.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/users/followers/#list-followers-of-a-user
|
|
|
|
func (s *UsersService) ListFollowers(ctx context.Context, user string, opt *ListOptions) ([]*User, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
var u string
|
|
|
|
if user != "" {
|
|
|
|
u = fmt.Sprintf("users/%v/followers", user)
|
|
|
|
} else {
|
|
|
|
u = "user/followers"
|
|
|
|
}
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
var users []*User
|
|
|
|
resp, err := s.client.Do(ctx, req, &users)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
return users, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
// ListFollowing lists the people that a user is following. Passing the empty
|
2017-02-07 21:33:23 +00:00
|
|
|
// string will list people the authenticated user is following.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user
|
|
|
|
func (s *UsersService) ListFollowing(ctx context.Context, user string, opt *ListOptions) ([]*User, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
var u string
|
|
|
|
if user != "" {
|
|
|
|
u = fmt.Sprintf("users/%v/following", user)
|
|
|
|
} else {
|
|
|
|
u = "user/following"
|
|
|
|
}
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
var users []*User
|
|
|
|
resp, err := s.client.Do(ctx, req, &users)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
return users, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
// IsFollowing checks if "user" is following "target". Passing the empty
|
2017-02-07 21:33:23 +00:00
|
|
|
// string for "user" will check if the authenticated user is following "target".
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/users/followers/#check-if-you-are-following-a-user
|
|
|
|
func (s *UsersService) IsFollowing(ctx context.Context, user, target string) (bool, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
following, err := parseBoolResponse(err)
|
|
|
|
return following, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow will cause the authenticated user to follow the specified user.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/users/followers/#follow-a-user
|
|
|
|
func (s *UsersService) Follow(ctx context.Context, user string) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("user/following/%v", user)
|
|
|
|
req, err := s.client.NewRequest("PUT", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unfollow will cause the authenticated user to unfollow the specified user.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/users/followers/#unfollow-a-user
|
|
|
|
func (s *UsersService) Unfollow(ctx context.Context, user string) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("user/following/%v", user)
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|