2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-07-18 10:02:43 -04:00
|
|
|
"fmt"
|
2020-07-11 14:11:41 -04:00
|
|
|
"net/http"
|
2020-04-29 15:59:18 -04:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-05-04 15:16:34 -04:00
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2020-04-29 15:59:18 -04:00
|
|
|
)
|
|
|
|
|
2020-06-27 23:53:59 -04:00
|
|
|
// PostService handles communication with the post
|
2020-06-22 21:52:34 -04:00
|
|
|
// related methods of the Reddit API.
|
2020-07-13 23:05:24 -04:00
|
|
|
//
|
|
|
|
// Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments
|
2020-07-21 23:05:24 -04:00
|
|
|
type PostService struct {
|
2020-08-02 19:04:53 -04:00
|
|
|
*postAndCommentService
|
2020-07-21 23:05:24 -04:00
|
|
|
client *Client
|
|
|
|
}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-08-31 12:51:45 -04:00
|
|
|
type rootSubmittedPost struct {
|
2020-05-04 15:16:34 -04:00
|
|
|
JSON struct {
|
|
|
|
Data *Submitted `json:"data,omitempty"`
|
|
|
|
} `json:"json"`
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Submitted is a newly submitted post on Reddit.
|
2020-05-04 15:16:34 -04:00
|
|
|
type Submitted struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
FullID string `json:"name,omitempty"`
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-09-01 22:35:28 -04:00
|
|
|
// SubmitTextRequest are options used for text posts.
|
|
|
|
type SubmitTextRequest struct {
|
2020-05-04 15:16:34 -04:00
|
|
|
Subreddit string `url:"sr,omitempty"`
|
|
|
|
Title string `url:"title,omitempty"`
|
|
|
|
Text string `url:"text,omitempty"`
|
|
|
|
|
|
|
|
FlairID string `url:"flair_id,omitempty"`
|
|
|
|
FlairText string `url:"flair_text,omitempty"`
|
|
|
|
|
|
|
|
SendReplies *bool `url:"sendreplies,omitempty"`
|
|
|
|
NSFW bool `url:"nsfw,omitempty"`
|
|
|
|
Spoiler bool `url:"spoiler,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-09-01 22:35:28 -04:00
|
|
|
// SubmitLinkRequest are options used for link posts.
|
|
|
|
type SubmitLinkRequest struct {
|
2020-05-04 15:16:34 -04:00
|
|
|
Subreddit string `url:"sr,omitempty"`
|
|
|
|
Title string `url:"title,omitempty"`
|
|
|
|
URL string `url:"url,omitempty"`
|
|
|
|
|
|
|
|
FlairID string `url:"flair_id,omitempty"`
|
|
|
|
FlairText string `url:"flair_text,omitempty"`
|
|
|
|
|
|
|
|
SendReplies *bool `url:"sendreplies,omitempty"`
|
|
|
|
Resubmit bool `url:"resubmit,omitempty"`
|
|
|
|
NSFW bool `url:"nsfw,omitempty"`
|
|
|
|
Spoiler bool `url:"spoiler,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:49:30 -04:00
|
|
|
// Get a post with its comments.
|
2020-07-18 10:02:43 -04:00
|
|
|
// id is the ID36 of the post, not its full id.
|
|
|
|
// Example: instead of t3_abc123, use abc123.
|
2020-07-29 14:11:06 -04:00
|
|
|
func (s *PostService) Get(ctx context.Context, id string) (*PostAndComments, *Response, error) {
|
2020-07-18 10:02:43 -04:00
|
|
|
path := fmt.Sprintf("comments/%s", id)
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
2020-07-29 14:11:06 -04:00
|
|
|
return nil, nil, err
|
2020-07-18 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
root := new(PostAndComments)
|
2020-07-18 10:02:43 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
2020-07-29 14:11:06 -04:00
|
|
|
return nil, resp, err
|
2020-07-18 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
return root, resp, nil
|
2020-07-18 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 11:29:13 -04:00
|
|
|
// Duplicates returns the post with the id, and a list of its duplicates.
|
|
|
|
// id is the ID36 of the post, not its full id.
|
|
|
|
// Example: instead of t3_abc123, use abc123.
|
2020-08-29 14:20:30 -04:00
|
|
|
func (s *PostService) Duplicates(ctx context.Context, id string, opts *ListDuplicatePostOptions) (*Post, []*Post, *Response, error) {
|
2020-08-14 11:29:13 -04:00
|
|
|
path := fmt.Sprintf("duplicates/%s", id)
|
|
|
|
path, err := addOptions(path, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-01 22:35:28 -04:00
|
|
|
var root [2]thing
|
2020-08-14 11:29:13 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, &root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-09-01 22:35:28 -04:00
|
|
|
listing1, _ := root[0].Listing()
|
|
|
|
listing2, _ := root[1].Listing()
|
2020-08-29 14:20:30 -04:00
|
|
|
|
2020-09-01 22:35:28 -04:00
|
|
|
post := listing1.Posts()[0]
|
|
|
|
duplicates := listing2.Posts()
|
|
|
|
|
|
|
|
resp.After = listing2.After()
|
|
|
|
resp.Before = listing2.Before()
|
2020-08-14 11:29:13 -04:00
|
|
|
|
|
|
|
return post, duplicates, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *Response, error) {
|
|
|
|
path := "api/submit"
|
|
|
|
|
|
|
|
form, err := query.Values(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-31 12:51:45 -04:00
|
|
|
root := new(rootSubmittedPost)
|
2020-07-13 23:05:24 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.JSON.Data, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitText submits a text post.
|
2020-09-01 22:35:28 -04:00
|
|
|
func (s *PostService) SubmitText(ctx context.Context, opts SubmitTextRequest) (*Submitted, *Response, error) {
|
|
|
|
form := struct {
|
|
|
|
SubmitTextRequest
|
2020-05-04 15:16:34 -04:00
|
|
|
Kind string `url:"kind,omitempty"`
|
2020-09-01 22:35:28 -04:00
|
|
|
}{opts, "self"}
|
|
|
|
return s.submit(ctx, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
}
|
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
// SubmitLink submits a link post.
|
2020-09-01 22:35:28 -04:00
|
|
|
func (s *PostService) SubmitLink(ctx context.Context, opts SubmitLinkRequest) (*Submitted, *Response, error) {
|
|
|
|
form := struct {
|
|
|
|
SubmitLinkRequest
|
2020-05-04 15:16:34 -04:00
|
|
|
Kind string `url:"kind,omitempty"`
|
2020-09-01 22:35:28 -04:00
|
|
|
}{opts, "link"}
|
|
|
|
return s.submit(ctx, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
}
|
|
|
|
|
2020-08-27 18:49:30 -04:00
|
|
|
// Edit a post.
|
2020-07-13 23:05:24 -04:00
|
|
|
func (s *PostService) Edit(ctx context.Context, id string, text string) (*Post, *Response, error) {
|
|
|
|
path := "api/editusertext"
|
2020-05-04 15:16:34 -04:00
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
form := url.Values{}
|
2020-05-04 15:16:34 -04:00
|
|
|
form.Set("api_type", "json")
|
2020-07-13 23:05:24 -04:00
|
|
|
form.Set("return_rtjson", "true")
|
|
|
|
form.Set("thing_id", id)
|
|
|
|
form.Set("text", text)
|
2020-05-04 15:16:34 -04:00
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
root := new(Post)
|
2020-05-04 15:16:34 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
return root, resp, nil
|
2020-05-04 15:16:34 -04:00
|
|
|
}
|
|
|
|
|
2020-08-27 18:49:30 -04:00
|
|
|
// Hide posts.
|
2020-07-13 23:05:24 -04:00
|
|
|
func (s *PostService) Hide(ctx context.Context, ids ...string) (*Response, error) {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, errors.New("must provide at least 1 id")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/hide"
|
2020-05-04 15:16:34 -04:00
|
|
|
|
|
|
|
form := url.Values{}
|
2020-07-13 23:05:24 -04:00
|
|
|
form.Set("id", strings.Join(ids, ","))
|
2020-05-04 15:16:34 -04:00
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:49:30 -04:00
|
|
|
// Unhide posts.
|
2020-07-13 23:05:24 -04:00
|
|
|
func (s *PostService) Unhide(ctx context.Context, ids ...string) (*Response, error) {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, errors.New("must provide at least 1 id")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/unhide"
|
2020-05-04 15:16:34 -04:00
|
|
|
|
|
|
|
form := url.Values{}
|
2020-07-13 23:05:24 -04:00
|
|
|
form.Set("id", strings.Join(ids, ","))
|
2020-05-04 15:16:34 -04:00
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// MarkNSFW marks a post as NSFW.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *PostService) MarkNSFW(ctx context.Context, id string) (*Response, error) {
|
2020-05-04 15:16:34 -04:00
|
|
|
path := "api/marknsfw"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("id", id)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// UnmarkNSFW unmarks a post as NSFW.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *PostService) UnmarkNSFW(ctx context.Context, id string) (*Response, error) {
|
2020-05-04 15:16:34 -04:00
|
|
|
path := "api/unmarknsfw"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("id", id)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Spoiler marks a post as a spoiler.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *PostService) Spoiler(ctx context.Context, id string) (*Response, error) {
|
2020-05-04 15:16:34 -04:00
|
|
|
path := "api/spoiler"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("id", id)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Unspoiler unmarks a post as a spoiler.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *PostService) Unspoiler(ctx context.Context, id string) (*Response, error) {
|
2020-05-04 15:16:34 -04:00
|
|
|
path := "api/unspoiler"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("id", id)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-05-04 15:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-07-15 22:28:52 -04:00
|
|
|
|
2020-08-27 18:49:30 -04:00
|
|
|
// Sticky a post in its subreddit.
|
2020-07-15 22:28:52 -04:00
|
|
|
// When bottom is true, the post will be set as the bottom sticky (the 2nd one).
|
|
|
|
// If no top sticky exists, the post will become the top sticky regardless.
|
|
|
|
// When attempting to sticky a post that's already stickied, it will return a 409 Conflict error.
|
|
|
|
func (s *PostService) Sticky(ctx context.Context, id string, bottom bool) (*Response, error) {
|
|
|
|
path := "api/set_subreddit_sticky"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("state", "true")
|
|
|
|
if !bottom {
|
|
|
|
form.Set("num", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsticky unstickies a post in its subreddit.
|
|
|
|
func (s *PostService) Unsticky(ctx context.Context, id string) (*Response, error) {
|
|
|
|
path := "api/set_subreddit_sticky"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("state", "false")
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PinToProfile pins one of your posts to your profile.
|
|
|
|
// TODO: very inconsistent behaviour, not sure I'm ready to include this parameter yet.
|
|
|
|
// The pos parameter should be a number between 1-4 (inclusive), indicating the position at which
|
|
|
|
// the post should appear on your profile.
|
|
|
|
// Note: The position will be bumped upward if there's space. E.g. if you only have 1 pinned post,
|
|
|
|
// and you try to pin another post to position 3, it will be pinned at 2.
|
|
|
|
// When attempting to pin a post that's already pinned, it will return a 409 Conflict error.
|
|
|
|
func (s *PostService) PinToProfile(ctx context.Context, id string) (*Response, error) {
|
|
|
|
path := "api/set_subreddit_sticky"
|
|
|
|
|
|
|
|
// if pos < 1 {
|
|
|
|
// pos = 1
|
|
|
|
// }
|
|
|
|
// if pos > 4 {
|
|
|
|
// pos = 4
|
|
|
|
// }
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("state", "true")
|
|
|
|
form.Set("to_profile", "true")
|
|
|
|
// form.Set("num", fmt.Sprint(pos))
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpinFromProfile unpins one of your posts from your profile.
|
|
|
|
func (s *PostService) UnpinFromProfile(ctx context.Context, id string) (*Response, error) {
|
|
|
|
path := "api/set_subreddit_sticky"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("state", "false")
|
|
|
|
form.Set("to_profile", "true")
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-07-16 18:33:43 -04:00
|
|
|
|
|
|
|
// setSuggestedSort sets the suggested comment sort for the post.
|
|
|
|
// sort must be one of: confidence (i.e. best), top, new, controversial, old, random, qa, live
|
|
|
|
func (s *PostService) setSuggestedSort(ctx context.Context, id string, sort string) (*Response, error) {
|
|
|
|
path := "api/set_suggested_sort"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("sort", sort)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortBest sets the suggested comment sort for the post to best.
|
|
|
|
func (s *PostService) SetSuggestedSortBest(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "confidence")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortTop sets the suggested comment sort for the post to top.
|
|
|
|
func (s *PostService) SetSuggestedSortTop(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "top")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortNew sets the suggested comment sort for the post to new.
|
|
|
|
func (s *PostService) SetSuggestedSortNew(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "new")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortControversial sets the suggested comment sort for the post to controversial.
|
|
|
|
func (s *PostService) SetSuggestedSortControversial(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "controversial")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortOld sorts the comments on the posts randomly.
|
|
|
|
func (s *PostService) SetSuggestedSortOld(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "old")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortRandom sets the suggested comment sort for the post to random.
|
|
|
|
func (s *PostService) SetSuggestedSortRandom(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "random")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortAMA sets the suggested comment sort for the post to a Q&A styled fashion.
|
|
|
|
func (s *PostService) SetSuggestedSortAMA(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "qa")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSuggestedSortLive sets the suggested comment sort for the post to stream new comments as they're posted.
|
|
|
|
// As of now, this is still in beta, so it's not a fully developed feature yet. It just sets the sort as "new" for now.
|
|
|
|
func (s *PostService) SetSuggestedSortLive(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "live")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearSuggestedSort clears the suggested comment sort for the post.
|
|
|
|
func (s *PostService) ClearSuggestedSort(ctx context.Context, id string) (*Response, error) {
|
|
|
|
return s.setSuggestedSort(ctx, id, "")
|
|
|
|
}
|
2020-07-16 19:07:19 -04:00
|
|
|
|
|
|
|
// EnableContestMode enables contest mode for the post.
|
|
|
|
// Comments will be sorted randomly and regular users cannot see comment scores.
|
|
|
|
func (s *PostService) EnableContestMode(ctx context.Context, id string) (*Response, error) {
|
|
|
|
path := "api/set_contest_mode"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("state", "true")
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableContestMode disables contest mode for the post.
|
|
|
|
func (s *PostService) DisableContestMode(ctx context.Context, id string) (*Response, error) {
|
|
|
|
path := "api/set_contest_mode"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("id", id)
|
|
|
|
form.Set("state", "false")
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-07-17 17:04:28 -04:00
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
// LoadMoreComments retrieves more comments that were left out when initially fetching the post.
|
|
|
|
func (s *PostService) LoadMoreComments(ctx context.Context, pc *PostAndComments) (*Response, error) {
|
|
|
|
if pc == nil {
|
2020-08-03 00:00:29 -04:00
|
|
|
return nil, errors.New("pc: cannot be nil")
|
2020-07-19 22:03:37 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 00:04:47 -04:00
|
|
|
if !pc.HasMore() {
|
2020-07-19 22:03:37 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
postID := pc.Post.FullID
|
2020-08-18 23:12:35 -04:00
|
|
|
commentIDs := pc.More.Children
|
2020-07-19 22:03:37 -04:00
|
|
|
|
2020-08-14 00:04:47 -04:00
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("link_id", postID)
|
|
|
|
form.Set("children", strings.Join(commentIDs, ","))
|
2020-07-17 17:04:28 -04:00
|
|
|
|
|
|
|
path := "api/morechildren"
|
|
|
|
|
2020-08-14 00:04:47 -04:00
|
|
|
// This was originally a GET, but with POST you can send a bigger payload
|
|
|
|
// since it's in the body and not the URI.
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-07-17 17:04:28 -04:00
|
|
|
if err != nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
return nil, err
|
2020-07-17 17:04:28 -04:00
|
|
|
}
|
|
|
|
|
2020-08-11 21:06:34 -04:00
|
|
|
root := new(struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
JSON struct {
|
|
|
|
Data struct {
|
2020-08-11 21:06:34 -04:00
|
|
|
Things things `json:"things"`
|
2020-07-19 22:03:37 -04:00
|
|
|
} `json:"data"`
|
|
|
|
} `json:"json"`
|
2020-08-11 21:06:34 -04:00
|
|
|
})
|
2020-07-17 17:04:28 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
comments := root.JSON.Data.Things.Comments
|
|
|
|
for _, c := range comments {
|
2020-08-14 00:04:47 -04:00
|
|
|
pc.addCommentToTree(c)
|
2020-07-17 17:04:28 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 00:04:47 -04:00
|
|
|
noMore := true
|
2020-07-19 22:03:37 -04:00
|
|
|
|
2020-08-14 12:07:10 -04:00
|
|
|
mores := root.JSON.Data.Things.Mores
|
2020-08-14 00:04:47 -04:00
|
|
|
for _, m := range mores {
|
|
|
|
if strings.HasPrefix(m.ParentID, kindPost+"_") {
|
|
|
|
noMore = false
|
|
|
|
}
|
|
|
|
pc.addMoreToTree(m)
|
2020-07-19 22:03:37 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 00:04:47 -04:00
|
|
|
if noMore {
|
2020-08-18 23:12:35 -04:00
|
|
|
pc.More = nil
|
2020-07-19 22:03:37 -04:00
|
|
|
}
|
2020-08-14 00:04:47 -04:00
|
|
|
|
|
|
|
return resp, nil
|
2020-07-17 17:04:28 -04:00
|
|
|
}
|
2020-07-20 21:03:57 -04:00
|
|
|
|
2020-07-30 12:22:39 -04:00
|
|
|
func (s *PostService) random(ctx context.Context, subreddits ...string) (*PostAndComments, *Response, error) {
|
2020-07-20 21:03:57 -04:00
|
|
|
path := "random"
|
|
|
|
if len(subreddits) > 0 {
|
|
|
|
path = fmt.Sprintf("r/%s/random", strings.Join(subreddits, "+"))
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
2020-07-30 12:22:39 -04:00
|
|
|
return nil, nil, err
|
2020-07-20 21:03:57 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
root := new(PostAndComments)
|
2020-07-20 21:03:57 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
2020-07-30 12:22:39 -04:00
|
|
|
return nil, resp, err
|
2020-07-20 21:03:57 -04:00
|
|
|
}
|
|
|
|
|
2020-07-30 12:22:39 -04:00
|
|
|
return root, resp, nil
|
2020-07-20 21:03:57 -04:00
|
|
|
}
|
|
|
|
|
2020-07-20 22:47:59 -04:00
|
|
|
// RandomFromSubreddits returns a random post and its comments from the subreddits.
|
|
|
|
// If no subreddits are provided, Reddit runs the query against your subscriptions.
|
2020-07-30 12:22:39 -04:00
|
|
|
func (s *PostService) RandomFromSubreddits(ctx context.Context, subreddits ...string) (*PostAndComments, *Response, error) {
|
2020-07-20 22:47:59 -04:00
|
|
|
return s.random(ctx, subreddits...)
|
|
|
|
}
|
|
|
|
|
2020-07-20 21:03:57 -04:00
|
|
|
// Random returns a random post and its comments from all of Reddit.
|
2020-07-30 12:22:39 -04:00
|
|
|
func (s *PostService) Random(ctx context.Context) (*PostAndComments, *Response, error) {
|
2020-07-20 22:47:59 -04:00
|
|
|
return s.random(ctx, "all")
|
2020-07-20 21:03:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RandomFromSubscriptions returns a random post and its comments from your subscriptions.
|
2020-07-30 12:22:39 -04:00
|
|
|
func (s *PostService) RandomFromSubscriptions(ctx context.Context) (*PostAndComments, *Response, error) {
|
2020-07-20 22:47:59 -04:00
|
|
|
return s.random(ctx)
|
2020-07-20 21:03:57 -04:00
|
|
|
}
|
2020-08-13 17:23:39 -04:00
|
|
|
|
|
|
|
// MarkVisited marks the post(s) as visited.
|
|
|
|
// This method requires a subscription to Reddit premium.
|
|
|
|
func (s *PostService) MarkVisited(ctx context.Context, ids ...string) (*Response, error) {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, errors.New("must provide at least 1 id")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/store_visits"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("links", strings.Join(ids, ","))
|
|
|
|
|
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|