Get more replies to a comment via /api/morechildren

Also started using slices of pointers instead of slices of structs. This
is useful especially in the case of adding to the list of replies of a
comment. If the comment is a plain old struct (non-pointer), the change
wouldn't happen

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-19 22:03:37 -04:00
parent 0bfff72973
commit 5c376a1af4
8 changed files with 108 additions and 67 deletions

View File

@ -36,8 +36,6 @@ var expectedCommentSubmitOrEdit = &Comment{
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
PostID: "t3_link1", PostID: "t3_link1",
Replies: &Replies{},
} }
func TestCommentService_Submit(t *testing.T) { func TestCommentService_Submit(t *testing.T) {

View File

@ -15,7 +15,7 @@ import (
type ListingsService service type ListingsService service
// Get returns posts, comments, and subreddits from their IDs. // Get returns posts, comments, and subreddits from their IDs.
func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]Post, []Comment, []Subreddit, *Response, error) { func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]*Post, []*Comment, []*Subreddit, *Response, error) {
type query struct { type query struct {
IDs []string `url:"id,comma"` IDs []string `url:"id,comma"`
} }
@ -45,7 +45,7 @@ func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]Post, []Com
} }
// GetPosts returns posts from their full IDs. // GetPosts returns posts from their full IDs.
func (s *ListingsService) GetPosts(ctx context.Context, ids ...string) ([]Post, *Response, error) { func (s *ListingsService) GetPosts(ctx context.Context, ids ...string) ([]*Post, *Response, error) {
if len(ids) == 0 { if len(ids) == 0 {
return nil, nil, errors.New("must provide at least 1 id") return nil, nil, errors.New("must provide at least 1 id")
} }

View File

@ -11,7 +11,7 @@ import (
) )
var expectedModActionsResult = &ModActions{ var expectedModActionsResult = &ModActions{
ModActions: []ModAction{ ModActions: []*ModAction{
{ {
ID: "ModAction_b4e7979a-c4ad-11ea-8440-0ea1b7c2b8f9", ID: "ModAction_b4e7979a-c4ad-11ea-8440-0ea1b7c2b8f9",
Action: "spamcomment", Action: "spamcomment",

58
post.go
View File

@ -62,7 +62,7 @@ type SubmitLinkOptions struct {
// Get returns a post with its comments. // Get returns a post with its comments.
// id is the ID36 of the post, not its full id. // id is the ID36 of the post, not its full id.
// Example: instead of t3_abc123, use abc123. // Example: instead of t3_abc123, use abc123.
func (s *PostService) Get(ctx context.Context, id string) (*Post, []Comment, *Response, error) { func (s *PostService) Get(ctx context.Context, id string) (*Post, []*Comment, *Response, error) {
path := fmt.Sprintf("comments/%s", id) path := fmt.Sprintf("comments/%s", id)
req, err := s.client.NewRequest(http.MethodGet, path, nil) req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil { if err != nil {
@ -433,7 +433,22 @@ func (s *PostService) DisableContestMode(ctx context.Context, id string) (*Respo
// More retrieves more comments that were left out when initially fetching the post. // More retrieves more comments that were left out when initially fetching the post.
// id is the post's full ID. // id is the post's full ID.
// commentIDs are the ID36s of comments. // commentIDs are the ID36s of comments.
func (s *PostService) More(ctx context.Context, id string, commentIDs ...string) (interface{}, *Response, error) { func (s *PostService) More(ctx context.Context, comment *Comment) (*Response, error) {
if comment == nil {
return nil, errors.New("comment: cannot be nil")
}
if comment.Replies.MoreComments == nil {
return nil, nil
}
postID := comment.PostID
commentIDs := comment.Replies.MoreComments.Children
if len(commentIDs) == 0 {
return nil, nil
}
type query struct { type query struct {
PostID string `url:"link_id"` PostID string `url:"link_id"`
IDs []string `url:"children,comma"` IDs []string `url:"children,comma"`
@ -441,21 +456,48 @@ func (s *PostService) More(ctx context.Context, id string, commentIDs ...string)
} }
path := "api/morechildren" path := "api/morechildren"
path, err := addOptions(path, query{id, commentIDs, "json"}) path, err := addOptions(path, query{postID, commentIDs, "json"})
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
req, err := s.client.NewRequest(http.MethodGet, path, nil) req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
root := new(interface{}) type rootResponse struct {
JSON struct {
Data struct {
Things Things `json:"things"`
} `json:"data"`
} `json:"json"`
}
root := new(rootResponse)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, resp, err return resp, err
} }
return root, resp, err comments := root.JSON.Data.Things.Comments
for _, c := range comments {
addCommentToReplies(comment, c)
}
comment.Replies.MoreComments = nil
return resp, nil
}
// addCommentToReplies traverses the comment tree to find the one
// that the 2nd comment is replying to. It then adds it to its replies.
func addCommentToReplies(parent *Comment, comment *Comment) {
if parent.FullID == comment.ParentID {
parent.Replies.Comments = append(parent.Replies.Comments, comment)
return
}
for _, reply := range parent.Replies.Comments {
addCommentToReplies(reply, comment)
}
} }

View File

@ -116,12 +116,12 @@ func (s *SubredditService) GetModerated(ctx context.Context, opts *ListOptions)
} }
// GetSticky1 returns the first stickied post on a subreddit (if it exists). // GetSticky1 returns the first stickied post on a subreddit (if it exists).
func (s *SubredditService) GetSticky1(ctx context.Context, name string) (*Post, []Comment, *Response, error) { func (s *SubredditService) GetSticky1(ctx context.Context, name string) (*Post, []*Comment, *Response, error) {
return s.getSticky(ctx, name, 1) return s.getSticky(ctx, name, 1)
} }
// GetSticky2 returns the second stickied post on a subreddit (if it exists). // GetSticky2 returns the second stickied post on a subreddit (if it exists).
func (s *SubredditService) GetSticky2(ctx context.Context, name string) (*Post, []Comment, *Response, error) { func (s *SubredditService) GetSticky2(ctx context.Context, name string) (*Post, []*Comment, *Response, error) {
return s.getSticky(ctx, name, 2) return s.getSticky(ctx, name, 2)
} }
@ -240,7 +240,7 @@ func (s *SubredditService) getSubreddits(ctx context.Context, path string, opts
// getSticky returns one of the 2 stickied posts of the subreddit (if they exist). // getSticky returns one of the 2 stickied posts of the subreddit (if they exist).
// Num should be equal to 1 or 2, depending on which one you want. // Num should be equal to 1 or 2, depending on which one you want.
func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num int) (*Post, []Comment, *Response, error) { func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num int) (*Post, []*Comment, *Response, error) {
type query struct { type query struct {
Num int `url:"num"` Num int `url:"num"`
} }

View File

@ -30,7 +30,7 @@ var expectedSubreddit = &Subreddit{
var expectedSubreddits = &Subreddits{ var expectedSubreddits = &Subreddits{
After: "t5_2qh0u", After: "t5_2qh0u",
Before: "", Before: "",
Subreddits: []Subreddit{ Subreddits: []*Subreddit{
{ {
ID: "2qs0k", ID: "2qs0k",
FullID: "t5_2qs0k", FullID: "t5_2qs0k",

View File

@ -120,34 +120,34 @@ type Listing struct {
// Things are objects/entities coming from the Reddit API. // Things are objects/entities coming from the Reddit API.
type Things struct { type Things struct {
Comments []Comment Comments []*Comment
MoreComments []More MoreComments []*More
Users []User Users []*User
Posts []Post Posts []*Post
Subreddits []Subreddit Subreddits []*Subreddit
ModActions []ModAction ModActions []*ModAction
// todo: add the other kinds of things // todo: add the other kinds of things
} }
func (t *Things) init() { func (t *Things) init() {
if t.Comments == nil { if t.Comments == nil {
t.Comments = make([]Comment, 0) t.Comments = make([]*Comment, 0)
} }
if t.MoreComments == nil { if t.MoreComments == nil {
t.MoreComments = make([]More, 0) t.MoreComments = make([]*More, 0)
} }
if t.Users == nil { if t.Users == nil {
t.Users = make([]User, 0) t.Users = make([]*User, 0)
} }
if t.Posts == nil { if t.Posts == nil {
t.Posts = make([]Post, 0) t.Posts = make([]*Post, 0)
} }
if t.Subreddits == nil { if t.Subreddits == nil {
t.Subreddits = make([]Subreddit, 0) t.Subreddits = make([]*Subreddit, 0)
} }
if t.ModActions == nil { if t.ModActions == nil {
t.ModActions = make([]ModAction, 0) t.ModActions = make([]*ModAction, 0)
} }
} }
@ -166,35 +166,35 @@ func (t *Things) UnmarshalJSON(b []byte) error {
switch child["kind"] { switch child["kind"] {
case kindComment: case kindComment:
var v Comment v := new(Comment)
if err := json.Unmarshal(byteValue, &v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {
t.Comments = append(t.Comments, v) t.Comments = append(t.Comments, v)
} }
case kindMore: case kindMore:
var v More v := new(More)
if err := json.Unmarshal(byteValue, &v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {
t.MoreComments = append(t.MoreComments, v) t.MoreComments = append(t.MoreComments, v)
} }
case kindAccount: case kindAccount:
var v User v := new(User)
if err := json.Unmarshal(byteValue, &v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {
t.Users = append(t.Users, v) t.Users = append(t.Users, v)
} }
case kindLink: case kindLink:
var v Post v := new(Post)
if err := json.Unmarshal(byteValue, &v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {
t.Posts = append(t.Posts, v) t.Posts = append(t.Posts, v)
} }
case kindMessage: case kindMessage:
case kindSubreddit: case kindSubreddit:
var v Subreddit v := new(Subreddit)
if err := json.Unmarshal(byteValue, &v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {
t.Subreddits = append(t.Subreddits, v) t.Subreddits = append(t.Subreddits, v)
} }
case kindAward: case kindAward:
case kindModAction: case kindModAction:
var v ModAction v := new(ModAction)
if err := json.Unmarshal(byteValue, &v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {
t.ModActions = append(t.ModActions, v) t.ModActions = append(t.ModActions, v)
} }
} }
@ -238,7 +238,8 @@ type Comment struct {
PostPermalink string `json:"link_permalink,omitempty"` PostPermalink string `json:"link_permalink,omitempty"`
// This doesn't appear when submitting a comment. // This doesn't appear when submitting a comment.
PostAuthor string `json:"link_author,omitempty"` PostAuthor string `json:"link_author,omitempty"`
// This doesn't appear when submitting a comment. // This doesn't appear when submitting a comment
// or when getting a post with its comments.
PostNumComments int `json:"num_comments"` PostNumComments int `json:"num_comments"`
IsSubmitter bool `json:"is_submitter"` IsSubmitter bool `json:"is_submitter"`
@ -249,15 +250,15 @@ type Comment struct {
CanGild bool `json:"can_gild"` CanGild bool `json:"can_gild"`
NSFW bool `json:"over_18"` NSFW bool `json:"over_18"`
Replies *Replies `json:"replies"` Replies Replies `json:"replies"`
} }
// Replies holds replies to a comment. // Replies holds replies to a comment.
// It contains both comments and "more" comments, which are entrypoints to other // It contains both comments and "more" comments, which are entrypoints to other
// comments that were left out. // comments that were left out.
type Replies struct { type Replies struct {
Comments []Comment `json:"comments,omitempty"` Comments []*Comment `json:"comments,omitempty"`
MoreComments []More `json:"more,omitempty"` MoreComments *More `json:"more,omitempty"`
} }
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
@ -276,7 +277,9 @@ func (r *Replies) UnmarshalJSON(data []byte) error {
if root.Data != nil { if root.Data != nil {
r.Comments = root.Data.Things.Comments r.Comments = root.Data.Things.Comments
r.MoreComments = root.Data.Things.MoreComments if len(root.Data.Things.MoreComments) > 0 {
r.MoreComments = root.Data.Things.MoreComments[0]
}
} }
return nil return nil
@ -431,43 +434,43 @@ func (rl *rootListing) getModeratorActions() *ModActions {
// Comments is a list of comments // Comments is a list of comments
type Comments struct { type Comments struct {
Comments []Comment `json:"comments"` Comments []*Comment `json:"comments"`
After string `json:"after"` After string `json:"after"`
Before string `json:"before"` Before string `json:"before"`
} }
// Users is a list of users // Users is a list of users
type Users struct { type Users struct {
Users []User `json:"users"` Users []*User `json:"users"`
After string `json:"after"` After string `json:"after"`
Before string `json:"before"` Before string `json:"before"`
} }
// Subreddits is a list of subreddits // Subreddits is a list of subreddits
type Subreddits struct { type Subreddits struct {
Subreddits []Subreddit `json:"subreddits"` Subreddits []*Subreddit `json:"subreddits"`
After string `json:"after"` After string `json:"after"`
Before string `json:"before"` Before string `json:"before"`
} }
// Posts is a list of posts. // Posts is a list of posts.
type Posts struct { type Posts struct {
Posts []Post `json:"posts"` Posts []*Post `json:"posts"`
After string `json:"after"` After string `json:"after"`
Before string `json:"before"` Before string `json:"before"`
} }
// ModActions is a list of moderator action. // ModActions is a list of moderator action.
type ModActions struct { type ModActions struct {
ModActions []ModAction `json:"moderator_actions"` ModActions []*ModAction `json:"moderator_actions"`
After string `json:"after"` After string `json:"after"`
Before string `json:"before"` Before string `json:"before"`
} }
// postAndComments is a post and its comments // postAndComments is a post and its comments
type postAndComments struct { type postAndComments struct {
Post *Post Post *Post
Comments []Comment Comments []*Comment
} }
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
@ -489,7 +492,7 @@ func (pc *postAndComments) UnmarshalJSON(data []byte) error {
post := l[0].getPosts().Posts[0] post := l[0].getPosts().Posts[0]
comments := l[1].getComments().Comments comments := l[1].getComments().Comments
pc.Post = &post pc.Post = post
pc.Comments = comments pc.Comments = comments
return nil return nil

View File

@ -46,7 +46,7 @@ var expectedUsers = map[string]*UserShort{
}, },
} }
var expectedPost = Post{ var expectedPost = &Post{
ID: "gczwql", ID: "gczwql",
FullID: "t3_gczwql", FullID: "t3_gczwql",
Created: &Timestamp{time.Date(2020, 5, 3, 22, 46, 25, 0, time.UTC)}, Created: &Timestamp{time.Date(2020, 5, 3, 22, 46, 25, 0, time.UTC)},
@ -74,7 +74,7 @@ var expectedPost = Post{
IsSelfPost: true, IsSelfPost: true,
} }
var expectedComment = Comment{ var expectedComment = &Comment{
ID: "f0zsa37", ID: "f0zsa37",
FullID: "t1_f0zsa37", FullID: "t1_f0zsa37",
Created: &Timestamp{time.Date(2019, 9, 21, 21, 38, 16, 0, time.UTC)}, Created: &Timestamp{time.Date(2019, 9, 21, 21, 38, 16, 0, time.UTC)},
@ -101,8 +101,6 @@ var expectedComment = Comment{
PostPermalink: "https://www.reddit.com/r/apple/comments/d7ejpn/im_giving_away_an_iphone_11_pro_to_a_commenter_at/", PostPermalink: "https://www.reddit.com/r/apple/comments/d7ejpn/im_giving_away_an_iphone_11_pro_to_a_commenter_at/",
PostAuthor: "iamthatis", PostAuthor: "iamthatis",
PostNumComments: 89751, PostNumComments: 89751,
Replies: &Replies{},
} }
var expectedRelationship = &Relationship{ var expectedRelationship = &Relationship{