2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-05-03 19:17:39 -04:00
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-06-29 21:38:47 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-05-16 11:38:47 -04:00
|
|
|
)
|
2020-05-03 19:17:39 -04:00
|
|
|
|
|
|
|
const (
|
2020-06-18 21:41:17 -04:00
|
|
|
kindComment = "t1"
|
|
|
|
kindAccount = "t2"
|
2020-06-22 21:52:34 -04:00
|
|
|
kindLink = "t3" // a link is a post
|
2020-06-18 21:41:17 -04:00
|
|
|
kindMessage = "t4"
|
|
|
|
kindSubreddit = "t5"
|
|
|
|
kindAward = "t6"
|
|
|
|
kindListing = "Listing"
|
2020-06-27 23:15:27 -04:00
|
|
|
kindKarmaList = "KarmaList"
|
|
|
|
kindTrophyList = "TrophyList"
|
2020-06-18 21:41:17 -04:00
|
|
|
kindUserList = "UserList"
|
2020-07-12 22:53:19 -04:00
|
|
|
kindMore = "more"
|
|
|
|
kindModAction = "modaction"
|
2020-05-03 19:17:39 -04:00
|
|
|
)
|
|
|
|
|
2020-06-27 13:06:38 -04:00
|
|
|
// Permalink is the link to a post or comment.
|
|
|
|
type Permalink string
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
|
|
|
func (p *Permalink) UnmarshalJSON(data []byte) error {
|
|
|
|
var v string
|
|
|
|
err := json.Unmarshal(data, &v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-30 13:01:18 -04:00
|
|
|
v = "https://www.reddit.com" + v
|
|
|
|
*p = Permalink(v)
|
2020-06-27 13:06:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 00:04:24 -04:00
|
|
|
// todo: rename this to thing
|
2020-05-03 19:17:39 -04:00
|
|
|
type root struct {
|
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rootListing struct {
|
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data *Listing `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listing holds things coming from the Reddit API
|
|
|
|
// It also contains the after/before anchors useful for subsequent requests
|
|
|
|
type Listing struct {
|
|
|
|
Things Things `json:"children"`
|
|
|
|
After string `json:"after"`
|
|
|
|
Before string `json:"before"`
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Things are objects/entities coming from the Reddit API.
|
2020-05-03 19:17:39 -04:00
|
|
|
type Things struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
Comments []*Comment
|
2020-07-29 14:11:06 -04:00
|
|
|
MoreComments *More
|
2020-07-17 17:04:28 -04:00
|
|
|
|
2020-07-19 22:03:37 -04:00
|
|
|
Users []*User
|
|
|
|
Posts []*Post
|
|
|
|
Subreddits []*Subreddit
|
|
|
|
ModActions []*ModAction
|
2020-05-03 19:17:39 -04:00
|
|
|
// todo: add the other kinds of things
|
|
|
|
}
|
|
|
|
|
2020-06-27 13:06:38 -04:00
|
|
|
func (t *Things) init() {
|
|
|
|
if t.Comments == nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
t.Comments = make([]*Comment, 0)
|
2020-06-27 13:06:38 -04:00
|
|
|
}
|
|
|
|
if t.Users == nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
t.Users = make([]*User, 0)
|
2020-06-27 13:06:38 -04:00
|
|
|
}
|
|
|
|
if t.Posts == nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
t.Posts = make([]*Post, 0)
|
2020-06-27 13:06:38 -04:00
|
|
|
}
|
|
|
|
if t.Subreddits == nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
t.Subreddits = make([]*Subreddit, 0)
|
2020-06-27 13:06:38 -04:00
|
|
|
}
|
2020-07-12 22:53:19 -04:00
|
|
|
if t.ModActions == nil {
|
2020-07-19 22:03:37 -04:00
|
|
|
t.ModActions = make([]*ModAction, 0)
|
2020-07-12 22:53:19 -04:00
|
|
|
}
|
2020-06-27 13:06:38 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-06-22 21:52:34 -04:00
|
|
|
func (t *Things) UnmarshalJSON(b []byte) error {
|
2020-06-27 13:06:38 -04:00
|
|
|
t.init()
|
|
|
|
|
2020-07-30 13:01:18 -04:00
|
|
|
type thing struct {
|
2020-07-31 08:24:28 -04:00
|
|
|
Kind string `json:"kind"`
|
|
|
|
Data json.RawMessage `json:"data"`
|
2020-07-30 13:01:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var things []thing
|
|
|
|
if err := json.Unmarshal(b, &things); err != nil {
|
2020-05-03 19:17:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:01:18 -04:00
|
|
|
for _, thing := range things {
|
|
|
|
switch thing.Kind {
|
2020-05-03 19:17:39 -04:00
|
|
|
case kindComment:
|
2020-07-19 22:03:37 -04:00
|
|
|
v := new(Comment)
|
2020-07-31 08:24:28 -04:00
|
|
|
if err := json.Unmarshal(thing.Data, v); err == nil {
|
2020-07-17 17:04:28 -04:00
|
|
|
t.Comments = append(t.Comments, v)
|
|
|
|
}
|
|
|
|
case kindMore:
|
2020-07-19 22:03:37 -04:00
|
|
|
v := new(More)
|
2020-07-31 08:24:28 -04:00
|
|
|
if err := json.Unmarshal(thing.Data, v); err == nil {
|
2020-07-29 14:11:06 -04:00
|
|
|
t.MoreComments = v
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
case kindAccount:
|
2020-07-19 22:03:37 -04:00
|
|
|
v := new(User)
|
2020-07-31 08:24:28 -04:00
|
|
|
if err := json.Unmarshal(thing.Data, v); err == nil {
|
2020-07-17 17:04:28 -04:00
|
|
|
t.Users = append(t.Users, v)
|
2020-05-04 20:51:16 -04:00
|
|
|
}
|
2020-05-03 19:17:39 -04:00
|
|
|
case kindLink:
|
2020-07-19 22:03:37 -04:00
|
|
|
v := new(Post)
|
2020-07-31 08:24:28 -04:00
|
|
|
if err := json.Unmarshal(thing.Data, v); err == nil {
|
2020-07-17 17:04:28 -04:00
|
|
|
t.Posts = append(t.Posts, v)
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
case kindMessage:
|
|
|
|
case kindSubreddit:
|
2020-07-19 22:03:37 -04:00
|
|
|
v := new(Subreddit)
|
2020-07-31 08:24:28 -04:00
|
|
|
if err := json.Unmarshal(thing.Data, v); err == nil {
|
2020-07-17 17:04:28 -04:00
|
|
|
t.Subreddits = append(t.Subreddits, v)
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
case kindAward:
|
2020-07-12 22:53:19 -04:00
|
|
|
case kindModAction:
|
2020-07-19 22:03:37 -04:00
|
|
|
v := new(ModAction)
|
2020-07-31 08:24:28 -04:00
|
|
|
if err := json.Unmarshal(thing.Data, v); err == nil {
|
2020-07-17 17:04:28 -04:00
|
|
|
t.ModActions = append(t.ModActions, v)
|
2020-07-12 22:53:19 -04:00
|
|
|
}
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment is a comment posted by a user
|
|
|
|
type Comment struct {
|
2020-06-17 22:35:19 -04:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
FullID string `json:"name,omitempty"`
|
2020-05-18 23:11:47 -04:00
|
|
|
Created *Timestamp `json:"created_utc,omitempty"`
|
|
|
|
Edited *Timestamp `json:"edited,omitempty"`
|
|
|
|
|
2020-06-27 13:06:38 -04:00
|
|
|
ParentID string `json:"parent_id,omitempty"`
|
|
|
|
Permalink Permalink `json:"permalink,omitempty"`
|
2020-06-17 22:35:19 -04:00
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
Body string `json:"body,omitempty"`
|
|
|
|
Author string `json:"author,omitempty"`
|
|
|
|
AuthorID string `json:"author_fullname,omitempty"`
|
|
|
|
AuthorFlairText string `json:"author_flair_text,omitempty"`
|
|
|
|
AuthorFlairID string `json:"author_flair_template_id,omitempty"`
|
|
|
|
|
|
|
|
Subreddit string `json:"subreddit,omitempty"`
|
|
|
|
SubredditNamePrefixed string `json:"subreddit_name_prefixed,omitempty"`
|
|
|
|
SubredditID string `json:"subreddit_id,omitempty"`
|
|
|
|
|
2020-08-02 15:33:06 -04:00
|
|
|
// Indicates if you've upvote/downvoted (true/false).
|
|
|
|
// If neither, it will be nil.
|
2020-05-03 20:32:08 -04:00
|
|
|
Likes *bool `json:"likes"`
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
Score int `json:"score"`
|
|
|
|
Controversiality int `json:"controversiality"`
|
|
|
|
|
2020-08-03 00:00:29 -04:00
|
|
|
// todo: check the validity of these comments
|
2020-06-22 21:52:34 -04:00
|
|
|
PostID string `json:"link_id,omitempty"`
|
2020-06-29 21:38:47 -04:00
|
|
|
// This doesn't appear when submitting a comment.
|
|
|
|
PostTitle string `json:"link_title,omitempty"`
|
|
|
|
// This doesn't appear when submitting a comment.
|
|
|
|
PostPermalink string `json:"link_permalink,omitempty"`
|
|
|
|
// This doesn't appear when submitting a comment.
|
|
|
|
PostAuthor string `json:"link_author,omitempty"`
|
2020-07-19 22:03:37 -04:00
|
|
|
// This doesn't appear when submitting a comment
|
|
|
|
// or when getting a post with its comments.
|
2020-08-02 15:33:06 -04:00
|
|
|
PostNumComments *int `json:"num_comments,omitempty"`
|
2020-05-03 19:17:39 -04:00
|
|
|
|
|
|
|
IsSubmitter bool `json:"is_submitter"`
|
|
|
|
ScoreHidden bool `json:"score_hidden"`
|
|
|
|
Saved bool `json:"saved"`
|
|
|
|
Stickied bool `json:"stickied"`
|
|
|
|
Locked bool `json:"locked"`
|
|
|
|
CanGild bool `json:"can_gild"`
|
|
|
|
NSFW bool `json:"over_18"`
|
|
|
|
|
2020-07-19 22:03:37 -04:00
|
|
|
Replies Replies `json:"replies"`
|
2020-05-16 11:38:47 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
func (c *Comment) hasMore() bool {
|
|
|
|
return c.Replies.MoreComments != nil && len(c.Replies.MoreComments.Children) > 0
|
|
|
|
}
|
|
|
|
|
2020-07-17 17:04:28 -04:00
|
|
|
// Replies holds replies to a comment.
|
|
|
|
// It contains both comments and "more" comments, which are entrypoints to other
|
|
|
|
// comments that were left out.
|
|
|
|
type Replies struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
Comments []*Comment `json:"comments,omitempty"`
|
|
|
|
MoreComments *More `json:"more,omitempty"`
|
2020-07-17 17:04:28 -04:00
|
|
|
}
|
2020-05-16 11:38:47 -04:00
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
|
|
|
func (r *Replies) UnmarshalJSON(data []byte) error {
|
|
|
|
// if a comment has no replies, its "replies" field is set to ""
|
|
|
|
if string(data) == `""` {
|
2020-07-17 17:04:28 -04:00
|
|
|
r = nil
|
2020-05-16 11:38:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(rootListing)
|
|
|
|
err := json.Unmarshal(data, root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 17:04:28 -04:00
|
|
|
if root.Data != nil {
|
|
|
|
r.Comments = root.Data.Things.Comments
|
2020-07-29 14:11:06 -04:00
|
|
|
r.MoreComments = root.Data.Things.MoreComments
|
2020-07-17 17:04:28 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 11:38:47 -04:00
|
|
|
return nil
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-20 21:03:57 -04:00
|
|
|
// todo: should we implement json.Marshaler?
|
2020-07-17 17:04:28 -04:00
|
|
|
|
|
|
|
// More holds information
|
|
|
|
type More struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
FullID string `json:"name"`
|
|
|
|
ParentID string `json:"parent_id"`
|
|
|
|
// Total number of replies to the parent + replies to those replies (recursively).
|
|
|
|
Count int `json:"count"`
|
|
|
|
// Number of comment nodes from the parent down to the furthest comment node.
|
|
|
|
Depth int `json:"depth"`
|
|
|
|
Children []string `json:"children"`
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Post is a submitted post on Reddit.
|
|
|
|
type Post struct {
|
2020-05-03 19:17:39 -04:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
FullID string `json:"name,omitempty"`
|
|
|
|
Created *Timestamp `json:"created_utc,omitempty"`
|
|
|
|
Edited *Timestamp `json:"edited,omitempty"`
|
|
|
|
|
2020-06-27 13:06:38 -04:00
|
|
|
Permalink Permalink `json:"permalink,omitempty"`
|
|
|
|
URL string `json:"url,omitempty"`
|
2020-05-03 19:17:39 -04:00
|
|
|
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Body string `json:"selftext,omitempty"`
|
|
|
|
|
2020-08-02 15:33:06 -04:00
|
|
|
// Indicates if you've upvote/downvoted (true/false).
|
|
|
|
// If neither, it will be nil.
|
2020-05-03 20:32:08 -04:00
|
|
|
Likes *bool `json:"likes"`
|
|
|
|
|
2020-05-04 20:51:16 -04:00
|
|
|
Score int `json:"score"`
|
|
|
|
UpvoteRatio float32 `json:"upvote_ratio"`
|
|
|
|
NumberOfComments int `json:"num_comments"`
|
2020-05-03 19:17:39 -04:00
|
|
|
|
|
|
|
SubredditID string `json:"subreddit_id,omitempty"`
|
|
|
|
SubredditName string `json:"subreddit,omitempty"`
|
|
|
|
SubredditNamePrefixed string `json:"subreddit_name_prefixed,omitempty"`
|
|
|
|
|
|
|
|
AuthorID string `json:"author_fullname,omitempty"`
|
|
|
|
AuthorName string `json:"author,omitempty"`
|
|
|
|
|
|
|
|
Spoiler bool `json:"spoiler"`
|
|
|
|
Locked bool `json:"locked"`
|
|
|
|
NSFW bool `json:"over_18"`
|
|
|
|
IsSelfPost bool `json:"is_self"`
|
|
|
|
Saved bool `json:"saved"`
|
|
|
|
Stickied bool `json:"stickied"`
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:38:47 -04:00
|
|
|
func (p Post) String() string {
|
|
|
|
chunks := []string{
|
|
|
|
fmt.Sprintf("[%d]", p.Score),
|
|
|
|
p.SubredditNamePrefixed,
|
|
|
|
"-",
|
|
|
|
p.Title,
|
|
|
|
"-",
|
|
|
|
string(p.Permalink),
|
|
|
|
}
|
|
|
|
return strings.Join(chunks, " ")
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
// Subreddit holds information about a subreddit
|
|
|
|
type Subreddit struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
FullID string `json:"name,omitempty"`
|
|
|
|
Created *Timestamp `json:"created_utc,omitempty"`
|
|
|
|
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
Name string `json:"display_name,omitempty"`
|
|
|
|
NamePrefixed string `json:"display_name_prefixed,omitempty"`
|
|
|
|
Title string `json:"title,omitempty"`
|
2020-06-20 23:04:01 -04:00
|
|
|
Description string `json:"public_description,omitempty"`
|
2020-05-03 19:17:39 -04:00
|
|
|
Type string `json:"subreddit_type,omitempty"`
|
|
|
|
SuggestedCommentSort string `json:"suggested_comment_sort,omitempty"`
|
|
|
|
|
|
|
|
Subscribers int `json:"subscribers"`
|
|
|
|
ActiveUserCount *int `json:"active_user_count,omitempty"`
|
|
|
|
NSFW bool `json:"over18"`
|
|
|
|
UserIsMod bool `json:"user_is_moderator"`
|
2020-07-06 22:10:47 -04:00
|
|
|
Favorite bool `json:"user_has_favorited"`
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rootListing) getAfter() string {
|
|
|
|
if rl == nil || rl.Data == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return rl.Data.After
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rootListing) getBefore() string {
|
|
|
|
if rl == nil || rl.Data == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return rl.Data.Before
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rootListing) getComments() *Comments {
|
2020-05-03 20:24:22 -04:00
|
|
|
v := new(Comments)
|
|
|
|
if rl != nil && rl.Data != nil {
|
|
|
|
v.Comments = rl.Data.Things.Comments
|
|
|
|
v.After = rl.Data.After
|
|
|
|
v.Before = rl.Data.Before
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
2020-05-03 20:24:22 -04:00
|
|
|
return v
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
func (rl *rootListing) getMoreComments() *More {
|
|
|
|
if rl == nil || rl.Data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return rl.Data.Things.MoreComments
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:51:16 -04:00
|
|
|
func (rl *rootListing) getUsers() *Users {
|
|
|
|
v := new(Users)
|
|
|
|
if rl != nil && rl.Data != nil {
|
|
|
|
v.Users = rl.Data.Things.Users
|
|
|
|
v.After = rl.Data.After
|
|
|
|
v.Before = rl.Data.Before
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
func (rl *rootListing) getPosts() *Posts {
|
|
|
|
v := new(Posts)
|
2020-05-03 20:24:22 -04:00
|
|
|
if rl != nil && rl.Data != nil {
|
2020-06-22 21:52:34 -04:00
|
|
|
v.Posts = rl.Data.Things.Posts
|
2020-05-03 20:24:22 -04:00
|
|
|
v.After = rl.Data.After
|
|
|
|
v.Before = rl.Data.Before
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
2020-05-03 20:24:22 -04:00
|
|
|
return v
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rootListing) getSubreddits() *Subreddits {
|
2020-05-03 20:24:22 -04:00
|
|
|
v := new(Subreddits)
|
|
|
|
if rl != nil && rl.Data != nil {
|
|
|
|
v.Subreddits = rl.Data.Things.Subreddits
|
|
|
|
v.After = rl.Data.After
|
|
|
|
v.Before = rl.Data.Before
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
2020-05-03 20:24:22 -04:00
|
|
|
return v
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-12 22:53:19 -04:00
|
|
|
func (rl *rootListing) getModeratorActions() *ModActions {
|
|
|
|
v := new(ModActions)
|
|
|
|
if rl != nil && rl.Data != nil {
|
|
|
|
v.ModActions = rl.Data.Things.ModActions
|
|
|
|
v.After = rl.Data.After
|
|
|
|
v.Before = rl.Data.Before
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
// Comments is a list of comments
|
|
|
|
type Comments struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
Comments []*Comment `json:"comments"`
|
|
|
|
After string `json:"after"`
|
|
|
|
Before string `json:"before"`
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:51:16 -04:00
|
|
|
// Users is a list of users
|
|
|
|
type Users struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
Users []*User `json:"users"`
|
|
|
|
After string `json:"after"`
|
|
|
|
Before string `json:"before"`
|
2020-05-04 20:51:16 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 19:17:39 -04:00
|
|
|
// Subreddits is a list of subreddits
|
|
|
|
type Subreddits struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
Subreddits []*Subreddit `json:"subreddits"`
|
|
|
|
After string `json:"after"`
|
|
|
|
Before string `json:"before"`
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// Posts is a list of posts.
|
|
|
|
type Posts struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
Posts []*Post `json:"posts"`
|
|
|
|
After string `json:"after"`
|
|
|
|
Before string `json:"before"`
|
2020-05-03 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
2020-07-12 22:53:19 -04:00
|
|
|
// ModActions is a list of moderator action.
|
|
|
|
type ModActions struct {
|
2020-07-19 22:03:37 -04:00
|
|
|
ModActions []*ModAction `json:"moderator_actions"`
|
|
|
|
After string `json:"after"`
|
|
|
|
Before string `json:"before"`
|
2020-07-12 22:53:19 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:11:06 -04:00
|
|
|
// PostAndComments is a post and its comments.
|
|
|
|
type PostAndComments struct {
|
|
|
|
Post *Post `json:"post"`
|
|
|
|
Comments []*Comment `json:"comments"`
|
|
|
|
moreComments *More
|
2020-05-16 11:38:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
|
|
|
// When getting a sticky post, you get an array of 2 Listings
|
|
|
|
// The 1st one contains the single post in its children array
|
|
|
|
// The 2nd one contains the comments to the post
|
2020-07-29 14:11:06 -04:00
|
|
|
func (pc *PostAndComments) UnmarshalJSON(data []byte) error {
|
2020-05-16 11:38:47 -04:00
|
|
|
var l []rootListing
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, &l)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) < 2 {
|
2020-06-22 21:52:34 -04:00
|
|
|
return errors.New("unexpected json response when getting post")
|
2020-05-16 11:38:47 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
post := l[0].getPosts().Posts[0]
|
|
|
|
comments := l[1].getComments().Comments
|
2020-07-29 14:11:06 -04:00
|
|
|
moreComments := l[1].getMoreComments()
|
2020-05-16 11:38:47 -04:00
|
|
|
|
2020-07-19 22:03:37 -04:00
|
|
|
pc.Post = post
|
2020-06-22 21:52:34 -04:00
|
|
|
pc.Comments = comments
|
2020-07-29 14:11:06 -04:00
|
|
|
pc.moreComments = moreComments
|
2020-05-16 11:38:47 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-29 14:11:06 -04:00
|
|
|
|
|
|
|
func (pc *PostAndComments) hasMore() bool {
|
|
|
|
return pc.moreComments != nil && len(pc.moreComments.Children) > 0
|
|
|
|
}
|