diff --git a/listings.go b/listings.go index d4f1319..e1619fb 100644 --- a/listings.go +++ b/listings.go @@ -64,22 +64,3 @@ func (s *ListingsService) GetPosts(ctx context.Context, ids ...string) ([]Post, return root.getPosts().Posts, resp, nil } - -// GetPost returns a post with its comments. -// The id here is the ID36 of the post, not its full id. -// Example: instead of t3_abc123, use abc123. -func (s *ListingsService) GetPost(ctx context.Context, id string) (*Post, []Comment, *Response, error) { - path := fmt.Sprintf("comments/%s", id) - req, err := s.client.NewRequest(http.MethodGet, path, nil) - if err != nil { - return nil, nil, nil, err - } - - root := new(postAndComments) - resp, err := s.client.Do(ctx, req, root) - if err != nil { - return nil, nil, resp, err - } - - return root.Post, root.Comments, resp, nil -} diff --git a/post.go b/post.go index 437fd52..6833f16 100644 --- a/post.go +++ b/post.go @@ -3,6 +3,7 @@ package reddit import ( "context" "errors" + "fmt" "net/http" "net/url" "strings" @@ -58,6 +59,25 @@ type SubmitLinkOptions struct { Spoiler bool `url:"spoiler,omitempty"` } +// Get returns a post with its comments. +// id is the ID36 of the post, not its full id. +// Example: instead of t3_abc123, use abc123. +func (s *PostService) Get(ctx context.Context, id string) (*Post, []Comment, *Response, error) { + path := fmt.Sprintf("comments/%s", id) + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return nil, nil, nil, err + } + + root := new(postAndComments) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return nil, nil, resp, err + } + + return root.Post, root.Comments, resp, nil +} + func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *Response, error) { path := "api/submit" diff --git a/post_test.go b/post_test.go index c501ea7..eb5b6d8 100644 --- a/post_test.go +++ b/post_test.go @@ -8,6 +8,22 @@ import ( "github.com/stretchr/testify/assert" ) +// func TestPostService_Get(t *testing.T) { +// setup() +// defer teardown() + +// blob := readFileContents(t, "testdata/post/post.json") + +// mux.HandleFunc("/comments/test", func(w http.ResponseWriter, r *http.Request) { +// assert.Equal(t, http.MethodGet, r.Method) +// fmt.Fprint(w, blob) +// }) + +// post, comments, _, err := client.Post.Get(ctx, "test") +// assert.NoError(t, err) +// assert.Equal(t, http.StatusOK, res.StatusCode) +// } + func TestPostService_Hide(t *testing.T) { setup() defer teardown() diff --git a/testdata/post/post.json b/testdata/post/post.json new file mode 100644 index 0000000..ee9ea26 --- /dev/null +++ b/testdata/post/post.json @@ -0,0 +1,286 @@ +[ + { + "kind": "Listing", + "data": { + "modhash": null, + "dist": 1, + "children": [ + { + "kind": "t3", + "data": { + "approved_at_utc": null, + "subreddit": "test", + "selftext": "Hello", + "user_reports": [], + "saved": false, + "mod_reason_title": null, + "gilded": 0, + "clicked": false, + "title": "Test", + "link_flair_richtext": [], + "subreddit_name_prefixed": "r/test", + "hidden": false, + "pwls": 6, + "link_flair_css_class": null, + "downs": 0, + "thumbnail_height": null, + "top_awarded_type": null, + "parent_whitelist_status": "all_ads", + "hide_score": false, + "name": "t3_testpost", + "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": {}, + "thumbnail_width": null, + "author_flair_template_id": null, + "is_original_content": false, + "author_fullname": "t2_testuser", + "secure_media": null, + "is_reddit_media_domain": false, + "is_meta": false, + "category": null, + "secure_media_embed": {}, + "link_flair_text": null, + "can_mod_post": false, + "score": 1, + "approved_by": null, + "author_premium": false, + "thumbnail": "self", + "edited": false, + "author_flair_css_class": null, + "author_flair_richtext": [], + "gildings": {}, + "content_categories": null, + "is_self": true, + "mod_note": null, + "created": 1595096767.0, + "link_flair_type": "text", + "wls": 6, + "removed_by_category": null, + "banned_by": null, + "author_flair_type": "text", + "domain": "self.test", + "allow_live_comments": false, + "selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>Hello</p>\n</div><!-- SC_ON -->", + "likes": null, + "suggested_sort": null, + "banned_at_utc": null, + "view_count": null, + "archived": false, + "no_follow": true, + "is_crosspostable": true, + "pinned": false, + "over_18": false, + "all_awardings": [], + "awarders": [], + "media_only": false, + "can_gild": true, + "spoiler": false, + "locked": false, + "author_flair_text": null, + "treatment_tags": [], + "visited": false, + "removed_by": null, + "num_reports": null, + "distinguished": null, + "subreddit_id": "t5_2qh23", + "mod_reason_by": null, + "removal_reason": null, + "link_flair_background_color": "", + "id": "testpost", + "is_robot_indexable": true, + "num_duplicates": 0, + "report_reasons": null, + "author": "testuser", + "discussion_type": null, + "num_comments": 2, + "send_replies": true, + "media": null, + "contest_mode": false, + "author_patreon_flair": false, + "author_flair_text_color": null, + "permalink": "/r/test/comments/testpost/test/", + "whitelist_status": "all_ads", + "stickied": false, + "url": "https://www.reddit.com/r/test/comments/testpost/test/", + "subreddit_subscribers": 8077, + "created_utc": 1595067967.0, + "num_crossposts": 0, + "mod_reports": [], + "is_video": false + } + } + ], + "after": null, + "before": null + } + }, + { + "kind": "Listing", + "data": { + "modhash": null, + "dist": null, + "children": [ + { + "kind": "t1", + "data": { + "total_awards_received": 0, + "approved_at_utc": null, + "ups": 1, + "awarders": [], + "mod_reason_by": null, + "banned_by": null, + "author_flair_type": "text", + "removal_reason": null, + "link_id": "t3_testpost", + "author_flair_template_id": null, + "likes": null, + "replies": { + "kind": "Listing", + "data": { + "modhash": null, + "dist": null, + "children": [ + { + "kind": "t1", + "data": { + "total_awards_received": 0, + "approved_at_utc": null, + "ups": 1, + "awarders": [], + "mod_reason_by": null, + "banned_by": null, + "author_flair_type": "text", + "removal_reason": null, + "link_id": "t3_testpost", + "author_flair_template_id": null, + "likes": null, + "replies": "", + "user_reports": [], + "saved": false, + "id": "testc2", + "banned_at_utc": null, + "mod_reason_title": null, + "gilded": 0, + "archived": false, + "no_follow": true, + "author": "testuser", + "can_mod_post": false, + "send_replies": true, + "parent_id": "t1_testc1", + "score": 1, + "author_fullname": "t2_testuser", + "report_reasons": null, + "approved_by": null, + "all_awardings": [], + "subreddit_id": "t5_2qh23", + "collapsed": false, + "body": "Hello", + "edited": false, + "author_flair_css_class": null, + "is_submitter": true, + "downs": 0, + "author_flair_richtext": [], + "author_patreon_flair": false, + "body_html": "<div class=\"md\"><p>Hello</p>\n</div>", + "gildings": {}, + "collapsed_reason": null, + "associated_award": null, + "stickied": false, + "author_premium": false, + "subreddit_type": "public", + "can_gild": true, + "top_awarded_type": null, + "author_flair_text_color": null, + "score_hidden": false, + "permalink": "/r/test/comments/testpost/test/testc2/", + "num_reports": null, + "locked": false, + "name": "t1_testc2", + "created": 1595097148.0, + "subreddit": "test", + "author_flair_text": null, + "treatment_tags": [], + "created_utc": 1595068348.0, + "subreddit_name_prefixed": "r/test", + "controversiality": 0, + "depth": 1, + "author_flair_background_color": null, + "collapsed_because_crowd_control": null, + "mod_reports": [], + "mod_note": null, + "distinguished": null + } + } + ], + "after": null, + "before": null + } + }, + "user_reports": [], + "saved": false, + "id": "testc1", + "banned_at_utc": null, + "mod_reason_title": null, + "gilded": 0, + "archived": false, + "no_follow": true, + "author": "testuser", + "can_mod_post": false, + "send_replies": true, + "parent_id": "t3_testpost", + "score": 1, + "author_fullname": "t2_testuser", + "report_reasons": null, + "approved_by": null, + "all_awardings": [], + "subreddit_id": "t5_2qh23", + "body": "Hi", + "edited": false, + "downs": 0, + "author_flair_css_class": null, + "is_submitter": true, + "collapsed": false, + "author_flair_richtext": [], + "author_patreon_flair": false, + "body_html": "<div class=\"md\"><p>Hi</p>\n</div>", + "gildings": {}, + "collapsed_reason": null, + "associated_award": null, + "stickied": false, + "author_premium": false, + "subreddit_type": "public", + "can_gild": true, + "top_awarded_type": null, + "author_flair_text_color": null, + "score_hidden": false, + "permalink": "/r/test/comments/testpost/test/testc1/", + "num_reports": null, + "locked": false, + "name": "t1_testc1", + "created": 1595097119.0, + "subreddit": "test", + "author_flair_text": null, + "treatment_tags": [], + "created_utc": 1595068319.0, + "subreddit_name_prefixed": "r/test", + "controversiality": 0, + "depth": 0, + "author_flair_background_color": null, + "collapsed_because_crowd_control": null, + "mod_reports": [], + "mod_note": null, + "distinguished": null + } + } + ], + "after": null, + "before": null + } + } +]