Add method to get moderators of a subreddit
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
8f95f3880a
commit
10a5d5ac86
3 changed files with 78 additions and 0 deletions
32
subreddit.go
32
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
25
testdata/subreddit/moderators.json
vendored
Normal file
25
testdata/subreddit/moderators.json
vendored
Normal file
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue