From b2204e0d6ae82b5bf18f54b8bc951c11771193e3 Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Sat, 4 Jun 2022 15:44:49 +0200 Subject: [PATCH] add support for pinned posts --- accounts.go | 12 ++++++++++++ accounts_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/accounts.go b/accounts.go index 6b481fb..a5e6823 100644 --- a/accounts.go +++ b/accounts.go @@ -139,6 +139,18 @@ func (c *Client) GetAccountStatuses(ctx context.Context, id ID, pg *Pagination) return statuses, nil } +// GetAccountPinnedStatuses return statuses pinned by specified accuont. +func (c *Client) GetAccountPinnedStatuses(ctx context.Context, id ID) ([]*Status, error) { + var statuses []*Status + params := url.Values{} + params.Set("pinned", "true") + err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), params, &statuses, nil) + if err != nil { + return nil, err + } + return statuses, nil +} + // GetAccountFollowers return followers list. func (c *Client) GetAccountFollowers(ctx context.Context, id ID, pg *Pagination) ([]*Account, error) { var accounts []*Account diff --git a/accounts_test.go b/accounts_test.go index 4eed9ea..73f59c2 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -144,6 +144,44 @@ func TestGetAccountStatuses(t *testing.T) { } } +func TestGetAccountPinnedStatuses(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/statuses" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + pinned := r.URL.Query().Get("pinned") + if pinned != "true" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetAccountPinnedStatuses(context.Background(), "123") + if err == nil { + t.Fatalf("should be fail: %v", err) + } + ss, err := client.GetAccountPinnedStatuses(context.Background(), "1234567") + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if ss[0].Content != "foo" { + t.Fatalf("want %q but %q", "foo", ss[0].Content) + } + if ss[1].Content != "bar" { + t.Fatalf("want %q but %q", "bar", ss[1].Content) + } +} + func TestGetAccountFollowers(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v1/accounts/1234567/followers" {