/
index.js
608 lines (493 loc) · 14 KB
/
index.js
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
'use strict';
const {generateUUID} = FrankerFaceZ.utilities.object;
const VALID_ROUTES = [
'embed-chat',
'popout'
];
class PollShim extends Addon {
constructor(...args) {
super(...args);
this.inject('site');
this.inject('site.router');
this.inject('site.twitch_data');
this.settings.addUI('addon.poll-shim.info', {
path: 'Add-Ons > Poll-Shim >> Info @{"sort": -1000}',
no_filter: true,
component: () => import('./components/info.vue'),
shouldBeActive: () => this.shouldBeActive,
isActive: () => this.active,
hasPubSub: () => this.pubsub_open,
hasWS: () => this._socket != null && this._socket.readyState === WebSocket.OPEN,
isAuthed: () => this.socket_auth,
getChannel: () => this.getUser(),
getSelf: () => this.getSelf(),
getWS: () => this._socket,
getPolls: () => this.polls,
on: (...args) => this.on(...args),
off: (...args) => this.off(...args)
});
this.settings.add('addon.poll-shim.enabled', {
default: true,
ui: {
path: 'Add-Ons > Poll-Shim >> General @{"sort": -500}',
title: 'Enable Poll-Shim.',
component: 'setting-check-box'
},
changed: () => this.onNavigate()
});
this.settings.add('addon.poll-shim.user', {
default: null,
requires: ['context.channelID', 'context.session.user.id', 'context.route.name'],
process: (ctx, val) => {
if ( val == null || typeof val !== 'string' ) {
const route = ctx.get('context.route.name');
if ( typeof route === 'string' && route.startsWith('dash-') )
return ctx.get('context.channelID');
return ctx.get('context.session.user.id');
}
return val;
},
ui: {
path: 'Add-Ons > Poll-Shim >> General',
title: 'Channel ID',
description: 'Set this to override the channel where polls should be run. Please note that you must be a moderator of the channel in question to run a poll.',
component: 'setting-text-box'
},
changed: () => {
if ( this.active ) {
this.deactivate();
if ( this.shouldBeActive )
this.activate();
} else
this.onNavigate();
}
});
this.settings.add('addon.poll-shim.address', {
default: 'ws://localhost:31337',
ui: {
path: 'Add-Ons > Poll-Shim >> Connectivity',
title: 'Address',
description: 'Connect to this address for polls.',
component: 'setting-text-box'
},
changed: () => {
if ( this._socket )
this._socket.reconnect();
}
});
this.settings.add('addon.poll-shim.key', {
default: generateUUID(),
ui: {
path: 'Add-Ons > Poll-Shim >> Authentication',
title: 'Passphrase',
description: 'The server you connect to must authenticate with this passphrase to be allowed to create polls.\n\n**Note:** This value changes every page load until you manually set it.',
component: 'setting-text-box'
},
changed: () => {
if ( this._socket )
this._socket.reconnect();
}
});
this.settings.add('addon.poll-shim.client-key', {
default: '',
ui: {
path: 'Add-Ons > Poll-Shim >> Authentication',
title: 'Client Passphrase',
description: 'If this is set, the client will authenticate by sending this passphrase to the server once the server connects.',
component: 'setting-text-box'
},
changed: () => {
if ( this._socket )
this._socket.reconnect();
}
});
this.url_provider = () => this.settings.get('addon.poll-shim.address');
this.active = false;
this.polls = new Set;
this.updating_polls = new Set;
this.socket_auth = false;
this.onWSClose = this.onWSClose.bind(this);
this.onWSMessage = this.onWSMessage.bind(this);
this.onWSOpen = this.onWSOpen.bind(this);
}
get shouldBeActive() {
if ( ! this.enabled && ! this.enabling )
return false;
if ( this.errored )
return false;
if ( ! this.settings.get('addon.poll-shim.enabled') )
return false;
// Make sure this updates on navigation / whatever.
this.settings.main_context.update('addon.poll-shim.user');
const user_id = this.settings.get('addon.poll-shim.user'),
core = this.site.getCore();
if ( ! user_id || ! core || ! core.pubsub )
return false;
const route = this.router.current_name;
if ( typeof route !== 'string' )
return false;
if ( ! VALID_ROUTES.includes(route) && ! route.startsWith('dash-') )
return false;
const current_id = this.settings.get('context.channelID');
return current_id && current_id == user_id;
}
async onEnable() {
this.ReconnectingWebSocket = (await import('reconnecting-websocket')).default;
this.on('site.router:route', this.onNavigate, this);
this.onNavigate();
}
onDisable() {
this.off('site.router:route', this.onNavigate, this);
if ( this.active )
this.deactivate();
}
onNavigate() {
const should = this.shouldBeActive;
if ( should && ! this.active )
this.activate();
else if ( ! should && this.active )
this.deactivate();
}
async activate() {
if ( ! this.enabled && ! this.enabling )
return;
const user_id = this.settings.get('addon.poll-shim.user'),
core = this.site.getCore();
if ( ! user_id || ! core || ! core.pubsub )
return;
this.channel_id = user_id;
const [user, self] = await Promise.all([
this.getUser(),
this.getSelf()
]);
if ( ! user ) {
this.channel_id = null;
return;
}
this.log.info('Activating shim.');
this._unsub = core.pubsub.subscribe({
topic: `polls.${user_id}`,
success: () => {
this.pubsub_open = true;
this.emit(':update');
},
failure: e => {
this.log.error('Failed to listen to pubsub', e);
this.errored = true;
this.pubsub_open = false;
this.deactivate();
},
onMessage: e => this.onPubSub(e)
});
this._socket = new this.ReconnectingWebSocket(this.url_provider, [], {
maxEnqueuedMessages: 0
});
this._socket.addEventListener('open', this.onWSOpen);
this._socket.addEventListener('close', this.onWSClose);
this._socket.addEventListener('message', this.onWSMessage);
this.active = true;
this.emit(':update');
}
deactivate() {
this.log.info('Deactivating shim.');
if ( this._unsub ) {
this._unsub();
this._unsub = null;
}
if ( this._socket ) {
this._socket.close();
this._socket.removeEventListener('open', this.onWSOpen);
this._socket.removeEventListener('close', this.onWSClose);
this._socket.removeEventListener('message', this.onWSMessage);
this._socket = null;
}
if ( this._open_timer ) {
clearTimeout(this._open_timer);
this._open_timer = null;
}
this.updating_polls.clear();
this.polls.clear();
this.pubsub_open = false;
this.socket_auth = false;
this.channel_id = null;
this._user = null;
this._self = null;
this.active = false;
this.emit(':update');
}
onPubSub(event) {
const type = event.type;
if ( type === 'POLL_UPDATE' || type === 'POLL_COMPLETE' )
this.handlePoll(event.data.poll);
}
send(type, msg = {}) {
if ( ! this._socket )
return;
if ( typeof msg !== 'object' )
msg = {data: msg};
msg.type = type;
this._socket.send(JSON.stringify(msg));
}
onWSOpen() {
this.log.debug('WebSocket Connected');
this.emit(':update');
const key = this.settings.get('addon.poll-shim.client-key');
if ( key && key.length )
this.send('auth', {data: key});
this._open_timer = setTimeout(() => {
if ( this._open_timer ) {
clearTimeout(this._open_timer);
this._open_timer = null;
}
if ( this._socket )
this._socket.reconnect();
}, 1000);
}
onWSClose() {
if ( this._open_timer ) {
clearTimeout(this._open_timer);
this._open_timer = null;
}
this.log.debug('WebSocket Closed');
this.polls.clear();
this.updating_polls.clear();
this.socket_auth = false;
this.emit(':update');
}
async getUser() {
if ( this._user )
return this._user;
const user_id = this.channel_id;
if ( ! user_id ) {
this._user = null;
return null;
}
this._user = await this.twitch_data.getUser(user_id);
return this._user;
}
async getSelf() {
if ( this._self )
return this._self;
const user_id = this.channel_id;
if ( ! user_id ) {
this._self = null;
return null;
}
const current_user = this.site.getUser();
if ( current_user && current_user.id == user_id ) {
this._self = {
isModerator: true,
isEditor: true
}
} else
this._self = await this.twitch_data.getUserSelf(user_id);
return this._self;
}
onWSMessage(event) {
let msg;
try {
msg = JSON.parse(event.data);
} catch(err) {
this.log.debug('Invalid WS Message', err);
return;
}
if ( ! msg || ! msg.type )
return;
this.log.debug('WebSocket Message', msg);
const type = msg.type;
if ( type === 'auth' ) {
if ( this.socket_auth || msg.data !== this.settings.get('addon.poll-shim.key') ) {
this.log.debug('WebSocket Authentication Error. Reconnecting.');
this.send('error', {id: 'bad_auth'});
this._socket.close();
setTimeout(() => {
if ( this._socket )
this._socket.reconnect();
}, 1000);
this.emit(':update');
return;
}
this.socket_auth = true;
this.send('auth_ok');
this.log.info('WebSocket Connected and Authorized');
Promise.all([
this.getUser(),
this.getSelf()
]).then(([user, self]) => {
if ( ! user )
return this.send('error', {id: 'no_user'});
const is_mod = (self && self.isModerator);
const roles = user.roles || {};
this.send('user', {
id: user.id,
login: user.login,
display_name: user.displayName,
can_create: is_mod && (roles.isAffiliate || roles.isPartner)
});
});
if ( this._open_timer ) {
clearTimeout(this._open_timer);
this._open_timer = null;
}
this.emit(':update');
return;
} else if ( ! this.socket_auth )
this.send('error', {id: 'no_auth'});
if ( type === 'create' ) {
if ( typeof msg.title !== 'string' )
return this.send('error', {id: 'no_title'});
const title = msg.title.trim();
if ( ! title.length )
return this.send('error', {id: 'no_title'});
if ( ! Array.isArray(msg.choices) || msg.choices.some(x => typeof x !== 'string' && x.length > 50) )
return this.send('error', {id: 'bad_choices'});
if ( ! this.channel_id )
return this.send('error', {id: 'no_channel'});
const duration = msg.duration || 60;
if ( typeof duration !== 'number' || duration < 15 || isNaN(duration) || duration > 180 )
return this.send('error', {id: 'bad_duration'});
const subscriberMultiplier = msg.subscriberMultiplier || false;
const subscriberOnly = msg.subscriberOnly || false;
const bits = msg.bits || 0;
if ( typeof bits !== 'number' || bits > 10000 )
return this.send('error', {id: 'bad_bits'});
this.twitch_data.createPoll(
this.channel_id,
title,
msg.choices,
{
duration,
subscriberMultiplier,
subscriberOnly,
bits
}
).then(async poll => {
if ( ! poll ) {
const user = await this.getUser(),
roles = user?.roles || {};
if ( ! user )
return this.send('error', {id: 'cannot_poll', err: 'Invalid channel ID or API error.'});
if ( ! roles.isAffiliate && ! roles.isPartner )
return this.send('error', {id: 'cannot_poll', err: 'Channel does not have access to Polls. Must be affiliate or partner.'});
this.send('error', {id: 'cannot_poll', err: 'Unable to create poll.'})
}
this.polls.add(poll.id);
this.send('created', {id: poll.id});
poll.durationSeconds = duration;
this.handlePoll(poll);
this.emit(':update');
}).catch(err => {
this.send('error', {id: 'cannot_poll', err: String(err)});
});
return;
}
if ( type === 'listen' ) {
if ( ! msg.id )
this.send('error', {id: 'no_id'});
this.polls.add(msg.id);
this.updating_polls.add(msg.id);
this.twitch_data.getPoll(msg.id).then(data => {
if ( ! this.updating_polls.has(msg.id) )
return;
if ( ! data )
data = {
id: msg.id,
status: 'COMPLETED',
choices: []
};
this.handlePoll(data);
});
this.emit(':update');
return;
}
if ( type === 'get' ) {
if ( ! msg.id )
this.send('error', {id: 'no_id'});
this.updating_polls.add(msg.id);
this.twitch_data.getPoll(msg.id).then(data => {
if ( ! this.updating_polls.has(msg.id) )
return;
if ( ! data )
data = {
id: msg.id,
status: 'COMPLETED',
choices: []
};
this.handlePoll(data, true);
});
this.emit(':update');
return;
}
if ( type === 'end' ) {
if ( ! msg.id )
this.send('error', {id: 'no_id'});
this.twitch_data.terminatePoll(msg.id).then(() => {
this.updating_polls.delete(msg.id);
this.polls.delete(msg.id);
this.send('ended', {id: msg.id})
});
this.emit(':update');
return;
}
this.send('error', {id: 'unknown_command'});
}
handlePoll(data, override = false, type = 'update') {
if ( ! this.active || ! this._socket )
return;
const poll = formatPoll(data);
this.updating_polls.delete(poll.id);
if ( ! this.polls.has(poll.id) && ! override )
return;
if ( poll.ended ) {
this.polls.delete(poll.id);
this.emit(':update');
}
this.send(type, {poll});
}
}
PollShim.register();
function formatPoll(data, poll_id) {
if ( ! data )
return {
id: poll_id,
ended: true,
choices: []
};
const ended = data.status !== 'ACTIVE';
let duration = data.durationSeconds || data.duration_seconds,
remaining = data.remaining_duration_milliseconds;
if ( duration == null ) {
const ended = data.endedAt || data.ended_at,
started = data.startedAt || data.startedAt;
if ( started && ended )
duration = Math.floor((Date.parse(ended) - Date.parse(started)) / 1000);
else
duration = 0;
}
if ( remaining == null ) {
if ( ended )
remaining = 0;
else {
const started = data.startedAt || data.startedAt;
if ( started )
remaining = Math.max(0, (duration * 1000) - (Date.now() - Date.parse(started)));
else
remaining = duration * 1000;
}
}
const choices = [],
out = {
id: data.id || data.poll_id,
ended,
duration,
remaining,
choices
};
for(const choice of data.choices)
if ( choice )
choices.push({
text: choice.title,
votes: choice.votes?.total || 0
});
return out;
}