author | Mikael Berthe <mikael@lilotux.net> |
Mon, 29 Jul 2019 21:57:47 +0200 | |
changeset 253 | 0e8c8026cf40 |
parent 244 | 4508376c8fcb |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 3 |
|
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
7 |
package madon |
117 | 8 |
|
9 |
import ( |
|
10 |
"encoding/json" |
|
140 | 11 |
"net/url" |
117 | 12 |
"strings" |
13 |
||
140 | 14 |
"github.com/gorilla/websocket" |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
15 |
"github.com/pkg/errors" |
117 | 16 |
) |
17 |
||
18 |
// StreamEvent contains a single event from the streaming API |
|
19 |
type StreamEvent struct { |
|
20 |
Event string // Name of the event (error, update, notification or delete) |
|
21 |
Data interface{} // Status, Notification or status ID |
|
22 |
Error error // Error message from the StreamListener |
|
23 |
} |
|
24 |
||
25 |
// openStream opens a stream URL and returns an http.Response |
|
26 |
// Note that the caller should close the connection when it's done reading |
|
27 |
// the stream. |
|
244
4508376c8fcb
GetTimelines & StreamListener: Accept "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
238
diff
changeset
|
28 |
// The stream name can be "user", "local", "public", "direct", "list" or |
4508376c8fcb
GetTimelines & StreamListener: Accept "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
238
diff
changeset
|
29 |
// "hashtag". |
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
30 |
// When it is "hashtag", the param argument contains the hashtag. |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
31 |
// When it is "list", the param argument contains the list ID. |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
32 |
func (mc *Client) openStream(streamName, param string) (*websocket.Conn, error) { |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
33 |
var tag, list string |
117 | 34 |
|
35 |
switch streamName { |
|
244
4508376c8fcb
GetTimelines & StreamListener: Accept "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
238
diff
changeset
|
36 |
case "user", "public", "public:local", "direct": |
117 | 37 |
case "hashtag": |
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
38 |
if param == "" { |
117 | 39 |
return nil, ErrInvalidParameter |
40 |
} |
|
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
41 |
tag = param |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
42 |
case "list": |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
43 |
if param == "" { |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
44 |
return nil, ErrInvalidParameter |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
45 |
} |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
46 |
list = param |
117 | 47 |
default: |
48 |
return nil, ErrInvalidParameter |
|
49 |
} |
|
50 |
||
140 | 51 |
if !strings.HasPrefix(mc.APIBase, "http") { |
52 |
return nil, errors.New("cannot create Websocket URL: unexpected API base URL") |
|
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
53 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
54 |
|
140 | 55 |
// Build streaming websocket URL |
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
217
diff
changeset
|
56 |
u, err := url.Parse("ws" + mc.APIBase[4:] + "/v1/streaming/") |
117 | 57 |
if err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
58 |
return nil, errors.Wrap(err, "cannot create Websocket URL") |
117 | 59 |
} |
120 | 60 |
|
140 | 61 |
urlParams := url.Values{} |
62 |
urlParams.Add("stream", streamName) |
|
63 |
urlParams.Add("access_token", mc.UserToken.AccessToken) |
|
64 |
if tag != "" { |
|
65 |
urlParams.Add("tag", tag) |
|
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
66 |
} else if list != "" { |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
67 |
urlParams.Add("list", list) |
117 | 68 |
} |
140 | 69 |
u.RawQuery = urlParams.Encode() |
70 |
||
71 |
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) |
|
72 |
return c, err |
|
117 | 73 |
} |
74 |
||
75 |
// readStream reads from the http.Response and sends events to the events channel |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
76 |
// It stops when the connection is closed or when the stopCh channel is closed. |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
77 |
// The foroutine will close the doneCh channel when it terminates. |
140 | 78 |
func (mc *Client) readStream(events chan<- StreamEvent, stopCh <-chan bool, doneCh chan bool, c *websocket.Conn) { |
79 |
defer c.Close() |
|
80 |
defer close(doneCh) |
|
117 | 81 |
|
140 | 82 |
go func() { |
117 | 83 |
select { |
84 |
case <-stopCh: |
|
140 | 85 |
// Close connection |
86 |
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) |
|
87 |
case <-doneCh: |
|
88 |
// Leave |
|
117 | 89 |
} |
140 | 90 |
}() |
117 | 91 |
|
140 | 92 |
for { |
93 |
var msg struct { |
|
94 |
Event string |
|
95 |
Payload interface{} |
|
117 | 96 |
} |
97 |
||
140 | 98 |
err := c.ReadJSON(&msg) |
99 |
if err != nil { |
|
100 |
if strings.Contains(err.Error(), "close 1000 (normal)") { |
|
101 |
break // Connection properly closed |
|
102 |
} |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
103 |
e := errors.Wrap(err, "read error") |
117 | 104 |
events <- StreamEvent{Event: "error", Error: e} |
140 | 105 |
break |
117 | 106 |
} |
107 |
||
108 |
var obj interface{} |
|
109 |
||
110 |
// Decode API object |
|
140 | 111 |
switch msg.Event { |
117 | 112 |
case "update": |
140 | 113 |
strPayload, ok := msg.Payload.(string) |
114 |
if !ok { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
115 |
e := errors.New("could not decode status: payload isn't a string") |
140 | 116 |
events <- StreamEvent{Event: "error", Error: e} |
117 |
continue |
|
118 |
} |
|
117 | 119 |
var s Status |
140 | 120 |
if err := json.Unmarshal([]byte(strPayload), &s); err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
121 |
e := errors.Wrap(err, "could not decode status") |
117 | 122 |
events <- StreamEvent{Event: "error", Error: e} |
123 |
continue |
|
124 |
} |
|
125 |
obj = s |
|
126 |
case "notification": |
|
140 | 127 |
strPayload, ok := msg.Payload.(string) |
128 |
if !ok { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
129 |
e := errors.New("could not decode notification: payload isn't a string") |
140 | 130 |
events <- StreamEvent{Event: "error", Error: e} |
131 |
continue |
|
132 |
} |
|
117 | 133 |
var notif Notification |
140 | 134 |
if err := json.Unmarshal([]byte(strPayload), ¬if); err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
135 |
e := errors.Wrap(err, "could not decode notification") |
117 | 136 |
events <- StreamEvent{Event: "error", Error: e} |
137 |
continue |
|
138 |
} |
|
139 |
obj = notif |
|
140 |
case "delete": |
|
198
2dd1ed9394f0
Fix stream deletion events with Mastodon v2 API
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
141 |
strPayload, ok := msg.Payload.(string) |
140 | 142 |
if !ok { |
198
2dd1ed9394f0
Fix stream deletion events with Mastodon v2 API
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
143 |
e := errors.New("could not decode deletion: payload isn't a string") |
117 | 144 |
events <- StreamEvent{Event: "error", Error: e} |
145 |
continue |
|
146 |
} |
|
198
2dd1ed9394f0
Fix stream deletion events with Mastodon v2 API
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
147 |
obj = strPayload // statusID |
117 | 148 |
default: |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
149 |
e := errors.Errorf("unhandled event '%s'", msg.Event) |
117 | 150 |
events <- StreamEvent{Event: "error", Error: e} |
151 |
continue |
|
152 |
} |
|
153 |
||
154 |
// Send event to the channel |
|
140 | 155 |
events <- StreamEvent{Event: msg.Event, Data: obj} |
117 | 156 |
} |
157 |
} |
|
158 |
||
159 |
// StreamListener listens to a stream from the Mastodon server |
|
140 | 160 |
// The stream 'name' can be "user", "local", "public" or "hashtag". |
117 | 161 |
// For 'hashtag', the hashTag argument cannot be empty. |
162 |
// The events are sent to the events channel (the errors as well). |
|
122
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
163 |
// The streaming is terminated if the 'stopCh' channel is closed. |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
164 |
// The 'doneCh' channel is closed if the connection is closed by the server. |
117 | 165 |
// Please note that this method launches a goroutine to listen to the events. |
140 | 166 |
func (mc *Client) StreamListener(name, hashTag string, events chan<- StreamEvent, stopCh <-chan bool, doneCh chan bool) error { |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
167 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
168 |
return ErrUninitializedClient |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
169 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
170 |
|
140 | 171 |
conn, err := mc.openStream(name, hashTag) |
117 | 172 |
if err != nil { |
173 |
return err |
|
174 |
} |
|
140 | 175 |
go mc.readStream(events, stopCh, doneCh, conn) |
117 | 176 |
return nil |
177 |
} |