From ead2077107418d4e2895da6ebef9756e28a518a4 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Sat, 1 Aug 2020 17:20:26 -0400 Subject: [PATCH] Edit search implementation, add method to get submission text Signed-off-by: Vartan Benohanian --- search.go | 7 ++++ subreddit.go | 54 +++++++++++++++---------- subreddit_test.go | 43 +++++++++++--------- testdata/subreddit/search.json | 74 ---------------------------------- 4 files changed, 65 insertions(+), 113 deletions(-) delete mode 100644 testdata/subreddit/search.json diff --git a/search.go b/search.go index 74b55ae..af0e459 100644 --- a/search.go +++ b/search.go @@ -86,10 +86,17 @@ func SortByTop(opts url.Values) { } // SortByRelevance sets the sort option to return the most relevant results first. +// This can be used when searching for subreddits and users. func SortByRelevance(opts url.Values) { opts.Set("sort", "relevance") } +// SortByActivity sets the sort option to return results with the most activity first. +// This can be used when searching for subreddits and users. +func SortByActivity(opts url.Values) { + opts.Set("sort", "activity") +} + // SortByNumberOfComments sets the sort option to return the results with the highest // number of comments first. func SortByNumberOfComments(opts url.Values) { diff --git a/subreddit.go b/subreddit.go index 520916d..066e522 100644 --- a/subreddit.go +++ b/subreddit.go @@ -22,17 +22,6 @@ type rootSubreddit struct { Data *Subreddit `json:"data,omitempty"` } -type rootSubredditInfoList struct { - Subreddits []*SubredditInfo `json:"subreddits,omitempty"` -} - -// SubredditInfo represents minimal information about a subreddit. -type SubredditInfo struct { - Name string `json:"name,omitempty"` - Subscribers int `json:"subscriber_count"` - ActiveUsers int `json:"active_user_count"` -} - type rootSubredditNames struct { Names []string `json:"names,omitempty"` } @@ -216,26 +205,24 @@ func (s *SubredditService) UnsubscribeByID(ctx context.Context, ids ...string) ( return s.handleSubscription(ctx, form) } -// Search searches for subreddits with names beginning with the query provided. -// They hold a very minimal amount of info. -func (s *SubredditService) Search(ctx context.Context, query string) ([]*SubredditInfo, *Response, error) { - path := "api/search_subreddits" +// Search searches for subreddits. +func (s *SubredditService) Search(ctx context.Context, query string, opts ...SearchOptionSetter) (*Subreddits, *Response, error) { + opts = append(opts, setQuery(query)) + form := newSearchOptions(opts...) - form := url.Values{} - form.Set("query", query) - - req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + path := addQuery("subreddits/search", form) + req, err := s.client.NewRequest(http.MethodGet, path, nil) if err != nil { return nil, nil, err } - root := new(rootSubredditInfoList) + root := new(rootListing) resp, err := s.client.Do(ctx, req, root) if err != nil { return nil, resp, err } - return root.Subreddits, resp, nil + return root.getSubreddits(), resp, nil } // SearchNames searches for subreddits with names beginning with the query provided. @@ -376,3 +363,28 @@ func (s *SubredditService) Random(ctx context.Context) (*Subreddit, *Response, e func (s *SubredditService) RandomNSFW(ctx context.Context) (*Subreddit, *Response, error) { return s.random(ctx, true) } + +// SubmissionText gets the submission text for the subreddit. +// This text is set by the subreddit moderators and intended to be displayed on the submission form. +func (s *SubredditService) SubmissionText(ctx context.Context, name string) (string, *Response, error) { + if name == "" { + return "", nil, errors.New("name: must not be empty") + } + + path := fmt.Sprintf("r/%s/api/submit_text", name) + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return "", nil, err + } + + type response struct { + Text string `json:"submit_text"` + } + root := new(response) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return "", resp, err + } + + return root.Text, resp, err +} diff --git a/subreddit_test.go b/subreddit_test.go index 348971c..cc578a3 100644 --- a/subreddit_test.go +++ b/subreddit_test.go @@ -140,17 +140,7 @@ var expectedSubreddits = &Subreddits{ }, } -var expectSubredditInfos = []*SubredditInfo{ - {Name: "golang", Subscribers: 119_722, ActiveUsers: 531}, - {Name: "golang_infosec", Subscribers: 1_776, ActiveUsers: 0}, - {Name: "GolangJobOfferings", Subscribers: 863, ActiveUsers: 1}, - {Name: "golang2", Subscribers: 626, ActiveUsers: 0}, - {Name: "GolangUnofficial", Subscribers: 239, ActiveUsers: 4}, - {Name: "golanguage", Subscribers: 247, ActiveUsers: 4}, - {Name: "golang_jobs", Subscribers: 16, ActiveUsers: 4}, -} - -var expectSubredditNames = []string{ +var expectedSubredditNames = []string{ "golang", "golang_infosec", "GolangJobOfferings", @@ -531,25 +521,25 @@ func TestSubredditService_Search(t *testing.T) { setup() defer teardown() - blob, err := readFileContents("testdata/subreddit/search.json") + blob, err := readFileContents("testdata/subreddit/list.json") assert.NoError(t, err) - mux.HandleFunc("/api/search_subreddits", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) + mux.HandleFunc("/subreddits/search", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) form := url.Values{} - form.Set("query", "golang") + form.Set("q", "golang") err := r.ParseForm() assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) + assert.Equal(t, form, r.Form) fmt.Fprint(w, blob) }) subreddits, _, err := client.Subreddit.Search(ctx, "golang") assert.NoError(t, err) - assert.Equal(t, expectSubredditInfos, subreddits) + assert.Equal(t, expectedSubreddits, subreddits) } func TestSubredditService_SearchNames(t *testing.T) { @@ -574,7 +564,7 @@ func TestSubredditService_SearchNames(t *testing.T) { names, _, err := client.Subreddit.SearchNames(ctx, "golang") assert.NoError(t, err) - assert.Equal(t, expectSubredditNames, names) + assert.Equal(t, expectedSubredditNames, names) } func TestSubredditService_Moderators(t *testing.T) { @@ -639,3 +629,20 @@ func TestSubredditService_RandomNSFW(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expectedRandomSubreddit, subreddit) } + +func TestSubredditService_SubmissionText(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/r/test/api/submit_text", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, `{ + "submit_text": "this is a test", + "submit_text_html": "" + }`) + }) + + text, _, err := client.Subreddit.SubmissionText(ctx, "test") + assert.NoError(t, err) + assert.Equal(t, "this is a test", text) +} diff --git a/testdata/subreddit/search.json b/testdata/subreddit/search.json deleted file mode 100644 index 707add0..0000000 --- a/testdata/subreddit/search.json +++ /dev/null @@ -1,74 +0,0 @@ -{ - "subreddits": [ - { - "active_user_count": 531, - "icon_img": "", - "key_color": "", - "name": "golang", - "subscriber_count": 119722, - "is_chat_post_feature_enabled": false, - "allow_chat_post_creation": false, - "allow_images": true - }, - { - "active_user_count": 0, - "icon_img": "", - "key_color": "", - "name": "golang_infosec", - "subscriber_count": 1776, - "is_chat_post_feature_enabled": true, - "allow_chat_post_creation": false, - "allow_images": true - }, - { - "active_user_count": 1, - "icon_img": "", - "key_color": "", - "name": "GolangJobOfferings", - "subscriber_count": 863, - "is_chat_post_feature_enabled": true, - "allow_chat_post_creation": false, - "allow_images": true - }, - { - "active_user_count": 0, - "icon_img": "", - "key_color": "#24a0ed", - "name": "golang2", - "subscriber_count": 626, - "is_chat_post_feature_enabled": false, - "allow_chat_post_creation": false, - "allow_images": true - }, - { - "active_user_count": 4, - "icon_img": "", - "key_color": "", - "name": "GolangUnofficial", - "subscriber_count": 239, - "is_chat_post_feature_enabled": false, - "allow_chat_post_creation": false, - "allow_images": true - }, - { - "active_user_count": 4, - "icon_img": "", - "key_color": "", - "name": "golanguage", - "subscriber_count": 247, - "is_chat_post_feature_enabled": false, - "allow_chat_post_creation": false, - "allow_images": true - }, - { - "active_user_count": 4, - "icon_img": "", - "key_color": "", - "name": "golang_jobs", - "subscriber_count": 16, - "is_chat_post_feature_enabled": false, - "allow_chat_post_creation": false, - "allow_images": true - } - ] -}