2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
2020-09-12 17:09:26 -04:00
|
|
|
"bytes"
|
2020-04-29 15:59:18 -04:00
|
|
|
"context"
|
2020-09-12 17:09:26 -04:00
|
|
|
"encoding/csv"
|
2020-09-08 20:00:44 -04:00
|
|
|
"errors"
|
2020-04-29 15:59:18 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-09-08 20:00:44 -04:00
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2020-04-29 15:59:18 -04:00
|
|
|
)
|
|
|
|
|
2020-06-27 23:53:59 -04:00
|
|
|
// FlairService handles communication with the flair
|
|
|
|
// related methods of the Reddit API.
|
|
|
|
//
|
|
|
|
// Reddit API docs: https://www.reddit.com/dev/api/#section_flair
|
2020-07-21 23:05:24 -04:00
|
|
|
type FlairService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-08-09 13:11:18 -04:00
|
|
|
// Flair is a tag that can be attached to a user or a post.
|
2020-04-29 15:59:18 -04:00
|
|
|
type Flair struct {
|
2020-08-09 13:11:18 -04:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"`
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-09-10 00:39:50 -04:00
|
|
|
// FlairChoice is a choice of flair when selecting one for yourself or for a post.
|
|
|
|
type FlairChoice struct {
|
|
|
|
TemplateID string `json:"flair_template_id"`
|
|
|
|
Text string `json:"flair_text"`
|
|
|
|
Editable bool `json:"flair_text_editable"`
|
|
|
|
Position string `json:"flair_position"`
|
|
|
|
CSSClass string `json:"flair_css_class"`
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:09:26 -04:00
|
|
|
// RequestConfigureFlair represents a request to configure a subreddit's flair settings.
|
2020-09-08 22:36:12 -04:00
|
|
|
// Not setting an attribute can have unexpected side effects, so assign every one just in case.
|
2020-09-12 17:09:26 -04:00
|
|
|
type RequestConfigureFlair struct {
|
2020-09-08 20:00:44 -04:00
|
|
|
// Enable user flair in the subreddit.
|
|
|
|
UserFlairEnabled *bool `url:"flair_enabled,omitempty"`
|
|
|
|
// One of: left, right.
|
|
|
|
UserFlairPosition string `url:"flair_position,omitempty"`
|
|
|
|
// Allow users to assign their own flair.
|
|
|
|
UserFlairSelfAssignEnabled *bool `url:"flair_self_assign_enabled,omitempty"`
|
2020-09-08 22:36:12 -04:00
|
|
|
|
2020-09-08 20:00:44 -04:00
|
|
|
// One of: none, left, right.
|
|
|
|
PostFlairPosition string `url:"link_flair_position,omitempty"`
|
|
|
|
// Allow submitters to assign their own post flair.
|
|
|
|
PostFlairSelfAssignEnabled *bool `url:"link_flair_self_assign_enabled,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:36:12 -04:00
|
|
|
// FlairTemplateCreateOrUpdateRequest represents a request to create/update a flair template.
|
|
|
|
// Not setting an attribute can have unexpected side effects, so assign every one just in case.
|
|
|
|
type FlairTemplateCreateOrUpdateRequest struct {
|
|
|
|
// The id of the template. Only provide this if it's an update request.
|
|
|
|
// If provided and it's not a valid id, the template will be created.
|
|
|
|
ID string `url:"flair_template_id,omitempty"`
|
|
|
|
|
|
|
|
// One of: all, emoji, text.
|
|
|
|
AllowableContent string `url:"allowable_content,omitempty"`
|
|
|
|
// No longer than 64 characters.
|
|
|
|
Text string `url:"text,omitempty"`
|
|
|
|
// One of: light, dark.
|
|
|
|
TextColor string `url:"text_color,omitempty"`
|
|
|
|
// Allow user to edit the text of the flair.
|
|
|
|
TextEditable *bool `url:"text_editable,omitempty"`
|
|
|
|
|
|
|
|
ModOnly *bool `url:"mod_only,omitempty"`
|
|
|
|
|
|
|
|
// Between 1 and 10 (inclusive). Default: 10.
|
|
|
|
MaxEmojis *int `url:"max_emojis,omitempty"`
|
|
|
|
|
|
|
|
// One of: none, transparent, 6-digit rgb hex color, e.g. #AABBCC.
|
|
|
|
BackgroundColor string `url:"background_color,omitempty"`
|
|
|
|
CSSClass string `url:"css_class,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlairTemplate is a generic flair structure that can users can use next to their username
|
|
|
|
// or posts in a subreddit.
|
|
|
|
type FlairTemplate struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
// USER_FLAIR (for users) or LINK_FLAIR (for posts).
|
|
|
|
Type string `json:"flairType"`
|
|
|
|
ModOnly bool `json:"modOnly"`
|
|
|
|
|
|
|
|
AllowableContent string `json:"allowableContent"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
TextType string `json:"type"`
|
|
|
|
TextColor string `json:"textColor"`
|
|
|
|
TextEditable bool `json:"textEditable"`
|
|
|
|
RichText []map[string]string `json:"richtext"`
|
|
|
|
|
|
|
|
OverrideCSS bool `json:"overrideCss"`
|
|
|
|
MaxEmojis int `json:"maxEmojis"`
|
|
|
|
BackgroundColor string `json:"backgroundColor"`
|
|
|
|
CSSClass string `json:"cssClass"`
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:41:17 -04:00
|
|
|
// FlairSelectRequest represents a request to select a flair.
|
|
|
|
type FlairSelectRequest struct {
|
|
|
|
// The id of the template.
|
|
|
|
ID string `url:"flair_template_id,omitempty"`
|
|
|
|
// No longer than 64 characters.
|
|
|
|
// Only use this if the flair is editable (it is by default if you're a mod of the subreddit).
|
|
|
|
Text string `url:"text,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:09:26 -04:00
|
|
|
// FlairChangeRequest represents a request to change a user's flair.
|
|
|
|
// If Text and CSSClass are empty, the request will just clear the user's flair.
|
|
|
|
type FlairChangeRequest struct {
|
|
|
|
User string
|
|
|
|
Text string
|
|
|
|
CSSClass string
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlairChangeResponse represents a response to a FlairChangeRequest.
|
|
|
|
type FlairChangeResponse struct {
|
|
|
|
// Whether or not the request was successful.
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Warnings map[string]string `json:"warnings,omitempty"`
|
|
|
|
Errors map[string]string `json:"errors,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-09 13:11:18 -04:00
|
|
|
// 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)
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-09 13:11:18 -04:00
|
|
|
var flairs []*Flair
|
2020-04-29 15:59:18 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, &flairs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return flairs, resp, nil
|
|
|
|
}
|
2020-08-09 13:11:18 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-09-08 20:00:44 -04:00
|
|
|
root := new(struct {
|
2020-08-09 13:11:18 -04:00
|
|
|
UserFlairs []*FlairSummary `json:"users"`
|
2020-09-08 20:00:44 -04:00
|
|
|
})
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2020-08-09 13:11:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.UserFlairs, resp, nil
|
|
|
|
}
|
2020-09-08 20:00:44 -04:00
|
|
|
|
|
|
|
// Configure the subreddit's flair settings.
|
2020-09-12 17:09:26 -04:00
|
|
|
func (s *FlairService) Configure(ctx context.Context, subreddit string, request *RequestConfigureFlair) (*Response, error) {
|
2020-09-08 20:00:44 -04:00
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request: cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("r/%s/api/flairconfig", subreddit)
|
|
|
|
|
|
|
|
form, err := query.Values(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:00:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable your flair in the subreddit.
|
|
|
|
func (s *FlairService) Enable(ctx context.Context, subreddit string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/setflairenabled", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_enabled", "true")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:00:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable your flair in the subreddit.
|
|
|
|
func (s *FlairService) Disable(ctx context.Context, subreddit string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/setflairenabled", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_enabled", "false")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:00:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-09-08 20:37:16 -04:00
|
|
|
|
2020-09-08 22:36:12 -04:00
|
|
|
// UpsertUserTemplate creates a user flair template, or updates it if the request.ID is valid.
|
|
|
|
// It returns the created/updated flair template.
|
|
|
|
func (s *FlairService) UpsertUserTemplate(ctx context.Context, subreddit string, request *FlairTemplateCreateOrUpdateRequest) (*FlairTemplate, *Response, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, nil, errors.New("request: cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("r/%s/api/flairtemplate_v2", subreddit)
|
|
|
|
|
|
|
|
form, err := query.Values(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_type", "USER_FLAIR")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 22:36:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(FlairTemplate)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpsertPostTemplate creates a post flair template, or updates it if the request.ID is valid.
|
|
|
|
// It returns the created/updated flair template.
|
|
|
|
func (s *FlairService) UpsertPostTemplate(ctx context.Context, subreddit string, request *FlairTemplateCreateOrUpdateRequest) (*FlairTemplate, *Response, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, nil, errors.New("request: cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("r/%s/api/flairtemplate_v2", subreddit)
|
|
|
|
|
|
|
|
form, err := query.Values(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_type", "LINK_FLAIR")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 22:36:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(FlairTemplate)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 20:37:16 -04:00
|
|
|
// Delete the flair of the user.
|
|
|
|
func (s *FlairService) Delete(ctx context.Context, subreddit, username string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/deleteflair", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("name", username)
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:37:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTemplate deletes the flair template via its id.
|
|
|
|
func (s *FlairService) DeleteTemplate(ctx context.Context, subreddit, id string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/deleteflairtemplate", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_template_id", id)
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:37:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAllUserTemplates deletes all user flair templates.
|
|
|
|
func (s *FlairService) DeleteAllUserTemplates(ctx context.Context, subreddit string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/clearflairtemplates", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_type", "USER_FLAIR")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:37:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAllPostTemplates deletes all post flair templates.
|
|
|
|
func (s *FlairService) DeleteAllPostTemplates(ctx context.Context, subreddit string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/clearflairtemplates", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("flair_type", "LINK_FLAIR")
|
|
|
|
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
2020-09-08 20:37:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-09-08 22:36:12 -04:00
|
|
|
|
|
|
|
// ReorderUserTemplates reorders the user flair templates in the order provided in the slice.
|
2020-09-10 00:39:50 -04:00
|
|
|
// The order should contain every single flair id of this flair type; omitting any id will result in an error.
|
2020-09-08 22:36:12 -04:00
|
|
|
func (s *FlairService) ReorderUserTemplates(ctx context.Context, subreddit string, ids []string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("api/v1/%s/flair_template_order/USER_FLAIR", subreddit)
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewJSONRequest(http.MethodPatch, path, ids)
|
2020-09-08 22:36:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReorderPostTemplates reorders the post flair templates in the order provided in the slice.
|
2020-09-10 00:39:50 -04:00
|
|
|
// The order should contain every single flair id of this flair type; omitting any id will result in an error.
|
2020-09-08 22:36:12 -04:00
|
|
|
func (s *FlairService) ReorderPostTemplates(ctx context.Context, subreddit string, ids []string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("api/v1/%s/flair_template_order/LINK_FLAIR", subreddit)
|
2020-09-09 23:02:06 -04:00
|
|
|
req, err := s.client.NewJSONRequest(http.MethodPatch, path, ids)
|
2020-09-08 22:36:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-09-10 00:39:50 -04:00
|
|
|
|
|
|
|
// Choices returns a list of flairs you can assign to yourself in the subreddit, and your current one.
|
|
|
|
func (s *FlairService) Choices(ctx context.Context, subreddit string) ([]*FlairChoice, *FlairChoice, *Response, error) {
|
|
|
|
return s.ChoicesOf(ctx, subreddit, s.client.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChoicesOf returns a list of flairs the user can assign to themself in the subreddit, and their current one.
|
|
|
|
// Unless the user is you, this only works if you're a moderator of the subreddit.
|
|
|
|
func (s *FlairService) ChoicesOf(ctx context.Context, subreddit, username string) ([]*FlairChoice, *FlairChoice, *Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/flairselector", subreddit)
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("name", username)
|
|
|
|
return s.choices(ctx, path, form)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChoicesForPost returns a list of flairs you can assign to an existing post, and the current one assigned to it.
|
|
|
|
// If the post isn't yours, this only works if you're the moderator of the subreddit it's in.
|
|
|
|
func (s *FlairService) ChoicesForPost(ctx context.Context, postID string) ([]*FlairChoice, *FlairChoice, *Response, error) {
|
|
|
|
path := "api/flairselector"
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("link", postID)
|
|
|
|
return s.choices(ctx, path, form)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChoicesForNewPost returns a list of flairs you can assign to a new post in a subreddit.
|
|
|
|
func (s *FlairService) ChoicesForNewPost(ctx context.Context, subreddit string) ([]*FlairChoice, *Response, error) {
|
|
|
|
path := fmt.Sprintf("r/%s/api/flairselector", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("is_newlink", "true")
|
|
|
|
|
|
|
|
choices, _, resp, err := s.choices(ctx, path, form)
|
|
|
|
return choices, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FlairService) choices(ctx context.Context, path string, form url.Values) ([]*FlairChoice, *FlairChoice, *Response, error) {
|
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(struct {
|
|
|
|
Choices []*FlairChoice `json:"choices"`
|
|
|
|
Current *FlairChoice `json:"current"`
|
|
|
|
})
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Choices, root.Current, resp, nil
|
|
|
|
}
|
2020-09-10 23:41:17 -04:00
|
|
|
|
|
|
|
// Select a flair to display next to your username in the subreddit.
|
|
|
|
func (s *FlairService) Select(ctx context.Context, subreddit string, request *FlairSelectRequest) (*Response, error) {
|
|
|
|
return s.Assign(ctx, subreddit, s.client.Username, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign a flair to another user in the subreddit.
|
|
|
|
// This only works if you're a moderator of the subreddit, or if the user is you.
|
|
|
|
func (s *FlairService) Assign(ctx context.Context, subreddit, user string, request *FlairSelectRequest) (*Response, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request: cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("r/%s/api/selectflair", subreddit)
|
|
|
|
|
|
|
|
form, err := query.Values(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("name", user)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectForPost assigns a flair to the post.
|
|
|
|
// If the post isn't yours, you have to be a moderator of the post's subreddit for this to work.
|
|
|
|
func (s *FlairService) SelectForPost(ctx context.Context, postID string, request *FlairSelectRequest) (*Response, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request: cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/selectflair"
|
|
|
|
|
|
|
|
form, err := query.Values(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("link", postID)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFromPost removes the flair from the post.
|
2020-09-12 17:09:26 -04:00
|
|
|
// If the post isn't yours, you have to be a moderator of the post's subreddit for this to work.
|
2020-09-10 23:41:17 -04:00
|
|
|
func (s *FlairService) RemoveFromPost(ctx context.Context, postID string) (*Response, error) {
|
|
|
|
path := "api/selectflair"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("link", postID)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.Do(ctx, req, nil)
|
|
|
|
}
|
2020-09-12 17:09:26 -04:00
|
|
|
|
|
|
|
// Change the flair of multiple users in the subreddit at once.
|
|
|
|
// You have to be a moderator of the subreddit for this to work.
|
|
|
|
func (s *FlairService) Change(ctx context.Context, subreddit string, requests []FlairChangeRequest) ([]*FlairChangeResponse, *Response, error) {
|
|
|
|
if len(requests) == 0 || len(requests) > 100 {
|
|
|
|
return nil, nil, errors.New("requests: must provide between 1 and 100")
|
|
|
|
}
|
|
|
|
|
|
|
|
records := make([][]string, len(requests))
|
|
|
|
for i, req := range requests {
|
|
|
|
records[i] = []string{req.User, req.Text, req.CSSClass}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := csv.NewWriter(buf)
|
|
|
|
|
|
|
|
err := w.WriteAll(records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Error()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("r/%s/api/flaircsv", subreddit)
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("flair_csv", buf.String())
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodPost, path, form)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var root []*FlairChangeResponse
|
|
|
|
resp, err := s.client.Do(ctx, req, &root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
|
|
|
}
|