From 730317321abbdea25c96311c94eee46ca253dfc9 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 25 Oct 2017 10:22:39 +0900 Subject: [PATCH] also convert to string in attachments, pagenation --- cmd/mstdn/cmd_followers.go | 2 +- cmd/mstdn/cmd_toot.go | 3 ++- example_test.go | 2 +- mastodon.go | 36 ++++++++++++++--------------- mastodon_test.go | 46 +++++++++++++++++++------------------- status.go | 7 +++--- status_test.go | 4 ++-- 7 files changed, 50 insertions(+), 50 deletions(-) diff --git a/cmd/mstdn/cmd_followers.go b/cmd/mstdn/cmd_followers.go index ecb4a79..1b94430 100644 --- a/cmd/mstdn/cmd_followers.go +++ b/cmd/mstdn/cmd_followers.go @@ -25,7 +25,7 @@ func cmdFollowers(c *cli.Context) error { return err } followers = append(followers, fs...) - if pg.MaxID == 0 { + if pg.MaxID == "" { break } time.Sleep(10 * time.Second) diff --git a/cmd/mstdn/cmd_toot.go b/cmd/mstdn/cmd_toot.go index 29b6f4c..b2e41f4 100644 --- a/cmd/mstdn/cmd_toot.go +++ b/cmd/mstdn/cmd_toot.go @@ -3,6 +3,7 @@ package main import ( "context" "errors" + "fmt" "github.com/mattn/go-mastodon" "github.com/urfave/cli" @@ -26,7 +27,7 @@ func cmdToot(c *cli.Context) error { client := c.App.Metadata["client"].(*mastodon.Client) _, err := client.PostStatus(context.Background(), &mastodon.Toot{ Status: toot, - InReplyToID: c.Int64("i"), + InReplyToID: mastodon.ID(fmt.Sprint(c.Int64("i"))), }) return err } diff --git a/example_test.go b/example_test.go index 18716a2..a25a3ca 100644 --- a/example_test.go +++ b/example_test.go @@ -56,7 +56,7 @@ func ExamplePagination() { log.Fatal(err) } followers = append(followers, fs...) - if pg.MaxID == 0 { + if pg.MaxID == "" { break } time.Sleep(10 * time.Second) diff --git a/mastodon.go b/mastodon.go index a81d9bd..118cdcd 100644 --- a/mastodon.go +++ b/mastodon.go @@ -173,12 +173,12 @@ func (c *Client) Authenticate(ctx context.Context, username, password string) er // Toot is struct to post status. type Toot struct { - Status string `json:"status"` - InReplyToID int64 `json:"in_reply_to_id"` - MediaIDs []int64 `json:"media_ids"` - Sensitive bool `json:"sensitive"` - SpoilerText string `json:"spoiler_text"` - Visibility string `json:"visibility"` + Status string `json:"status"` + InReplyToID ID `json:"in_reply_to_id"` + MediaIDs []ID `json:"media_ids"` + Sensitive bool `json:"sensitive"` + SpoilerText string `json:"spoiler_text"` + Visibility string `json:"visibility"` } // Mention hold information for mention. @@ -186,7 +186,7 @@ type Mention struct { URL string `json:"url"` Username string `json:"username"` Acct string `json:"acct"` - ID int64 `json:"id"` + ID ID `json:"id"` } // Tag hold information for tag. @@ -197,7 +197,7 @@ type Tag struct { // Attachment hold information for attachment. type Attachment struct { - ID int64 `json:"id"` + ID ID `json:"id"` Type string `json:"type"` URL string `json:"url"` RemoteURL string `json:"remote_url"` @@ -214,8 +214,8 @@ type Results struct { // Pagination is a struct for specifying the get range. type Pagination struct { - MaxID int64 - SinceID int64 + MaxID ID + SinceID ID Limit int64 } @@ -245,18 +245,18 @@ func newPagination(rawlink string) (*Pagination, error) { return p, nil } -func getPaginationID(rawurl, key string) (int64, error) { +func getPaginationID(rawurl, key string) (ID, error) { u, err := url.Parse(rawurl) if err != nil { - return 0, err + return "", err } id, err := strconv.ParseInt(u.Query().Get(key), 10, 64) if err != nil { - return 0, err + return "", err } - return id, nil + return ID(fmt.Sprint(id)), nil } func (p *Pagination) toValues() url.Values { @@ -264,10 +264,10 @@ func (p *Pagination) toValues() url.Values { } func (p *Pagination) setValues(params url.Values) url.Values { - if p.MaxID > 0 { - params.Set("max_id", fmt.Sprint(p.MaxID)) - } else if p.SinceID > 0 { - params.Set("since_id", fmt.Sprint(p.SinceID)) + if p.MaxID != "" { + params.Set("max_id", string(p.MaxID)) + } else if p.SinceID != "" { + params.Set("since_id", string(p.SinceID)) } if p.Limit > 0 { params.Set("limit", fmt.Sprint(p.Limit)) diff --git a/mastodon_test.go b/mastodon_test.go index eb6c476..4651cae 100644 --- a/mastodon_test.go +++ b/mastodon_test.go @@ -25,26 +25,26 @@ func TestDoAPI(t *testing.T) { c := NewClient(&Config{Server: ts.URL}) var accounts []Account err := c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, &Pagination{ - MaxID: 999, + MaxID: "999", }) if err == nil { t.Fatalf("should be fail: %v", err) } pg := &Pagination{ - MaxID: 123, - SinceID: 789, + MaxID: "123", + SinceID: "789", Limit: 10, } err = c.doAPI(context.Background(), http.MethodGet, "/", url.Values{}, &accounts, pg) if err != nil { t.Fatalf("should not be fail: %v", err) } - if pg.MaxID != 234 { - t.Fatalf("want %d but %d", 234, pg.MaxID) + if pg.MaxID != "234" { + t.Fatalf("want %q but %q", "234", pg.MaxID) } - if pg.SinceID != 890 { - t.Fatalf("want %d but %d", 890, pg.SinceID) + if pg.SinceID != "890" { + t.Fatalf("want %q but %q", "890", pg.SinceID) } if accounts[0].Username != "foo" { t.Fatalf("want %q but %q", "foo", accounts[0].Username) @@ -54,19 +54,19 @@ func TestDoAPI(t *testing.T) { } pg = &Pagination{ - MaxID: 123, - SinceID: 789, + MaxID: "123", + SinceID: "789", Limit: 10, } err = c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, pg) if err != nil { t.Fatalf("should not be fail: %v", err) } - if pg.MaxID != 234 { - t.Fatalf("want %d but %d", 234, pg.MaxID) + if pg.MaxID != "234" { + t.Fatalf("want %q but %q", "234", pg.MaxID) } - if pg.SinceID != 890 { - t.Fatalf("want %d but %d", 890, pg.SinceID) + if pg.SinceID != "890" { + t.Fatalf("want %q but %q", "890", pg.SinceID) } if accounts[0].Username != "foo" { t.Fatalf("want %q but %q", "foo", accounts[0].Username) @@ -297,11 +297,11 @@ func TestNewPagination(t *testing.T) { if err != nil { t.Fatalf("should not be fail: %v", err) } - if pg.MaxID != 123 { - t.Fatalf("want %d but %d", 123, pg.MaxID) + if pg.MaxID != "123" { + t.Fatalf("want %q but %q", "123", pg.MaxID) } - if pg.SinceID != 789 { - t.Fatalf("want %d but %d", 789, pg.SinceID) + if pg.SinceID != "789" { + t.Fatalf("want %q but %q", "789", pg.SinceID) } } @@ -320,15 +320,15 @@ func TestGetPaginationID(t *testing.T) { if err != nil { t.Fatalf("should not be fail: %v", err) } - if id != 123 { - t.Fatalf("want %d but %d", 123, id) + if id != "123" { + t.Fatalf("want %q but %q", "123", id) } } func TestPaginationSetValues(t *testing.T) { p := &Pagination{ - MaxID: 123, - SinceID: 789, + MaxID: "123", + SinceID: "789", Limit: 10, } before := url.Values{"key": {"value"}} @@ -347,8 +347,8 @@ func TestPaginationSetValues(t *testing.T) { } p = &Pagination{ - MaxID: 0, - SinceID: 789, + MaxID: "", + SinceID: "789", } before = url.Values{} after = p.setValues(before) diff --git a/status.go b/status.go index 38ea606..b04a687 100644 --- a/status.go +++ b/status.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "net/url" - "strconv" "time" ) @@ -208,12 +207,12 @@ func (c *Client) GetTimelineMedia(ctx context.Context, isLocal bool, pg *Paginat func (c *Client) PostStatus(ctx context.Context, toot *Toot) (*Status, error) { params := url.Values{} params.Set("status", toot.Status) - if toot.InReplyToID > 0 { - params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID)) + if toot.InReplyToID != "" { + params.Set("in_reply_to_id", string(toot.InReplyToID)) } if toot.MediaIDs != nil { for _, media := range toot.MediaIDs { - params.Add("media_ids[]", strconv.FormatInt(media, 10)) + params.Add("media_ids[]", string(media)) } } if toot.Visibility != "" { diff --git a/status_test.go b/status_test.go index 1359bb9..e6fd767 100644 --- a/status_test.go +++ b/status_test.go @@ -534,7 +534,7 @@ func TestUploadMedia(t *testing.T) { if err != nil { t.Fatalf("should not be fail: %v", err) } - if attachment.ID != 123 { - t.Fatalf("want %q but %q", 123, attachment.ID) + if attachment.ID != "123" { + t.Fatalf("want %q but %q", "123", attachment.ID) } }