From 10a5d5ac863eaa8eb80a691dd76b30ddac3ffaa6 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Sat, 11 Jul 2020 23:26:14 -0400 Subject: [PATCH] Add method to get moderators of a subreddit Signed-off-by: Vartan Benohanian --- subreddit.go | 32 ++++++++++++++++++++++++++++++ subreddit_test.go | 21 ++++++++++++++++++++ testdata/subreddit/moderators.json | 25 +++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 testdata/subreddit/moderators.json diff --git a/subreddit.go b/subreddit.go index 3761ee2..92455ae 100644 --- a/subreddit.go +++ b/subreddit.go @@ -30,6 +30,20 @@ type SubredditShort struct { ActiveUsers int `json:"active_user_count"` } +type rootModeratorList struct { + Kind string `json:"kind,omitempty"` + Data struct { + Moderators []Moderator `json:"children"` + } `json:"data"` +} + +// Moderator is a user who moderates a subreddit. +type Moderator struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Permissions []string `json:"mod_permissions"` +} + // GetPosts returns posts. // By default, it'll look for the hottest posts from r/all. // Note: when looking for hot posts in a subreddit, it will include the @@ -327,3 +341,21 @@ func (f *PostFinder) Do(ctx context.Context) (*Posts, *Response, error) { return root.getPosts(), resp, nil } + +// Moderators returns the moderators of a subreddit. +func (s *SubredditService) Moderators(ctx context.Context, subreddit string) (interface{}, *Response, error) { + path := fmt.Sprintf("r/%s/about/moderators", subreddit) + + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + + root := new(rootModeratorList) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + return root.Data.Moderators, resp, nil +} diff --git a/subreddit_test.go b/subreddit_test.go index 330dfc0..da50f3d 100644 --- a/subreddit_test.go +++ b/subreddit_test.go @@ -113,6 +113,11 @@ var expectedSticky = &PostAndComments{ }, } +var expectedModerators = []Moderator{ + {ID: "t2_test1", Name: "testuser1", Permissions: []string{"all"}}, + {ID: "t2_test2", Name: "testuser2", Permissions: []string{"all"}}, +} + func TestSubredditService_GetByName(t *testing.T) { setup() defer teardown() @@ -264,3 +269,19 @@ func TestSubredditService_GetSticky1(t *testing.T) { // b, _ := json.MarshalIndent(sticky.Comments, "", " ") // fmt.Println(string(b)) } + +func TestSubredditService_Moderators(t *testing.T) { + setup() + defer teardown() + + blob := readFileContents(t, "testdata/subreddit/moderators.json") + + mux.HandleFunc("/r/test/about/moderators", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + moderators, _, err := client.Subreddit.Moderators(ctx, "test") + assert.NoError(t, err) + assert.Equal(t, expectedModerators, moderators) +} diff --git a/testdata/subreddit/moderators.json b/testdata/subreddit/moderators.json new file mode 100644 index 0000000..6e07def --- /dev/null +++ b/testdata/subreddit/moderators.json @@ -0,0 +1,25 @@ +{ + "kind": "UserList", + "data": { + "children": [ + { + "name": "testuser1", + "author_flair_text": "[test1]", + "mod_permissions": ["all"], + "date": 1375130667.0, + "rel_id": "rb_tmatb9", + "id": "t2_test1", + "author_flair_css_class": "test1" + }, + { + "name": "testuser2", + "author_flair_text": "[test2]", + "mod_permissions": ["all"], + "date": 1393697633.0, + "rel_id": "rb_5c9s4d", + "id": "t2_test2", + "author_flair_css_class": "test2" + } + ] + } +}