forked from jsdir/blarg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
340 lines (285 loc) · 7.28 KB
/
Copy pathstate.go
File metadata and controls
340 lines (285 loc) · 7.28 KB
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
330
331
332
333
334
335
336
337
338
339
340
package main
import "errors"
type StateMessage struct {
Type string
Payload interface{}
SenderId string
End bool
}
type Comment struct {
Text string `json:"text"`
SenderId string `json:"senderId"`
}
type Room struct {
title string
comments []Comment
subscribers map[chan StateMessage]string
totalAnons map[string]bool
totalMembers map[string]bool
callers []string
seats []string
sessionId string
}
var noRoomError = errors.New("room not found")
func setAdd(values []string, value string) ([]string, bool) {
for _, v := range values {
if v == value {
return values, false
}
}
return append(values, value), true
}
func setRemove(values []string, value string) ([]string, bool) {
newValues := []string{}
ok := false
for _, v := range values {
if v == value {
ok = true
} else {
newValues = append(newValues, v)
}
}
return newValues, ok
}
func (r *Room) ToJSON() map[string]interface{} {
totalViewers, activeViewers := r.getViewerCounts()
viewerMap := map[string]bool{}
for _, userId := range r.subscribers {
if userId != "" {
viewerMap[userId] = true
}
}
viewers := []string{}
for userId, _ := range viewerMap {
viewers = append(viewers, userId)
}
// Limit the number of returned comments.
commentLimit := 100
commentIndex := len(r.comments) - commentLimit
if commentIndex < 0 {
commentIndex = 0
}
return map[string]interface{}{
"title": r.title,
"viewers": viewers,
"totalViewers": totalViewers,
"activeViewers": activeViewers,
"comments": r.comments[commentIndex:],
"callers": r.callers,
"seats": r.seats,
"sessionId": r.sessionId,
}
}
func (r *Room) getViewerCounts() (int, int) {
totalViewers := len(r.totalMembers) + len(r.totalAnons)
activeViewers := len(r.subscribers)
return totalViewers, activeViewers
}
func (r *Room) getViewerPayload(userId string) map[string]interface{} {
totalViewers, activeViewers := r.getViewerCounts()
return map[string]interface{}{
"userId": userId,
"totalViewers": totalViewers,
"activeViewers": activeViewers,
}
}
type State interface {
SetRoomSessionId(roomId string, sessionId string)
// userId is "" if the user is unauthenticated
Join(roomId string, userId string, address string) (*Room, chan StateMessage)
// userId is "" if the user is unauthenticated
Leave(roomId string, skip chan StateMessage, userId string)
AddComment(roomId string, skip chan StateMessage, comment Comment)
ChangeRoomTitle(roomId string, messages chan StateMessage, title string)
Call(roomId string, messages chan StateMessage, userId string)
CancelCall(roomId string, messages chan StateMessage, userId string)
AcceptCaller(roomId string, messages chan StateMessage, userId string)
LeaveSeat(roomId string, messages chan StateMessage, userId string)
Reset(roomId string, messages chan StateMessage)
}
type LocalState struct {
rooms map[string]Room
}
func NewLocalState() LocalState {
return LocalState{
rooms: map[string]Room{},
}
}
func (s *LocalState) Join(roomId string, userId string, address string) (*Room, chan StateMessage) {
room, ok := s.rooms[roomId]
if !ok {
room = Room{
title: "@" + roomId + "'s Room",
comments: []Comment{},
subscribers: map[chan StateMessage]string{},
totalAnons: map[string]bool{},
totalMembers: map[string]bool{},
callers: []string{},
seats: []string{},
sessionId: "",
}
}
// Only send the leave event for the member if there are no more
// subscribers left.
alreadyJoined := false
for _, id := range room.subscribers {
if id == userId {
alreadyJoined = true
break
}
}
messages := make(chan StateMessage)
room.subscribers[messages] = userId
if userId == "" {
room.totalAnons[address] = true
} else {
room.totalMembers[userId] = true
}
s.rooms[roomId] = room
if alreadyJoined {
userId = ""
}
s.broadcast(roomId, messages, StateMessage{
Type: JOIN,
Payload: room.getViewerPayload(userId),
})
return &room, messages
}
func (s *LocalState) Leave(roomId string, messages chan StateMessage, userId string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
messages <- StateMessage{End: true}
delete(room.subscribers, messages)
s.rooms[roomId] = room
// Only send the leave event for the member if there are no more
// subscribers left.
for _, id := range room.subscribers {
if id == userId {
userId = ""
break
}
}
s.broadcast(roomId, messages, StateMessage{
Type: LEAVE,
Payload: room.getViewerPayload(userId),
})
}
func (s *LocalState) AddComment(roomId string, skip chan StateMessage, comment Comment) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.comments = append(room.comments, comment)
s.rooms[roomId] = room
s.broadcast(roomId, skip, StateMessage{
Type: ADD_COMMENT,
Payload: map[string]interface{}{
"text": comment.Text,
"senderId": comment.SenderId,
},
})
}
func (s *LocalState) ChangeRoomTitle(roomId string, messages chan StateMessage, title string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.title = title
s.rooms[roomId] = room
s.broadcast(roomId, messages, StateMessage{
Type: CHANGE_TITLE,
Payload: title,
})
}
func (s *LocalState) Call(roomId string, messages chan StateMessage, userId string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.callers, ok = setAdd(room.callers, userId)
s.rooms[roomId] = room
// Only send if the user is not already calling.
if ok {
s.broadcast(roomId, messages, StateMessage{
Type: CALL,
Payload: userId,
})
}
}
func (s *LocalState) CancelCall(roomId string, messages chan StateMessage, userId string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.callers, ok = setRemove(room.callers, userId)
s.rooms[roomId] = room
// Only send if the user was already calling.
if ok {
s.broadcast(roomId, messages, StateMessage{
Type: CANCEL_CALL,
Payload: userId,
})
}
}
func (s *LocalState) AcceptCaller(roomId string, messages chan StateMessage, userId string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.callers, _ = setRemove(room.callers, userId)
room.seats, ok = setAdd(room.seats, userId)
s.rooms[roomId] = room
if ok {
s.broadcast(roomId, messages, StateMessage{
Type: ACCEPT_CALLER,
Payload: userId,
})
}
}
func (s *LocalState) LeaveSeat(roomId string, messages chan StateMessage, userId string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.seats, _ = setRemove(room.seats, userId)
s.rooms[roomId] = room
s.broadcast(roomId, messages, StateMessage{
Type: LEAVE_SEAT,
Payload: userId,
})
}
func (s *LocalState) SetRoomSessionId(roomId string, sessionId string) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.sessionId = sessionId
s.rooms[roomId] = room
}
func (s *LocalState) Reset(roomId string, messages chan StateMessage) {
room, ok := s.rooms[roomId]
if !ok {
return
}
room.callers = []string{}
room.seats = []string{}
s.rooms[roomId] = room
s.broadcast(roomId, messages, StateMessage{
Type: RESET_ROOM,
Payload: "",
})
}
func (s *LocalState) broadcast(roomId string, skip chan StateMessage, message StateMessage) {
room, ok := s.rooms[roomId]
if !ok {
return
}
for subscriber := range room.subscribers {
if subscriber == skip {
continue
}
subscriber <- message
}
}