go-mastodon/streaming.go

159 lines
3.3 KiB
Go
Raw Normal View History

2017-04-14 16:45:18 +02:00
package mastodon
import (
"bufio"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"strings"
)
// UpdateEvent is struct for passing status event to app.
2017-04-17 16:29:44 +02:00
type UpdateEvent struct {
Status *Status `json:"status"`
}
2017-04-14 16:45:18 +02:00
func (e *UpdateEvent) event() {}
// NotificationEvent is struct for passing notification event to app.
2017-04-19 16:21:19 +02:00
type NotificationEvent struct {
Notification *Notification `json:"notification"`
}
2017-04-14 16:45:18 +02:00
func (e *NotificationEvent) event() {}
// DeleteEvent is struct for passing deletion event to app.
2017-10-25 08:22:17 +02:00
type DeleteEvent struct{ ID ID }
2017-04-14 16:45:18 +02:00
func (e *DeleteEvent) event() {}
// ErrorEvent is struct for passing errors to app.
type ErrorEvent struct{ err error }
func (e *ErrorEvent) event() {}
func (e *ErrorEvent) Error() string { return e.err.Error() }
// Event is interface passing events to app.
type Event interface {
event()
}
2017-04-29 20:12:55 +02:00
func handleReader(q chan Event, r io.Reader) error {
var name string
2017-04-14 16:45:18 +02:00
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
token := strings.SplitN(line, ":", 2)
if len(token) != 2 {
continue
}
switch strings.TrimSpace(token[0]) {
case "event":
name = strings.TrimSpace(token[1])
case "data":
2017-04-29 20:12:55 +02:00
var err error
2017-04-14 16:45:18 +02:00
switch name {
case "update":
var status Status
2017-04-29 20:12:55 +02:00
err = json.Unmarshal([]byte(token[1]), &status)
2017-04-14 16:45:18 +02:00
if err == nil {
q <- &UpdateEvent{&status}
}
case "notification":
2017-04-19 16:21:19 +02:00
var notification Notification
2017-04-29 20:12:55 +02:00
err = json.Unmarshal([]byte(token[1]), &notification)
2017-04-19 16:21:19 +02:00
if err == nil {
q <- &NotificationEvent{&notification}
}
2017-04-14 16:45:18 +02:00
case "delete":
2017-10-25 10:29:02 +02:00
q <- &DeleteEvent{ID(strings.TrimSpace(token[1]))}
2017-04-14 16:45:18 +02:00
}
2017-04-29 20:12:55 +02:00
if err != nil {
q <- &ErrorEvent{err}
}
2017-04-14 16:45:18 +02:00
}
}
2017-04-29 20:12:55 +02:00
return s.Err()
2017-04-14 16:45:18 +02:00
}
2017-04-19 09:13:07 +02:00
func (c *Client) streaming(ctx context.Context, p string, params url.Values) (chan Event, error) {
2017-04-14 21:36:27 +02:00
u, err := url.Parse(c.config.Server)
2017-04-14 16:45:18 +02:00
if err != nil {
return nil, err
}
2017-04-28 07:09:10 +02:00
u.Path = path.Join(u.Path, "/api/v1/streaming", p)
u.RawQuery = params.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
2017-04-14 16:45:18 +02:00
2017-04-29 20:12:55 +02:00
q := make(chan Event)
2017-04-14 16:45:18 +02:00
go func() {
2017-04-29 20:12:55 +02:00
defer close(q)
2017-04-14 16:45:18 +02:00
for {
2017-04-29 20:12:55 +02:00
select {
case <-ctx.Done():
return
default:
2017-04-14 16:45:18 +02:00
}
2017-04-29 20:12:55 +02:00
c.doStreaming(req, q)
2017-04-14 16:45:18 +02:00
}
}()
return q, nil
2017-04-15 14:03:02 +02:00
}
2017-04-29 20:12:55 +02:00
func (c *Client) doStreaming(req *http.Request, q chan Event) {
resp, err := c.Do(req)
if err != nil {
q <- &ErrorEvent{err}
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
q <- &ErrorEvent{parseAPIError("bad request", resp)}
return
}
err = handleReader(q, resp.Body)
if err != nil {
q <- &ErrorEvent{err}
}
}
2017-04-28 07:09:10 +02:00
// StreamingUser return channel to read events on home.
func (c *Client) StreamingUser(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "user", nil)
2017-04-15 14:03:02 +02:00
}
2017-04-28 07:09:10 +02:00
// StreamingPublic return channel to read events on public.
func (c *Client) StreamingPublic(ctx context.Context, isLocal bool) (chan Event, error) {
p := "public"
if isLocal {
p = path.Join(p, "local")
}
2017-04-19 09:13:07 +02:00
2017-04-28 07:09:10 +02:00
return c.streaming(ctx, p, nil)
2017-04-15 14:03:02 +02:00
}
// StreamingHashtag return channel to read events on tagged timeline.
2017-04-28 07:09:10 +02:00
func (c *Client) StreamingHashtag(ctx context.Context, tag string, isLocal bool) (chan Event, error) {
2017-04-19 09:13:07 +02:00
params := url.Values{}
params.Set("tag", tag)
2017-04-28 07:09:10 +02:00
p := "hashtag"
if isLocal {
p = path.Join(p, "local")
}
return c.streaming(ctx, p, params)
2017-04-14 16:45:18 +02:00
}