From 29fdb0fa19f08a3644b8875a48fdd1fc472296a4 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Mon, 17 Aug 2020 22:01:04 -0400 Subject: [PATCH] Finish some todos Signed-off-by: Vartan Benohanian --- collection.go | 24 +++++++++++++++++++----- collection_test.go | 24 ++++++++++++++++++++++-- comment_test.go | 2 +- errors.go | 18 +++++++----------- listings_test.go | 22 ++++++++++------------ post_test.go | 34 +++++++++++++++++----------------- reddit.go | 2 +- subreddit_test.go | 24 ++++++++++++------------ things.go | 26 ++++++++++---------------- user_test.go | 8 ++++---- 10 files changed, 103 insertions(+), 81 deletions(-) diff --git a/collection.go b/collection.go index 4d4a501..9039de0 100644 --- a/collection.go +++ b/collection.go @@ -223,15 +223,29 @@ func (s *CollectionService) UpdateDescription(ctx context.Context, id string, de return s.client.Do(ctx, req, nil) } -// UpdateLayout updates a collection's layout. -// layout is one of: TIMELINE, GALLERY. -func (s *CollectionService) UpdateLayout(ctx context.Context, id string, layout string) (*Response, error) { - // todo: should we validate layout? +// UpdateLayoutTimeline updates a collection's layout to the timeline format. +func (s *CollectionService) UpdateLayoutTimeline(ctx context.Context, id string) (*Response, error) { path := "api/v1/collections/update_collection_display_layout" form := url.Values{} form.Set("collection_id", id) - form.Set("display_layout", layout) + form.Set("display_layout", "TIMELINE") + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// UpdateLayoutGallery updates a collection's layout to the gallery format. +func (s *CollectionService) UpdateLayoutGallery(ctx context.Context, id string) (*Response, error) { + path := "api/v1/collections/update_collection_display_layout" + + form := url.Values{} + form.Set("collection_id", id) + form.Set("display_layout", "GALLERY") req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) if err != nil { diff --git a/collection_test.go b/collection_test.go index 1001cd4..a022937 100644 --- a/collection_test.go +++ b/collection_test.go @@ -274,7 +274,7 @@ func TestCollectionService_UpdateDescription(t *testing.T) { require.NoError(t, err) } -func TestCollectionService_UpdateLayout(t *testing.T) { +func TestCollectionService_UpdateLayoutTimeline(t *testing.T) { setup() defer teardown() @@ -290,7 +290,27 @@ func TestCollectionService_UpdateLayout(t *testing.T) { require.Equal(t, form, r.Form) }) - _, err := client.Collection.UpdateLayout(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7", "TIMELINE") + _, err := client.Collection.UpdateLayoutTimeline(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + require.NoError(t, err) +} + +func TestCollectionService_UpdateLayoutGallery(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/collections/update_collection_display_layout", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + form.Set("display_layout", "GALLERY") + + err := r.ParseForm() + require.NoError(t, err) + require.Equal(t, form, r.Form) + }) + + _, err := client.Collection.UpdateLayoutGallery(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") require.NoError(t, err) } diff --git a/comment_test.go b/comment_test.go index 1be5e95..95de9d0 100644 --- a/comment_test.go +++ b/comment_test.go @@ -22,7 +22,7 @@ var expectedCommentSubmitOrEdit = &Comment{ AuthorFlairText: "Flair", AuthorFlairID: "024b2b66-05ca-11e1-96f4-12313d096aae", - Subreddit: "subreddit", + SubredditName: "subreddit", SubredditNamePrefixed: "r/subreddit", SubredditID: "t5_test", diff --git a/errors.go b/errors.go index b1acca4..a7d6cd4 100644 --- a/errors.go +++ b/errors.go @@ -38,23 +38,19 @@ type JSONErrorResponse struct { // HTTP response that caused this error. Response *http.Response `json:"-"` - JSON *struct { + JSON struct { Errors []APIError `json:"errors,omitempty"` - } `json:"json,omitempty"` + } `json:"json"` } func (r *JSONErrorResponse) Error() string { var message string - if r.JSON != nil && len(r.JSON.Errors) > 0 { - for i, err := range r.JSON.Errors { - message += err.Error() - if i < len(r.JSON.Errors)-1 { - message += ";" - } - } + if len(r.JSON.Errors) > 0 { + message = r.JSON.Errors[0].Error() } + return fmt.Sprintf( - "%v %v: %d %v", + "%s %s: %d %s", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, message, ) } @@ -70,7 +66,7 @@ type ErrorResponse struct { func (r *ErrorResponse) Error() string { return fmt.Sprintf( - "%v %v: %d %v", + "%s %s: %d %s", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.Message, ) } diff --git a/listings_test.go b/listings_test.go index 70c19b3..7c4234a 100644 --- a/listings_test.go +++ b/listings_test.go @@ -28,12 +28,12 @@ var expectedListingPosts = []*Post{ UpvoteRatio: 1, NumberOfComments: 1, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_164ab8", - AuthorName: "v_95", + Author: "v_95", + AuthorID: "t2_164ab8", IsSelfPost: true, }, @@ -49,13 +49,11 @@ var expectedListingComments = []*Comment{ ParentID: "t3_i2gvg4", Permalink: "https://www.reddit.com/r/test/comments/i2gvg4/this_is_a_title/g05v931/", - // todo fix naming Body: "Test comment", Author: "v_95", AuthorID: "t2_164ab8", - // todo fix naming - Subreddit: "test", + SubredditName: "test", SubredditNamePrefixed: "r/test", SubredditID: "t5_2qh23", @@ -104,12 +102,12 @@ var expectedListingPosts2 = []*Post{ UpvoteRatio: 1, NumberOfComments: 1, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_164ab8", - AuthorName: "v_95", + Author: "v_95", + AuthorID: "t2_164ab8", IsSelfPost: true, }, @@ -129,12 +127,12 @@ var expectedListingPosts2 = []*Post{ UpvoteRatio: 1, NumberOfComments: 0, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_164ab8", - AuthorName: "v_95", + Author: "v_95", + AuthorID: "t2_164ab8", }, } diff --git a/post_test.go b/post_test.go index b574dc6..8d56c95 100644 --- a/post_test.go +++ b/post_test.go @@ -27,12 +27,12 @@ var expectedPostAndComments = &PostAndComments{ UpvoteRatio: 1, NumberOfComments: 2, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_testuser", - AuthorName: "testuser", + Author: "testuser", + AuthorID: "t2_testuser", IsSelfPost: true, }, @@ -50,7 +50,7 @@ var expectedPostAndComments = &PostAndComments{ Author: "testuser", AuthorID: "t2_testuser", - Subreddit: "test", + SubredditName: "test", SubredditNamePrefixed: "r/test", SubredditID: "t5_2qh23", @@ -77,7 +77,7 @@ var expectedPostAndComments = &PostAndComments{ Author: "testuser", AuthorID: "t2_testuser", - Subreddit: "test", + SubredditName: "test", SubredditNamePrefixed: "r/test", SubredditID: "t5_2qh23", @@ -119,12 +119,12 @@ var expectedEditedPost = &Post{ UpvoteRatio: 1, NumberOfComments: 0, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_164ab8", - AuthorName: "v_95", + Author: "v_95", + AuthorID: "t2_164ab8", Spoiler: true, IsSelfPost: true, @@ -147,12 +147,12 @@ var expectedPost2 = &Post{ UpvoteRatio: 1, NumberOfComments: 0, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_164ab8", - AuthorName: "v_95", + Author: "v_95", + AuthorID: "t2_164ab8", } var expectedPostDuplicates = &Posts{ @@ -174,12 +174,12 @@ var expectedPostDuplicates = &Posts{ UpvoteRatio: 0.66, NumberOfComments: 1, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_d2v1r90", - AuthorName: "GarlicoinAccount", + Author: "GarlicoinAccount", + AuthorID: "t2_d2v1r90", }, { ID: "le1tc", @@ -198,12 +198,12 @@ var expectedPostDuplicates = &Posts{ UpvoteRatio: 1, NumberOfComments: 1, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_8dyo", - AuthorName: "prog101", + Author: "prog101", + AuthorID: "t2_8dyo", }, }, After: "t3_le1tc", diff --git a/reddit.go b/reddit.go index 299bc35..376ce6a 100644 --- a/reddit.go +++ b/reddit.go @@ -320,7 +320,7 @@ func CheckResponse(r *http.Response) error { data, err := ioutil.ReadAll(r.Body) if err == nil && len(data) > 0 { json.Unmarshal(data, jsonErrorResponse) - if jsonErrorResponse.JSON != nil && len(jsonErrorResponse.JSON.Errors) > 0 { + if len(jsonErrorResponse.JSON.Errors) > 0 { return jsonErrorResponse } } diff --git a/subreddit_test.go b/subreddit_test.go index 36306a5..75ff973 100644 --- a/subreddit_test.go +++ b/subreddit_test.go @@ -28,12 +28,12 @@ var expectedPosts = &Posts{ UpvoteRatio: 0.99, NumberOfComments: 1634, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_30a5ktgt", - AuthorName: "kmiller0112", + Author: "kmiller0112", + AuthorID: "t2_30a5ktgt", IsSelfPost: true, Stickied: true, @@ -53,12 +53,12 @@ var expectedPosts = &Posts{ UpvoteRatio: 1, NumberOfComments: 0, - SubredditID: "t5_2qh23", SubredditName: "test", SubredditNamePrefixed: "r/test", + SubredditID: "t5_2qh23", - AuthorID: "t2_6fqntbwq", - AuthorName: "MuckleMcDuckle", + Author: "MuckleMcDuckle", + AuthorID: "t2_6fqntbwq", }, }, After: "t3_hyhquk", @@ -167,12 +167,12 @@ var expectedSearchPosts = &Posts{ UpvoteRatio: 0.88, NumberOfComments: 3748, - SubredditID: "t5_3h4zq", SubredditName: "WatchPeopleDieInside", SubredditNamePrefixed: "r/WatchPeopleDieInside", + SubredditID: "t5_3h4zq", - AuthorID: "t2_3p32m02", - AuthorName: "chocolat_ice_cream", + Author: "chocolat_ice_cream", + AuthorID: "t2_3p32m02", }, { ID: "hmwhd7", @@ -189,12 +189,12 @@ var expectedSearchPosts = &Posts{ UpvoteRatio: 0.94, NumberOfComments: 7415, - SubredditID: "t5_2qh13", SubredditName: "worldnews", SubredditNamePrefixed: "r/worldnews", + SubredditID: "t5_2qh13", - AuthorID: "t2_wgrkg", - AuthorName: "Jeremy_Martin", + Author: "Jeremy_Martin", + AuthorID: "t2_wgrkg", }, }, After: "t3_hmwhd7", diff --git a/things.go b/things.go index efe09f7..517bce7 100644 --- a/things.go +++ b/things.go @@ -62,7 +62,6 @@ type things struct { Posts []*Post Subreddits []*Subreddit ModActions []*ModAction - // todo: add the other kinds of things } // init initializes or clears the listing. @@ -111,7 +110,6 @@ func (t *things) UnmarshalJSON(b []byte) error { if err := json.Unmarshal(thing.Data, v); err == nil { t.Subreddits = append(t.Subreddits, v) } - case kindAward: case kindModAction: v := new(ModAction) if err := json.Unmarshal(thing.Data, v); err == nil { @@ -123,7 +121,7 @@ func (t *things) UnmarshalJSON(b []byte) error { return nil } -// Comment is a comment posted by a user +// Comment is a comment posted by a user. type Comment struct { ID string `json:"id,omitempty"` FullID string `json:"name,omitempty"` @@ -139,7 +137,7 @@ type Comment struct { AuthorFlairText string `json:"author_flair_text,omitempty"` AuthorFlairID string `json:"author_flair_template_id,omitempty"` - Subreddit string `json:"subreddit,omitempty"` + SubredditName string `json:"subreddit,omitempty"` SubredditNamePrefixed string `json:"subreddit_name_prefixed,omitempty"` SubredditID string `json:"subreddit_id,omitempty"` @@ -150,16 +148,14 @@ type Comment struct { Score int `json:"score"` Controversiality int `json:"controversiality"` - // todo: check the validity of these comments PostID string `json:"link_id,omitempty"` - // This doesn't appear when submitting a comment. + // This doesn't appear consistently. PostTitle string `json:"link_title,omitempty"` - // This doesn't appear when submitting a comment. + // This doesn't appear consistently. PostPermalink string `json:"link_permalink,omitempty"` - // This doesn't appear when submitting a comment. + // This doesn't appear consistently. PostAuthor string `json:"link_author,omitempty"` - // This doesn't appear when submitting a comment - // or when getting a post with its comments. + // This doesn't appear consistently. PostNumComments *int `json:"num_comments,omitempty"` IsSubmitter bool `json:"is_submitter"` @@ -238,9 +234,7 @@ func (r *Replies) MarshalJSON() ([]byte, error) { return json.Marshal(r.Comments) } -// todo: should we implement json.Marshaler? - -// More holds information +// More holds information used to retrieve additional comments omitted from a base comment tree. type More struct { ID string `json:"id"` FullID string `json:"name"` @@ -273,12 +267,12 @@ type Post struct { UpvoteRatio float32 `json:"upvote_ratio"` NumberOfComments int `json:"num_comments"` - SubredditID string `json:"subreddit_id,omitempty"` SubredditName string `json:"subreddit,omitempty"` SubredditNamePrefixed string `json:"subreddit_name_prefixed,omitempty"` + SubredditID string `json:"subreddit_id,omitempty"` - AuthorID string `json:"author_fullname,omitempty"` - AuthorName string `json:"author,omitempty"` + Author string `json:"author,omitempty"` + AuthorID string `json:"author_fullname,omitempty"` Spoiler bool `json:"spoiler"` Locked bool `json:"locked"` diff --git a/user_test.go b/user_test.go index 6d119ca..661582d 100644 --- a/user_test.go +++ b/user_test.go @@ -64,12 +64,12 @@ var expectedPost = &Post{ UpvoteRatio: 0.86, NumberOfComments: 2, - SubredditID: "t5_2qizd", SubredditName: "redditdev", SubredditNamePrefixed: "r/redditdev", + SubredditID: "t5_2qizd", - AuthorID: "t2_164ab8", - AuthorName: "v_95", + Author: "v_95", + AuthorID: "t2_164ab8", IsSelfPost: true, } @@ -87,7 +87,7 @@ var expectedComment = &Comment{ Author: "v_95", AuthorID: "t2_164ab8", - Subreddit: "apple", + SubredditName: "apple", SubredditNamePrefixed: "r/apple", SubredditID: "t5_2qh1f",