Tweak FlairService
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -14,25 +14,37 @@ type FlairService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// Flair is a flair on Reddit
|
||||
// Flair is a tag that can be attached to a user or a post.
|
||||
type Flair struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
CSS string `json:"css_class,omitempty"`
|
||||
ModOnly bool `json:"mod_only"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
|
||||
Color string `json:"text_color,omitempty"`
|
||||
BackgroundColor string `json:"background_color,omitempty"`
|
||||
CSSClass string `json:"css_class,omitempty"`
|
||||
|
||||
Editable bool `json:"text_editable"`
|
||||
ModOnly bool `json:"mod_only"`
|
||||
}
|
||||
|
||||
// GetFromSubreddit returns the flairs from the subreddit
|
||||
func (s *FlairService) GetFromSubreddit(ctx context.Context, name string) ([]Flair, *Response, error) {
|
||||
path := fmt.Sprintf("r/%s/api/user_flair_v2", name)
|
||||
// FlairSummary is a condensed version of Flair.
|
||||
type FlairSummary struct {
|
||||
User string `json:"user,omitempty"`
|
||||
Text string `json:"flair_text,omitempty"`
|
||||
CSSClass string `json:"flair_css_class,omitempty"`
|
||||
}
|
||||
|
||||
// GetUserFlairs returns the user flairs from the subreddit.
|
||||
func (s *FlairService) GetUserFlairs(ctx context.Context, subreddit string) ([]*Flair, *Response, error) {
|
||||
path := fmt.Sprintf("r/%s/api/user_flair_v2", subreddit)
|
||||
|
||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var flairs []Flair
|
||||
var flairs []*Flair
|
||||
resp, err := s.client.Do(ctx, req, &flairs)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
@@ -40,3 +52,41 @@ func (s *FlairService) GetFromSubreddit(ctx context.Context, name string) ([]Fla
|
||||
|
||||
return flairs, resp, nil
|
||||
}
|
||||
|
||||
// GetPostFlairs returns the post flairs from the subreddit.
|
||||
func (s *FlairService) GetPostFlairs(ctx context.Context, subreddit string) ([]*Flair, *Response, error) {
|
||||
path := fmt.Sprintf("r/%s/api/link_flair_v2", subreddit)
|
||||
|
||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var flairs []*Flair
|
||||
resp, err := s.client.Do(ctx, req, &flairs)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return flairs, resp, nil
|
||||
}
|
||||
|
||||
// ListUserFlairs returns all flairs of individual users in the subreddit.
|
||||
func (s *FlairService) ListUserFlairs(ctx context.Context, subreddit string) ([]*FlairSummary, *Response, error) {
|
||||
path := fmt.Sprintf("r/%s/api/flairlist", subreddit)
|
||||
|
||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var root struct {
|
||||
UserFlairs []*FlairSummary `json:"users"`
|
||||
}
|
||||
resp, err := s.client.Do(ctx, req, &root)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return root.UserFlairs, resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user