Add UpvotedOf and DownvotedOf methods

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-03 14:27:56 -04:00
parent 6ffcf70090
commit c199a4f712
2 changed files with 59 additions and 4 deletions

20
user.go
View File

@ -245,11 +245,17 @@ func (s *UserService) Saved(ctx context.Context, opts ...SearchOptionSetter) (*P
return root.getPosts(), root.getComments(), resp, nil return root.getPosts(), root.getComments(), resp, nil
} }
// Upvoted returns a list of the user's upvoted posts. // Upvoted returns a list of your upvoted posts.
func (s *UserService) Upvoted(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) { func (s *UserService) Upvoted(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.UpvotedOf(ctx, s.client.Username, opts...)
}
// UpvotedOf returns a list of the user's upvoted posts.
// The user's votes must be public for this to work.
func (s *UserService) UpvotedOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
form := newSearchOptions(opts...) form := newSearchOptions(opts...)
path := fmt.Sprintf("user/%s/upvoted", s.client.Username) path := fmt.Sprintf("user/%s/upvoted", username)
path = addQuery(path, form) path = addQuery(path, form)
req, err := s.client.NewRequest(http.MethodGet, path, nil) req, err := s.client.NewRequest(http.MethodGet, path, nil)
@ -266,11 +272,17 @@ func (s *UserService) Upvoted(ctx context.Context, opts ...SearchOptionSetter) (
return root.getPosts(), resp, nil return root.getPosts(), resp, nil
} }
// Downvoted returns a list of the user's downvoted posts. // Downvoted returns a list of your downvoted posts.
func (s *UserService) Downvoted(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) { func (s *UserService) Downvoted(ctx context.Context, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.DownvotedOf(ctx, s.client.Username, opts...)
}
// DownvotedOf returns a list of the user's downvoted posts.
// The user's votes must be public for this to work.
func (s *UserService) DownvotedOf(ctx context.Context, username string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
form := newSearchOptions(opts...) form := newSearchOptions(opts...)
path := fmt.Sprintf("user/%s/downvoted", s.client.Username) path := fmt.Sprintf("user/%s/downvoted", username)
path = addQuery(path, form) path = addQuery(path, form)
req, err := s.client.NewRequest(http.MethodGet, path, nil) req, err := s.client.NewRequest(http.MethodGet, path, nil)

View File

@ -490,6 +490,28 @@ func TestUserService_Upvoted_Options(t *testing.T) {
_, _, err := client.User.Upvoted(ctx, SetLimit(30), SetAfter("t3_after")) _, _, err := client.User.Upvoted(ctx, SetLimit(30), SetAfter("t3_after"))
assert.NoError(t, err) assert.NoError(t, err)
} }
func TestUserService_UpvotedOf(t *testing.T) {
setup()
defer teardown()
// we'll use this, similar payloads
blob := readFileContents(t, "testdata/user/submitted.json")
mux.HandleFunc("/user/user2/upvoted", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, blob)
})
posts, _, err := client.User.UpvotedOf(ctx, "user2")
assert.NoError(t, err)
assert.Len(t, posts.Posts, 1)
assert.Equal(t, expectedPost, posts.Posts[0])
assert.Equal(t, "t3_gczwql", posts.After)
assert.Equal(t, "", posts.Before)
}
func TestUserService_Downvoted(t *testing.T) { func TestUserService_Downvoted(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@ -536,6 +558,27 @@ func TestUserService_Downvoted_Options(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
func TestUserService_DownvotedOf(t *testing.T) {
setup()
defer teardown()
// we'll use this, similar payloads
blob := readFileContents(t, "testdata/user/submitted.json")
mux.HandleFunc("/user/user2/downvoted", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, blob)
})
posts, _, err := client.User.DownvotedOf(ctx, "user2")
assert.NoError(t, err)
assert.Len(t, posts.Posts, 1)
assert.Equal(t, expectedPost, posts.Posts[0])
assert.Equal(t, "t3_gczwql", posts.After)
assert.Equal(t, "", posts.Before)
}
func TestUserService_Hidden(t *testing.T) { func TestUserService_Hidden(t *testing.T) {
setup() setup()
defer teardown() defer teardown()