Merge branch 'master' of github.com:mattn/go-mastodon

This commit is contained in:
Yasuhiro Matsumoto 2017-04-18 17:18:17 +09:00
commit 20174d5b4a
7 changed files with 485 additions and 93 deletions

View File

@ -206,13 +206,15 @@ func (c *Client) AccountUnmute(ctx context.Context, id int64) (*Relationship, er
return &relationship, nil
}
// GetAccountRelationship return relationship for the account.
func (c *Client) GetAccountRelationship(ctx context.Context, id int64) ([]*Relationship, error) {
// GetAccountRelationships return relationship for the account.
func (c *Client) GetAccountRelationships(ctx context.Context, ids []int64) ([]*Relationship, error) {
params := url.Values{}
params.Set("id", fmt.Sprint(id))
for _, id := range ids {
params.Add("id[]", fmt.Sprint(id))
}
var relationships []*Relationship
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationship", params, &relationships, nil)
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationships", params, &relationships, nil)
if err != nil {
return nil, err
}

View File

@ -8,8 +8,12 @@ import (
"testing"
)
func TestAccountUpdate(t *testing.T) {
func TestGetAccount(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
@ -21,6 +25,74 @@ func TestAccountUpdate(t *testing.T) {
ClientSecret: "bar",
AccessToken: "zoo",
})
a, err := client.GetAccount(context.Background(), 1)
if err == nil {
t.Fatalf("should not be fail: %v", err)
}
a, err = client.GetAccount(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
}
}
func TestGetAccountCurrentUser(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetAccountCurrentUser(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
a, err := client.GetAccountCurrentUser(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
}
}
func TestAccountUpdate(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.AccountUpdate(context.Background(), &Profile{})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
a, err := client.AccountUpdate(context.Background(), &Profile{
DisplayName: String("display_name"),
Note: String("note"),
@ -35,8 +107,45 @@ func TestAccountUpdate(t *testing.T) {
}
}
func TestGetBlocks(t *testing.T) {
func TestGetAccountStatuses(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
}
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.GetAccountStatuses(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ss, err := client.GetAccountStatuses(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" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
@ -48,6 +157,84 @@ func TestGetBlocks(t *testing.T) {
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetAccountFollowers(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
fl, err := client.GetAccountFollowers(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(fl) != 2 {
t.Fatalf("result should be two: %d", len(fl))
}
if fl[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", fl[0].Username)
}
if fl[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", fl[1].Username)
}
}
func TestGetAccountFollowing(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/following" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
fl, err := client.GetAccountFollowing(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
fl, err = client.GetAccountFollowing(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(fl) != 2 {
t.Fatalf("result should be two: %d", len(fl))
}
if fl[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", fl[0].Username)
}
if fl[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", fl[1].Username)
}
}
func TestGetBlocks(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetBlocks(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
bl, err := client.GetBlocks(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
@ -129,6 +316,237 @@ func TestAccountUnfollow(t *testing.T) {
}
}
func TestAccountBlock(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/block" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"id":1234567,"blocking":true}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.AccountBlock(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rel, err := client.AccountBlock(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rel.ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, rel.ID)
}
if !rel.Blocking {
t.Fatalf("want %t but %t", true, rel.Blocking)
}
}
func TestAccountUnblock(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/unblock" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"id":1234567,"blocking":false}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.AccountUnblock(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rel, err := client.AccountUnblock(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rel.ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, rel.ID)
}
if rel.Blocking {
t.Fatalf("want %t but %t", false, rel.Blocking)
}
}
func TestAccountMute(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/mute" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"id":1234567,"muting":true}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.AccountMute(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rel, err := client.AccountMute(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rel.ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, rel.ID)
}
if !rel.Muting {
t.Fatalf("want %t but %t", true, rel.Muting)
}
}
func TestAccountUnmute(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/unmute" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"id":1234567,"muting":false}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.AccountUnmute(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rel, err := client.AccountUnmute(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rel.ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, rel.ID)
}
if rel.Muting {
t.Fatalf("want %t but %t", false, rel.Muting)
}
}
func TestGetAccountRelationship(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ids := r.URL.Query()["id[]"]
if ids[0] == "1234567" && ids[1] == "8901234" {
fmt.Fprintln(w, `[{"id":1234567},{"id":8901234}]`)
return
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetAccountRelationships(context.Background(), []int64{123, 456})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rels, err := client.GetAccountRelationships(context.Background(), []int64{1234567, 8901234})
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rels[0].ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, rels[0].ID)
}
if rels[1].ID != 8901234 {
t.Fatalf("want %d but %d", 8901234, rels[1].ID)
}
}
func TestAccountsSearch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query()["q"][0] != "foo" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `[{"Username": "foobar"}, {"Username": "barfoo"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.AccountsSearch(context.Background(), "zzz", 2)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
res, err := client.AccountsSearch(context.Background(), "foo", 2)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(res) != 2 {
t.Fatalf("result should be two: %d", len(res))
}
if res[0].Username != "foobar" {
t.Fatalf("want %q but %q", "foobar", res[0].Username)
}
if res[1].Username != "barfoo" {
t.Fatalf("want %q but %q", "barfoo", res[1].Username)
}
}
func TestFollowRemoteUser(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.PostFormValue("uri") != "foo@success.social" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.FollowRemoteUser(context.Background(), "foo@fail.social")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ru, err := client.FollowRemoteUser(context.Background(), "foo@success.social")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if ru.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", ru.Username)
}
}
func TestGetFollowRequests(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
@ -11,7 +12,15 @@ import (
"github.com/urfave/cli"
)
type SimpleJSON struct {
Username string `json:"username"`
Avatar string `json:"avatar"`
Content string `json:"content"`
}
func cmdStream(c *cli.Context) error {
asJSON := c.Bool("json")
asSimpleJSON := c.Bool("simplejson")
client := c.App.Metadata["client"].(*mastodon.Client)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@ -27,16 +36,28 @@ func cmdStream(c *cli.Context) error {
close(q)
}()
for e := range q {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
color.Set(color.Reset)
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Fprintln(c.App.Writer, t.Error())
color.Set(color.Reset)
if asJSON {
json.NewEncoder(c.App.Writer).Encode(e)
} else if asSimpleJSON {
if t, ok := e.(*mastodon.UpdateEvent); ok {
json.NewEncoder(c.App.Writer).Encode(&SimpleJSON{
Username: t.Status.Account.Username,
Avatar: t.Status.Account.AvatarStatic,
Content: textContent(t.Status.Content),
})
}
} else {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
color.Set(color.Reset)
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Fprintln(c.App.Writer, t.Error())
color.Set(color.Reset)
}
}
}
return nil

View File

@ -66,12 +66,6 @@ var (
)
func prompt() (string, string, error) {
t, err := tty.Open()
if err != nil {
return "", "", err
}
defer t.Close()
fmt.Print("E-Mail: ")
email, err := readUsername()
if err != nil {
@ -81,6 +75,11 @@ func prompt() (string, string, error) {
fmt.Print("Password: ")
var password string
if readPassword == nil {
t, err := tty.Open()
if err != nil {
return "", "", err
}
defer t.Close()
password, err = t.ReadPassword()
} else {
password, err = readPassword()
@ -179,8 +178,18 @@ func makeApp() *cli.App {
Action: cmdToot,
},
{
Name: "stream",
Usage: "stream statuses",
Name: "stream",
Usage: "stream statuses",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "json",
Usage: "output JSON",
},
cli.BoolFlag{
Name: "simplejson",
Usage: "output simple JSON",
},
},
Action: cmdStream,
},
{

View File

@ -66,7 +66,13 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
var req *http.Request
ct := "application/x-www-form-urlencoded"
if values, ok := params.(url.Values); ok {
req, err = http.NewRequest(method, u.String(), strings.NewReader(values.Encode()))
var body io.Reader
if method == http.MethodGet {
u.RawQuery = values.Encode()
} else {
body = strings.NewReader(values.Encode())
}
req, err = http.NewRequest(method, u.String(), body)
if err != nil {
return err
}

View File

@ -125,69 +125,3 @@ func TestForTheCoverages(t *testing.T) {
(*ErrorEvent)(nil).event()
_ = (&ErrorEvent{io.EOF}).Error()
}
func TestGetAccount(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
a, err := client.GetAccount(context.Background(), 1)
if err == nil {
t.Fatalf("should not be fail: %v", err)
}
a, err = client.GetAccount(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
}
}
func TestGetAccountFollowing(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/following" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
fl, err := client.GetAccountFollowing(context.Background(), 123)
if err == nil {
t.Fatalf("should not be fail: %v", err)
}
fl, err = client.GetAccountFollowing(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(fl) != 2 {
t.Fatalf("result should be two: %d", len(fl))
}
if fl[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", fl[0].Username)
}
if fl[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", fl[1].Username)
}
}

View File

@ -14,7 +14,9 @@ import (
)
// UpdateEvent is struct for passing status event to app.
type UpdateEvent struct{ Status *Status }
type UpdateEvent struct {
Status *Status `json:"status"`
}
func (e *UpdateEvent) event() {}
@ -92,7 +94,7 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even
if err == nil {
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err = c.Do(req)
if resp.StatusCode != 200 {
if resp != nil && resp.StatusCode != 200 {
err = fmt.Errorf("bad request: %v", resp.Status)
}
}