diff --git a/collection.go b/collection.go index c7cfdf5..4d4a501 100644 --- a/collection.go +++ b/collection.go @@ -109,7 +109,6 @@ func (s *CollectionService) Create(ctx context.Context, createRequest *Collectio path := "api/v1/collections/create_collection" - // todo: do this for multireddit stuff too form, err := query.Values(createRequest) if err != nil { return nil, nil, err @@ -191,3 +190,85 @@ func (s *CollectionService) ReorderPosts(ctx context.Context, collectionID strin return s.client.Do(ctx, req, nil) } + +// UpdateTitle updates a collection's title. +func (s *CollectionService) UpdateTitle(ctx context.Context, id string, title string) (*Response, error) { + path := "api/v1/collections/update_collection_title" + + form := url.Values{} + form.Set("collection_id", id) + form.Set("title", title) + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// UpdateDescription updates a collection's description. +func (s *CollectionService) UpdateDescription(ctx context.Context, id string, description string) (*Response, error) { + path := "api/v1/collections/update_collection_description" + + form := url.Values{} + form.Set("collection_id", id) + form.Set("description", description) + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + 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? + path := "api/v1/collections/update_collection_display_layout" + + form := url.Values{} + form.Set("collection_id", id) + form.Set("display_layout", layout) + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// Follow follows a collection. +func (s *CollectionService) Follow(ctx context.Context, id string) (*Response, error) { + path := "api/v1/collections/follow_collection" + + form := url.Values{} + form.Set("collection_id", id) + form.Set("follow", "true") + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// Unfollow unfollows a collection. +func (s *CollectionService) Unfollow(ctx context.Context, id string) (*Response, error) { + path := "api/v1/collections/follow_collection" + + form := url.Values{} + form.Set("collection_id", id) + form.Set("follow", "false") + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/collection_test.go b/collection_test.go index bc3f890..821dad8 100644 --- a/collection_test.go +++ b/collection_test.go @@ -233,3 +233,103 @@ func TestCollectionService_ReorderPosts(t *testing.T) { _, err := client.Collection.ReorderPosts(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7", "t3_hs0cyh", "t3_hqrg8s", "t3_hs03f3") assert.NoError(t, err) } + +func TestCollectionService_UpdateTitle(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/collections/update_collection_title", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + form.Set("title", "Test Title") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.Form) + }) + + _, err := client.Collection.UpdateTitle(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7", "Test Title") + assert.NoError(t, err) +} + +func TestCollectionService_UpdateDescription(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/collections/update_collection_description", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + form.Set("description", "Test Description") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.Form) + }) + + _, err := client.Collection.UpdateDescription(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7", "Test Description") + assert.NoError(t, err) +} + +func TestCollectionService_UpdateLayout(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/collections/update_collection_display_layout", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + form.Set("display_layout", "TIMELINE") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.Form) + }) + + _, err := client.Collection.UpdateLayout(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7", "TIMELINE") + assert.NoError(t, err) +} + +func TestCollectionService_Follow(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/collections/follow_collection", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + form.Set("follow", "true") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.Form) + }) + + _, err := client.Collection.Follow(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + assert.NoError(t, err) +} + +func TestCollectionService_Unfollow(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/collections/follow_collection", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + form.Set("follow", "false") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.Form) + }) + + _, err := client.Collection.Unfollow(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7") + assert.NoError(t, err) +}