Set mod permissions, remove some duplicate code
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
1eb2acf318
commit
3c9ea61859
2 changed files with 75 additions and 148 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/google/go-querystring/query"
|
"github.com/google/go-querystring/query"
|
||||||
)
|
)
|
||||||
|
@ -216,15 +217,15 @@ func (s *ModerationService) UnignoreReports(ctx context.Context, id string) (*Re
|
||||||
// ModPermissions are the different permissions moderators have or don't have on a subreddit.
|
// ModPermissions are the different permissions moderators have or don't have on a subreddit.
|
||||||
// Read about them here: https://mods.reddithelp.com/hc/en-us/articles/360009381491-User-Management-moderators-and-permissions
|
// Read about them here: https://mods.reddithelp.com/hc/en-us/articles/360009381491-User-Management-moderators-and-permissions
|
||||||
type ModPermissions struct {
|
type ModPermissions struct {
|
||||||
All bool
|
All bool `permission:"all"`
|
||||||
Access bool
|
Access bool `permission:"access"`
|
||||||
ChatConfig bool
|
ChatConfig bool `permission:"chat_config"`
|
||||||
ChatOperator bool
|
ChatOperator bool `permission:"chat_operator"`
|
||||||
Config bool
|
Config bool `permission:"config"`
|
||||||
Flair bool
|
Flair bool `permission:"flair"`
|
||||||
Mail bool
|
Mail bool `permission:"mail"`
|
||||||
Posts bool
|
Posts bool `permission:"posts"`
|
||||||
Wiki bool
|
Wiki bool `permission:"wiki"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ModPermissions) String() (s string) {
|
func (p *ModPermissions) String() (s string) {
|
||||||
|
@ -232,68 +233,29 @@ func (p *ModPermissions) String() (s string) {
|
||||||
return "+all"
|
return "+all"
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.All {
|
t := reflect.TypeOf(*p)
|
||||||
s += "+"
|
v := reflect.ValueOf(*p)
|
||||||
} else {
|
|
||||||
s += "-"
|
|
||||||
}
|
|
||||||
s += "all,"
|
|
||||||
|
|
||||||
if p.Access {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
s += "+"
|
if v.Field(i).Kind() != reflect.Bool {
|
||||||
} else {
|
continue
|
||||||
s += "-"
|
}
|
||||||
}
|
|
||||||
s += "access,"
|
|
||||||
|
|
||||||
if p.ChatConfig {
|
permission := t.Field(i).Tag.Get("permission")
|
||||||
s += "+"
|
permitted := v.Field(i).Bool()
|
||||||
} else {
|
|
||||||
s += "-"
|
|
||||||
}
|
|
||||||
s += "chat_config,"
|
|
||||||
|
|
||||||
if p.ChatOperator {
|
if permitted {
|
||||||
s += "+"
|
s += "+"
|
||||||
} else {
|
} else {
|
||||||
s += "-"
|
s += "-"
|
||||||
}
|
}
|
||||||
s += "chat_operator,"
|
|
||||||
|
|
||||||
if p.Config {
|
s += permission
|
||||||
s += "+"
|
|
||||||
} else {
|
|
||||||
s += "-"
|
|
||||||
}
|
|
||||||
s += "config,"
|
|
||||||
|
|
||||||
if p.Flair {
|
if i != t.NumField()-1 {
|
||||||
s += "+"
|
s += ","
|
||||||
} else {
|
}
|
||||||
s += "-"
|
|
||||||
}
|
}
|
||||||
s += "flair,"
|
|
||||||
|
|
||||||
if p.Mail {
|
|
||||||
s += "+"
|
|
||||||
} else {
|
|
||||||
s += "-"
|
|
||||||
}
|
|
||||||
s += "mail,"
|
|
||||||
|
|
||||||
if p.Posts {
|
|
||||||
s += "+"
|
|
||||||
} else {
|
|
||||||
s += "-"
|
|
||||||
}
|
|
||||||
s += "posts,"
|
|
||||||
|
|
||||||
if p.Wiki {
|
|
||||||
s += "+"
|
|
||||||
} else {
|
|
||||||
s += "-"
|
|
||||||
}
|
|
||||||
s += "wiki"
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -319,12 +281,19 @@ func (s *ModerationService) Invite(ctx context.Context, subreddit string, userna
|
||||||
|
|
||||||
// Uninvite a user from becoming a moderator of the subreddit.
|
// Uninvite a user from becoming a moderator of the subreddit.
|
||||||
func (s *ModerationService) Uninvite(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) Uninvite(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
return s.deleteRelationship(ctx, subreddit, username, "moderator_invite")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPermissions sets the mod permissions for the user in the subreddit.
|
||||||
|
// If permissions is nil, all permissions will be granted.
|
||||||
|
func (s *ModerationService) SetPermissions(ctx context.Context, subreddit string, username string, permissions *ModPermissions) (*Response, error) {
|
||||||
|
path := fmt.Sprintf("r/%s/api/setpermissions", subreddit)
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("api_type", "json")
|
form.Set("api_type", "json")
|
||||||
form.Set("name", username)
|
form.Set("name", username)
|
||||||
form.Set("type", "moderator_invite")
|
form.Set("type", "moderator_invite")
|
||||||
|
form.Set("permissions", permissions.String())
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -368,19 +337,7 @@ func (s *ModerationService) Ban(ctx context.Context, subreddit string, username
|
||||||
|
|
||||||
// Unban a user from the subreddit.
|
// Unban a user from the subreddit.
|
||||||
func (s *ModerationService) Unban(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) Unban(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
return s.deleteRelationship(ctx, subreddit, username, "banned")
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("api_type", "json")
|
|
||||||
form.Set("name", username)
|
|
||||||
form.Set("type", "banned")
|
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.client.Do(ctx, req, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BanWiki a user from contributing to the subreddit wiki.
|
// BanWiki a user from contributing to the subreddit wiki.
|
||||||
|
@ -406,97 +363,46 @@ func (s *ModerationService) BanWiki(ctx context.Context, subreddit string, usern
|
||||||
|
|
||||||
// UnbanWiki a user from contributing to the subreddit wiki.
|
// UnbanWiki a user from contributing to the subreddit wiki.
|
||||||
func (s *ModerationService) UnbanWiki(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) UnbanWiki(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
return s.deleteRelationship(ctx, subreddit, username, "wikibanned")
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("api_type", "json")
|
|
||||||
form.Set("name", username)
|
|
||||||
form.Set("type", "wikibanned")
|
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.client.Do(ctx, req, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mute a user in the subreddit.
|
// Mute a user in the subreddit.
|
||||||
func (s *ModerationService) Mute(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) Mute(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/friend", subreddit)
|
return s.createRelationship(ctx, subreddit, username, "muted")
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("api_type", "json")
|
|
||||||
form.Set("name", username)
|
|
||||||
form.Set("type", "muted")
|
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.client.Do(ctx, req, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmute a user in the subreddit.
|
// Unmute a user in the subreddit.
|
||||||
func (s *ModerationService) Unmute(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) Unmute(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
return s.deleteRelationship(ctx, subreddit, username, "muted")
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("api_type", "json")
|
|
||||||
form.Set("name", username)
|
|
||||||
form.Set("type", "muted")
|
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.client.Do(ctx, req, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApproveUser adds a user as an approved user to the subreddit.
|
// ApproveUser adds a user as an approved user to the subreddit.
|
||||||
func (s *ModerationService) ApproveUser(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) ApproveUser(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/friend", subreddit)
|
return s.createRelationship(ctx, subreddit, username, "contributor")
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("api_type", "json")
|
|
||||||
form.Set("name", username)
|
|
||||||
form.Set("type", "contributor")
|
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.client.Do(ctx, req, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnapproveUser removes a user as an approved user to the subreddit.
|
// UnapproveUser removes a user as an approved user to the subreddit.
|
||||||
func (s *ModerationService) UnapproveUser(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) UnapproveUser(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
return s.deleteRelationship(ctx, subreddit, username, "contributor")
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("api_type", "json")
|
|
||||||
form.Set("name", username)
|
|
||||||
form.Set("type", "contributor")
|
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.client.Do(ctx, req, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApproveUserWiki adds a user as an approved wiki contributor in the subreddit.
|
// ApproveUserWiki adds a user as an approved wiki contributor in the subreddit.
|
||||||
func (s *ModerationService) ApproveUserWiki(ctx context.Context, subreddit string, username string) (*Response, error) {
|
func (s *ModerationService) ApproveUserWiki(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
|
return s.createRelationship(ctx, subreddit, username, "wikicontributor")
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnapproveUserWiki removes a user as an approved wiki contributor in the subreddit.
|
||||||
|
func (s *ModerationService) UnapproveUserWiki(ctx context.Context, subreddit string, username string) (*Response, error) {
|
||||||
|
return s.deleteRelationship(ctx, subreddit, username, "wikicontributor")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ModerationService) createRelationship(ctx context.Context, subreddit, username, relationship string) (*Response, error) {
|
||||||
path := fmt.Sprintf("r/%s/api/friend", subreddit)
|
path := fmt.Sprintf("r/%s/api/friend", subreddit)
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("api_type", "json")
|
form.Set("api_type", "json")
|
||||||
form.Set("name", username)
|
form.Set("name", username)
|
||||||
form.Set("type", "wikicontributor")
|
form.Set("type", relationship)
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -506,14 +412,13 @@ func (s *ModerationService) ApproveUserWiki(ctx context.Context, subreddit strin
|
||||||
return s.client.Do(ctx, req, nil)
|
return s.client.Do(ctx, req, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnapproveUserWiki removes a user as an approved wiki contributor in the subreddit.
|
func (s *ModerationService) deleteRelationship(ctx context.Context, subreddit, username, relationship string) (*Response, error) {
|
||||||
func (s *ModerationService) UnapproveUserWiki(ctx context.Context, subreddit string, username string) (*Response, error) {
|
|
||||||
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
path := fmt.Sprintf("r/%s/api/unfriend", subreddit)
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("api_type", "json")
|
form.Set("api_type", "json")
|
||||||
form.Set("name", username)
|
form.Set("name", username)
|
||||||
form.Set("type", "wikicontributor")
|
form.Set("type", relationship)
|
||||||
|
|
||||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -326,6 +326,28 @@ func TestModerationService_Uninvite(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestModerationService_SetPermissions(t *testing.T) {
|
||||||
|
client, mux, teardown := setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
mux.HandleFunc("/r/testsubreddit/api/setpermissions", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
require.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("api_type", "json")
|
||||||
|
form.Set("name", "testuser")
|
||||||
|
form.Set("type", "moderator_invite")
|
||||||
|
form.Set("permissions", "-all,+access,-chat_config,-chat_operator,-config,+flair,-mail,+posts,-wiki")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, form, r.PostForm)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := client.Moderation.SetPermissions(ctx, "testsubreddit", "testuser", &ModPermissions{Access: true, Flair: true, Posts: true})
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestModerationService_Ban(t *testing.T) {
|
func TestModerationService_Ban(t *testing.T) {
|
||||||
client, mux, teardown := setup()
|
client, mux, teardown := setup()
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
Loading…
Reference in a new issue