go-mastodon/cmd/mstdn/cmd_stream.go

117 lines
2.6 KiB
Go
Raw Normal View History

2017-04-15 14:52:11 +02:00
package main
import (
"context"
2017-04-17 16:29:44 +02:00
"encoding/json"
2017-04-19 09:13:07 +02:00
"errors"
2017-04-15 14:52:11 +02:00
"os"
"os/signal"
2017-04-19 09:13:07 +02:00
"strings"
"text/template"
2017-04-15 14:52:11 +02:00
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
2017-04-20 14:20:40 +02:00
// SimpleJSON is a struct for output JSON for data to be simple used
2017-04-17 16:43:11 +02:00
type SimpleJSON struct {
2017-04-19 08:17:26 +02:00
ID int64 `json:"id"`
2017-04-17 16:43:11 +02:00
Username string `json:"username"`
Acct string `json:"acct"`
2017-04-17 16:43:11 +02:00
Avatar string `json:"avatar"`
Content string `json:"content"`
}
func checkFlag(f ...bool) bool {
n := 0
for _, on := range f {
if on {
n++
}
}
return n > 1
}
2017-04-15 14:52:11 +02:00
func cmdStream(c *cli.Context) error {
2017-04-17 16:29:44 +02:00
asJSON := c.Bool("json")
2017-04-17 16:43:11 +02:00
asSimpleJSON := c.Bool("simplejson")
asFormat := c.String("template")
if checkFlag(asJSON, asSimpleJSON, asFormat != "") {
return errors.New("cannot speicify two or three options in --json/--simplejson/--template")
}
tx, err := template.New("mstdn").Funcs(template.FuncMap{
"nl": func(s string) string {
return s + "\n"
},
"text": func(s string) string {
return textContent(s)
},
}).Parse(asFormat)
if err != nil {
return err
}
2017-04-19 16:21:19 +02:00
2017-04-15 14:52:11 +02:00
client := c.App.Metadata["client"].(*mastodon.Client)
2017-04-19 16:21:19 +02:00
config := c.App.Metadata["config"].(*mastodon.Config)
2017-04-15 14:52:11 +02:00
ctx, cancel := context.WithCancel(context.Background())
2017-04-16 16:38:53 +02:00
defer cancel()
2017-04-15 14:52:11 +02:00
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
2017-04-19 09:13:07 +02:00
var q chan mastodon.Event
t := c.String("type")
if t == "public" {
2017-04-28 07:19:40 +02:00
q, err = client.StreamingPublic(ctx, false)
2017-04-19 09:13:07 +02:00
} else if t == "" || t == "public/local" {
2017-04-28 07:19:40 +02:00
q, err = client.StreamingPublic(ctx, true)
2017-04-19 09:13:07 +02:00
} else if strings.HasPrefix(t, "user:") {
2017-04-28 07:19:40 +02:00
q, err = client.StreamingUser(ctx)
2017-04-19 09:13:07 +02:00
} else if strings.HasPrefix(t, "hashtag:") {
2017-04-28 07:19:40 +02:00
q, err = client.StreamingHashtag(ctx, t[8:], false)
2017-04-19 09:13:07 +02:00
} else {
return errors.New("invalid type")
}
2017-04-15 14:52:11 +02:00
if err != nil {
return err
}
go func() {
<-sc
close(q)
2017-04-15 14:52:11 +02:00
cancel()
}()
2017-04-19 16:21:19 +02:00
2017-04-20 03:39:20 +02:00
c.App.Metadata["signal"] = sc
2017-04-19 16:21:19 +02:00
s := newScreen(config)
2017-04-15 14:52:11 +02:00
for e := range q {
2017-04-17 16:29:44 +02:00
if asJSON {
json.NewEncoder(c.App.Writer).Encode(e)
2017-04-17 16:43:11 +02:00
} else if asSimpleJSON {
if t, ok := e.(*mastodon.UpdateEvent); ok {
json.NewEncoder(c.App.Writer).Encode(&SimpleJSON{
2017-04-19 08:17:26 +02:00
ID: t.Status.ID,
2017-04-17 16:43:11 +02:00
Username: t.Status.Account.Username,
Acct: t.Status.Account.Acct,
2017-04-17 16:43:11 +02:00
Avatar: t.Status.Account.AvatarStatic,
Content: textContent(t.Status.Content),
})
}
} else if asFormat != "" {
tx.ExecuteTemplate(c.App.Writer, "mstdn", e)
2017-04-17 16:29:44 +02:00
} else {
switch t := e.(type) {
case *mastodon.UpdateEvent:
2017-04-19 16:21:19 +02:00
s.displayStatus(c.App.Writer, t.Status)
case *mastodon.NotificationEvent:
2017-04-19 16:58:58 +02:00
// TODO s.displayStatus(c.App.Writer, t.Notification.Status)
2017-04-17 16:29:44 +02:00
case *mastodon.ErrorEvent:
2017-04-19 16:21:19 +02:00
s.displayError(c.App.Writer, t)
2017-04-17 16:29:44 +02:00
}
2017-04-15 14:52:11 +02:00
}
}
return nil
}