forked from goguardian/pusher-ws-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence_channel.go
329 lines (269 loc) · 8.72 KB
/
presence_channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package pusher
import (
"encoding/json"
"errors"
"fmt"
"sync"
)
var (
// ErrNotSubscribed is returned by functions that require the channel to be
// subscribed before being called.
ErrNotSubscribed = errors.New("not subscribed")
// ErrMissingMe indicates a presence channel was subscribed to, but the pusher
// server violated the message protocol by not providing a member for the
// current user.
ErrMissingMe = errors.New("missing member for current user")
)
// Member represents a channel member.
type Member struct {
ID string
// Info is the JSON set by the auth server. Do not modify the underlying byte
// slice, it's shared by all instances of the member.
Info json.RawMessage
}
// PresenceChannel provides information about the users that are currently
// subscribed.
//
// Note: Bind() does not fire pusher:member_added/removed, use
// BindMemberAdded/Removed() instead.
type PresenceChannel interface {
Channel
// BindMemberAdded returns a channel that receives a Member value when a user
// joins the channel. Events may be delivered out of order. Use
// UnbindMemberAdded when finished listening to events.
BindMemberAdded() chan Member
// UnbindMemberAdded removes bindings created by BindMemberAdded(). If chans
// are passed, only those bindings will be removed. Otherwise, all bindings
// for this event will be removed.
UnbindMemberAdded(...chan Member)
// BindMemberRemoved returns a channel that receives a user ID when a user
// leaves the channel. Events may be delivered out of order. Use
// UnbindMemberRemoved when finished listening for events.
BindMemberRemoved() chan string
// UnbindMemberRemoved removes bindings created by UnbindMemberRemoved(). If
// chans are passed, only those bindings will be removed. Otherwise, all
// bindings for this event will be removed.
UnbindMemberRemoved(...chan string)
// Members returns the member info of each user currently subscribed to the
// channel.
Members() map[string]Member
// Member returns a representation of the channel member with the given ID.
// `nil` is returned if the member isn't in the channel.
Member(id string) *Member
// Me returns the member for the current user.
//
// Possible errors:
// - not subscribed - subscription must succeed before calling Me()
// - invalid channel data - pusher server provided invalid JSON
// - missing member for current user - pusher server violated protocol
Me() (*Member, error)
// MemberCount returns the number of users connected to the channel.
MemberCount() int
}
// presenceChannel implements the internalChannel and PresenceChannel interfaces
type presenceChannel struct {
*privateChannel
membersMutex sync.RWMutex
memberAddedChans map[chan Member]chan struct{}
memberRemovedChans map[chan string]chan struct{}
members map[string]Member
}
func newPresenceChannel(baseChannel *channel) *presenceChannel {
privateChannel := &privateChannel{channel: baseChannel}
return &presenceChannel{
privateChannel: privateChannel,
memberAddedChans: map[chan Member]chan struct{}{},
memberRemovedChans: map[chan string]chan struct{}{},
members: map[string]Member{},
}
}
// presenceChannelSubscriptionData is sent from Pusher to the client in the
// subscription_succeeded event.
// https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol/#pusher_internalsubscription_succeeded-pusher-channels-greater-client
type presenceChannelSubscriptionData struct {
Presence struct {
IDs []string `json:"ids"`
Hash map[string]json.RawMessage `json:"hash"`
Count int `json:"count"`
} `json:"presence"`
}
type presenceChannelData struct {
UserID string `json:"user_id"`
}
type presenceChannelMemberAddedData struct {
UserID string `json:"user_id"`
UserInfo json.RawMessage `json:"user_info"`
}
type presenceChannelMemberRemovedData struct {
UserID string `json:"user_id"`
}
func (pc *presenceChannel) handleEvent(event string, data json.RawMessage) {
switch event {
case pusherInternalMemberAdded:
var member presenceChannelMemberAddedData
err := UnmarshalDataString(data, &member)
if err != nil {
pc.privateChannel.channel.client.sendError(fmt.Errorf("decoding member added event data: %w", err))
return
}
pc.membersMutex.Lock()
pc.members[member.UserID] = Member{
ID: member.UserID,
Info: member.UserInfo,
}
sendMemberAdded(pc.memberAddedChans, pc.members[member.UserID])
pc.membersMutex.Unlock()
case pusherInternalMemberRemoved:
var member presenceChannelMemberRemovedData
err := UnmarshalDataString(data, &member)
if err != nil {
pc.privateChannel.channel.client.sendError(fmt.Errorf("decoding member removed event data: %w", err))
return
}
pc.membersMutex.Lock()
delete(pc.members, member.UserID)
sendMemberRemoved(pc.memberRemovedChans, member.UserID)
pc.membersMutex.Unlock()
case pusherInternalSubSucceeded:
var subscriptionData presenceChannelSubscriptionData
err := UnmarshalDataString(data, &subscriptionData)
if err != nil {
pc.privateChannel.channel.client.sendError(fmt.Errorf("decoding sub succeeded event data: %w", err))
}
members := make(map[string]Member, len(subscriptionData.Presence.Hash))
for id, info := range subscriptionData.Presence.Hash {
member := Member{
ID: id,
Info: info,
}
members[id] = member
}
pc.membersMutex.Lock()
pc.members = members
pc.privateChannel.channel.handleEvent(event, data)
for _, member := range pc.members {
sendMemberAdded(pc.memberAddedChans, member)
}
pc.membersMutex.Unlock()
default:
pc.privateChannel.channel.handleEvent(event, data)
}
}
func sendMemberAdded(channels map[chan Member]chan struct{}, member Member) {
for ch, doneChan := range channels {
go func(ch chan Member, member Member, doneChan chan struct{}) {
select {
case ch <- member:
case <-doneChan:
}
}(ch, member, doneChan)
}
}
func sendMemberRemoved(channels map[chan string]chan struct{}, id string) {
for ch, doneChan := range channels {
go func(ch chan string, id string, doneChan chan struct{}) {
select {
case ch <- id:
case <-doneChan:
}
}(ch, id, doneChan)
}
}
func (pc *presenceChannel) BindMemberAdded() chan Member {
pc.membersMutex.Lock()
defer pc.membersMutex.Unlock()
ch := make(chan Member)
pc.memberAddedChans[ch] = make(chan struct{})
return ch
}
func (pc *presenceChannel) UnbindMemberAdded(chans ...chan Member) {
pc.membersMutex.Lock()
defer pc.membersMutex.Unlock()
// Remove all channels when no channels were specified
if len(chans) == 0 {
for _, doneChan := range pc.memberAddedChans {
close(doneChan)
}
pc.memberAddedChans = map[chan Member]chan struct{}{}
return
}
// Remove given channels
for _, ch := range chans {
doneChan, exists := pc.memberAddedChans[ch]
if !exists {
continue
}
close(doneChan)
delete(pc.memberAddedChans, ch)
}
}
func (pc *presenceChannel) BindMemberRemoved() chan string {
pc.membersMutex.Lock()
defer pc.membersMutex.Unlock()
ch := make(chan string)
pc.memberRemovedChans[ch] = make(chan struct{})
return ch
}
func (pc *presenceChannel) UnbindMemberRemoved(chans ...chan string) {
pc.membersMutex.Lock()
defer pc.membersMutex.Unlock()
// Remove all channels when no channels were specified
if len(chans) == 0 {
for _, doneChan := range pc.memberRemovedChans {
close(doneChan)
}
pc.memberRemovedChans = map[chan string]chan struct{}{}
return
}
// Remove given channels
for _, ch := range chans {
doneChan, exists := pc.memberRemovedChans[ch]
if !exists {
continue
}
close(doneChan)
delete(pc.memberRemovedChans, ch)
}
}
func (pc *presenceChannel) Members() map[string]Member {
pc.membersMutex.RLock()
defer pc.membersMutex.RUnlock()
// Maps are passed by reference, so a copy must be made to avoid giving the
// caller a reference to the internal map.
members := make(map[string]Member, len(pc.members))
for id, member := range pc.members {
members[id] = member
}
return members
}
func (pc *presenceChannel) Member(id string) *Member {
pc.membersMutex.RLock()
defer pc.membersMutex.RUnlock()
member, ok := pc.members[id]
if ok {
return &member
}
return nil
}
func (pc *presenceChannel) Me() (*Member, error) {
if !pc.privateChannel.channel.IsSubscribed() {
return nil, ErrNotSubscribed
}
pc.membersMutex.RLock()
defer pc.membersMutex.RUnlock()
var data presenceChannelData
err := UnmarshalDataString(pc.channelData.ChannelData, &data)
if err != nil {
return nil, fmt.Errorf("invalid channel data: %w", err)
}
member, ok := pc.members[data.UserID]
if !ok {
return nil, ErrMissingMe
}
return &member, nil
}
func (pc *presenceChannel) MemberCount() int {
pc.membersMutex.RLock()
defer pc.membersMutex.RUnlock()
return len(pc.members)
}