2017-04-14 16:45:18 +02:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
type NotificationEvent struct{}
|
|
|
|
|
|
|
|
func (e *NotificationEvent) event() {}
|
|
|
|
|
|
|
|
// DeleteEvent is struct for passing deletion event to app.
|
|
|
|
type DeleteEvent struct{ ID int64 }
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleReader(ctx context.Context, q chan Event, r io.Reader) error {
|
|
|
|
name := ""
|
|
|
|
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":
|
|
|
|
switch name {
|
|
|
|
case "update":
|
|
|
|
var status Status
|
|
|
|
err := json.Unmarshal([]byte(token[1]), &status)
|
|
|
|
if err == nil {
|
|
|
|
q <- &UpdateEvent{&status}
|
|
|
|
}
|
|
|
|
case "notification":
|
|
|
|
case "delete":
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2017-04-15 14:03:02 +02:00
|
|
|
func (c *Client) streaming(ctx context.Context, p string, tag string) (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-15 14:03:02 +02:00
|
|
|
u.Path = path.Join(u.Path, "/api/v1/streaming/"+p)
|
2017-04-14 16:45:18 +02:00
|
|
|
|
2017-04-15 14:03:02 +02:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("tag", tag)
|
2017-04-14 16:45:18 +02:00
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
q := make(chan Event, 10)
|
|
|
|
go func() {
|
|
|
|
defer ctx.Done()
|
|
|
|
|
|
|
|
for {
|
2017-04-15 14:03:02 +02:00
|
|
|
var in io.Reader
|
|
|
|
if tag != "" {
|
|
|
|
in = strings.NewReader(params.Encode())
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), in)
|
2017-04-14 16:45:18 +02:00
|
|
|
if err == nil {
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
|
|
|
resp, err = c.Do(req)
|
2017-04-17 16:36:21 +02:00
|
|
|
if resp != nil && resp.StatusCode != 200 {
|
2017-04-19 07:32:53 +02:00
|
|
|
err = parseAPIError("bad request", resp)
|
2017-04-15 13:20:22 +02:00
|
|
|
}
|
2017-04-14 16:45:18 +02:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = handleReader(ctx, q, resp.Body)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
q <- &ErrorEvent{err}
|
|
|
|
}
|
2017-04-15 13:20:22 +02:00
|
|
|
resp.Body.Close()
|
2017-04-14 16:45:18 +02:00
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return q, nil
|
2017-04-15 14:03:02 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamingPublic return channel to read events on public.
|
|
|
|
func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) {
|
|
|
|
return c.streaming(ctx, "public", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamingHome return channel to read events on home.
|
|
|
|
func (c *Client) StreamingHome(ctx context.Context) (chan Event, error) {
|
|
|
|
return c.streaming(ctx, "home", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2017-04-14 16:45:18 +02:00
|
|
|
}
|