WIP: load more comments for a post

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-29 14:11:06 -04:00
parent 8541c81438
commit 112f7f0370
4 changed files with 115 additions and 41 deletions

View file

@ -2,6 +2,7 @@ package reddit
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
"net/url" "net/url"
) )
@ -63,3 +64,69 @@ func (s *CommentService) Edit(ctx context.Context, id string, text string) (*Com
return root, resp, nil return root, resp, nil
} }
// LoadMoreReplies retrieves more replies that were left out when initially fetching the comment.
func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment) (*Response, error) {
if comment == nil {
return nil, errors.New("comment: must not be nil")
}
if !comment.hasMore() {
return nil, nil
}
postID := comment.PostID
commentIDs := comment.Replies.MoreComments.Children
type query struct {
PostID string `url:"link_id"`
IDs []string `url:"children,comma"`
APIType string `url:"api_type"`
}
path := "api/morechildren"
path, err := addOptions(path, query{postID, commentIDs, "json"})
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
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)
if err != nil {
return 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)
}
}

46
post.go
View file

@ -65,20 +65,20 @@ 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) (*PostAndComments, *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 {
return nil, nil, nil, err return nil, nil, err
} }
root := new(postAndComments) root := new(PostAndComments)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, nil, resp, err return nil, resp, err
} }
return root.Post, root.Comments, resp, nil return root, resp, nil
} }
func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *Response, error) { func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *Response, error) {
@ -433,24 +433,18 @@ func (s *PostService) DisableContestMode(ctx context.Context, id string) (*Respo
return s.client.Do(ctx, req, nil) return s.client.Do(ctx, req, nil)
} }
// More retrieves more comments that were left out when initially fetching the post. // LoadMoreComments retrieves more comments that were left out when initially fetching the post.
// id is the post's full ID. func (s *PostService) LoadMoreComments(ctx context.Context, pc *PostAndComments) (*Response, error) {
// commentIDs are the ID36s of comments. if pc == nil {
func (s *PostService) More(ctx context.Context, comment *Comment) (*Response, error) { return nil, errors.New("pc: must not be nil")
if comment == nil {
return nil, errors.New("comment: must not be nil")
} }
if comment.Replies.MoreComments == nil { if !pc.hasMore() {
return nil, nil return nil, nil
} }
postID := comment.PostID postID := pc.Post.FullID
commentIDs := comment.Replies.MoreComments.Children commentIDs := pc.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"`
@ -485,22 +479,20 @@ func (s *PostService) More(ctx context.Context, comment *Comment) (*Response, er
comments := root.JSON.Data.Things.Comments comments := root.JSON.Data.Things.Comments
for _, c := range comments { for _, c := range comments {
addCommentToReplies(comment, c) addCommentToTree(pc, c)
} }
comment.Replies.MoreComments = nil pc.moreComments = nil
return resp, nil return resp, nil
} }
// addCommentToReplies traverses the comment tree to find the one func addCommentToTree(pc *PostAndComments, comment *Comment) {
// that the 2nd comment is replying to. It then adds it to its replies. if pc.Post.FullID == comment.ParentID {
func addCommentToReplies(parent *Comment, comment *Comment) { pc.Comments = append(pc.Comments, comment)
if parent.FullID == comment.ParentID {
parent.Replies.Comments = append(parent.Replies.Comments, comment)
return return
} }
for _, reply := range parent.Replies.Comments { for _, reply := range pc.Comments {
addCommentToReplies(reply, comment) addCommentToReplies(reply, comment)
} }
} }
@ -516,7 +508,7 @@ func (s *PostService) random(ctx context.Context, subreddits ...string) (*Post,
return nil, nil, nil, err return nil, nil, nil, err
} }
root := new(postAndComments) root := new(PostAndComments)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, nil, resp, err return nil, nil, resp, err

View file

@ -294,7 +294,7 @@ func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num
return nil, nil, nil, err return nil, nil, nil, err
} }
root := new(postAndComments) root := new(PostAndComments)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, nil, resp, err return nil, nil, resp, err

View file

@ -58,7 +58,7 @@ 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
@ -71,9 +71,6 @@ 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 {
t.MoreComments = make([]*More, 0)
}
if t.Users == nil { if t.Users == nil {
t.Users = make([]*User, 0) t.Users = make([]*User, 0)
} }
@ -110,7 +107,7 @@ func (t *Things) UnmarshalJSON(b []byte) error {
case kindMore: case kindMore:
v := new(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 = v
} }
case kindAccount: case kindAccount:
v := new(User) v := new(User)
@ -190,6 +187,10 @@ type Comment struct {
Replies Replies `json:"replies"` Replies Replies `json:"replies"`
} }
func (c *Comment) hasMore() bool {
return c.Replies.MoreComments != nil && len(c.Replies.MoreComments.Children) > 0
}
// 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.
@ -214,9 +215,7 @@ 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
if len(root.Data.Things.MoreComments) > 0 { r.MoreComments = root.Data.Things.MoreComments
r.MoreComments = root.Data.Things.MoreComments[0]
}
} }
return nil return nil
@ -329,6 +328,13 @@ func (rl *rootListing) getComments() *Comments {
return v return v
} }
func (rl *rootListing) getMoreComments() *More {
if rl == nil || rl.Data == nil {
return nil
}
return rl.Data.Things.MoreComments
}
func (rl *rootListing) getUsers() *Users { func (rl *rootListing) getUsers() *Users {
v := new(Users) v := new(Users)
if rl != nil && rl.Data != nil { if rl != nil && rl.Data != nil {
@ -404,17 +410,18 @@ type ModActions struct {
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 `json:"post"`
Comments []*Comment Comments []*Comment `json:"comments"`
moreComments *More
} }
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
// When getting a sticky post, you get an array of 2 Listings // When getting a sticky post, you get an array of 2 Listings
// The 1st one contains the single post in its children array // The 1st one contains the single post in its children array
// The 2nd one contains the comments to the post // The 2nd one contains the comments to the post
func (pc *postAndComments) UnmarshalJSON(data []byte) error { func (pc *PostAndComments) UnmarshalJSON(data []byte) error {
var l []rootListing var l []rootListing
err := json.Unmarshal(data, &l) err := json.Unmarshal(data, &l)
@ -428,9 +435,17 @@ 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
moreComments := l[1].getMoreComments()
pc.Post = post pc.Post = post
pc.Comments = comments pc.Comments = comments
pc.moreComments = moreComments
return nil return nil
} }
func (pc *PostAndComments) hasMore() bool {
return pc.moreComments != nil && len(pc.moreComments.Children) > 0
}
func (pc *PostAndComments) M() *More { return pc.moreComments }