go-mastodon/streaming.go

191 lines
3.6 KiB
Go
Raw 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"
)
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() {}
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() {}
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() {}
type ErrorEvent struct{ err error }
func (e *ErrorEvent) event() {}
func (e *ErrorEvent) Error() string { return e.err.Error() }
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 "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
}
}
}
2022-11-20 21:31:59 +01:00
func streaming(p string, params url.Values) (chan Event, error) {
checkInit()
c := client
ctx := context.Background()
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
2022-11-20 21:31:59 +01:00
doStreaming(req, q)
2017-04-14 16:45:18 +02:00
}
}()
return q, nil
2017-04-15 14:03:02 +02:00
}
2022-11-20 21:31:59 +01:00
func doStreaming(req *http.Request, q chan Event) {
resp, err := client.Do(req)
2017-04-29 20:12:55 +02:00
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.
2022-11-20 21:31:59 +01:00
func StreamingUser() (chan Event, error) {
return streaming("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.
2022-11-20 21:31:59 +01:00
func StreamingPublic(isLocal bool) (chan Event, error) {
2017-04-28 07:09:10 +02:00
p := "public"
if isLocal {
p = path.Join(p, "local")
}
2017-04-19 09:13:07 +02:00
2022-11-20 21:31:59 +01:00
return streaming(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.
2022-11-20 21:31:59 +01:00
func StreamingHashtag(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")
}
2022-11-20 21:31:59 +01:00
return streaming(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.
2022-11-20 21:31:59 +01:00
func (l *List) Streaming() (chan Event, error) {
2019-04-27 03:53:58 +02:00
params := url.Values{}
2022-11-20 21:31:59 +01:00
params.Set("list", l.GetID())
2019-04-27 03:53:58 +02:00
2022-11-20 21:31:59 +01:00
return streaming("list", params)
2019-04-27 03:53:58 +02:00
}
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.
2022-11-20 21:31:59 +01:00
func StreamingDirect() (chan Event, error) {
return streaming("direct", nil)
2021-02-25 15:26:45 +01:00
}