From 840d644a4ebfeb53aed246888363de8456033989 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 26 Oct 2022 13:15:59 -0400 Subject: [PATCH] handle threads with no comments --- internal/reddit/testdata/thread_empty.json | 182 +++++++++++++++++++++ internal/reddit/types.go | 8 +- internal/reddit/types_test.go | 18 ++ 3 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 internal/reddit/testdata/thread_empty.json diff --git a/internal/reddit/testdata/thread_empty.json b/internal/reddit/testdata/thread_empty.json new file mode 100644 index 0000000..53e335e --- /dev/null +++ b/internal/reddit/testdata/thread_empty.json @@ -0,0 +1,182 @@ +[ + { + "kind": "Listing", + "data": { + "after": null, + "dist": 1, + "modhash": "", + "geo_filter": "", + "children": [ + { + "kind": "t3", + "data": { + "approved_at_utc": null, + "subreddit": "shittymobilegameads", + "selftext": "", + "user_reports": [], + "saved": false, + "mod_reason_title": null, + "gilded": 0, + "clicked": false, + "title": "So many knives… so little time.", + "link_flair_richtext": [], + "subreddit_name_prefixed": "r/shittymobilegameads", + "hidden": false, + "pwls": 0, + "link_flair_css_class": "", + "downs": 0, + "thumbnail_height": 140, + "top_awarded_type": null, + "parent_whitelist_status": "no_ads", + "hide_score": false, + "name": "t3_so0808", + "quarantine": false, + "link_flair_text_color": "dark", + "upvote_ratio": 1.0, + "author_flair_background_color": null, + "subreddit_type": "public", + "ups": 1, + "total_awards_received": 0, + "media_embed": { + "content": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstreamable.com%2Fo%2F9dzjf8&display_name=Streamable&url=https%3A%2F%2Fstreamable.com%2F9dzjf8&image=https%3A%2F%2Fcdn-cf-east.streamable.com%2Fimage%2F9dzjf8.jpg%3FExpires%3D1644627060%26Signature%3DckIpMltI-tat-05IX8%7EBDuhY9NQwn%7EqfYsC3niRDwpJQw1BoAZl90NHs9hwVN4VOcA24L%7Eh%7ERthoLsVs2zl9Z4fLbwVM0Zgt-4DpVNIbyS49VSGlnEIgFc0WLumJ%7EiRO0-h7R5TuzB3PurTyXQigR6JDOB89VIeuEZ8vqSEwgljJzc-tcxdx7%7E0yepNdoz%7E29Q2jNkau9bwQPpyqfG9eSk8d5DRrL1bo4nDA3N%7Edd3a3FL%7EiSaxzqdRXezLCB38WEILSTBP0VCcSfgmNomFnjWUHfNfJFlogJcyhG9X0aXUYnMSwvbIQpXOrAcsWP1YWEJQxVafm44lmbiK%7E5XzV5Q__%26Key-Pair-Id%3DAPKAIEYUVEN4EVB2OKEQ&key=ed8fa8699ce04833838e66ce79ba05f1&type=text%2Fhtml&schema=streamable\" width=\"592\" height=\"1280\" scrolling=\"no\" title=\"Streamable embed\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen=\"true\"></iframe>", + "width": 592, + "scrolling": false, + "height": 1280 + }, + "thumbnail_width": 140, + "author_flair_template_id": null, + "is_original_content": false, + "author_fullname": "t2_1ia22", + "secure_media": { + "oembed": { + "provider_url": "https://streamable.com", + "description": "Watch this video on Streamable.", + "title": "Streamable Video", + "thumbnail_width": 592, + "height": 1280, + "width": 592, + "html": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstreamable.com%2Fo%2F9dzjf8&display_name=Streamable&url=https%3A%2F%2Fstreamable.com%2F9dzjf8&image=https%3A%2F%2Fcdn-cf-east.streamable.com%2Fimage%2F9dzjf8.jpg%3FExpires%3D1644627060%26Signature%3DckIpMltI-tat-05IX8%7EBDuhY9NQwn%7EqfYsC3niRDwpJQw1BoAZl90NHs9hwVN4VOcA24L%7Eh%7ERthoLsVs2zl9Z4fLbwVM0Zgt-4DpVNIbyS49VSGlnEIgFc0WLumJ%7EiRO0-h7R5TuzB3PurTyXQigR6JDOB89VIeuEZ8vqSEwgljJzc-tcxdx7%7E0yepNdoz%7E29Q2jNkau9bwQPpyqfG9eSk8d5DRrL1bo4nDA3N%7Edd3a3FL%7EiSaxzqdRXezLCB38WEILSTBP0VCcSfgmNomFnjWUHfNfJFlogJcyhG9X0aXUYnMSwvbIQpXOrAcsWP1YWEJQxVafm44lmbiK%7E5XzV5Q__%26Key-Pair-Id%3DAPKAIEYUVEN4EVB2OKEQ&key=ed8fa8699ce04833838e66ce79ba05f1&type=text%2Fhtml&schema=streamable\" width=\"592\" height=\"1280\" scrolling=\"no\" title=\"Streamable embed\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen=\"true\"></iframe>", + "version": "1.0", + "provider_name": "Streamable", + "thumbnail_url": "https://cdn-cf-east.streamable.com/image/9dzjf8.jpg?Expires=1644627060&Signature=ckIpMltI-tat-05IX8~BDuhY9NQwn~qfYsC3niRDwpJQw1BoAZl90NHs9hwVN4VOcA24L~h~RthoLsVs2zl9Z4fLbwVM0Zgt-4DpVNIbyS49VSGlnEIgFc0WLumJ~iRO0-h7R5TuzB3PurTyXQigR6JDOB89VIeuEZ8vqSEwgljJzc-tcxdx7~0yepNdoz~29Q2jNkau9bwQPpyqfG9eSk8d5DRrL1bo4nDA3N~dd3a3FL~iSaxzqdRXezLCB38WEILSTBP0VCcSfgmNomFnjWUHfNfJFlogJcyhG9X0aXUYnMSwvbIQpXOrAcsWP1YWEJQxVafm44lmbiK~5XzV5Q__&Key-Pair-Id=APKAIEYUVEN4EVB2OKEQ", + "type": "video", + "thumbnail_height": 1280 + }, + "type": "streamable.com" + }, + "is_reddit_media_domain": false, + "is_meta": false, + "category": null, + "secure_media_embed": { + "content": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstreamable.com%2Fo%2F9dzjf8&display_name=Streamable&url=https%3A%2F%2Fstreamable.com%2F9dzjf8&image=https%3A%2F%2Fcdn-cf-east.streamable.com%2Fimage%2F9dzjf8.jpg%3FExpires%3D1644627060%26Signature%3DckIpMltI-tat-05IX8%7EBDuhY9NQwn%7EqfYsC3niRDwpJQw1BoAZl90NHs9hwVN4VOcA24L%7Eh%7ERthoLsVs2zl9Z4fLbwVM0Zgt-4DpVNIbyS49VSGlnEIgFc0WLumJ%7EiRO0-h7R5TuzB3PurTyXQigR6JDOB89VIeuEZ8vqSEwgljJzc-tcxdx7%7E0yepNdoz%7E29Q2jNkau9bwQPpyqfG9eSk8d5DRrL1bo4nDA3N%7Edd3a3FL%7EiSaxzqdRXezLCB38WEILSTBP0VCcSfgmNomFnjWUHfNfJFlogJcyhG9X0aXUYnMSwvbIQpXOrAcsWP1YWEJQxVafm44lmbiK%7E5XzV5Q__%26Key-Pair-Id%3DAPKAIEYUVEN4EVB2OKEQ&key=ed8fa8699ce04833838e66ce79ba05f1&type=text%2Fhtml&schema=streamable\" width=\"592\" height=\"1280\" scrolling=\"no\" title=\"Streamable embed\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen=\"true\"></iframe>", + "width": 592, + "scrolling": false, + "media_domain_url": "https://www.redditmedia.com/mediaembed/so0808", + "height": 1280 + }, + "link_flair_text": "Shitty Ad", + "can_mod_post": false, + "score": 1, + "approved_by": null, + "is_created_from_ads_ui": false, + "author_premium": false, + "thumbnail": "default", + "edited": false, + "author_flair_css_class": null, + "author_flair_richtext": [], + "gildings": {}, + "content_categories": null, + "is_self": false, + "mod_note": null, + "created": 1644368372.0, + "link_flair_type": "text", + "wls": 0, + "removed_by_category": "reddit", + "banned_by": null, + "author_flair_type": "text", + "domain": "streamable.com", + "allow_live_comments": false, + "selftext_html": null, + "likes": null, + "suggested_sort": null, + "banned_at_utc": null, + "url_overridden_by_dest": "https://streamable.com/9dzjf8", + "view_count": null, + "archived": false, + "no_follow": true, + "is_crosspostable": false, + "pinned": false, + "over_18": false, + "all_awardings": [], + "awarders": [], + "media_only": false, + "link_flair_template_id": "b161a66a-f5d2-11e9-943a-0e3f5e06e97c", + "can_gild": false, + "spoiler": false, + "locked": false, + "author_flair_text": null, + "treatment_tags": [], + "visited": false, + "removed_by": null, + "num_reports": null, + "distinguished": null, + "subreddit_id": "t5_unzk3", + "author_is_blocked": false, + "mod_reason_by": null, + "removal_reason": null, + "link_flair_background_color": "#ffd635", + "id": "so0808", + "is_robot_indexable": false, + "num_duplicates": 0, + "report_reasons": null, + "author": "changelog", + "discussion_type": null, + "num_comments": 0, + "send_replies": true, + "media": { + "oembed": { + "provider_url": "https://streamable.com", + "description": "Watch this video on Streamable.", + "title": "Streamable Video", + "thumbnail_width": 592, + "height": 1280, + "width": 592, + "html": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstreamable.com%2Fo%2F9dzjf8&display_name=Streamable&url=https%3A%2F%2Fstreamable.com%2F9dzjf8&image=https%3A%2F%2Fcdn-cf-east.streamable.com%2Fimage%2F9dzjf8.jpg%3FExpires%3D1644627060%26Signature%3DckIpMltI-tat-05IX8%7EBDuhY9NQwn%7EqfYsC3niRDwpJQw1BoAZl90NHs9hwVN4VOcA24L%7Eh%7ERthoLsVs2zl9Z4fLbwVM0Zgt-4DpVNIbyS49VSGlnEIgFc0WLumJ%7EiRO0-h7R5TuzB3PurTyXQigR6JDOB89VIeuEZ8vqSEwgljJzc-tcxdx7%7E0yepNdoz%7E29Q2jNkau9bwQPpyqfG9eSk8d5DRrL1bo4nDA3N%7Edd3a3FL%7EiSaxzqdRXezLCB38WEILSTBP0VCcSfgmNomFnjWUHfNfJFlogJcyhG9X0aXUYnMSwvbIQpXOrAcsWP1YWEJQxVafm44lmbiK%7E5XzV5Q__%26Key-Pair-Id%3DAPKAIEYUVEN4EVB2OKEQ&key=ed8fa8699ce04833838e66ce79ba05f1&type=text%2Fhtml&schema=streamable\" width=\"592\" height=\"1280\" scrolling=\"no\" title=\"Streamable embed\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen=\"true\"></iframe>", + "version": "1.0", + "provider_name": "Streamable", + "thumbnail_url": "https://cdn-cf-east.streamable.com/image/9dzjf8.jpg?Expires=1644627060&Signature=ckIpMltI-tat-05IX8~BDuhY9NQwn~qfYsC3niRDwpJQw1BoAZl90NHs9hwVN4VOcA24L~h~RthoLsVs2zl9Z4fLbwVM0Zgt-4DpVNIbyS49VSGlnEIgFc0WLumJ~iRO0-h7R5TuzB3PurTyXQigR6JDOB89VIeuEZ8vqSEwgljJzc-tcxdx7~0yepNdoz~29Q2jNkau9bwQPpyqfG9eSk8d5DRrL1bo4nDA3N~dd3a3FL~iSaxzqdRXezLCB38WEILSTBP0VCcSfgmNomFnjWUHfNfJFlogJcyhG9X0aXUYnMSwvbIQpXOrAcsWP1YWEJQxVafm44lmbiK~5XzV5Q__&Key-Pair-Id=APKAIEYUVEN4EVB2OKEQ", + "type": "video", + "thumbnail_height": 1280 + }, + "type": "streamable.com" + }, + "contest_mode": false, + "author_patreon_flair": false, + "author_flair_text_color": null, + "permalink": "/r/shittymobilegameads/comments/so0808/so_many_knives_so_little_time/", + "whitelist_status": "no_ads", + "stickied": false, + "url": "https://streamable.com/9dzjf8", + "subreddit_subscribers": 143074, + "created_utc": 1644368372.0, + "num_crossposts": 0, + "mod_reports": [], + "is_video": false + } + } + ], + "before": null + } + }, + { + "kind": "Listing", + "data": { + "after": null, + "dist": null, + "modhash": "", + "geo_filter": "", + "children": [], + "before": null + } + } +] diff --git a/internal/reddit/types.go b/internal/reddit/types.go index 3794df9..66945c4 100644 --- a/internal/reddit/types.go +++ b/internal/reddit/types.go @@ -77,9 +77,11 @@ func NewThreadResponse(val *fastjson.Value) interface{} { // Comments come in the second element of the array also as a listing comments := listings[1].Get("data").GetArray("children") - t.Children = make([]*Thing, len(comments)-1) - for i, comment := range comments[:len(comments)-1] { - t.Children[i] = NewThing(comment) + if len(comments) > 1 { + t.Children = make([]*Thing, len(comments)-1) + for i, comment := range comments[:len(comments)-1] { + t.Children[i] = NewThing(comment) + } } return t } diff --git a/internal/reddit/types_test.go b/internal/reddit/types_test.go index adc5eae..70fcd6a 100644 --- a/internal/reddit/types_test.go +++ b/internal/reddit/types_test.go @@ -199,3 +199,21 @@ func TestThreadResponseParsing(t *testing.T) { assert.Equal(t, "The Deck is a lot more portable than the Pi though.", tr.Children[0].Body) assert.Equal(t, "PhonicUK", tr.Children[1].Author) } + +func TestEmptyThreadResponseParsing(t *testing.T) { + t.Parallel() + + bb, err := ioutil.ReadFile("testdata/thread_empty.json") + assert.NoError(t, err) + + parser := NewTestParser(t) + val, err := parser.ParseBytes(bb) + assert.NoError(t, err) + + ret := reddit.NewThreadResponse(val) + tr := ret.(*reddit.ThreadResponse) + assert.NotNil(t, tr) + + assert.Equal(t, "So many knives… so little time.", tr.Post.Title) + assert.Equal(t, 0, len(tr.Children)) +}