go-mastodon/streaming.go

206 lines
4.4 KiB
Go
Raw Permalink Normal View History

2017-04-14 16:45:18 +02:00
package mastodon
import (
"bufio"
"bytes"
2017-04-14 16:45:18 +02:00
"context"
"encoding/json"
"errors"
2017-04-14 16:45:18 +02:00
"io"
"net/http"
"net/url"
"path"
"strings"
)
2022-11-13 22:14:40 +01:00
// UpdateEvent is a 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() {}
// UpdateEditEvent is a struct for passing status edit event to app.
type UpdateEditEvent struct {
Status *Status `json:"status"`
}
func (e *UpdateEditEvent) event() {}
2022-11-13 22:14:40 +01:00
// NotificationEvent is a 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() {}
2022-11-13 22:14:40 +01:00
// DeleteEvent is a 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() {}
2022-11-13 22:14:40 +01:00
// ErrorEvent is a struct for passing errors to app.
2017-04-14 16:45:18 +02:00
type ErrorEvent struct{ err error }
func (e *ErrorEvent) event() {}
func (e *ErrorEvent) Error() string { return e.err.Error() }
2022-11-13 22:14:40 +01:00
// Event is an interface passing events to app.
2017-04-14 16:45:18 +02:00
type Event interface {
event()
}
2017-04-29 20:12:55 +02:00
func handleReader(q chan Event, r io.Reader) error {
var name string
var lineBuf bytes.Buffer
br := bufio.NewReader(r)
for {
line, isPrefix, err := br.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
if isPrefix {
lineBuf.Write(line)
continue
}
if lineBuf.Len() > 0 {
lineBuf.Write(line)
line = lineBuf.Bytes()
lineBuf.Reset()
}
token := strings.SplitN(string(line), ":", 2)
2017-04-14 16:45:18 +02:00
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 "status.update":
var status Status
err = json.Unmarshal([]byte(token[1]), &status)
if err == nil {
q <- &UpdateEditEvent{&status}
}
2017-04-14 16:45:18 +02:00
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-11-20 01:58:20 +01:00
q <- &DeleteEvent{ID: 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-19 09:13:07 +02:00
func (c *Client) streaming(ctx context.Context, p string, params url.Values) (chan Event, error) {
2019-08-20 13:12:09 +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)
if c.Config.AccessToken != "" {
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}
}
}
2022-11-13 22:14:40 +01:00
// StreamingUser returns a channel to read events on home.
2017-04-28 07:09:10 +02:00
func (c *Client) StreamingUser(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "user", nil)
2017-04-15 14:03:02 +02:00
}
2022-11-13 22:14:40 +01:00
// StreamingPublic returns a channel to read events on public.
2017-04-28 07:09:10 +02:00
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
}
2022-11-13 22:14:40 +01:00
// StreamingHashtag returns a 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
}
2019-04-27 03:53:58 +02:00
2022-11-13 22:14:40 +01:00
// StreamingList returns a channel to read events on a list.
2019-04-27 03:53:58 +02:00
func (c *Client) StreamingList(ctx context.Context, id ID) (chan Event, error) {
params := url.Values{}
params.Set("list", string(id))
return c.streaming(ctx, "list", params)
}
2021-02-25 15:26:45 +01:00
2022-11-13 22:14:40 +01:00
// StreamingDirect returns a channel to read events on a direct messages.
2021-02-25 15:26:45 +01:00
func (c *Client) StreamingDirect(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "direct", nil)
}