Tweak FlairService
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
a422ba631b
commit
25fd86d89f
5 changed files with 180 additions and 26 deletions
64
flair.go
64
flair.go
|
@ -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"`
|
||||
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
|
||||
}
|
||||
|
|
|
@ -8,36 +8,106 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var expectedFlairs = []Flair{
|
||||
var expectedUserFlairs = []*Flair{
|
||||
{
|
||||
ID: "b8a1c822-3feb-11e8-88e1-0e5f55d58ce0",
|
||||
Text: "Beginner",
|
||||
Type: "text",
|
||||
CSS: "Beginner1",
|
||||
Text: "Beginner",
|
||||
|
||||
Color: "dark",
|
||||
BackgroundColor: "",
|
||||
CSSClass: "Beginner1",
|
||||
|
||||
Editable: false,
|
||||
ModOnly: false,
|
||||
},
|
||||
{
|
||||
ID: "b8ea0fce-3feb-11e8-af7a-0e263a127cf8",
|
||||
Text: "Moderator",
|
||||
Type: "text",
|
||||
CSS: "Moderator1",
|
||||
|
||||
Color: "dark",
|
||||
BackgroundColor: "",
|
||||
CSSClass: "Moderator1",
|
||||
|
||||
Editable: false,
|
||||
ModOnly: true,
|
||||
},
|
||||
}
|
||||
|
||||
func TestFlairService_GetFlairs(t *testing.T) {
|
||||
var expectedPostFlairs = []*Flair{
|
||||
{
|
||||
ID: "305b503e-da60-11ea-9681-0e9f1d580d2d",
|
||||
Type: "richtext",
|
||||
Text: "test",
|
||||
|
||||
Color: "light",
|
||||
BackgroundColor: "#373c3f",
|
||||
CSSClass: "test",
|
||||
|
||||
Editable: false,
|
||||
ModOnly: true,
|
||||
},
|
||||
}
|
||||
|
||||
var expectedListUserFlairs = []*FlairSummary{
|
||||
{
|
||||
User: "TestUser1",
|
||||
Text: "TestFlair1",
|
||||
},
|
||||
{
|
||||
User: "TestUser2",
|
||||
Text: "TestFlair2",
|
||||
},
|
||||
}
|
||||
|
||||
func TestFlairService_GetUserFlairs(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
blob, err := readFileContents("testdata/flair/flairs.json")
|
||||
blob, err := readFileContents("testdata/flair/user-flairs.json")
|
||||
assert.NoError(t, err)
|
||||
|
||||
mux.HandleFunc("/r/subreddit/api/user_flair_v2", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("/r/testsubreddit/api/user_flair_v2", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
fmt.Fprint(w, blob)
|
||||
})
|
||||
|
||||
flairs, _, err := client.Flair.GetFromSubreddit(ctx, "subreddit")
|
||||
userFlairs, _, err := client.Flair.GetUserFlairs(ctx, "testsubreddit")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedFlairs, flairs)
|
||||
assert.Equal(t, expectedUserFlairs, userFlairs)
|
||||
}
|
||||
|
||||
func TestFlairService_GetPostFlairs(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
blob, err := readFileContents("testdata/flair/post-flairs.json")
|
||||
assert.NoError(t, err)
|
||||
|
||||
mux.HandleFunc("/r/testsubreddit/api/link_flair_v2", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
fmt.Fprint(w, blob)
|
||||
})
|
||||
|
||||
postFlairs, _, err := client.Flair.GetPostFlairs(ctx, "testsubreddit")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedPostFlairs, postFlairs)
|
||||
}
|
||||
|
||||
func TestFlairService_ListUserFlairs(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
blob, err := readFileContents("testdata/flair/list-user-flairs.json")
|
||||
assert.NoError(t, err)
|
||||
|
||||
mux.HandleFunc("/r/testsubreddit/api/flairlist", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
fmt.Fprint(w, blob)
|
||||
})
|
||||
|
||||
userFlairs, _, err := client.Flair.ListUserFlairs(ctx, "testsubreddit")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedListUserFlairs, userFlairs)
|
||||
}
|
||||
|
|
14
testdata/flair/list-user-flairs.json
vendored
Normal file
14
testdata/flair/list-user-flairs.json
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"users": [
|
||||
{
|
||||
"flair_css_class": null,
|
||||
"user": "TestUser1",
|
||||
"flair_text": "TestFlair1"
|
||||
},
|
||||
{
|
||||
"flair_css_class": null,
|
||||
"user": "TestUser2",
|
||||
"flair_text": "TestFlair2"
|
||||
}
|
||||
]
|
||||
}
|
20
testdata/flair/post-flairs.json
vendored
Normal file
20
testdata/flair/post-flairs.json
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
[
|
||||
{
|
||||
"type": "richtext",
|
||||
"text_editable": false,
|
||||
"allowable_content": "all",
|
||||
"text": "test",
|
||||
"max_emojis": 10,
|
||||
"text_color": "light",
|
||||
"mod_only": true,
|
||||
"css_class": "test",
|
||||
"richtext": [
|
||||
{
|
||||
"e": "text",
|
||||
"t": "test"
|
||||
}
|
||||
],
|
||||
"background_color": "#373c3f",
|
||||
"id": "305b503e-da60-11ea-9681-0e9f1d580d2d"
|
||||
}
|
||||
]
|
Loading…
Reference in a new issue