2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-05-03 17:31:35 -04:00
|
|
|
"net/url"
|
2020-04-29 15:59:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserService handles communication with the user
|
2020-06-22 21:52:34 -04:00
|
|
|
// related methods of the Reddit API.
|
2020-06-27 23:53:59 -04:00
|
|
|
//
|
|
|
|
// Reddit API docs: https://www.reddit.com/dev/api/#section_users
|
2020-07-21 23:05:24 -04:00
|
|
|
type UserService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-07-12 22:53:19 -04:00
|
|
|
type rootUser struct {
|
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data *User `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// User represents a Reddit user.
|
2020-04-29 15:59:18 -04:00
|
|
|
type User struct {
|
2020-06-22 21:52:34 -04:00
|
|
|
// this is not the full ID, watch out.
|
2020-05-03 17:31:35 -04:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Created *Timestamp `json:"created_utc,omitempty"`
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
PostKarma int `json:"link_karma"`
|
2020-04-29 15:59:18 -04:00
|
|
|
CommentKarma int `json:"comment_karma"`
|
|
|
|
|
|
|
|
IsFriend bool `json:"is_friend"`
|
|
|
|
IsEmployee bool `json:"is_employee"`
|
|
|
|
HasVerifiedEmail bool `json:"has_verified_email"`
|
|
|
|
NSFW bool `json:"over_18"`
|
2020-05-18 19:25:24 -04:00
|
|
|
IsSuspended bool `json:"is_suspended"`
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-08-01 17:46:12 -04:00
|
|
|
// UserSummary represents a Reddit user, but
|
2020-06-22 21:52:34 -04:00
|
|
|
// contains fewer pieces of information.
|
2020-08-01 17:46:12 -04:00
|
|
|
type UserSummary struct {
|
2020-05-03 17:31:35 -04:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Created *Timestamp `json:"created_utc,omitempty"`
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
PostKarma int `json:"link_karma"`
|
2020-04-29 15:59:18 -04:00
|
|
|
CommentKarma int `json:"comment_karma"`
|
|
|
|
|
|
|
|
NSFW bool `json:"profile_over_18"`
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:36:44 -04:00
|
|
|
// Relationship holds information about a relationship (friend/blocked).
|
|
|
|
type Relationship struct {
|
|
|
|
ID string `json:"rel_id,omitempty"`
|
|
|
|
User string `json:"name,omitempty"`
|
|
|
|
UserID string `json:"id,omitempty"`
|
|
|
|
Created *Timestamp `json:"date,omitempty"`
|
2020-06-18 21:41:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Blocked represents a blocked relationship.
|
|
|
|
type Blocked struct {
|
|
|
|
Blocked string `json:"name,omitempty"`
|
|
|
|
BlockedID string `json:"id,omitempty"`
|
|
|
|
Created *Timestamp `json:"date,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rootTrophyListing struct {
|
2020-06-27 23:15:27 -04:00
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data struct {
|
|
|
|
Trophies []rootTrophy `json:"trophies"`
|
|
|
|
} `json:"data"`
|
2020-06-18 21:41:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-27 23:15:27 -04:00
|
|
|
type rootTrophy struct {
|
2020-07-12 22:53:19 -04:00
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data *Trophy `json:"data,omitempty"`
|
2020-06-18 21:41:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-27 23:15:27 -04:00
|
|
|
// Trophy is a Reddit award.
|
|
|
|
type Trophy struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
2020-06-18 21:41:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Get returns information about the user.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Get(ctx context.Context, username string) (*User, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
path := fmt.Sprintf("user/%s/about", username)
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-12 22:53:19 -04:00
|
|
|
root := new(rootUser)
|
2020-04-29 15:59:18 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// GetMultipleByID returns multiple users from their full IDs.
|
2020-08-01 17:46:12 -04:00
|
|
|
// The response body is a map where the keys are the IDs (if they exist), and the value is the user.
|
|
|
|
func (s *UserService) GetMultipleByID(ctx context.Context, ids ...string) (map[string]*UserSummary, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
type query struct {
|
|
|
|
IDs []string `url:"ids,omitempty,comma"`
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/user_data_by_account_ids"
|
|
|
|
path, err := addOptions(path, query{ids})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-01 17:46:12 -04:00
|
|
|
root := make(map[string]*UserSummary)
|
|
|
|
resp, err := s.client.Do(ctx, req, &root)
|
2020-04-29 15:59:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-08-01 17:46:12 -04:00
|
|
|
return root, resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// UsernameAvailable checks whether a username is available for registration.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) UsernameAvailable(ctx context.Context, username string) (bool, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
type query struct {
|
|
|
|
User string `url:"user,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/username_available"
|
|
|
|
path, err := addOptions(path, query{username})
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(bool)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return false, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *root, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Overview returns a list of the client's posts and comments.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Overview(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Comments, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
return s.OverviewOf(ctx, s.client.Username, opts...)
|
2020-05-03 20:24:22 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// OverviewOf returns a list of the user's posts and comments.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) OverviewOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Posts, *Comments, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
2020-05-03 17:31:35 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := fmt.Sprintf("user/%s/overview", username)
|
|
|
|
path = addQuery(path, form)
|
2020-05-03 17:31:35 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, resp, err
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), root.getComments(), resp, nil
|
2020-05-03 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// Posts returns a list of the client's posts.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Posts(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
return s.PostsOf(ctx, s.client.Username, opts...)
|
2020-05-03 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// PostsOf returns a list of the user's posts.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) PostsOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := fmt.Sprintf("user/%s/submitted", username)
|
|
|
|
path = addQuery(path, form)
|
2020-05-03 17:31:35 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-05-03 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2020-05-03 18:54:41 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, resp, err
|
2020-05-03 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), resp, nil
|
2020-05-03 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// Comments returns a list of the client's comments.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Comments(ctx context.Context, opts ...SearchOptionSetter) (*Comments, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
return s.CommentsOf(ctx, s.client.Username, opts...)
|
|
|
|
}
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// CommentsOf returns a list of the user's comments.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) CommentsOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Comments, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := fmt.Sprintf("user/%s/comments", username)
|
|
|
|
path = addQuery(path, form)
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
2020-05-03 18:54:41 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, nil, err
|
2020-05-03 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2020-05-03 17:31:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2020-05-28 21:31:43 -04:00
|
|
|
|
|
|
|
return root.getComments(), resp, nil
|
2020-05-03 17:31:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// Saved returns a list of the user's saved posts and comments.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Saved(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Comments, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
|
|
|
|
|
|
|
path := fmt.Sprintf("user/%s/saved", s.client.Username)
|
|
|
|
path = addQuery(path, form)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
2020-05-03 17:31:35 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, nil, nil, err
|
2020-05-03 17:31:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2020-05-03 17:31:35 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, nil, resp, err
|
2020-05-03 17:31:35 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), root.getComments(), resp, nil
|
2020-05-03 17:31:35 -04:00
|
|
|
}
|
2020-05-03 18:54:41 -04:00
|
|
|
|
2020-07-03 14:27:56 -04:00
|
|
|
// Upvoted returns a list of your upvoted posts.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Upvoted(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-07-03 14:27:56 -04:00
|
|
|
return s.UpvotedOf(ctx, s.client.Username, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpvotedOf returns a list of the user's upvoted posts.
|
|
|
|
// The user's votes must be public for this to work.
|
|
|
|
func (s *UserService) UpvotedOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
|
|
|
|
2020-07-03 14:27:56 -04:00
|
|
|
path := fmt.Sprintf("user/%s/upvoted", username)
|
2020-05-28 21:31:43 -04:00
|
|
|
path = addQuery(path, form)
|
2020-05-03 18:54:41 -04:00
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), resp, nil
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:27:56 -04:00
|
|
|
// Downvoted returns a list of your downvoted posts.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Downvoted(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-07-03 14:27:56 -04:00
|
|
|
return s.DownvotedOf(ctx, s.client.Username, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownvotedOf returns a list of the user's downvoted posts.
|
|
|
|
// The user's votes must be public for this to work.
|
|
|
|
func (s *UserService) DownvotedOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
2020-05-18 23:11:47 -04:00
|
|
|
|
2020-07-03 14:27:56 -04:00
|
|
|
path := fmt.Sprintf("user/%s/downvoted", username)
|
2020-05-28 21:31:43 -04:00
|
|
|
path = addQuery(path, form)
|
2020-05-17 20:16:59 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2020-05-18 23:11:47 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, resp, err
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), resp, nil
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// Hidden returns a list of the user's hidden posts.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Hidden(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
2020-05-18 23:11:47 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := fmt.Sprintf("user/%s/hidden", s.client.Username)
|
|
|
|
path = addQuery(path, form)
|
2020-05-17 20:16:59 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
2020-05-17 20:16:59 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, nil, err
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2020-05-17 20:16:59 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, resp, err
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), resp, nil
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
// Gilded returns a list of the user's gilded posts.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Gilded(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
form := newSearchOptions(opts...)
|
2020-05-18 23:11:47 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := fmt.Sprintf("user/%s/gilded", s.client.Username)
|
|
|
|
path = addQuery(path, form)
|
2020-05-18 23:11:47 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
2020-05-17 20:16:59 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, nil, err
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
return root.getPosts(), resp, nil
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-07-02 20:36:44 -04:00
|
|
|
// GetFriendship returns relationship details with the specified user.
|
2020-06-18 21:41:17 -04:00
|
|
|
// If the user is not your friend, it will return an error.
|
2020-07-02 20:36:44 -04:00
|
|
|
func (s *UserService) GetFriendship(ctx context.Context, username string) (*Relationship, *Response, error) {
|
2020-06-18 21:41:17 -04:00
|
|
|
path := fmt.Sprintf("api/v1/me/friends/%s", username)
|
2020-06-17 22:35:19 -04:00
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-06-17 22:35:19 -04:00
|
|
|
|
2020-07-02 20:36:44 -04:00
|
|
|
root := new(Relationship)
|
2020-06-18 21:41:17 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2020-06-17 22:35:19 -04:00
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
return root, resp, nil
|
|
|
|
}
|
2020-06-17 22:35:19 -04:00
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
// Friend friends a user.
|
2020-07-02 20:36:44 -04:00
|
|
|
func (s *UserService) Friend(ctx context.Context, username string) (*Relationship, *Response, error) {
|
2020-05-28 21:31:43 -04:00
|
|
|
type request struct {
|
2020-06-18 21:41:17 -04:00
|
|
|
Username string `json:"name"`
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
2020-05-17 20:16:59 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := fmt.Sprintf("api/v1/me/friends/%s", username)
|
2020-06-18 21:41:17 -04:00
|
|
|
body := request{username}
|
2020-05-17 20:16:59 -04:00
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPut, path, body)
|
2020-05-18 23:11:47 -04:00
|
|
|
if err != nil {
|
2020-06-18 21:41:17 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:36:44 -04:00
|
|
|
root := new(Relationship)
|
2020-06-18 21:41:17 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfriend unfriends a user.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Unfriend(ctx context.Context, username string) (*Response, error) {
|
2020-06-18 21:41:17 -04:00
|
|
|
path := fmt.Sprintf("api/v1/me/friends/%s", username)
|
|
|
|
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block blocks a user.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Block(ctx context.Context, username string) (*Blocked, *Response, error) {
|
2020-06-18 21:41:17 -04:00
|
|
|
path := "api/block_user"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("name", username)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-06-18 21:41:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
root := new(Blocked)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
// // BlockByID blocks a user via their full id.
|
2020-06-27 23:53:59 -04:00
|
|
|
// func (s *UserService) BlockByID(ctx context.Context, id string) (*Blocked, *Response, error) {
|
2020-06-18 21:41:17 -04:00
|
|
|
// path := "api/block_user"
|
|
|
|
|
|
|
|
// form := url.Values{}
|
|
|
|
// form.Set("account_id", id)
|
|
|
|
|
|
|
|
// req, err := s.client.NewPostForm(path, form)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// root := new(Blocked)
|
|
|
|
// resp, err := s.client.Do(ctx, req, root)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, resp, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return root, resp, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Unblock unblocks a user.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Unblock(ctx context.Context, username string) (*Response, error) {
|
2020-07-21 23:05:24 -04:00
|
|
|
selfID, err := s.client.id(ctx)
|
2020-06-18 21:41:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
path := "api/unfriend"
|
2020-05-17 20:16:59 -04:00
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
form := url.Values{}
|
|
|
|
form.Set("name", username)
|
|
|
|
form.Set("type", "enemy")
|
2020-06-18 21:41:17 -04:00
|
|
|
form.Set("container", selfID)
|
2020-05-18 23:11:47 -04:00
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-17 20:16:59 -04:00
|
|
|
if err != nil {
|
2020-05-28 21:31:43 -04:00
|
|
|
return nil, err
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:31:43 -04:00
|
|
|
return s.client.Do(ctx, req, nil)
|
2020-05-18 23:11:47 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
// // UnblockByID unblocks a user via their full id.
|
2020-06-27 23:53:59 -04:00
|
|
|
// func (s *UserService) UnblockByID(ctx context.Context, id string) (*Response, error) {
|
2020-06-18 21:41:17 -04:00
|
|
|
// selfID, err := s.client.GetRedditID(ctx)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// path := "api/unfriend"
|
|
|
|
|
|
|
|
// form := url.Values{}
|
|
|
|
// form.Set("id", id)
|
|
|
|
// form.Set("type", "enemy")
|
|
|
|
// form.Set("container", selfID)
|
|
|
|
|
|
|
|
// req, err := s.client.NewPostForm(path, form)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return s.client.Do(ctx, req, nil)
|
|
|
|
// }
|
|
|
|
|
2020-06-22 20:03:53 -04:00
|
|
|
// Trophies returns a list of your trophies.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) Trophies(ctx context.Context) ([]Trophy, *Response, error) {
|
2020-06-22 20:03:53 -04:00
|
|
|
return s.TrophiesOf(ctx, s.client.Username)
|
2020-06-18 21:41:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 20:03:53 -04:00
|
|
|
// TrophiesOf returns a list of the specified user's trophies.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *UserService) TrophiesOf(ctx context.Context, username string) ([]Trophy, *Response, error) {
|
2020-06-18 21:41:17 -04:00
|
|
|
path := fmt.Sprintf("api/v1/user/%s/trophies", username)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
2020-05-28 21:31:43 -04:00
|
|
|
if err != nil {
|
2020-06-18 21:41:17 -04:00
|
|
|
return nil, nil, err
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 21:41:17 -04:00
|
|
|
root := new(rootTrophyListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-06-27 23:15:27 -04:00
|
|
|
var trophies []Trophy
|
|
|
|
for _, trophy := range root.Data.Trophies {
|
2020-07-12 22:53:19 -04:00
|
|
|
if trophy.Data != nil {
|
|
|
|
trophies = append(trophies, *trophy.Data)
|
|
|
|
}
|
2020-06-27 23:15:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return trophies, resp, nil
|
2020-05-17 20:16:59 -04:00
|
|
|
}
|
2020-08-01 16:31:57 -04:00
|
|
|
|
|
|
|
// Popular gets the user subreddits with the most activity.
|
|
|
|
func (s *UserService) Popular(ctx context.Context, opts ...SearchOptionSetter) (*Subreddits, *Response, error) {
|
|
|
|
form := newSearchOptions(opts...)
|
|
|
|
path := addQuery("users/popular", form)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.getSubreddits(), resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New gets the most recently created user subreddits.
|
|
|
|
func (s *UserService) New(ctx context.Context, opts ...SearchOptionSetter) (*Subreddits, *Response, error) {
|
|
|
|
form := newSearchOptions(opts...)
|
|
|
|
path := addQuery("users/new", form)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.getSubreddits(), resp, nil
|
|
|
|
}
|
2020-08-01 17:46:12 -04:00
|
|
|
|
|
|
|
// Search searches for users.
|
|
|
|
func (s *UserService) Search(ctx context.Context, query string, opts ...SearchOptionSetter) (*Users, *Response, error) {
|
|
|
|
opts = append(opts, setQuery(query))
|
|
|
|
form := newSearchOptions(opts...)
|
|
|
|
|
|
|
|
path := addQuery("users/search", form)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootListing)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.getUsers(), resp, nil
|
|
|
|
}
|