Add Options struct for listing mod actions
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
304b02f47d
commit
2b495417b6
3 changed files with 26 additions and 42 deletions
|
@ -40,34 +40,14 @@ type ModAction struct {
|
|||
}
|
||||
|
||||
// GetActions gets a list of moderator actions on a subreddit.
|
||||
// By default, the limit parameter is 25; max is 500.
|
||||
func (s *ModerationService) GetActions(ctx context.Context, subreddit string, opts *ListOptions) (*ModActions, *Response, error) {
|
||||
return s.GetActionsByType(ctx, subreddit, "", opts)
|
||||
}
|
||||
|
||||
// GetActionsByType gets a list of moderator actions of the specified type on a subreddit.
|
||||
// By default, the limit parameter is 25; max is 500.
|
||||
// The type should be one of: banuser, unbanuser, spamlink, removelink, approvelink, spamcomment,
|
||||
// removecomment, approvecomment, addmoderator, showcomment, invitemoderator, uninvitemoderator,
|
||||
// acceptmoderatorinvite, removemoderator, addcontributor, removecontributor, editsettings, editflair,
|
||||
// distinguish, marknsfw, wikibanned, wikicontributor, wikiunbanned, wikipagelisted, removewikicontributor,
|
||||
// wikirevise, wikipermlevel, ignorereports, unignorereports, setpermissions, setsuggestedsort, sticky,
|
||||
// unsticky, setcontestmode, unsetcontestmode, lock, unlock, muteuser, unmuteuser, createrule, editrule,
|
||||
// reorderrules, deleterule, spoiler, unspoiler, modmail_enrollment, community_styling, community_widgets,
|
||||
// markoriginalcontent, collections, events, hidden_award, add_community_topics, remove_community_topics,
|
||||
// create_scheduled_post, edit_scheduled_post, delete_scheduled_post, submit_scheduled_post, edit_post_requirements,
|
||||
// invitesubscriber, submit_content_rating_survey.
|
||||
func (s *ModerationService) GetActionsByType(ctx context.Context, subreddit string, actionType string, opts *ListOptions) (*ModActions, *Response, error) {
|
||||
func (s *ModerationService) GetActions(ctx context.Context, subreddit string, opts *ListModActionOptions) (*ModActions, *Response, error) {
|
||||
path := fmt.Sprintf("r/%s/about/log", subreddit)
|
||||
path, err := addOptions(path, opts)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
type params struct {
|
||||
Type string `url:"type,omitempty"`
|
||||
}
|
||||
path, err = addOptions(path, params{actionType})
|
||||
path, err = addOptions(path, opts)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var expectedModActionsResult = &ModActions{
|
||||
var expectedModActions = &ModActions{
|
||||
ModActions: []*ModAction{
|
||||
{
|
||||
ID: "ModAction_b4e7979a-c4ad-11ea-8440-0ea1b7c2b8f9",
|
||||
|
@ -56,28 +56,12 @@ func TestModerationService_GetActions(t *testing.T) {
|
|||
blob, err := readFileContents("testdata/moderation/actions.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
mux.HandleFunc("/r/testsubreddit/about/log", func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, http.MethodGet, r.Method)
|
||||
fmt.Fprint(w, blob)
|
||||
})
|
||||
|
||||
result, _, err := client.Moderation.GetActions(ctx, "testsubreddit", nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedModActionsResult, result)
|
||||
}
|
||||
|
||||
func TestModerationService_GetActionsByType(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
blob, err := readFileContents("testdata/moderation/actions.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
mux.HandleFunc("/r/testsubreddit/about/log", func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, http.MethodGet, r.Method)
|
||||
|
||||
form := url.Values{}
|
||||
form.Set("type", "testtype")
|
||||
form.Set("mod", "testmod")
|
||||
|
||||
err := r.ParseForm()
|
||||
require.NoError(t, err)
|
||||
|
@ -86,9 +70,9 @@ func TestModerationService_GetActionsByType(t *testing.T) {
|
|||
fmt.Fprint(w, blob)
|
||||
})
|
||||
|
||||
result, _, err := client.Moderation.GetActionsByType(ctx, "testsubreddit", "testtype", nil)
|
||||
modActions, _, err := client.Moderation.GetActions(ctx, "testsubreddit", &ListModActionOptions{Type: "testtype", Moderator: "testmod"})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedModActionsResult, result)
|
||||
require.Equal(t, expectedModActions, modActions)
|
||||
}
|
||||
|
||||
func TestModerationService_AcceptInvite(t *testing.T) {
|
||||
|
|
20
reddit.go
20
reddit.go
|
@ -389,6 +389,26 @@ type ListUserOverviewOptions struct {
|
|||
Time string `url:"t,omitempty"`
|
||||
}
|
||||
|
||||
// ListModActionOptions defines possible options used when getting moderation actions in a subreddit.
|
||||
type ListModActionOptions struct {
|
||||
// The max for the limit parameter here is 500.
|
||||
ListOptions
|
||||
// If empty, the search will return all action types.
|
||||
// One of: banuser, unbanuser, spamlink, removelink, approvelink, spamcomment, removecomment,
|
||||
// approvecomment, addmoderator, showcomment, invitemoderator, uninvitemoderator, acceptmoderatorinvite,
|
||||
// removemoderator, addcontributor, removecontributor, editsettings, editflair, distinguish, marknsfw,
|
||||
// wikibanned, wikicontributor, wikiunbanned, wikipagelisted, removewikicontributor, wikirevise,
|
||||
// wikipermlevel, ignorereports, unignorereports, setpermissions, setsuggestedsort, sticky, unsticky,
|
||||
// setcontestmode, unsetcontestmode, lock, unlock, muteuser, unmuteuser, createrule, editrule,
|
||||
// reorderrules, deleterule, spoiler, unspoiler, modmail_enrollment, community_styling, community_widgets,
|
||||
// markoriginalcontent, collections, events, hidden_award, add_community_topics, remove_community_topics,
|
||||
// create_scheduled_post, edit_scheduled_post, delete_scheduled_post, submit_scheduled_post,
|
||||
// edit_post_requirements, invitesubscriber, submit_content_rating_survey.
|
||||
Type string `url:"type,omitempty"`
|
||||
// If provided, only return the actions of this moderator.
|
||||
Moderator string `url:"mod,omitempty"`
|
||||
}
|
||||
|
||||
func addOptions(s string, opt interface{}) (string, error) {
|
||||
v := reflect.ValueOf(opt)
|
||||
if v.Kind() == reflect.Ptr && v.IsNil() {
|
||||
|
|
Loading…
Reference in a new issue