Merge pull request #4 from PaulWaldo/develop

Adds TagUnfollow
pull/182/head
Paul Waldo 2023-06-25 07:17:12 -04:00 committed by GitHub
commit 3b27689851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 4 deletions

View File

@ -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)

View File

@ -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))
}
}

View File

@ -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])
}
}

17
tags.go 100644
View File

@ -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
tags_test.go 100644
View File

@ -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)
}
}