From d39c10ba5e9485a540bc9c33d75ade2da98e95b7 Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Tue, 29 Jun 2021 17:02:28 +0200 Subject: [PATCH] Add bookmark support --- status.go | 31 ++++++++++++++++++ status_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/status.go b/status.go index 28c3f0e..bd86a15 100644 --- a/status.go +++ b/status.go @@ -30,6 +30,7 @@ type Status struct { FavouritesCount int64 `json:"favourites_count"` Reblogged interface{} `json:"reblogged"` Favourited interface{} `json:"favourited"` + Bookmarked interface{} `json:"bookmarked"` Muted interface{} `json:"muted"` Sensitive bool `json:"sensitive"` SpoilerText string `json:"spoiler_text"` @@ -149,6 +150,16 @@ func (c *Client) GetFavourites(ctx context.Context, pg *Pagination) ([]*Status, return statuses, nil } +// GetBookmarks return the bookmark list of the current user. +func (c *Client) GetBookmarks(ctx context.Context, pg *Pagination) ([]*Status, error) { + var statuses []*Status + err := c.doAPI(ctx, http.MethodGet, "/api/v1/bookmarks", nil, &statuses, pg) + if err != nil { + return nil, err + } + return statuses, nil +} + // GetStatus return status specified by id. func (c *Client) GetStatus(ctx context.Context, id ID) (*Status, error) { var status Status @@ -239,6 +250,26 @@ func (c *Client) Unfavourite(ctx context.Context, id ID) (*Status, error) { return &status, nil } +// Bookmark is bookmark the toot of id and return status of the bookmark toot. +func (c *Client) Bookmark(ctx context.Context, id ID) (*Status, error) { + var status Status + err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/bookmark", id), nil, &status, nil) + if err != nil { + return nil, err + } + return &status, nil +} + +// Unbookmark is unbookmark the toot of id and return status of the unbookmark toot. +func (c *Client) Unbookmark(ctx context.Context, id ID) (*Status, error) { + var status Status + err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unbookmark", id), nil, &status, nil) + if err != nil { + return nil, err + } + return &status, nil +} + // GetTimelineHome return statuses from home timeline. func (c *Client) GetTimelineHome(ctx context.Context, pg *Pagination) ([]*Status, error) { var statuses []*Status diff --git a/status_test.go b/status_test.go index 8bedbb2..0a5231f 100644 --- a/status_test.go +++ b/status_test.go @@ -37,6 +37,34 @@ func TestGetFavourites(t *testing.T) { } } +func TestGetBookmarks(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + books, err := client.GetBookmarks(context.Background(), nil) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if len(books) != 2 { + t.Fatalf("result should be two: %d", len(books)) + } + if books[0].Content != "foo" { + t.Fatalf("want %q but %q", "foo", books[0].Content) + } + if books[1].Content != "bar" { + t.Fatalf("want %q but %q", "bar", books[1].Content) + } +} + func TestGetStatus(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v1/statuses/1234567" { @@ -340,6 +368,66 @@ func TestUnfavourite(t *testing.T) { } } +func TestBookmark(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/statuses/1234567/bookmark" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"content": "zzz"}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.Bookmark(context.Background(), "123") + if err == nil { + t.Fatalf("should be fail: %v", err) + } + status, err := client.Bookmark(context.Background(), "1234567") + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if status.Content != "zzz" { + t.Fatalf("want %q but %q", "zzz", status.Content) + } +} + +func TestUnbookmark(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/statuses/1234567/unbookmark" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"content": "zzz"}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.Unbookmark(context.Background(), "123") + if err == nil { + t.Fatalf("should be fail: %v", err) + } + status, err := client.Unbookmark(context.Background(), "1234567") + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if status.Content != "zzz" { + t.Fatalf("want %q but %q", "zzz", status.Content) + } +} + func TestGetTimelinePublic(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Get("local") == "" {