author | Mikael Berthe <mikael@lilotux.net> |
Wed, 05 Sep 2018 13:11:54 +0200 | |
changeset 234 | e37050f8a4bf |
parent 217 | cc086e49cd0c |
child 238 | 1c0042e76902 |
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. |
|
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
28 |
// The stream name can be "user", "local", "public", "list" or "hashtag". |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
29 |
// 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
|
30 |
// 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
|
31 |
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
|
32 |
var tag, list string |
117 | 33 |
|
34 |
switch streamName { |
|
140 | 35 |
case "user", "public", "public:local": |
117 | 36 |
case "hashtag": |
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
37 |
if param == "" { |
117 | 38 |
return nil, ErrInvalidParameter |
39 |
} |
|
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
40 |
tag = param |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
41 |
case "list": |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
42 |
if param == "" { |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
43 |
return nil, ErrInvalidParameter |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
44 |
} |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
45 |
list = param |
117 | 46 |
default: |
47 |
return nil, ErrInvalidParameter |
|
48 |
} |
|
49 |
||
140 | 50 |
if !strings.HasPrefix(mc.APIBase, "http") { |
51 |
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
|
52 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
53 |
|
140 | 54 |
// Build streaming websocket URL |
55 |
u, err := url.Parse("ws" + mc.APIBase[4:] + "/streaming/") |
|
117 | 56 |
if err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
57 |
return nil, errors.Wrap(err, "cannot create Websocket URL") |
117 | 58 |
} |
120 | 59 |
|
140 | 60 |
urlParams := url.Values{} |
61 |
urlParams.Add("stream", streamName) |
|
62 |
urlParams.Add("access_token", mc.UserToken.AccessToken) |
|
63 |
if tag != "" { |
|
64 |
urlParams.Add("tag", tag) |
|
217
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
65 |
} else if list != "" { |
cc086e49cd0c
Add support for list-based streams
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
66 |
urlParams.Add("list", list) |
117 | 67 |
} |
140 | 68 |
u.RawQuery = urlParams.Encode() |
69 |
||
70 |
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) |
|
71 |
return c, err |
|
117 | 72 |
} |
73 |
||
74 |
// 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
|
75 |
// 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
|
76 |
// The foroutine will close the doneCh channel when it terminates. |
140 | 77 |
func (mc *Client) readStream(events chan<- StreamEvent, stopCh <-chan bool, doneCh chan bool, c *websocket.Conn) { |
78 |
defer c.Close() |
|
79 |
defer close(doneCh) |
|
117 | 80 |
|
140 | 81 |
go func() { |
117 | 82 |
select { |
83 |
case <-stopCh: |
|
140 | 84 |
// Close connection |
85 |
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) |
|
86 |
case <-doneCh: |
|
87 |
// Leave |
|
117 | 88 |
} |
140 | 89 |
}() |
117 | 90 |
|
140 | 91 |
for { |
92 |
var msg struct { |
|
93 |
Event string |
|
94 |
Payload interface{} |
|
117 | 95 |
} |
96 |
||
140 | 97 |
err := c.ReadJSON(&msg) |
98 |
if err != nil { |
|
99 |
if strings.Contains(err.Error(), "close 1000 (normal)") { |
|
100 |
break // Connection properly closed |
|
101 |
} |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
102 |
e := errors.Wrap(err, "read error") |
117 | 103 |
events <- StreamEvent{Event: "error", Error: e} |
140 | 104 |
break |
117 | 105 |
} |
106 |
||
107 |
var obj interface{} |
|
108 |
||
109 |
// Decode API object |
|
140 | 110 |
switch msg.Event { |
117 | 111 |
case "update": |
140 | 112 |
strPayload, ok := msg.Payload.(string) |
113 |
if !ok { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
114 |
e := errors.New("could not decode status: payload isn't a string") |
140 | 115 |
events <- StreamEvent{Event: "error", Error: e} |
116 |
continue |
|
117 |
} |
|
117 | 118 |
var s Status |
140 | 119 |
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
|
120 |
e := errors.Wrap(err, "could not decode status") |
117 | 121 |
events <- StreamEvent{Event: "error", Error: e} |
122 |
continue |
|
123 |
} |
|
124 |
obj = s |
|
125 |
case "notification": |
|
140 | 126 |
strPayload, ok := msg.Payload.(string) |
127 |
if !ok { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
128 |
e := errors.New("could not decode notification: payload isn't a string") |
140 | 129 |
events <- StreamEvent{Event: "error", Error: e} |
130 |
continue |
|
131 |
} |
|
117 | 132 |
var notif Notification |
140 | 133 |
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
|
134 |
e := errors.Wrap(err, "could not decode notification") |
117 | 135 |
events <- StreamEvent{Event: "error", Error: e} |
136 |
continue |
|
137 |
} |
|
138 |
obj = notif |
|
139 |
case "delete": |
|
198
2dd1ed9394f0
Fix stream deletion events with Mastodon v2 API
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
140 |
strPayload, ok := msg.Payload.(string) |
140 | 141 |
if !ok { |
198
2dd1ed9394f0
Fix stream deletion events with Mastodon v2 API
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
142 |
e := errors.New("could not decode deletion: payload isn't a string") |
117 | 143 |
events <- StreamEvent{Event: "error", Error: e} |
144 |
continue |
|
145 |
} |
|
198
2dd1ed9394f0
Fix stream deletion events with Mastodon v2 API
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
146 |
obj = strPayload // statusID |
117 | 147 |
default: |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
148 |
e := errors.Errorf("unhandled event '%s'", msg.Event) |
117 | 149 |
events <- StreamEvent{Event: "error", Error: e} |
150 |
continue |
|
151 |
} |
|
152 |
||
153 |
// Send event to the channel |
|
140 | 154 |
events <- StreamEvent{Event: msg.Event, Data: obj} |
117 | 155 |
} |
156 |
} |
|
157 |
||
158 |
// StreamListener listens to a stream from the Mastodon server |
|
140 | 159 |
// The stream 'name' can be "user", "local", "public" or "hashtag". |
117 | 160 |
// For 'hashtag', the hashTag argument cannot be empty. |
161 |
// 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
|
162 |
// The streaming is terminated if the 'stopCh' channel is closed. |
50c7733ee886
Change StreamListener prototype
Mikael Berthe <mikael@lilotux.net>
parents:
120
diff
changeset
|
163 |
// The 'doneCh' channel is closed if the connection is closed by the server. |
117 | 164 |
// Please note that this method launches a goroutine to listen to the events. |
140 | 165 |
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
|
166 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
167 |
return ErrUninitializedClient |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
168 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
122
diff
changeset
|
169 |
|
140 | 170 |
conn, err := mc.openStream(name, hashTag) |
117 | 171 |
if err != nil { |
172 |
return err |
|
173 |
} |
|
140 | 174 |
go mc.readStream(events, stopCh, doneCh, conn) |
117 | 175 |
return nil |
176 |
} |