2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-07-06 22:10:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MultiService handles communication with the multireddit
|
|
|
|
// related methods of the Reddit API.
|
|
|
|
//
|
|
|
|
// Reddit API docs: https://www.reddit.com/dev/api#section_multis
|
2020-07-21 23:05:24 -04:00
|
|
|
type MultiService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
2020-07-06 22:10:47 -04:00
|
|
|
|
|
|
|
type multiRoot struct {
|
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data *Multi `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multi is a multireddit, i.e. a customizable group of subreddits.
|
|
|
|
// Users can create multis for custom navigation, instead of browsing
|
|
|
|
// one subreddit or all subreddits at a time.
|
|
|
|
type Multi struct {
|
2020-07-11 01:37:19 -04:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
DisplayName string `json:"display_name,omitempty"`
|
|
|
|
// Format: user/{username}/m/{multiname}
|
2020-07-06 22:10:47 -04:00
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Description string `json:"description_md,omitempty"`
|
|
|
|
Subreddits SubredditNames `json:"subreddits"`
|
2020-07-11 01:37:19 -04:00
|
|
|
CopiedFrom *string `json:"copied_from"`
|
2020-07-06 22:10:47 -04:00
|
|
|
|
|
|
|
Owner string `json:"owner,omitempty"`
|
2020-07-11 01:37:19 -04:00
|
|
|
OwnerID string `json:"owner_id,omitempty"`
|
2020-07-06 22:10:47 -04:00
|
|
|
Created *Timestamp `json:"created_utc,omitempty"`
|
|
|
|
|
|
|
|
NumberOfSubscribers int `json:"num_subscribers"`
|
|
|
|
Visibility string `json:"visibility,omitempty"`
|
|
|
|
Subscribed bool `json:"is_subscriber"`
|
|
|
|
Favorite bool `json:"is_favorited"`
|
|
|
|
CanEdit bool `json:"can_edit"`
|
|
|
|
NSFW bool `json:"over_18"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubredditNames is a list of subreddit names.
|
|
|
|
type SubredditNames []string
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
|
|
|
func (n *SubredditNames) UnmarshalJSON(data []byte) error {
|
|
|
|
var subreddits []map[string]string
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, &subreddits)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subreddit := range subreddits {
|
|
|
|
name, ok := subreddit["name"]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*n = append(*n, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-11 01:37:19 -04:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (n *SubredditNames) MarshalJSON() ([]byte, error) {
|
|
|
|
var subreddits []map[string]string
|
|
|
|
|
|
|
|
for _, sr := range *n {
|
|
|
|
subreddits = append(subreddits, map[string]string{
|
|
|
|
"name": sr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(subreddits)
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:10:47 -04:00
|
|
|
// MultiCopyRequest represents a request to copy a multireddit.
|
|
|
|
type MultiCopyRequest struct {
|
2020-07-11 01:37:19 -04:00
|
|
|
FromPath string
|
|
|
|
ToPath string
|
2020-07-06 22:10:47 -04:00
|
|
|
// Raw markdown text.
|
|
|
|
Description string
|
|
|
|
// No longer than 50 characters.
|
|
|
|
DisplayName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Form parameterizes the fields and returns the form.
|
|
|
|
func (r *MultiCopyRequest) Form() url.Values {
|
|
|
|
form := url.Values{}
|
2020-07-11 01:37:19 -04:00
|
|
|
form.Set("from", r.FromPath)
|
|
|
|
form.Set("to", r.ToPath)
|
2020-07-06 22:10:47 -04:00
|
|
|
form.Set("description_md", r.Description)
|
|
|
|
form.Set("display_name", r.DisplayName)
|
|
|
|
return form
|
|
|
|
}
|
|
|
|
|
2020-07-11 01:37:19 -04:00
|
|
|
// MultiCreateOrUpdateRequest represents a request to create/update a multireddit.
|
|
|
|
type MultiCreateOrUpdateRequest struct {
|
|
|
|
// For updates, this is the display name, i.e. the header of the multi.
|
|
|
|
// Not part of the path necessarily.
|
|
|
|
Name string `json:"display_name,omitempty"`
|
|
|
|
Description string `json:"description_md,omitempty"`
|
|
|
|
Subreddits SubredditNames `json:"subreddits,omitempty"`
|
|
|
|
// One of: private, public, hidden
|
|
|
|
Visibility string `json:"visibility,omitempty"`
|
2020-07-06 22:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Form parameterizes the fields and returns the form.
|
2020-07-11 01:37:19 -04:00
|
|
|
func (r *MultiCreateOrUpdateRequest) Form() url.Values {
|
2020-07-06 22:10:47 -04:00
|
|
|
byteValue, _ := json.Marshal(r)
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("model", string(byteValue))
|
|
|
|
return form
|
|
|
|
}
|
|
|
|
|
2020-07-11 01:37:19 -04:00
|
|
|
type rootMultiDescription struct {
|
|
|
|
Data struct {
|
|
|
|
Body string `json:"body_md"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:10:47 -04:00
|
|
|
// Get gets information about the multireddit from its url path.
|
|
|
|
func (s *MultiService) Get(ctx context.Context, multiPath string) (*Multi, *Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/%s", multiPath)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(multiRoot)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine returns your multireddits.
|
|
|
|
func (s *MultiService) Mine(ctx context.Context) ([]Multi, *Response, error) {
|
|
|
|
path := "api/multi/mine"
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var root []multiRoot
|
|
|
|
resp, err := s.client.Do(ctx, req, &root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multis := make([]Multi, 0)
|
|
|
|
for _, multi := range root {
|
|
|
|
multis = append(multis, *multi.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return multis, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Of returns the user's public multireddits.
|
2020-07-11 01:37:19 -04:00
|
|
|
// Or, if the user is you, all of your multireddits.
|
2020-07-06 22:10:47 -04:00
|
|
|
func (s *MultiService) Of(ctx context.Context, username string) ([]Multi, *Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/user/%s", username)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var root []multiRoot
|
|
|
|
resp, err := s.client.Do(ctx, req, &root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multis := make([]Multi, 0)
|
|
|
|
for _, multi := range root {
|
|
|
|
multis = append(multis, *multi.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return multis, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies a multireddit.
|
|
|
|
func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest) (*Multi, *Response, error) {
|
|
|
|
if copyRequest == nil {
|
|
|
|
return nil, nil, errors.New("copyRequest cannot be nil")
|
|
|
|
}
|
|
|
|
|
2020-07-11 01:37:19 -04:00
|
|
|
path := "api/multi/copy"
|
2020-07-06 22:10:47 -04:00
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, copyRequest.Form())
|
2020-07-06 22:10:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(multiRoot)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a multireddit.
|
2020-07-11 01:37:19 -04:00
|
|
|
func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) {
|
2020-07-06 22:10:47 -04:00
|
|
|
if createRequest == nil {
|
|
|
|
return nil, nil, errors.New("createRequest cannot be nil")
|
|
|
|
}
|
|
|
|
|
2020-07-11 01:37:19 -04:00
|
|
|
path := "api/multi"
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, createRequest.Form())
|
2020-07-11 01:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(multiRoot)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates a multireddit.
|
|
|
|
// If the multireddit does not exist, it will be created.
|
|
|
|
func (s *MultiService) Update(ctx context.Context, multiPath string, updateRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) {
|
|
|
|
if updateRequest == nil {
|
|
|
|
return nil, nil, errors.New("updateRequest cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("api/multi/%s", multiPath)
|
2020-07-06 22:10:47 -04:00
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPut, path, updateRequest.Form())
|
2020-07-06 22:10:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(multiRoot)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a multireddit.
|
|
|
|
func (s *MultiService) Delete(ctx context.Context, multiPath string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/%s", multiPath)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-07-11 01:37:19 -04:00
|
|
|
|
|
|
|
// GetDescription gets a multireddit's description.
|
|
|
|
func (s *MultiService) GetDescription(ctx context.Context, multiPath string) (string, *Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/%s/description", multiPath)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootMultiDescription)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return "", resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data.Body, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDescription updates a multireddit's description.
|
|
|
|
func (s *MultiService) UpdateDescription(ctx context.Context, multiPath string, description string) (string, *Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/%s/description", multiPath)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("model", fmt.Sprintf(`{"body_md":"%s"}`, description))
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPut, path, form)
|
2020-07-11 01:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootMultiDescription)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return "", resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Data.Body, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSubreddit adds a subreddit to a multireddit.
|
|
|
|
func (s *MultiService) AddSubreddit(ctx context.Context, multiPath string, subreddit string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/%s/r/%s", multiPath, subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("model", fmt.Sprintf(`{"name":"%s"}`, subreddit))
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPut, path, form)
|
2020-07-11 01:37:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSubreddit removes a subreddit from a multireddit.
|
|
|
|
func (s *MultiService) DeleteSubreddit(ctx context.Context, multiPath string, subreddit string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("api/multi/%s/r/%s", multiPath, subreddit)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|