From dd0b467062ba3da72262e0dd537e1cef683139b8 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Fri, 5 May 2017 00:18:17 +0900 Subject: [PATCH] Fix pagination for mstdn command --- cmd/mstdn/cmd_followers.go | 18 +++++++++++++++--- cmd/mstdn/cmd_followers_test.go | 1 + cmd/mstdn/cmd_notification.go | 2 +- cmd/mstdn/cmd_timeline.go | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/mstdn/cmd_followers.go b/cmd/mstdn/cmd_followers.go index 8824332..4c21e8d 100644 --- a/cmd/mstdn/cmd_followers.go +++ b/cmd/mstdn/cmd_followers.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "time" "github.com/mattn/go-mastodon" "github.com/urfave/cli" @@ -16,9 +17,20 @@ func cmdFollowers(c *cli.Context) error { if err != nil { return err } - followers, err := client.GetAccountFollowers(context.Background(), account.ID) - if err != nil { - return err + var maxID *int64 + var followers []*mastodon.Account + for { + fs, pg, err := client.GetAccountFollowers( + context.Background(), account.ID, &mastodon.Pagination{MaxID: maxID}) + if err != nil { + return err + } + followers = append(followers, fs...) + if pg.MaxID == nil { + break + } + maxID = pg.MaxID + time.Sleep(10 * time.Second) } s := newScreen(config) for _, follower := range followers { diff --git a/cmd/mstdn/cmd_followers_test.go b/cmd/mstdn/cmd_followers_test.go index 442a068..f8c5080 100644 --- a/cmd/mstdn/cmd_followers_test.go +++ b/cmd/mstdn/cmd_followers_test.go @@ -17,6 +17,7 @@ func TestCmdFollowers(t *testing.T) { fmt.Fprintln(w, `{"id": 123}`) return case "/api/v1/accounts/123/followers": + w.Header().Set("Link", `; rel="prev"`) fmt.Fprintln(w, `[{"id": 234, "username": "ZZZ", "acct": "zzz"}]`) return } diff --git a/cmd/mstdn/cmd_notification.go b/cmd/mstdn/cmd_notification.go index 6d4192c..d96b0ce 100644 --- a/cmd/mstdn/cmd_notification.go +++ b/cmd/mstdn/cmd_notification.go @@ -11,7 +11,7 @@ import ( func cmdNotification(c *cli.Context) error { client := c.App.Metadata["client"].(*mastodon.Client) - notifications, err := client.GetNotifications(context.Background()) + notifications, _, err := client.GetNotifications(context.Background(), nil) if err != nil { return err } diff --git a/cmd/mstdn/cmd_timeline.go b/cmd/mstdn/cmd_timeline.go index d634ebf..4024826 100644 --- a/cmd/mstdn/cmd_timeline.go +++ b/cmd/mstdn/cmd_timeline.go @@ -10,7 +10,7 @@ import ( func cmdTimeline(c *cli.Context) error { client := c.App.Metadata["client"].(*mastodon.Client) config := c.App.Metadata["config"].(*mastodon.Config) - timeline, err := client.GetTimelineHome(context.Background()) + timeline, _, err := client.GetTimelineHome(context.Background(), nil) if err != nil { return err }