Add overview, saved endpoints. Delete duplicate code

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-05-03 18:54:41 -04:00
parent 7922711d51
commit 9e7cb10526
3 changed files with 210 additions and 158 deletions

View file

@ -564,15 +564,18 @@ type Subreddit struct {
UserIsMod bool `json:"user_is_moderator"`
}
func (rl *rootListing) getLinks() *LinkList {
func (rl *rootListing) getAfter() string {
if rl == nil || rl.Data == nil {
return nil
return ""
}
return &LinkList{
Links: rl.Data.Things.Links,
After: rl.Data.After,
Before: rl.Data.Before,
return rl.Data.After
}
func (rl *rootListing) getBefore() string {
if rl == nil || rl.Data == nil {
return ""
}
return rl.Data.Before
}
func (rl *rootListing) getComments() *CommentList {
@ -585,3 +588,25 @@ func (rl *rootListing) getComments() *CommentList {
Before: rl.Data.Before,
}
}
func (rl *rootListing) getLinks() *LinkList {
if rl == nil || rl.Data == nil {
return nil
}
return &LinkList{
Links: rl.Data.Things.Links,
After: rl.Data.After,
Before: rl.Data.Before,
}
}
func (rl *rootListing) getSubreddits() *SubredditList {
if rl == nil || rl.Data == nil {
return nil
}
return &SubredditList{
Subreddits: rl.Data.Things.Subreddits,
After: rl.Data.After,
Before: rl.Data.Before,
}
}

View file

@ -8,7 +8,7 @@ import (
// ListingsService handles communication with the link (post)
// related methods of the Reddit API
type ListingsService interface {
Get(ctx context.Context, ids ...string) (*Listing, *Response, error)
Get(ctx context.Context, ids ...string) (*CommentsLinksSubreddits, *Response, error)
}
// ListingsServiceOp implements the Vote interface
@ -35,10 +35,16 @@ type listingRoot struct {
// Subreddits []*Subreddit `json:"subreddits,omitempty"`
// }
// CommentsLinksSubreddits holds comments, links, and subreddits
type CommentsLinksSubreddits struct {
Comments []Comment `json:"comments,omitempty"`
Links []Link `json:"links,omitempty"`
Subreddits []Subreddit `json:"subreddits,omitempty"`
}
// Get gets a list of things based on their IDs
// Only links, comments, and subreddits are allowed
// todo: only links, comments, subreddits
func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*Listing, *Response, error) {
func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLinksSubreddits, *Response, error) {
type query struct {
IDs []string `url:"id,comma"`
}
@ -60,7 +66,12 @@ func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*Listing, *
return nil, resp, err
}
return root.Data, resp, nil
v := new(CommentsLinksSubreddits)
v.Comments = root.getComments().Comments
v.Links = root.getLinks().Links
v.Subreddits = root.getSubreddits().Subreddits
return v, resp, nil
}
// todo: do by_id next

312
user.go
View file

@ -14,6 +14,8 @@ type UserService interface {
GetMultipleByID(ctx context.Context, ids ...string) (map[string]*UserShort, *Response, error)
UsernameAvailable(ctx context.Context, username string) (bool, *Response, error)
Overview(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
// returns the client's links
GetHotLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error)
GetNewLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error)
@ -29,6 +31,8 @@ type UserService interface {
GetUpvoted(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error)
GetDownvoted(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error)
GetHidden(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error)
GetSaved(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
GetGilded(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error)
// returns the client's comments
GetHotComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error)
@ -42,6 +46,7 @@ type UserService interface {
GetTopCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error)
GetControversialCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error)
Friend(ctx context.Context, username string, note string) (interface{}, *Response, error)
Unblock(ctx context.Context, username string) (*Response, error)
Unfriend(ctx context.Context, username string) (*Response, error)
}
@ -86,6 +91,14 @@ type UserShort struct {
NSFW bool `json:"profile_over_18"`
}
// CommentsLinks holds comments and links
type CommentsLinks struct {
Comments []Comment `json:"comments,omitempty"`
Links []Link `json:"links,omitempty"`
After string `json:"after"`
Before string `json:"before"`
}
// Get returns information about the user
func (s *UserServiceOp) Get(ctx context.Context, username string) (*User, *Response, error) {
path := fmt.Sprintf("user/%s/about", username)
@ -157,6 +170,130 @@ func (s *UserServiceOp) UsernameAvailable(ctx context.Context, username string)
return *root, resp, nil
}
// Overview returns a list of the client's comments and links
func (s *UserServiceOp) Overview(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
path := fmt.Sprintf("user/%s/overview", s.client.Username)
return s.getCommentsAndLinks(ctx, path, opts)
}
// GetHotLinks returns a list of the client's hottest submissions
func (s *UserServiceOp) GetHotLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortHot])
return s.getLinks(ctx, path, opts)
}
// GetNewLinks returns a list of the client's newest submissions
func (s *UserServiceOp) GetNewLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortNew])
return s.getLinks(ctx, path, opts)
}
// GetTopLinks returns a list of the client's top submissions
func (s *UserServiceOp) GetTopLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortTop])
return s.getLinks(ctx, path, opts)
}
// GetControversialLinks returns a list of the client's most controversial submissions
func (s *UserServiceOp) GetControversialLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortControversial])
return s.getLinks(ctx, path, opts)
}
// GetHotLinksOf returns a list of the user's hottest submissions
func (s *UserServiceOp) GetHotLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", username, sorts[sortHot])
return s.getLinks(ctx, path, opts)
}
// GetNewLinksOf returns a list of the user's newest submissions
func (s *UserServiceOp) GetNewLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", username, sorts[sortNew])
return s.getLinks(ctx, path, opts)
}
// GetTopLinksOf returns a list of the user's top submissions
func (s *UserServiceOp) GetTopLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", username, sorts[sortTop])
return s.getLinks(ctx, path, opts)
}
// GetControversialLinksOf returns a list of the user's most controversial submissions
func (s *UserServiceOp) GetControversialLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", username, sorts[sortControversial])
return s.getLinks(ctx, path, opts)
}
// GetUpvoted returns a list of the client's upvoted submissions
func (s *UserServiceOp) GetUpvoted(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/upvoted", s.client.Username)
return s.getLinks(ctx, path, opts)
}
// GetDownvoted returns a list of the client's downvoted submissions
func (s *UserServiceOp) GetDownvoted(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/downvoted", s.client.Username)
return s.getLinks(ctx, path, opts)
}
// GetHidden returns a list of the client's hidden submissions
func (s *UserServiceOp) GetHidden(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/hidden", s.client.Username)
return s.getLinks(ctx, path, opts)
}
// GetSaved returns a list of the client's saved comments and links
func (s *UserServiceOp) GetSaved(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
path := fmt.Sprintf("user/%s/saved", s.client.Username)
return s.getCommentsAndLinks(ctx, path, opts)
}
// GetGilded returns a list of the client's gilded comments and links
func (s *UserServiceOp) GetGilded(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
path := fmt.Sprintf("user/%s/gilded", s.client.Username)
return s.getCommentsAndLinks(ctx, path, opts)
}
// GetHotComments returns a list of the client's hottest comments
func (s *UserServiceOp) GetHotComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortHot, opts)
}
// GetNewComments returns a list of the client's newest comments
func (s *UserServiceOp) GetNewComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortNew, opts)
}
// GetTopComments returns a list of the client's top comments
func (s *UserServiceOp) GetTopComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortTop, opts)
}
// GetControversialComments returns a list of the client's most controversial comments
func (s *UserServiceOp) GetControversialComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortControversial, opts)
}
// GetHotCommentsOf returns a list of the user's hottest comments
func (s *UserServiceOp) GetHotCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortHot, opts)
}
// GetNewCommentsOf returns a list of the user's newest comments
func (s *UserServiceOp) GetNewCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortNew, opts)
}
// GetTopCommentsOf returns a list of the user's top comments
func (s *UserServiceOp) GetTopCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortTop, opts)
}
// GetControversialCommentsOf returns a list of the user's most controversial comments
func (s *UserServiceOp) GetControversialCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortControversial, opts)
}
// Friend creates or updates a "friend" relationship
// Request body contains JSON data with:
// name: existing Reddit username
@ -207,154 +344,7 @@ func (s *UserServiceOp) Unfriend(ctx context.Context, username string) (*Respons
return s.client.Do(ctx, req, nil)
}
// GetUpvoted returns a list of the client's upvoted submissions
func (s *UserServiceOp) GetUpvoted(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/upvoted", s.client.Username)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.getLinks(), resp, nil
}
// GetDownvoted returns a list of the client's downvoted submissions
func (s *UserServiceOp) GetDownvoted(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/downvoted", s.client.Username)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.getLinks(), resp, nil
}
// GetHidden returns a list of the client's hidden submissions
func (s *UserServiceOp) GetHidden(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/hidden", s.client.Username)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.getLinks(), resp, nil
}
// GetHotLinks returns a list of the client's hottest submissions
func (s *UserServiceOp) GetHotLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, s.client.Username, sortHot, opts)
}
// GetNewLinks returns a list of the client's newest submissions
func (s *UserServiceOp) GetNewLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, s.client.Username, sortNew, opts)
}
// GetTopLinks returns a list of the client's top submissions
func (s *UserServiceOp) GetTopLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, s.client.Username, sortTop, opts)
}
// GetControversialLinks returns a list of the client's most controversial submissions
func (s *UserServiceOp) GetControversialLinks(ctx context.Context, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, s.client.Username, sortControversial, opts)
}
// GetHotLinksOf returns a list of the user's hottest submissions
func (s *UserServiceOp) GetHotLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, username, sortHot, opts)
}
// GetNewLinksOf returns a list of the user's newest submissions
func (s *UserServiceOp) GetNewLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, username, sortNew, opts)
}
// GetTopLinksOf returns a list of the user's top submissions
func (s *UserServiceOp) GetTopLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, username, sortTop, opts)
}
// GetControversialLinksOf returns a list of the user's most controversial submissions
func (s *UserServiceOp) GetControversialLinksOf(ctx context.Context, username string, opts *ListOptions) (*LinkList, *Response, error) {
return s.getLinks(ctx, username, sortControversial, opts)
}
// GetHotComments returns a list of the client's hottest comments
func (s *UserServiceOp) GetHotComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortHot, opts)
}
// GetNewComments returns a list of the client's newest comments
func (s *UserServiceOp) GetNewComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortNew, opts)
}
// GetTopComments returns a list of the client's top comments
func (s *UserServiceOp) GetTopComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortTop, opts)
}
// GetControversialComments returns a list of the client's most controversial comments
func (s *UserServiceOp) GetControversialComments(ctx context.Context, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, s.client.Username, sortControversial, opts)
}
// GetHotCommentsOf returns a list of the user's hottest comments
func (s *UserServiceOp) GetHotCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortHot, opts)
}
// GetNewCommentsOf returns a list of the user's newest comments
func (s *UserServiceOp) GetNewCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortNew, opts)
}
// GetTopCommentsOf returns a list of the user's top comments
func (s *UserServiceOp) GetTopCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortTop, opts)
}
// GetControversialCommentsOf returns a list of the user's most controversial comments
func (s *UserServiceOp) GetControversialCommentsOf(ctx context.Context, username string, opts *ListOptions) (*CommentList, *Response, error) {
return s.getComments(ctx, username, sortControversial, opts)
}
func (s *UserServiceOp) getLinks(ctx context.Context, username string, sort sort, opts *ListOptions) (*LinkList, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", username, sorts[sort])
func (s *UserServiceOp) getLinks(ctx context.Context, path string, opts *ListOptions) (*LinkList, *Response, error) {
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
@ -394,3 +384,29 @@ func (s *UserServiceOp) getComments(ctx context.Context, username string, sort s
return root.getComments(), resp, nil
}
func (s *UserServiceOp) getCommentsAndLinks(ctx context.Context, path string, opts *ListOptions) (*CommentsLinks, *Response, error) {
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
v := new(CommentsLinks)
v.Comments = root.getComments().Comments
v.Links = root.getLinks().Links
v.After = root.getAfter()
v.Before = root.getBefore()
return v, resp, nil
}