Skip to content

Commit

Permalink
Adds TagUnfollow
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWaldo authored and mattn committed Apr 17, 2024
1 parent 2b4786a commit 3f69159
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" {
Expand All @@ -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))
}
}
2 changes: 1 addition & 1 deletion status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down
17 changes: 17 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
@@ -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
}
75 changes: 75 additions & 0 deletions tags_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 3f69159

Please sign in to comment.