Add tests
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
29fdb0fa19
commit
011cd2a78b
7 changed files with 399 additions and 68 deletions
18
comment.go
18
comment.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CommentService handles communication with the comment
|
// CommentService handles communication with the comment
|
||||||
|
@ -78,19 +79,16 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
|
||||||
postID := comment.PostID
|
postID := comment.PostID
|
||||||
commentIDs := comment.Replies.More.Children
|
commentIDs := comment.Replies.More.Children
|
||||||
|
|
||||||
type params struct {
|
form := url.Values{}
|
||||||
PostID string `url:"link_id"`
|
form.Set("api_type", "json")
|
||||||
IDs []string `url:"children,comma"`
|
form.Set("link_id", postID)
|
||||||
APIType string `url:"api_type"`
|
form.Set("children", strings.Join(commentIDs, ","))
|
||||||
}
|
|
||||||
|
|
||||||
path := "api/morechildren"
|
path := "api/morechildren"
|
||||||
path, err := addOptions(path, params{postID, commentIDs, "json"})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
// 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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -301,3 +301,49 @@ func TestCommentService_RemoveVote(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCommentService_LoadMoreReplies(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("testdata/comment/more.json")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/morechildren", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
require.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("link_id", "t3_123")
|
||||||
|
form.Set("children", "def,ghi,jkl")
|
||||||
|
form.Set("api_type", "json")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, form, r.PostForm)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err = client.Comment.LoadMoreReplies(ctx, nil)
|
||||||
|
require.EqualError(t, err, "comment: cannot be nil")
|
||||||
|
|
||||||
|
resp, err := client.Comment.LoadMoreReplies(ctx, &Comment{})
|
||||||
|
require.Nil(t, resp)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
comment := &Comment{
|
||||||
|
FullID: "t1_abc",
|
||||||
|
PostID: "t3_123",
|
||||||
|
Replies: Replies{
|
||||||
|
More: &More{
|
||||||
|
Children: []string{"def", "ghi", "jkl"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.Comment.LoadMoreReplies(ctx, comment)
|
||||||
|
require.Nil(t, err)
|
||||||
|
require.False(t, comment.HasMore())
|
||||||
|
require.Len(t, comment.Replies.Comments, 2)
|
||||||
|
require.Len(t, comment.Replies.Comments[0].Replies.Comments, 1)
|
||||||
|
}
|
||||||
|
|
4
post.go
4
post.go
|
@ -471,7 +471,7 @@ func (s *PostService) LoadMoreComments(ctx context.Context, pc *PostAndComments)
|
||||||
}
|
}
|
||||||
|
|
||||||
postID := pc.Post.FullID
|
postID := pc.Post.FullID
|
||||||
commentIDs := pc.MoreComments.Children
|
commentIDs := pc.More.Children
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("api_type", "json")
|
form.Set("api_type", "json")
|
||||||
|
@ -515,7 +515,7 @@ func (s *PostService) LoadMoreComments(ctx context.Context, pc *PostAndComments)
|
||||||
}
|
}
|
||||||
|
|
||||||
if noMore {
|
if noMore {
|
||||||
pc.MoreComments = nil
|
pc.More = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
|
|
73
post_test.go
73
post_test.go
|
@ -820,72 +820,56 @@ func TestPostService_DisableContestMode(t *testing.T) {
|
||||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostService_More(t *testing.T) {
|
func TestPostService_LoadMoreReplies(t *testing.T) {
|
||||||
setup()
|
setup()
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
parentComment := &Comment{
|
|
||||||
FullID: "t1_abc",
|
|
||||||
ParentID: "t3_123",
|
|
||||||
PostID: "t3_123",
|
|
||||||
Replies: Replies{
|
|
||||||
More: &More{
|
|
||||||
Children: []string{"def,ghi"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
blob, err := readFileContents("testdata/post/more.json")
|
blob, err := readFileContents("testdata/post/more.json")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
mux.HandleFunc("/api/morechildren", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/morechildren", func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, http.MethodGet, r.Method)
|
require.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("link_id", "t3_123")
|
form.Set("link_id", "t3_123")
|
||||||
form.Set("children", "def,ghi")
|
form.Set("children", "def,ghi,jkl")
|
||||||
form.Set("api_type", "json")
|
form.Set("api_type", "json")
|
||||||
|
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, form, r.Form)
|
require.Equal(t, form, r.PostForm)
|
||||||
|
|
||||||
fmt.Fprint(w, blob)
|
fmt.Fprint(w, blob)
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err = client.Comment.LoadMoreReplies(ctx, parentComment)
|
_, err = client.Post.LoadMoreComments(ctx, nil)
|
||||||
require.NoError(t, err)
|
require.EqualError(t, err, "pc: cannot be nil")
|
||||||
require.Nil(t, parentComment.Replies.More)
|
|
||||||
require.Len(t, parentComment.Replies.Comments, 1)
|
|
||||||
require.Len(t, parentComment.Replies.Comments[0].Replies.Comments, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPostService_MoreNil(t *testing.T) {
|
resp, err := client.Post.LoadMoreComments(ctx, &PostAndComments{})
|
||||||
setup()
|
require.Nil(t, resp)
|
||||||
defer teardown()
|
require.Nil(t, err)
|
||||||
|
|
||||||
_, err := client.Comment.LoadMoreReplies(ctx, nil)
|
pc := &PostAndComments{
|
||||||
require.EqualError(t, err, "comment: cannot be nil")
|
Post: &Post{
|
||||||
|
FullID: "t3_123",
|
||||||
parentComment := &Comment{
|
},
|
||||||
Replies: Replies{
|
Comments: []*Comment{
|
||||||
More: nil,
|
{
|
||||||
|
FullID: "t1_abc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
More: &More{
|
||||||
|
Children: []string{"def", "ghi", "jkl"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// should return nil, nil since comment does not have More struct
|
_, err = client.Post.LoadMoreComments(ctx, pc)
|
||||||
resp, err := client.Comment.LoadMoreReplies(ctx, parentComment)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, resp)
|
require.False(t, pc.HasMore())
|
||||||
|
require.Len(t, pc.Comments, 2)
|
||||||
parentComment.Replies.More = &More{
|
require.True(t, pc.Comments[1].HasMore())
|
||||||
Children: []string{},
|
require.Len(t, pc.Comments[0].Replies.Comments, 1)
|
||||||
}
|
require.Len(t, pc.Comments[0].Replies.Comments[0].Replies.Comments, 1)
|
||||||
|
|
||||||
// should return nil, nil since comment's More struct has 0 children
|
|
||||||
resp, err = client.Comment.LoadMoreReplies(ctx, parentComment)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Nil(t, resp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostService_RandomFromSubreddits(t *testing.T) {
|
func TestPostService_RandomFromSubreddits(t *testing.T) {
|
||||||
|
@ -1162,6 +1146,9 @@ func TestPostService_MarkVisited(t *testing.T) {
|
||||||
require.Equal(t, form, r.PostForm)
|
require.Equal(t, form, r.PostForm)
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err := client.Post.MarkVisited(ctx, "t3_test1", "t3_test2", "t3_test3")
|
_, err := client.Post.MarkVisited(ctx)
|
||||||
|
require.EqualError(t, err, "must provide at least 1 id")
|
||||||
|
|
||||||
|
_, err = client.Post.MarkVisited(ctx, "t3_test1", "t3_test2", "t3_test3")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
222
testdata/comment/more.json
vendored
Normal file
222
testdata/comment/more.json
vendored
Normal file
|
@ -0,0 +1,222 @@
|
||||||
|
{
|
||||||
|
"json": {
|
||||||
|
"errors": [],
|
||||||
|
"data": {
|
||||||
|
"things": [
|
||||||
|
{
|
||||||
|
"kind": "t1",
|
||||||
|
"data": {
|
||||||
|
"total_awards_received": 0,
|
||||||
|
"approved_at_utc": null,
|
||||||
|
"edited": false,
|
||||||
|
"mod_reason_by": null,
|
||||||
|
"banned_by": null,
|
||||||
|
"author_flair_type": "text",
|
||||||
|
"removal_reason": null,
|
||||||
|
"link_id": "t3_123",
|
||||||
|
"author_flair_template_id": null,
|
||||||
|
"likes": null,
|
||||||
|
"replies": "",
|
||||||
|
"user_reports": [],
|
||||||
|
"saved": false,
|
||||||
|
"id": "def",
|
||||||
|
"banned_at_utc": null,
|
||||||
|
"mod_reason_title": null,
|
||||||
|
"gilded": 0,
|
||||||
|
"archived": false,
|
||||||
|
"no_follow": false,
|
||||||
|
"author": "testuser1",
|
||||||
|
"can_mod_post": false,
|
||||||
|
"send_replies": true,
|
||||||
|
"parent_id": "t1_abc",
|
||||||
|
"score": 5,
|
||||||
|
"author_fullname": "t2_user1",
|
||||||
|
"approved_by": null,
|
||||||
|
"mod_note": null,
|
||||||
|
"all_awardings": [],
|
||||||
|
"subreddit_id": "t5_2qh1i",
|
||||||
|
"body": "Wow, I came here to say exactly this and figured it would be buried. Pleasantly shocked to see your comment as the top comment! So. Good. Thanks, random internet friend!",
|
||||||
|
"awarders": [],
|
||||||
|
"downs": 0,
|
||||||
|
"author_flair_css_class": null,
|
||||||
|
"name": "t1_def",
|
||||||
|
"author_patreon_flair": false,
|
||||||
|
"collapsed": false,
|
||||||
|
"author_flair_richtext": [],
|
||||||
|
"is_submitter": false,
|
||||||
|
"body_html": "<div class=\"md\"><p>Wow, I came here to say exactly this and figured it would be buried. Pleasantly shocked to see your comment as the top comment! So. Good. Thanks, random internet friend!</p>\n</div>",
|
||||||
|
"gildings": {},
|
||||||
|
"collapsed_reason": null,
|
||||||
|
"distinguished": null,
|
||||||
|
"associated_award": null,
|
||||||
|
"stickied": false,
|
||||||
|
"author_premium": false,
|
||||||
|
"can_gild": true,
|
||||||
|
"top_awarded_type": null,
|
||||||
|
"author_flair_text_color": null,
|
||||||
|
"score_hidden": false,
|
||||||
|
"permalink": "/r/AskReddit/comments/123/which_scene_from_an_animated_film_will_always_be/def/",
|
||||||
|
"num_reports": null,
|
||||||
|
"locked": false,
|
||||||
|
"report_reasons": null,
|
||||||
|
"created": 1595301409.0,
|
||||||
|
"subreddit": "AskReddit",
|
||||||
|
"author_flair_text": null,
|
||||||
|
"treatment_tags": [],
|
||||||
|
"created_utc": 1595272609.0,
|
||||||
|
"subreddit_name_prefixed": "r/AskReddit",
|
||||||
|
"controversiality": 0,
|
||||||
|
"depth": 1,
|
||||||
|
"author_flair_background_color": null,
|
||||||
|
"collapsed_because_crowd_control": null,
|
||||||
|
"mod_reports": [],
|
||||||
|
"subreddit_type": "public",
|
||||||
|
"ups": 5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "t1",
|
||||||
|
"data": {
|
||||||
|
"total_awards_received": 0,
|
||||||
|
"approved_at_utc": null,
|
||||||
|
"edited": false,
|
||||||
|
"mod_reason_by": null,
|
||||||
|
"banned_by": null,
|
||||||
|
"author_flair_type": "text",
|
||||||
|
"removal_reason": null,
|
||||||
|
"link_id": "t3_123",
|
||||||
|
"author_flair_template_id": null,
|
||||||
|
"likes": null,
|
||||||
|
"replies": "",
|
||||||
|
"user_reports": [],
|
||||||
|
"saved": false,
|
||||||
|
"id": "ghi",
|
||||||
|
"banned_at_utc": null,
|
||||||
|
"mod_reason_title": null,
|
||||||
|
"gilded": 0,
|
||||||
|
"archived": false,
|
||||||
|
"no_follow": true,
|
||||||
|
"author": "testuser2",
|
||||||
|
"can_mod_post": false,
|
||||||
|
"send_replies": true,
|
||||||
|
"parent_id": "t1_abc",
|
||||||
|
"score": 1,
|
||||||
|
"author_fullname": "t2_user2",
|
||||||
|
"approved_by": null,
|
||||||
|
"mod_note": null,
|
||||||
|
"all_awardings": [],
|
||||||
|
"subreddit_id": "t5_2qh1i",
|
||||||
|
"body": "Oh my god yes best movie ever love that scene",
|
||||||
|
"awarders": [],
|
||||||
|
"downs": 0,
|
||||||
|
"author_flair_css_class": null,
|
||||||
|
"name": "t1_ghi",
|
||||||
|
"author_patreon_flair": false,
|
||||||
|
"collapsed": false,
|
||||||
|
"author_flair_richtext": [],
|
||||||
|
"is_submitter": false,
|
||||||
|
"body_html": "<div class=\"md\"><p>Oh my god yes best movie ever love that scene</p>\n</div>",
|
||||||
|
"gildings": {},
|
||||||
|
"collapsed_reason": null,
|
||||||
|
"distinguished": null,
|
||||||
|
"associated_award": null,
|
||||||
|
"stickied": false,
|
||||||
|
"author_premium": false,
|
||||||
|
"can_gild": true,
|
||||||
|
"top_awarded_type": null,
|
||||||
|
"author_flair_text_color": null,
|
||||||
|
"score_hidden": false,
|
||||||
|
"permalink": "/r/AskReddit/comments/123/which_scene_from_an_animated_film_will_always_be/ghi/",
|
||||||
|
"num_reports": null,
|
||||||
|
"locked": false,
|
||||||
|
"report_reasons": null,
|
||||||
|
"created": 1595308578.0,
|
||||||
|
"subreddit": "AskReddit",
|
||||||
|
"author_flair_text": null,
|
||||||
|
"treatment_tags": [],
|
||||||
|
"created_utc": 1595279778.0,
|
||||||
|
"subreddit_name_prefixed": "r/AskReddit",
|
||||||
|
"controversiality": 0,
|
||||||
|
"depth": 1,
|
||||||
|
"author_flair_background_color": null,
|
||||||
|
"collapsed_because_crowd_control": null,
|
||||||
|
"mod_reports": [],
|
||||||
|
"subreddit_type": "public",
|
||||||
|
"ups": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "t1",
|
||||||
|
"data": {
|
||||||
|
"total_awards_received": 0,
|
||||||
|
"approved_at_utc": null,
|
||||||
|
"edited": false,
|
||||||
|
"mod_reason_by": null,
|
||||||
|
"banned_by": null,
|
||||||
|
"author_flair_type": "text",
|
||||||
|
"removal_reason": null,
|
||||||
|
"link_id": "t3_123",
|
||||||
|
"author_flair_template_id": null,
|
||||||
|
"likes": null,
|
||||||
|
"replies": "",
|
||||||
|
"user_reports": [],
|
||||||
|
"saved": false,
|
||||||
|
"id": "jkl",
|
||||||
|
"banned_at_utc": null,
|
||||||
|
"mod_reason_title": null,
|
||||||
|
"gilded": 0,
|
||||||
|
"archived": false,
|
||||||
|
"no_follow": true,
|
||||||
|
"author": "testuser2",
|
||||||
|
"can_mod_post": false,
|
||||||
|
"send_replies": true,
|
||||||
|
"parent_id": "t1_def",
|
||||||
|
"score": 1,
|
||||||
|
"author_fullname": "t2_user2",
|
||||||
|
"approved_by": null,
|
||||||
|
"mod_note": null,
|
||||||
|
"all_awardings": [],
|
||||||
|
"subreddit_id": "t5_2qh1i",
|
||||||
|
"body": "Hello this is a test",
|
||||||
|
"awarders": [],
|
||||||
|
"downs": 0,
|
||||||
|
"author_flair_css_class": null,
|
||||||
|
"name": "t1_jkl",
|
||||||
|
"author_patreon_flair": false,
|
||||||
|
"collapsed": false,
|
||||||
|
"author_flair_richtext": [],
|
||||||
|
"is_submitter": false,
|
||||||
|
"body_html": "<div class=\"md\"><p>Hello this is a test</p>\n</div>",
|
||||||
|
"gildings": {},
|
||||||
|
"collapsed_reason": null,
|
||||||
|
"distinguished": null,
|
||||||
|
"associated_award": null,
|
||||||
|
"stickied": false,
|
||||||
|
"author_premium": false,
|
||||||
|
"can_gild": true,
|
||||||
|
"top_awarded_type": null,
|
||||||
|
"author_flair_text_color": null,
|
||||||
|
"score_hidden": false,
|
||||||
|
"permalink": "/r/AskReddit/comments/123/which_scene_from_an_animated_film_will_always_be/ghi/",
|
||||||
|
"num_reports": null,
|
||||||
|
"locked": false,
|
||||||
|
"report_reasons": null,
|
||||||
|
"created": 1595308578.0,
|
||||||
|
"subreddit": "AskReddit",
|
||||||
|
"author_flair_text": null,
|
||||||
|
"treatment_tags": [],
|
||||||
|
"created_utc": 1595279778.0,
|
||||||
|
"subreddit_name_prefixed": "r/AskReddit",
|
||||||
|
"controversiality": 0,
|
||||||
|
"depth": 1,
|
||||||
|
"author_flair_background_color": null,
|
||||||
|
"collapsed_because_crowd_control": null,
|
||||||
|
"mod_reports": [],
|
||||||
|
"subreddit_type": "public",
|
||||||
|
"ups": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
82
testdata/post/more.json
vendored
82
testdata/post/more.json
vendored
|
@ -144,6 +144,88 @@
|
||||||
"subreddit_type": "public",
|
"subreddit_type": "public",
|
||||||
"ups": 1
|
"ups": 1
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "t1",
|
||||||
|
"data": {
|
||||||
|
"total_awards_received": 0,
|
||||||
|
"approved_at_utc": null,
|
||||||
|
"edited": false,
|
||||||
|
"mod_reason_by": null,
|
||||||
|
"banned_by": null,
|
||||||
|
"author_flair_type": "text",
|
||||||
|
"removal_reason": null,
|
||||||
|
"link_id": "t3_123",
|
||||||
|
"author_flair_template_id": null,
|
||||||
|
"likes": null,
|
||||||
|
"replies": "",
|
||||||
|
"user_reports": [],
|
||||||
|
"saved": false,
|
||||||
|
"id": "jkl",
|
||||||
|
"banned_at_utc": null,
|
||||||
|
"mod_reason_title": null,
|
||||||
|
"gilded": 0,
|
||||||
|
"archived": false,
|
||||||
|
"no_follow": true,
|
||||||
|
"author": "testuser2",
|
||||||
|
"can_mod_post": false,
|
||||||
|
"send_replies": true,
|
||||||
|
"parent_id": "t3_123",
|
||||||
|
"score": 1,
|
||||||
|
"author_fullname": "t2_user2",
|
||||||
|
"approved_by": null,
|
||||||
|
"mod_note": null,
|
||||||
|
"all_awardings": [],
|
||||||
|
"subreddit_id": "t5_2qh1i",
|
||||||
|
"body": "Hello this is a test",
|
||||||
|
"awarders": [],
|
||||||
|
"downs": 0,
|
||||||
|
"author_flair_css_class": null,
|
||||||
|
"name": "t1_jkl",
|
||||||
|
"author_patreon_flair": false,
|
||||||
|
"collapsed": false,
|
||||||
|
"author_flair_richtext": [],
|
||||||
|
"is_submitter": false,
|
||||||
|
"body_html": "<div class=\"md\"><p>Hello this is a test</p>\n</div>",
|
||||||
|
"gildings": {},
|
||||||
|
"collapsed_reason": null,
|
||||||
|
"distinguished": null,
|
||||||
|
"associated_award": null,
|
||||||
|
"stickied": false,
|
||||||
|
"author_premium": false,
|
||||||
|
"can_gild": true,
|
||||||
|
"top_awarded_type": null,
|
||||||
|
"author_flair_text_color": null,
|
||||||
|
"score_hidden": false,
|
||||||
|
"permalink": "/r/AskReddit/comments/123/which_scene_from_an_animated_film_will_always_be/ghi/",
|
||||||
|
"num_reports": null,
|
||||||
|
"locked": false,
|
||||||
|
"report_reasons": null,
|
||||||
|
"created": 1595308578.0,
|
||||||
|
"subreddit": "AskReddit",
|
||||||
|
"author_flair_text": null,
|
||||||
|
"treatment_tags": [],
|
||||||
|
"created_utc": 1595279778.0,
|
||||||
|
"subreddit_name_prefixed": "r/AskReddit",
|
||||||
|
"controversiality": 0,
|
||||||
|
"depth": 1,
|
||||||
|
"author_flair_background_color": null,
|
||||||
|
"collapsed_because_crowd_control": null,
|
||||||
|
"mod_reports": [],
|
||||||
|
"subreddit_type": "public",
|
||||||
|
"ups": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "more",
|
||||||
|
"data": {
|
||||||
|
"count": 1,
|
||||||
|
"name": "t1_fye3844",
|
||||||
|
"id": "fye3844",
|
||||||
|
"parent_id": "t1_jkl",
|
||||||
|
"depth": 6,
|
||||||
|
"children": ["fye3844"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
18
things.go
18
things.go
|
@ -221,7 +221,7 @@ func (r *Replies) UnmarshalJSON(data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Comments = root.Data.Things.Comments
|
r.Comments = root.Data.Things.Comments
|
||||||
r.More = root.getFirstMoreComments()
|
r.More = root.getFirstMore()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -311,11 +311,7 @@ func (l *rootListing) getComments() *Comments {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *rootListing) getMoreComments() []*More {
|
func (l *rootListing) getFirstMore() *More {
|
||||||
return l.Data.Things.Mores
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *rootListing) getFirstMoreComments() *More {
|
|
||||||
if len(l.Data.Things.Mores) == 0 {
|
if len(l.Data.Things.Mores) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -393,7 +389,7 @@ type ModActions struct {
|
||||||
type PostAndComments struct {
|
type PostAndComments struct {
|
||||||
Post *Post `json:"post"`
|
Post *Post `json:"post"`
|
||||||
Comments []*Comment `json:"comments"`
|
Comments []*Comment `json:"comments"`
|
||||||
MoreComments *More `json:"-"`
|
More *More `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
|
@ -410,18 +406,18 @@ 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].getFirstMoreComments()
|
moreComments := l[1].getFirstMore()
|
||||||
|
|
||||||
pc.Post = post
|
pc.Post = post
|
||||||
pc.Comments = comments
|
pc.Comments = comments
|
||||||
pc.MoreComments = moreComments
|
pc.More = moreComments
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasMore determines whether the post has more replies to load in its reply tree.
|
// HasMore determines whether the post has more replies to load in its reply tree.
|
||||||
func (pc *PostAndComments) HasMore() bool {
|
func (pc *PostAndComments) HasMore() bool {
|
||||||
return pc.MoreComments != nil && len(pc.MoreComments.Children) > 0
|
return pc.More != nil && len(pc.More.Children) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pc *PostAndComments) addCommentToTree(comment *Comment) {
|
func (pc *PostAndComments) addCommentToTree(comment *Comment) {
|
||||||
|
@ -437,7 +433,7 @@ func (pc *PostAndComments) addCommentToTree(comment *Comment) {
|
||||||
|
|
||||||
func (pc *PostAndComments) addMoreToTree(more *More) {
|
func (pc *PostAndComments) addMoreToTree(more *More) {
|
||||||
if pc.Post.FullID == more.ParentID {
|
if pc.Post.FullID == more.ParentID {
|
||||||
pc.MoreComments = more
|
pc.More = more
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, reply := range pc.Comments {
|
for _, reply := range pc.Comments {
|
||||||
|
|
Loading…
Reference in a new issue