2020-04-23 22:57:47 -04:00
|
|
|
package geddit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-05-03 17:31:35 -04:00
|
|
|
"net/url"
|
2020-04-29 15:59:18 -04:00
|
|
|
"strings"
|
2020-04-23 22:57:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SubredditService handles communication with the subreddit
|
|
|
|
// related methods of the Reddit API
|
|
|
|
type SubredditService interface {
|
2020-05-16 11:38:47 -04:00
|
|
|
GetByName(ctx context.Context, subreddit string) (*Subreddit, *Response, error)
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
GetNew(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
|
|
|
|
GetMineWhereSubscriber(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
GetMineWhereContributor(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
GetMineWhereModerator(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
GetMineWhereStreams(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
GetHotLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error)
|
|
|
|
GetBestLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error)
|
|
|
|
GetNewLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error)
|
|
|
|
GetRisingLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error)
|
|
|
|
GetControversialLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error)
|
|
|
|
GetTopLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error)
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
GetSticky1(ctx context.Context, subreddit string) (*LinkAndComments, *Response, error)
|
|
|
|
GetSticky2(ctx context.Context, subreddit string) (*LinkAndComments, *Response, error)
|
2020-05-03 17:31:35 -04:00
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
Subscribe(ctx context.Context, subreddits ...string) (*Response, error)
|
2020-05-03 17:31:35 -04:00
|
|
|
SubscribeByID(ctx context.Context, ids ...string) (*Response, error)
|
2020-05-16 11:38:47 -04:00
|
|
|
Unsubscribe(ctx context.Context, subreddits ...string) (*Response, error)
|
2020-05-03 17:31:35 -04:00
|
|
|
UnsubscribeByID(ctx context.Context, ids ...string) (*Response, error)
|
2020-04-23 22:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubredditServiceOp implements the SubredditService interface
|
|
|
|
type SubredditServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ SubredditService = &SubredditServiceOp{}
|
|
|
|
|
|
|
|
// GetByName gets a subreddit by name
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) GetByName(ctx context.Context, subreddit string) (*Subreddit, *Response, error) {
|
|
|
|
if subreddit == "" {
|
2020-04-23 22:57:47 -04:00
|
|
|
return nil, nil, errors.New("empty subreddit name provided")
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
path := fmt.Sprintf("r/%s/about", subreddit)
|
2020-04-23 22:57:47 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(subredditRoot)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data, resp, nil
|
|
|
|
}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
// GetPopular returns popular subreddits
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/popular", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNew returns new subreddits
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetNew(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/new", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGold returns gold subreddits
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/gold", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefault returns default subreddits
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/default", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMineWhereSubscriber returns the list of subreddits the client is subscribed to
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetMineWhereSubscriber(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/mine/subscriber", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMineWhereContributor returns the list of subreddits the client is a contributor to
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetMineWhereContributor(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMineWhereModerator returns the list of subreddits the client is a moderator in
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetMineWhereModerator(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMineWhereStreams returns the list of subreddits the client is subscribed to and has hosted videos in
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) GetMineWhereStreams(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
// GetHotLinks returns the hot links
|
2020-05-16 11:38:47 -04:00
|
|
|
// If no subreddit are provided, then it runs the search against all those the client is subscribed to
|
2020-04-29 15:59:18 -04:00
|
|
|
// IMPORTANT: for subreddits, this will include the stickied posts (if any)
|
|
|
|
// PLUS the number of posts from the limit parameter (which is 25 by default)
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) GetHotLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
|
|
|
return s.getLinks(ctx, sortHot, opts, subreddits...)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
// GetBestLinks returns the best links
|
2020-05-16 11:38:47 -04:00
|
|
|
// If no subreddit are provided, then it runs the search against all those the client is subscribed to
|
2020-04-29 15:59:18 -04:00
|
|
|
// IMPORTANT: for subreddits, this will include the stickied posts (if any)
|
|
|
|
// PLUS the number of posts from the limit parameter (which is 25 by default)
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) GetBestLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
|
|
|
return s.getLinks(ctx, sortBest, opts, subreddits...)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
// GetNewLinks returns the new links
|
2020-05-16 11:38:47 -04:00
|
|
|
// If no subreddit are provided, then it runs the search against all those the client is subscribed to
|
|
|
|
func (s *SubredditServiceOp) GetNewLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
|
|
|
return s.getLinks(ctx, sortNew, opts, subreddits...)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
// GetRisingLinks returns the rising links
|
2020-05-16 11:38:47 -04:00
|
|
|
// If no subreddit are provided, then it runs the search against all those the client is subscribed to
|
|
|
|
func (s *SubredditServiceOp) GetRisingLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
|
|
|
return s.getLinks(ctx, sortRising, opts, subreddits...)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
// GetControversialLinks returns the controversial links
|
2020-05-16 11:38:47 -04:00
|
|
|
// If no subreddit are provided, then it runs the search against all those the client is subscribed to
|
|
|
|
func (s *SubredditServiceOp) GetControversialLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
|
|
|
return s.getLinks(ctx, sortControversial, opts, subreddits...)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
// GetTopLinks returns the top links
|
2020-05-16 11:38:47 -04:00
|
|
|
// If no subreddit are provided, then it runs the search against all those the client is subscribed to
|
|
|
|
func (s *SubredditServiceOp) GetTopLinks(ctx context.Context, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
|
|
|
return s.getLinks(ctx, sortTop, opts, subreddits...)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
// GetSticky1 returns the first stickied post on a subreddit (if it exists)
|
|
|
|
func (s *SubredditServiceOp) GetSticky1(ctx context.Context, name string) (*LinkAndComments, *Response, error) {
|
|
|
|
return s.getSticky(ctx, name, sticky1)
|
|
|
|
}
|
2020-05-03 17:31:35 -04:00
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
// GetSticky2 returns the second stickied post on a subreddit (if it exists)
|
|
|
|
func (s *SubredditServiceOp) GetSticky2(ctx context.Context, name string) (*LinkAndComments, *Response, error) {
|
|
|
|
return s.getSticky(ctx, name, sticky2)
|
|
|
|
}
|
2020-05-03 17:31:35 -04:00
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
// Subscribe subscribes to subreddits based on their names
|
2020-05-03 17:31:35 -04:00
|
|
|
// Returns {} on success
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) Subscribe(ctx context.Context, subreddits ...string) (*Response, error) {
|
2020-05-03 17:31:35 -04:00
|
|
|
form := url.Values{}
|
|
|
|
form.Set("action", "sub")
|
2020-05-16 11:38:47 -04:00
|
|
|
form.Set("sr_name", strings.Join(subreddits, ","))
|
2020-05-03 17:31:35 -04:00
|
|
|
return s.handleSubscription(ctx, form)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeByID subscribes to subreddits based on their id
|
|
|
|
// Returns {} on success
|
|
|
|
func (s *SubredditServiceOp) SubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("action", "sub")
|
|
|
|
form.Set("sr", strings.Join(ids, ","))
|
|
|
|
return s.handleSubscription(ctx, form)
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
// Unsubscribe unsubscribes from subreddits based on their names
|
2020-05-03 17:31:35 -04:00
|
|
|
// Returns {} on success
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) Unsubscribe(ctx context.Context, subreddits ...string) (*Response, error) {
|
2020-05-03 17:31:35 -04:00
|
|
|
form := url.Values{}
|
|
|
|
form.Set("action", "unsub")
|
2020-05-16 11:38:47 -04:00
|
|
|
form.Set("sr_name", strings.Join(subreddits, ","))
|
2020-05-03 17:31:35 -04:00
|
|
|
return s.handleSubscription(ctx, form)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnsubscribeByID unsubscribes from subreddits based on their id
|
|
|
|
// Returns {} on success
|
|
|
|
func (s *SubredditServiceOp) UnsubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("action", "unsub")
|
|
|
|
form.Set("sr", strings.Join(ids, ","))
|
|
|
|
return s.handleSubscription(ctx, form)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SubredditServiceOp) handleSubscription(ctx context.Context, form url.Values) (*Response, error) {
|
|
|
|
path := "api/subscribe"
|
|
|
|
req, err := s.client.NewPostForm(path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
func (s *SubredditServiceOp) getSubreddits(ctx context.Context, path string, opts *ListOptions) (*Subreddits, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
path, err := addOptions(path, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
root := new(rootListing)
|
2020-04-29 15:59:18 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
l := new(Subreddits)
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
if root.Data != nil {
|
|
|
|
l.Subreddits = root.Data.Things.Subreddits
|
|
|
|
l.After = root.Data.After
|
|
|
|
l.Before = root.Data.Before
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
return l, resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) getLinks(ctx context.Context, sort sort, opts *ListOptions, subreddits ...string) (*Links, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
path := sorts[sort]
|
2020-05-16 11:38:47 -04:00
|
|
|
if len(subreddits) > 0 {
|
|
|
|
path = fmt.Sprintf("r/%s/%s", strings.Join(subreddits, "+"), sorts[sort])
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
path, err := addOptions(path, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
root := new(rootListing)
|
2020-04-29 15:59:18 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
l := new(Links)
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
if root.Data != nil {
|
|
|
|
l.Links = root.Data.Things.Links
|
|
|
|
l.After = root.Data.After
|
|
|
|
l.Before = root.Data.Before
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
return l, resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
// getSticky returns one of the 2 stickied posts of the subreddit (if they exist)
|
2020-04-29 15:59:18 -04:00
|
|
|
// Num should be equal to 1 or 2, depending on which one you want
|
|
|
|
// If it's <= 1, it's 1
|
|
|
|
// If it's >= 2, it's 2
|
2020-05-16 11:38:47 -04:00
|
|
|
func (s *SubredditServiceOp) getSticky(ctx context.Context, subreddit string, num sticky) (*LinkAndComments, *Response, error) {
|
|
|
|
type query struct {
|
|
|
|
Num sticky `url:"num"`
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("r/%s/about/sticky", subreddit)
|
|
|
|
path, err := addOptions(path, query{num})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(LinkAndComments)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
2020-05-03 17:31:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
return root, resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|