Load more comments for a post

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-08-14 00:04:47 -04:00
parent 2b495417b6
commit 076c5bf30c
3 changed files with 95 additions and 47 deletions
+10 -16
View File
@@ -71,7 +71,7 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
return nil, errors.New("comment: cannot be nil")
}
if !comment.hasMore() {
if !comment.HasMore() {
return nil, nil
}
@@ -108,23 +108,17 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
}
comments := root.JSON.Data.Things.Comments
mores := root.JSON.Data.Things.More
for _, c := range comments {
addCommentToReplies(comment, c)
comment.addCommentToReplies(c)
}
if len(mores) > 0 {
comment.Replies.More = mores[0]
} else {
comment.Replies.More = nil
}
comment.Replies.More = 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)
}
}