From b5541c9e38b823867a10120ba56a0c2d2cef5ca9 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 19 Apr 2017 16:13:07 +0900 Subject: [PATCH] fix stream types close #25 --- cmd/mstdn/cmd_stream.go | 19 ++++++++++++++++++- cmd/mstdn/main.go | 4 ++++ streaming.go | 27 ++++++++++++++++++--------- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/cmd/mstdn/cmd_stream.go b/cmd/mstdn/cmd_stream.go index 1b83d1a..0e645c2 100644 --- a/cmd/mstdn/cmd_stream.go +++ b/cmd/mstdn/cmd_stream.go @@ -3,9 +3,11 @@ package main import ( "context" "encoding/json" + "errors" "fmt" "os" "os/signal" + "strings" "github.com/fatih/color" "github.com/mattn/go-mastodon" @@ -27,7 +29,22 @@ func cmdStream(c *cli.Context) error { defer cancel() sc := make(chan os.Signal, 1) signal.Notify(sc, os.Interrupt) - q, err := client.StreamingPublic(ctx) + + var q chan mastodon.Event + var err error + + t := c.String("type") + if t == "public" { + q, err = client.StreamingPublic(ctx) + } else if t == "" || t == "public/local" { + q, err = client.StreamingPublicLocal(ctx) + } else if strings.HasPrefix(t, "user:") { + q, err = client.StreamingUser(ctx, t[5:]) + } else if strings.HasPrefix(t, "hashtag:") { + q, err = client.StreamingHashtag(ctx, t[8:]) + } else { + return errors.New("invalid type") + } if err != nil { return err } diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index a5656c8..73b99c7 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -190,6 +190,10 @@ func makeApp() *cli.App { Name: "json", Usage: "output JSON", }, + cli.StringFlag{ + Name: "type", + Usage: "straem type (public,public/local,user:NAME,hashtag:TAG)", + }, cli.BoolFlag{ Name: "simplejson", Usage: "output simple JSON", diff --git a/streaming.go b/streaming.go index b5e4397..3b4007a 100644 --- a/streaming.go +++ b/streaming.go @@ -69,15 +69,13 @@ func handleReader(ctx context.Context, q chan Event, r io.Reader) error { return ctx.Err() } -func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Event, error) { +func (c *Client) streaming(ctx context.Context, p string, params url.Values) (chan Event, error) { u, err := url.Parse(c.config.Server) if err != nil { return nil, err } u.Path = path.Join(u.Path, "/api/v1/streaming/"+p) - params := url.Values{} - params.Set("tag", tag) var resp *http.Response q := make(chan Event, 10) @@ -86,7 +84,7 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even for { var in io.Reader - if tag != "" { + if params != nil { in = strings.NewReader(params.Encode()) } req, err := http.NewRequest(http.MethodGet, u.String(), in) @@ -121,15 +119,26 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even // StreamingPublic return channel to read events on public. func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) { - return c.streaming(ctx, "public", "") + params := url.Values{} + return c.streaming(ctx, "public", params) } -// StreamingHome return channel to read events on home. -func (c *Client) StreamingHome(ctx context.Context) (chan Event, error) { - return c.streaming(ctx, "home", "") +// StreamingPublicLocal return channel to read events on public. +func (c *Client) StreamingPublicLocal(ctx context.Context) (chan Event, error) { + params := url.Values{} + return c.streaming(ctx, "public/local", params) +} + +// StreamingUser return channel to read events on home. +func (c *Client) StreamingUser(ctx context.Context, user string) (chan Event, error) { + params := url.Values{} + params.Set("user", user) + return c.streaming(ctx, "user", params) } // StreamingHashtag return channel to read events on tagged timeline. func (c *Client) StreamingHashtag(ctx context.Context, tag string) (chan Event, error) { - return c.streaming(ctx, "hashtag", tag) + params := url.Values{} + params.Set("tag", tag) + return c.streaming(ctx, "hashtag", params) }