also convert to string in attachments, pagenation

This commit is contained in:
Yasuhiro Matsumoto 2017-10-25 10:22:39 +09:00
parent e03b8ccefb
commit 730317321a
7 changed files with 50 additions and 50 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 != "" {

View File

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