Merge pull request #9 from 178inaba/blocks

Add GetBlocks, GetFavourites
pull/13/head
mattn 2017-04-15 12:18:15 +09:00 committed by GitHub
commit e4826e97a1
4 changed files with 115 additions and 6 deletions

View File

@ -66,6 +66,16 @@ func (c *Client) GetAccountFollowing(id int64) ([]*Account, error) {
return accounts, nil
}
// GetBlocks return block list.
func (c *Client) GetBlocks() ([]*Account, error) {
var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/blocks", nil, &accounts)
if err != nil {
return nil, err
}
return accounts, nil
}
// Relationship hold information for relation-ship to the account.
type Relationship struct {
ID int64 `json:"id"`
@ -177,12 +187,9 @@ func (c *Client) FollowRemoteUser(uri string) (*Account, error) {
}
// GetFollowRequests return follow-requests.
func (c *Client) GetFollowRequests(uri string) ([]*Account, error) {
params := url.Values{}
params.Set("uri", uri)
func (c *Client) GetFollowRequests() ([]*Account, error) {
var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/follow_requests", params, &accounts)
err := c.doAPI(http.MethodGet, "/api/v1/follow_requests", nil, &accounts)
if err != nil {
return nil, err
}

View File

@ -7,6 +7,34 @@ import (
"testing"
)
func TestGetBlocks(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
bl, err := client.GetBlocks()
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(bl) != 2 {
t.Fatalf("result should be two: %d", len(bl))
}
if bl[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", bl[0].Username)
}
if bl[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", bl[0].Username)
}
}
func TestAccountFollow(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/follow" {
@ -72,3 +100,31 @@ func TestAccountUnfollow(t *testing.T) {
t.Fatalf("want %t but %t", false, rel.Following)
}
}
func TestGetFollowRequests(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
fReqs, err := client.GetFollowRequests()
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(fReqs) != 2 {
t.Fatalf("result should be two: %d", len(fReqs))
}
if fReqs[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", fReqs[0].Username)
}
if fReqs[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", fReqs[0].Username)
}
}

View File

@ -45,6 +45,16 @@ type Card struct {
Image string `json:"image"`
}
// GetFavourites return the favorite list of the current user.
func (c *Client) GetFavourites() ([]*Status, error) {
var statuses []*Status
err := c.doAPI(http.MethodGet, "/api/v1/favourites", nil, &statuses)
if err != nil {
return nil, err
}
return statuses, nil
}
// GetStatus return status specified by id.
func (c *Client) GetStatus(id string) (*Status, error) {
var status Status

36
status_test.go 100644
View File

@ -0,0 +1,36 @@
package mastodon
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetFavourites(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",
})
favs, err := client.GetFavourites()
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(favs) != 2 {
t.Fatalf("result should be two: %d", len(favs))
}
if favs[0].Content != "foo" {
t.Fatalf("want %q but %q", "foo", favs[0].Content)
}
if favs[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", favs[1].Content)
}
}