From 559ed99cdfe84c737d0942f158d608939692bba7 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 22 Jun 2019 01:44:24 +0900 Subject: [PATCH] Add direct Closes #102 --- cmd/mstdn/cmd_timeline.go | 46 ++++++++++++++++++++++++++++++++++ cmd/mstdn/cmd_timeline_test.go | 28 ++++++++++++++++++--- cmd/mstdn/main.go | 20 +++++++++++++++ status.go | 12 +++++++++ status_test.go | 22 ++++++++++++++++ 5 files changed, 125 insertions(+), 3 deletions(-) diff --git a/cmd/mstdn/cmd_timeline.go b/cmd/mstdn/cmd_timeline.go index d9a0024..c610966 100644 --- a/cmd/mstdn/cmd_timeline.go +++ b/cmd/mstdn/cmd_timeline.go @@ -20,3 +20,49 @@ func cmdTimeline(c *cli.Context) error { } return nil } + +func cmdTimelineHome(c *cli.Context) error { + return cmdTimeline(c) +} + +func cmdTimelinePublic(c *cli.Context) error { + client := c.App.Metadata["client"].(*mastodon.Client) + config := c.App.Metadata["config"].(*mastodon.Config) + timeline, err := client.GetTimelinePublic(context.Background(), false, nil) + if err != nil { + return err + } + s := newScreen(config) + for i := len(timeline) - 1; i >= 0; i-- { + s.displayStatus(c.App.Writer, timeline[i]) + } + return nil +} + +func cmdTimelineLocal(c *cli.Context) error { + client := c.App.Metadata["client"].(*mastodon.Client) + config := c.App.Metadata["config"].(*mastodon.Config) + timeline, err := client.GetTimelinePublic(context.Background(), true, nil) + if err != nil { + return err + } + s := newScreen(config) + for i := len(timeline) - 1; i >= 0; i-- { + s.displayStatus(c.App.Writer, timeline[i]) + } + return nil +} + +func cmdTimelineDirect(c *cli.Context) error { + client := c.App.Metadata["client"].(*mastodon.Client) + config := c.App.Metadata["config"].(*mastodon.Config) + timeline, err := client.GetTimelineDirect(context.Background(), nil) + if err != nil { + return err + } + s := newScreen(config) + for i := len(timeline) - 1; i >= 0; i-- { + s.displayStatus(c.App.Writer, timeline[i]) + } + return nil +} diff --git a/cmd/mstdn/cmd_timeline_test.go b/cmd/mstdn/cmd_timeline_test.go index b0801a8..f9b6299 100644 --- a/cmd/mstdn/cmd_timeline_test.go +++ b/cmd/mstdn/cmd_timeline_test.go @@ -14,7 +14,13 @@ func TestCmdTimeline(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v1/timelines/home": - fmt.Fprintln(w, `[{"content": "zzz"}]`) + fmt.Fprintln(w, `[{"content": "home"}]`) + return + case "/api/v1/timelines/public": + fmt.Fprintln(w, `[{"content": "public"}]`) + return + case "/api/v1/timelines/direct": + fmt.Fprintln(w, `[{"content": "direct"}]`) return } http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) @@ -22,9 +28,25 @@ func TestCmdTimeline(t *testing.T) { }, func(app *cli.App) { app.Run([]string{"mstdn", "timeline"}) + app.Run([]string{"mstdn", "timeline-home"}) + app.Run([]string{"mstdn", "timeline-public"}) + app.Run([]string{"mstdn", "timeline-local"}) + app.Run([]string{"mstdn", "timeline-direct"}) }, ) - if !strings.Contains(out, "zzz") { - t.Fatalf("%q should be contained in output of command: %v", "zzz", out) + want := strings.Join([]string{ + "@example.com", + "home", + "@example.com", + "home", + "@example.com", + "public", + "@example.com", + "public", + "@example.com", + "direct", + }, "\n") + "\n" + if !strings.Contains(out, want) { + t.Fatalf("%q should be contained in output of command: %v", want, out) } } diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index 4386768..141a364 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -235,6 +235,26 @@ func makeApp() *cli.App { Usage: "show timeline", Action: cmdTimeline, }, + { + Name: "timeline-home", + Usage: "show timeline home", + Action: cmdTimelineHome, + }, + { + Name: "timeline-local", + Usage: "show timeline local", + Action: cmdTimelineLocal, + }, + { + Name: "timeline-public", + Usage: "show timeline public", + Action: cmdTimelinePublic, + }, + { + Name: "timeline-direct", + Usage: "show timeline direct", + Action: cmdTimelineDirect, + }, { Name: "notification", Usage: "show notification", diff --git a/status.go b/status.go index fa95ab8..7bee52a 100644 --- a/status.go +++ b/status.go @@ -295,3 +295,15 @@ func (c *Client) UploadMediaFromReader(ctx context.Context, reader io.Reader) (* } return &attachment, nil } + +// GetTimelineDirect return statuses from direct timeline. +func (c *Client) GetTimelineDirect(ctx context.Context, pg *Pagination) ([]*Status, error) { + params := url.Values{} + + var statuses []*Status + err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/direct", params, &statuses, pg) + if err != nil { + return nil, err + } + return statuses, nil +} diff --git a/status_test.go b/status_test.go index 9fca529..d6120d0 100644 --- a/status_test.go +++ b/status_test.go @@ -370,6 +370,28 @@ func TestGetTimelinePublic(t *testing.T) { } } +func TestGetTimelineDirect(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `[{"content": "direct"}, {"content": "status"}]`) + })) + defer ts.Close() + + client := NewClient(&Config{Server: ts.URL}) + tl, err := client.GetTimelineDirect(context.Background(), nil) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if len(tl) != 2 { + t.Fatalf("result should be two: %d", len(tl)) + } + if tl[0].Content != "direct" { + t.Fatalf("want %q but %q", "foo", tl[0].Content) + } + if tl[1].Content != "status" { + t.Fatalf("want %q but %q", "bar", tl[1].Content) + } +} + func TestGetTimelineHashtag(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v1/timelines/tag/zzz" {