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