From 4836b8899c3ea2cee4249174dcedfd1e4025cff9 Mon Sep 17 00:00:00 2001 From: Paul Waldo Date: Sun, 28 May 2023 15:16:54 -0400 Subject: [PATCH] Adds TagUnfollow --- accounts.go | 2 +- accounts_test.go | 5 ++-- status_test.go | 2 +- tags.go | 17 +++++++++++ tags_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 tags.go create mode 100644 tags_test.go diff --git a/accounts.go b/accounts.go index 2908261..42b1873 100644 --- a/accounts.go +++ b/accounts.go @@ -363,7 +363,7 @@ func (c *Client) GetMutes(ctx context.Context, pg *Pagination) ([]*Account, erro return accounts, nil } -// GetMutes returns the list of users muted by the current user. +// GetFollowedTags returns the list of Hashtags followed by the user. func (c *Client) GetFollowedTags(ctx context.Context, pg *Pagination) ([]*FollowedTag, error) { var followedTags []*FollowedTag err := c.doAPI(ctx, http.MethodGet, "/api/v1/followed_tags", nil, &followedTags, pg) diff --git a/accounts_test.go b/accounts_test.go index 9affd45..7d1ffcc 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -698,6 +698,7 @@ func TestGetMutes(t *testing.T) { } } func TestGetFollowedTags(t *testing.T) { + t.Parallel() canErr := true ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if canErr { @@ -770,7 +771,7 @@ func TestGetFollowedTags(t *testing.T) { if !followedTags[0].Following { t.Fatalf("want following, but got false") } - if 3 != len(followedTags[0].History){ + if 3 != len(followedTags[0].History) { t.Fatalf("expecting first tag history length to be %d but got %d", 3, len(followedTags[0].History)) } if followedTags[1].Name != "Test2" { @@ -782,7 +783,7 @@ func TestGetFollowedTags(t *testing.T) { if !followedTags[1].Following { t.Fatalf("want following, but got false") } - if 1 != len(followedTags[1].History){ + if 1 != len(followedTags[1].History) { t.Fatalf("expecting first tag history length to be %d but got %d", 1, len(followedTags[1].History)) } } diff --git a/status_test.go b/status_test.go index 2cac4b8..c576fbb 100644 --- a/status_test.go +++ b/status_test.go @@ -729,7 +729,7 @@ func TestSearch(t *testing.T) { t.Fatalf("Hashtags have %q entries, but %q", "3", len(ret.Hashtags)) } if ret.Hashtags[2].Name != "tag3" { - t.Fatalf("Hashtags[2] should %q , but %q", "tag3", ret.Hashtags[2]) + t.Fatalf("Hashtags[2] should %v , but %v", "tag3", ret.Hashtags[2]) } } diff --git a/tags.go b/tags.go new file mode 100644 index 0000000..54a9b67 --- /dev/null +++ b/tags.go @@ -0,0 +1,17 @@ +package mastodon + +import ( + "context" + "fmt" + "net/http" +) + +// TagUnfollow unfollows a hashtag. +func (c *Client) TagUnfollow(ctx context.Context, ID string) (*FollowedTag, error) { + var tag FollowedTag + err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/tags/%s/unfollow", ID), nil, &tag, nil) + if err != nil { + return nil, err + } + return &tag, nil +} diff --git a/tags_test.go b/tags_test.go new file mode 100644 index 0000000..a2eed27 --- /dev/null +++ b/tags_test.go @@ -0,0 +1,75 @@ +package mastodon + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func TestTagUnfollow(t *testing.T) { + t.Parallel() + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `{ + "name": "Test", + "url": "http://mastodon.example/tags/test", + "history": [ + { + "day": "1668556800", + "accounts": "0", + "uses": "0" + }, + { + "day": "1668470400", + "accounts": "0", + "uses": "0" + }, + { + "day": "1668384000", + "accounts": "0", + "uses": "0" + }, + { + "day": "1668297600", + "accounts": "1", + "uses": "1" + }, + { + "day": "1668211200", + "accounts": "0", + "uses": "0" + }, + { + "day": "1668124800", + "accounts": "0", + "uses": "0" + }, + { + "day": "1668038400", + "accounts": "0", + "uses": "0" + } + ], + "following": false + }`) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + tag, err := client.TagUnfollow(context.Background(), "Test") + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if tag.Name != "Test" { + t.Fatalf("want %q but %q", "Test", tag.Name) + } + if tag.Following { + t.Fatalf("want %t but %t", false, tag.Following) + } +}