diff --git a/testdata/user/user-subreddits.json b/testdata/user/user-subreddits.json new file mode 100644 index 0000000..ed56be0 --- /dev/null +++ b/testdata/user/user-subreddits.json @@ -0,0 +1,207 @@ +{ + "kind": "Listing", + "data": { + "modhash": null, + "dist": 2, + "children": [ + { + "kind": "t5", + "data": { + "user_flair_background_color": null, + "submit_text_html": null, + "restrict_posting": true, + "user_flair_enabled_in_sr": false, + "user_is_banned": false, + "free_form_reports": true, + "wiki_enabled": null, + "user_is_muted": false, + "user_can_flair_in_sr": null, + "display_name": "u_nickofnight", + "header_img": null, + "title": "nickofnight", + "allow_galleries": true, + "icon_size": [256, 256], + "primary_color": "", + "active_user_count": null, + "icon_img": "https://styles.redditmedia.com/t5_3kefx/styles/profileIcon_w1vytyimts541.png?width=256&height=256&crop=256:256,smart&s=e722798c6253d3ae3990bf42c3ae844d7c2a924b", + "display_name_prefixed": "u/nickofnight", + "accounts_active": null, + "public_traffic": false, + "subscribers": 0, + "user_flair_richtext": [], + "videostream_links_count": 0, + "name": "t5_3kefx", + "quarantine": false, + "hide_ads": false, + "emojis_enabled": false, + "advertiser_category": "", + "public_description": "Stories written for Writing Prompts, NoSleep, and originals. Current series: The Carnival of Night ", + "comment_score_hide_mins": 0, + "user_has_favorited": false, + "user_flair_template_id": null, + "community_icon": "", + "banner_background_image": "", + "original_content_tag_enabled": false, + "submit_text": "", + "description_html": "<!-- SC_OFF --><div class=\"md\"><p>Stories from Writing Prompts, and a carefully curated selection of other works.</p>\n</div><!-- SC_ON -->", + "spoilers_enabled": true, + "header_title": null, + "header_size": null, + "user_flair_position": "right", + "is_default_icon": false, + "all_original_content": false, + "has_menu_widget": false, + "is_enrolled_in_new_modmail": null, + "key_color": "#222222", + "can_assign_user_flair": false, + "created": 1494549436.0, + "wls": null, + "show_media_preview": true, + "submission_type": "any", + "user_is_subscriber": false, + "disable_contributor_requests": false, + "allow_videogifs": true, + "user_flair_type": "text", + "allow_polls": true, + "collapse_deleted_comments": false, + "emojis_custom_size": null, + "public_description_html": "<!-- SC_OFF --><div class=\"md\"><p>Stories written for Writing Prompts, NoSleep, and originals. Current series: The Carnival of Night </p>\n</div><!-- SC_ON -->", + "allow_videos": true, + "is_crosspostable_subreddit": false, + "suggested_comment_sort": null, + "can_assign_link_flair": false, + "accounts_active_is_fuzzed": false, + "submit_text_label": null, + "link_flair_position": "", + "user_sr_flair_enabled": null, + "is_default_banner": false, + "allow_discovery": true, + "user_sr_theme_enabled": true, + "link_flair_enabled": false, + "subreddit_type": "user", + "notification_level": null, + "banner_img": "https://b.thumbs.redditmedia.com/9KgnD8_adeV_jCLhObwY-rhHrESHgTP9_JQLmIH_GWQ.png", + "user_flair_text": null, + "banner_background_color": "", + "show_media": true, + "id": "3kefx", + "user_is_contributor": false, + "over18": false, + "description": "Stories from Writing Prompts, and a carefully curated selection of other works.", + "submit_link_label": null, + "user_flair_text_color": null, + "restrict_commenting": false, + "user_flair_css_class": null, + "allow_images": true, + "lang": "en", + "whitelist_status": null, + "url": "/user/nickofnight/", + "created_utc": 1494520636.0, + "banner_size": [900, 270], + "mobile_banner_image": "", + "user_is_moderator": false + } + }, + { + "kind": "t5", + "data": { + "user_flair_background_color": null, + "submit_text_html": null, + "restrict_posting": true, + "user_flair_enabled_in_sr": false, + "user_is_banned": false, + "free_form_reports": true, + "wiki_enabled": null, + "user_is_muted": false, + "user_can_flair_in_sr": null, + "display_name": "u_shittymorph", + "header_img": null, + "title": "shittymorph", + "allow_galleries": true, + "icon_size": [256, 256], + "primary_color": "", + "active_user_count": null, + "icon_img": "https://styles.redditmedia.com/t5_3knn1/styles/profileIcon_b51xzp4vbvs41.jpg?width=256&height=256&crop=256:256,smart&s=6535d6f05d037d43d72217899d3f81aba4fb442d", + "display_name_prefixed": "u/shittymorph", + "accounts_active": null, + "public_traffic": false, + "subscribers": 0, + "user_flair_richtext": [], + "videostream_links_count": 0, + "name": "t5_3knn1", + "quarantine": false, + "hide_ads": false, + "emojis_enabled": false, + "advertiser_category": "", + "public_description": "In nineteen ninety eight the undertaker threw mankind off hеll in a cell, and plummeted sixteen feet through an announcer's table.", + "comment_score_hide_mins": 0, + "user_has_favorited": false, + "user_flair_template_id": null, + "community_icon": "", + "banner_background_image": "", + "original_content_tag_enabled": false, + "submit_text": "", + "description_html": null, + "spoilers_enabled": true, + "header_title": "", + "header_size": null, + "user_flair_position": "right", + "is_default_icon": false, + "all_original_content": false, + "has_menu_widget": false, + "is_enrolled_in_new_modmail": null, + "key_color": "", + "can_assign_user_flair": false, + "created": 1495102555.0, + "wls": null, + "show_media_preview": true, + "submission_type": "any", + "user_is_subscriber": false, + "disable_contributor_requests": false, + "allow_videogifs": true, + "user_flair_type": "text", + "allow_polls": true, + "collapse_deleted_comments": true, + "emojis_custom_size": null, + "public_description_html": "<!-- SC_OFF --><div class=\"md\"><p>In nineteen ninety eight the undertaker threw mankind off hеll in a cell, and plummeted sixteen feet through an announcer&#39;s table.</p>\n</div><!-- SC_ON -->", + "allow_videos": true, + "is_crosspostable_subreddit": false, + "suggested_comment_sort": "qa", + "can_assign_link_flair": false, + "accounts_active_is_fuzzed": false, + "submit_text_label": "", + "link_flair_position": "", + "user_sr_flair_enabled": null, + "is_default_banner": false, + "allow_discovery": true, + "user_sr_theme_enabled": true, + "link_flair_enabled": false, + "subreddit_type": "user", + "notification_level": null, + "banner_img": "https://b.thumbs.redditmedia.com/VjGAJxyj4OL3Ghb1TzrGFtf1QT3D-r1kX72q7uSv8iA.png", + "user_flair_text": null, + "banner_background_color": "", + "show_media": true, + "id": "3knn1", + "user_is_contributor": false, + "over18": false, + "description": "", + "submit_link_label": "", + "user_flair_text_color": null, + "restrict_commenting": false, + "user_flair_css_class": null, + "allow_images": true, + "lang": "en", + "whitelist_status": null, + "url": "/user/shittymorph/", + "created_utc": 1495073755.0, + "banner_size": [1280, 384], + "mobile_banner_image": "", + "user_is_moderator": false + } + } + ], + "after": "t5_3knn1", + "before": null + } +} diff --git a/user.go b/user.go index ee40029..62be962 100644 --- a/user.go +++ b/user.go @@ -515,3 +515,41 @@ func (s *UserService) TrophiesOf(ctx context.Context, username string) ([]Trophy return trophies, resp, nil } + +// Popular gets the user subreddits with the most activity. +func (s *UserService) Popular(ctx context.Context, opts ...SearchOptionSetter) (*Subreddits, *Response, error) { + form := newSearchOptions(opts...) + path := addQuery("users/popular", form) + + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + + root := new(rootListing) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root.getSubreddits(), resp, nil +} + +// New gets the most recently created user subreddits. +func (s *UserService) New(ctx context.Context, opts ...SearchOptionSetter) (*Subreddits, *Response, error) { + form := newSearchOptions(opts...) + path := addQuery("users/new", form) + + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + + root := new(rootListing) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root.getSubreddits(), resp, nil +} diff --git a/user_test.go b/user_test.go index ca14c0f..c596150 100644 --- a/user_test.go +++ b/user_test.go @@ -129,6 +129,37 @@ var expectedTrophies = []Trophy{ }, } +var expectedUserSubreddits = &Subreddits{ + Subreddits: []*Subreddit{ + { + ID: "3kefx", + FullID: "t5_3kefx", + Created: &Timestamp{time.Date(2017, 5, 11, 16, 37, 16, 0, time.UTC)}, + + URL: "/user/nickofnight/", + Name: "u_nickofnight", + NamePrefixed: "u/nickofnight", + Title: "nickofnight", + Description: "Stories written for Writing Prompts, NoSleep, and originals. Current series: The Carnival of Night ", + Type: "user", + }, + { + ID: "3knn1", + FullID: "t5_3knn1", + Created: &Timestamp{time.Date(2017, 5, 18, 2, 15, 55, 0, time.UTC)}, + + URL: "/user/shittymorph/", + Name: "u_shittymorph", + NamePrefixed: "u/shittymorph", + Title: "shittymorph", + Description: "In nineteen ninety eight the undertaker threw mankind off hеll in a cell, and plummeted sixteen feet through an announcer's table.", + Type: "user", + SuggestedCommentSort: "qa", + }, + }, + After: "t5_3knn1", +} + func TestUserService_Get(t *testing.T) { setup() defer teardown() @@ -829,3 +860,37 @@ func TestUserService_TrophiesOf(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expectedTrophies, trophies) } + +func TestUserService_Popular(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/user/user-subreddits.json") + assert.NoError(t, err) + + mux.HandleFunc("/users/popular", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + userSubreddits, _, err := client.User.Popular(ctx) + assert.NoError(t, err) + assert.Equal(t, expectedUserSubreddits, userSubreddits) +} + +func TestUserService_New(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/user/user-subreddits.json") + assert.NoError(t, err) + + mux.HandleFunc("/users/new", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + userSubreddits, _, err := client.User.New(ctx) + assert.NoError(t, err) + assert.Equal(t, expectedUserSubreddits, userSubreddits) +}