Use searcher/options to get user posts/comments
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
7b83e8366a
commit
946318c27b
3 changed files with 319 additions and 159 deletions
|
@ -306,8 +306,6 @@ func CheckResponse(r *http.Response) error {
|
||||||
|
|
||||||
// ListOptions are the optional parameters to the various endpoints that return lists
|
// ListOptions are the optional parameters to the various endpoints that return lists
|
||||||
type ListOptions struct {
|
type ListOptions struct {
|
||||||
Type string `url:"type,omitempty"` // links or comments
|
|
||||||
|
|
||||||
// For getting submissions
|
// For getting submissions
|
||||||
// all, year, month, week, day, hour
|
// all, year, month, week, day, hour
|
||||||
Timespan string `url:"t,omitempty"`
|
Timespan string `url:"t,omitempty"`
|
||||||
|
@ -315,7 +313,7 @@ type ListOptions struct {
|
||||||
// Common for all listing endpoints
|
// Common for all listing endpoints
|
||||||
After string `url:"after,omitempty"`
|
After string `url:"after,omitempty"`
|
||||||
Before string `url:"before,omitempty"`
|
Before string `url:"before,omitempty"`
|
||||||
Limit int `url:"limit,omitempty"` // default: 25
|
Limit int `url:"limit,omitempty"` // default: 25, max: 100
|
||||||
}
|
}
|
||||||
|
|
||||||
func addOptions(s string, opt interface{}) (string, error) {
|
func addOptions(s string, opt interface{}) (string, error) {
|
||||||
|
|
|
@ -181,6 +181,9 @@ type Comment struct {
|
||||||
ParentID string `json:"parent_id,omitempty"`
|
ParentID string `json:"parent_id,omitempty"`
|
||||||
Permalink string `json:"permalink,omitempty"`
|
Permalink string `json:"permalink,omitempty"`
|
||||||
|
|
||||||
|
Created *Timestamp `json:"created_utc,omitempty"`
|
||||||
|
Edited *Timestamp `json:"edited,omitempty"`
|
||||||
|
|
||||||
Body string `json:"body,omitempty"`
|
Body string `json:"body,omitempty"`
|
||||||
Author string `json:"author,omitempty"`
|
Author string `json:"author,omitempty"`
|
||||||
AuthorID string `json:"author_fullname,omitempty"`
|
AuthorID string `json:"author_fullname,omitempty"`
|
||||||
|
@ -198,9 +201,6 @@ type Comment struct {
|
||||||
Score int `json:"score"`
|
Score int `json:"score"`
|
||||||
Controversiality int `json:"controversiality"`
|
Controversiality int `json:"controversiality"`
|
||||||
|
|
||||||
Created *Timestamp `json:"created_utc,omitempty"`
|
|
||||||
Edited *Timestamp `json:"edited,omitempty"`
|
|
||||||
|
|
||||||
LinkID string `json:"link_id,omitempty"`
|
LinkID string `json:"link_id,omitempty"`
|
||||||
|
|
||||||
// These don't appear when submitting a comment
|
// These don't appear when submitting a comment
|
||||||
|
|
466
user.go
466
user.go
|
@ -14,18 +14,18 @@ type UserService interface {
|
||||||
GetMultipleByID(ctx context.Context, ids ...string) (map[string]*UserShort, *Response, error)
|
GetMultipleByID(ctx context.Context, ids ...string) (map[string]*UserShort, *Response, error)
|
||||||
UsernameAvailable(ctx context.Context, username string) (bool, *Response, error)
|
UsernameAvailable(ctx context.Context, username string) (bool, *Response, error)
|
||||||
|
|
||||||
Overview(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
|
Overview(opts ...SearchOpt) *UserCommentPostSearcher
|
||||||
OverviewOf(ctx context.Context, username string, opts *ListOptions) (*CommentsLinks, *Response, error)
|
OverviewOf(username string, opts ...SearchOpt) *UserCommentPostSearcher
|
||||||
|
|
||||||
GetPosts() *UserPostFinder
|
Posts(opts ...SearchOpt) *UserPostSearcher
|
||||||
GetPostsOf(username string) *UserPostFinder
|
PostsOf(username string, opts ...SearchOpt) *UserPostSearcher
|
||||||
|
|
||||||
GetComments() *UserCommentFinder
|
Comments(opts ...SearchOpt) *UserCommentSearcher
|
||||||
GetCommentsOf(username string) *UserCommentFinder
|
CommentsOf(username string, opts ...SearchOpt) *UserCommentSearcher
|
||||||
|
|
||||||
GetUpvoted() *UserPostFinder
|
GetUpvoted(opts ...SearchOpt) *UserPostSearcher
|
||||||
GetDownvoted() *UserPostFinder
|
GetDownvoted(opts ...SearchOpt) *UserPostSearcher
|
||||||
GetHidden() *UserPostFinder
|
GetHidden(opts ...SearchOpt) *UserPostSearcher
|
||||||
GetSaved(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
|
GetSaved(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
|
||||||
GetGilded(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
|
GetGilded(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
|
||||||
|
|
||||||
|
@ -142,61 +142,98 @@ func (s *UserServiceOp) UsernameAvailable(ctx context.Context, username string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overview returns a list of the client's comments and links
|
// Overview returns a list of the client's comments and links
|
||||||
func (s *UserServiceOp) Overview(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
|
func (s *UserServiceOp) Overview(opts ...SearchOpt) *UserCommentPostSearcher {
|
||||||
return s.OverviewOf(ctx, s.client.Username, opts)
|
return s.OverviewOf(s.client.Username, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OverviewOf returns a list of the user's comments and links
|
// OverviewOf returns a list of the user's comments and links
|
||||||
func (s *UserServiceOp) OverviewOf(ctx context.Context, username string, opts *ListOptions) (*CommentsLinks, *Response, error) {
|
func (s *UserServiceOp) OverviewOf(username string, opts ...SearchOpt) *UserCommentPostSearcher {
|
||||||
path := fmt.Sprintf("user/%s/overview", username)
|
sr := new(UserCommentPostSearcher)
|
||||||
return s.getCommentsAndLinks(ctx, path, opts)
|
sr.client = s.client
|
||||||
|
sr.username = username
|
||||||
|
sr.where = "overview"
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(sr)
|
||||||
|
}
|
||||||
|
return sr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPosts returns a list of the client's posts.
|
// Posts returns a list of the client's posts.
|
||||||
func (s *UserServiceOp) GetPosts() *UserPostFinder {
|
func (s *UserServiceOp) Posts(opts ...SearchOpt) *UserPostSearcher {
|
||||||
return s.GetPostsOf(s.client.Username)
|
return s.PostsOf(s.client.Username, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPostsOf returns a list of the user's posts.
|
// PostsOf returns a list of the user's posts.
|
||||||
func (s *UserServiceOp) GetPostsOf(username string) *UserPostFinder {
|
func (s *UserServiceOp) PostsOf(username string, opts ...SearchOpt) *UserPostSearcher {
|
||||||
return newUserPostFinder(s.client, username, "submitted")
|
sr := new(UserPostSearcher)
|
||||||
|
sr.client = s.client
|
||||||
|
sr.username = username
|
||||||
|
sr.where = "submitted"
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(sr)
|
||||||
|
}
|
||||||
|
return sr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetComments returns a list of the client's comments.
|
// Comments returns a list of the client's comments.
|
||||||
func (s *UserServiceOp) GetComments() *UserCommentFinder {
|
func (s *UserServiceOp) Comments(opts ...SearchOpt) *UserCommentSearcher {
|
||||||
return s.GetCommentsOf(s.client.Username)
|
return s.CommentsOf(s.client.Username, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCommentsOf returns a list of a user's comments.
|
// CommentsOf returns a list of a user's comments.
|
||||||
func (s *UserServiceOp) GetCommentsOf(username string) *UserCommentFinder {
|
func (s *UserServiceOp) CommentsOf(username string, opts ...SearchOpt) *UserCommentSearcher {
|
||||||
f := new(UserCommentFinder)
|
sr := new(UserCommentSearcher)
|
||||||
f.client = s.client
|
sr.client = s.client
|
||||||
f.username = username
|
sr.username = username
|
||||||
return f
|
for _, opt := range opts {
|
||||||
|
opt(sr)
|
||||||
|
}
|
||||||
|
return sr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUpvoted returns a list of the client's upvoted submissions
|
// GetUpvoted returns a list of the client's upvoted submissions.
|
||||||
func (s *UserServiceOp) GetUpvoted() *UserPostFinder {
|
func (s *UserServiceOp) GetUpvoted(opts ...SearchOpt) *UserPostSearcher {
|
||||||
return newUserPostFinder(s.client, s.client.Username, "upvoted")
|
sr := new(UserPostSearcher)
|
||||||
|
sr.client = s.client
|
||||||
|
sr.username = s.client.Username
|
||||||
|
sr.where = "upvoted"
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(sr)
|
||||||
|
}
|
||||||
|
return sr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDownvoted returns a list of the client's downvoted submissions
|
// GetDownvoted returns a list of the client's downvoted submissions.
|
||||||
func (s *UserServiceOp) GetDownvoted() *UserPostFinder {
|
func (s *UserServiceOp) GetDownvoted(opts ...SearchOpt) *UserPostSearcher {
|
||||||
return newUserPostFinder(s.client, s.client.Username, "downvoted")
|
sr := new(UserPostSearcher)
|
||||||
|
sr.client = s.client
|
||||||
|
sr.username = s.client.Username
|
||||||
|
sr.where = "downvoted"
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(sr)
|
||||||
|
}
|
||||||
|
return sr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHidden returns a list of the client's hidden submissions
|
// GetHidden returns a list of the client's hidden submissions.
|
||||||
func (s *UserServiceOp) GetHidden() *UserPostFinder {
|
func (s *UserServiceOp) GetHidden(opts ...SearchOpt) *UserPostSearcher {
|
||||||
return newUserPostFinder(s.client, s.client.Username, "hidden")
|
sr := new(UserPostSearcher)
|
||||||
|
sr.client = s.client
|
||||||
|
sr.username = s.client.Username
|
||||||
|
sr.where = "hidden"
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(sr)
|
||||||
|
}
|
||||||
|
return sr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSaved returns a list of the client's saved comments and links
|
// GetSaved returns a list of the client's saved comments and links.
|
||||||
func (s *UserServiceOp) GetSaved(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
|
func (s *UserServiceOp) GetSaved(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
|
||||||
path := fmt.Sprintf("user/%s/saved", s.client.Username)
|
path := fmt.Sprintf("user/%s/saved", s.client.Username)
|
||||||
return s.getCommentsAndLinks(ctx, path, opts)
|
return s.getCommentsAndLinks(ctx, path, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGilded returns a list of the client's gilded comments and links
|
// GetGilded returns a list of the client's gilded comments and links.
|
||||||
func (s *UserServiceOp) GetGilded(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
|
func (s *UserServiceOp) GetGilded(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
|
||||||
path := fmt.Sprintf("user/%s/gilded", s.client.Username)
|
path := fmt.Sprintf("user/%s/gilded", s.client.Username)
|
||||||
return s.getCommentsAndLinks(ctx, path, opts)
|
return s.getCommentsAndLinks(ctx, path, opts)
|
||||||
|
@ -303,135 +340,260 @@ func (s *UserServiceOp) getListing(ctx context.Context, path string, opts *ListO
|
||||||
return root, resp, err
|
return root, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserPostFinder finds the posts of a user.
|
// UserPostSearcher finds the posts of a user.
|
||||||
type UserPostFinder struct {
|
type UserPostSearcher struct {
|
||||||
client *Client
|
clientSearcher
|
||||||
username string
|
username string
|
||||||
// where can be submitted, upvoted, downvoted, hidden
|
// where can be submitted, upvoted, downvoted, hidden
|
||||||
// https://www.reddit.com/dev/api/#GET_user_{username}_{where}
|
// https://www.reddit.com/dev/api/#GET_user_{username}_{where}
|
||||||
where string
|
where string
|
||||||
opts struct {
|
after string
|
||||||
After string `url:"after,omitempty"`
|
Results []Link
|
||||||
Before string `url:"before,omitempty"`
|
|
||||||
Limit int `url:"limit,omitempty"`
|
|
||||||
Sort string `url:"sort,omitempty"`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newUserPostFinder(cli *Client, username string, where string) *UserPostFinder {
|
func (s *UserPostSearcher) search(ctx context.Context) (*Links, *Response, error) {
|
||||||
f := new(UserPostFinder)
|
path := fmt.Sprintf("user/%s/%s", s.username, s.where)
|
||||||
f.client = cli
|
root, resp, err := s.clientSearcher.Do(ctx, path)
|
||||||
f.username = username
|
|
||||||
f.where = where
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// After sets the after option.
|
|
||||||
func (f *UserPostFinder) After(after string) *UserPostFinder {
|
|
||||||
f.opts.After = after
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Before sets the before option.
|
|
||||||
func (f *UserPostFinder) Before(before string) *UserPostFinder {
|
|
||||||
f.opts.Before = before
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit sets the limit option.
|
|
||||||
func (f *UserPostFinder) Limit(limit int) *UserPostFinder {
|
|
||||||
f.opts.Limit = limit
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort sets the sort option.
|
|
||||||
func (f *UserPostFinder) Sort(sort Sort) *UserPostFinder {
|
|
||||||
f.opts.Sort = sort.String()
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do conducts the search.
|
|
||||||
func (f *UserPostFinder) Do(ctx context.Context) (*Links, *Response, error) {
|
|
||||||
path := fmt.Sprintf("user/%s/%s", f.username, f.where)
|
|
||||||
path, err := addOptions(path, f.opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := f.client.NewRequest(http.MethodGet, path, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
root := new(rootListing)
|
|
||||||
resp, err := f.client.Do(ctx, req, root)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return root.getLinks(), resp, nil
|
return root.getLinks(), resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserCommentFinder finds the comments of a user.
|
// Search runs the searcher.
|
||||||
type UserCommentFinder struct {
|
// The first return value tells the user if there are
|
||||||
client *Client
|
// more results that were cut off (due to the limit).
|
||||||
|
func (s *UserPostSearcher) Search(ctx context.Context) (bool, *Response, error) {
|
||||||
|
root, resp, err := s.search(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Results = root.Links
|
||||||
|
s.after = root.After
|
||||||
|
|
||||||
|
// if the "after" value is non-empty, it
|
||||||
|
// means there are more results to come.
|
||||||
|
moreResultsExist := s.after != ""
|
||||||
|
|
||||||
|
return moreResultsExist, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// More runs the searcher again and adds to the results.
|
||||||
|
// The first return value tells the user if there are
|
||||||
|
// more results that were cut off (due to the limit).
|
||||||
|
func (s *UserPostSearcher) More(ctx context.Context) (bool, *Response, error) {
|
||||||
|
if s.after == "" {
|
||||||
|
return s.Search(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.setAfter(s.after)
|
||||||
|
|
||||||
|
root, resp, err := s.search(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Results = append(s.Results, root.Links...)
|
||||||
|
s.after = root.After
|
||||||
|
|
||||||
|
// if the "after" value is non-empty, it
|
||||||
|
// means there are more results to come.
|
||||||
|
moreResultsExist := s.after != ""
|
||||||
|
|
||||||
|
return moreResultsExist, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// All runs the searcher until it yields no more results.
|
||||||
|
// The limit is set to 100, just to make the least amount
|
||||||
|
// of requests possible. It is reset to its original value after.
|
||||||
|
func (s *UserPostSearcher) All(ctx context.Context) error {
|
||||||
|
limit := s.opts.Limit
|
||||||
|
|
||||||
|
s.setLimit(100)
|
||||||
|
defer s.setLimit(limit)
|
||||||
|
|
||||||
|
var ok = true
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for ok {
|
||||||
|
ok, _, err = s.More(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserCommentSearcher finds the comments of a user.
|
||||||
|
type UserCommentSearcher struct {
|
||||||
|
clientSearcher
|
||||||
username string
|
username string
|
||||||
opts struct {
|
after string
|
||||||
After string `url:"after,omitempty"`
|
Results []Comment
|
||||||
Before string `url:"before,omitempty"`
|
|
||||||
Limit int `url:"limit,omitempty"`
|
|
||||||
Sort string `url:"sort,omitempty"`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OfUser specified the user we want to get the comments of.
|
func (s *UserCommentSearcher) search(ctx context.Context) (*Comments, *Response, error) {
|
||||||
func (f *UserCommentFinder) OfUser(username string) *UserCommentFinder {
|
path := fmt.Sprintf("user/%s/comments", s.username)
|
||||||
f.username = username
|
root, resp, err := s.clientSearcher.Do(ctx, path)
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// After sets the after option.
|
|
||||||
func (f *UserCommentFinder) After(after string) *UserCommentFinder {
|
|
||||||
f.opts.After = after
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Before sets the before option.
|
|
||||||
func (f *UserCommentFinder) Before(before string) *UserCommentFinder {
|
|
||||||
f.opts.Before = before
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit sets the limit option.
|
|
||||||
func (f *UserCommentFinder) Limit(limit int) *UserCommentFinder {
|
|
||||||
f.opts.Limit = limit
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort sets the sort option.
|
|
||||||
func (f *UserCommentFinder) Sort(sort Sort) *UserCommentFinder {
|
|
||||||
f.opts.Sort = sort.String()
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do conducts the search.
|
|
||||||
func (f *UserCommentFinder) Do(ctx context.Context) (*Comments, *Response, error) {
|
|
||||||
path := fmt.Sprintf("user/%s/comments", f.username)
|
|
||||||
path, err := addOptions(path, f.opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := f.client.NewRequest(http.MethodGet, path, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
root := new(rootListing)
|
|
||||||
resp, err := f.client.Do(ctx, req, root)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, resp, err
|
return nil, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return root.getComments(), resp, nil
|
return root.getComments(), resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search runs the searcher.
|
||||||
|
// The first return value tells the user if there are
|
||||||
|
// more results that were cut off (due to the limit).
|
||||||
|
func (s *UserCommentSearcher) Search(ctx context.Context) (bool, *Response, error) {
|
||||||
|
root, resp, err := s.search(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Results = root.Comments
|
||||||
|
s.after = root.After
|
||||||
|
|
||||||
|
// if the "after" value is non-empty, it
|
||||||
|
// means there are more results to come.
|
||||||
|
moreResultsExist := s.after != ""
|
||||||
|
|
||||||
|
return moreResultsExist, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// More runs the searcher again and adds to the results.
|
||||||
|
// The first return value tells the user if there are
|
||||||
|
// more results that were cut off (due to the limit).
|
||||||
|
func (s *UserCommentSearcher) More(ctx context.Context) (bool, *Response, error) {
|
||||||
|
if s.after == "" {
|
||||||
|
return s.Search(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.setAfter(s.after)
|
||||||
|
|
||||||
|
root, resp, err := s.search(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Results = append(s.Results, root.Comments...)
|
||||||
|
s.after = root.After
|
||||||
|
|
||||||
|
// if the "after" value is non-empty, it
|
||||||
|
// means there are more results to come.
|
||||||
|
moreResultsExist := s.after != ""
|
||||||
|
|
||||||
|
return moreResultsExist, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// All runs the searcher until it yields no more results.
|
||||||
|
// The limit is set to 100, just to make the least amount
|
||||||
|
// of requests possible. It is reset to its original value after.
|
||||||
|
func (s *UserCommentSearcher) All(ctx context.Context) error {
|
||||||
|
limit := s.opts.Limit
|
||||||
|
|
||||||
|
s.setLimit(100)
|
||||||
|
defer s.setLimit(limit)
|
||||||
|
|
||||||
|
var ok = true
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for ok {
|
||||||
|
ok, _, err = s.More(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserCommentPostSearcher finds the comments and posts of a user.
|
||||||
|
type UserCommentPostSearcher struct {
|
||||||
|
clientSearcher
|
||||||
|
username string
|
||||||
|
where string
|
||||||
|
after string
|
||||||
|
Results struct {
|
||||||
|
Comments []Comment `json:"comments"`
|
||||||
|
Posts []Link `json:"posts"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserCommentPostSearcher) search(ctx context.Context) (*Comments, *Links, *Response, error) {
|
||||||
|
path := fmt.Sprintf("user/%s/%s", s.username, s.where)
|
||||||
|
root, resp, err := s.clientSearcher.Do(ctx, path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, resp, err
|
||||||
|
}
|
||||||
|
return root.getComments(), root.getLinks(), resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search runs the searcher.
|
||||||
|
// The first return value tells the user if there are
|
||||||
|
// more results that were cut off (due to the limit).
|
||||||
|
func (s *UserCommentPostSearcher) Search(ctx context.Context) (bool, *Response, error) {
|
||||||
|
rootComments, rootPosts, resp, err := s.search(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Results.Comments = rootComments.Comments
|
||||||
|
s.Results.Posts = rootPosts.Links
|
||||||
|
s.after = rootComments.After
|
||||||
|
|
||||||
|
// if the "after" value is non-empty, it
|
||||||
|
// means there are more results to come.
|
||||||
|
moreResultsExist := s.after != ""
|
||||||
|
|
||||||
|
return moreResultsExist, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// More runs the searcher again and adds to the results.
|
||||||
|
// The first return value tells the user if there are
|
||||||
|
// more results that were cut off (due to the limit).
|
||||||
|
func (s *UserCommentPostSearcher) More(ctx context.Context) (bool, *Response, error) {
|
||||||
|
if s.after == "" {
|
||||||
|
return s.Search(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.setAfter(s.after)
|
||||||
|
|
||||||
|
rootComments, rootPosts, resp, err := s.search(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Results.Comments = append(s.Results.Comments, rootComments.Comments...)
|
||||||
|
s.Results.Posts = append(s.Results.Posts, rootPosts.Links...)
|
||||||
|
s.after = rootComments.After
|
||||||
|
|
||||||
|
// if the "after" value is non-empty, it
|
||||||
|
// means there are more results to come.
|
||||||
|
moreResultsExist := s.after != ""
|
||||||
|
|
||||||
|
return moreResultsExist, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// All runs the searcher until it yields no more results.
|
||||||
|
// The limit is set to 100, just to make the least amount
|
||||||
|
// of requests possible. It is reset to its original value after.
|
||||||
|
func (s *UserCommentPostSearcher) All(ctx context.Context) error {
|
||||||
|
limit := s.opts.Limit
|
||||||
|
|
||||||
|
s.setLimit(100)
|
||||||
|
defer s.setLimit(limit)
|
||||||
|
|
||||||
|
var ok = true
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for ok {
|
||||||
|
ok, _, err = s.More(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue