-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebpush.js
More file actions
662 lines (568 loc) · 17.2 KB
/
webpush.js
File metadata and controls
662 lines (568 loc) · 17.2 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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/* global phpbb */
function PhpbbWebpush() {
'use strict';
/** @type {string} URL to service worker */
let serviceWorkerUrl = '';
/** @type {string} URL to subscribe to push */
let subscribeUrl = '';
/** @type {string} URL to unsubscribe from push */
let unsubscribeUrl = '';
/** @type { {creationTime: number, formToken: string} } Form tokens */
this.formTokens = {
creationTime: 0,
formToken: '',
};
/** @type {{endpoint: string, expirationTime: number}[]} Subscriptions */
let subscriptions;
/** @type {string} Title of an error message */
let ajaxErrorTitle = '';
/** @type {string} VAPID public key */
let vapidPublicKey = '';
/** @type {HTMLElement} Subscribe button */
let subscribeButton;
/** @type {HTMLElement} Unsubscribe button */
let unsubscribeButton;
/** @type {HTMLElement} Toggle popup button */
let togglePopupButton;
/** @type {string} URL to toggle popup prompt preference */
let togglePopupUrl = '';
/** @type {function} Escape key handler for popup */
let popupEscapeHandler;
/**
* Init function for phpBB Web Push
*
* @param {Object} options Init options
*/
this.init = function(options) {
serviceWorkerUrl = options.serviceWorkerUrl;
subscribeUrl = options.subscribeUrl;
unsubscribeUrl = options.unsubscribeUrl;
togglePopupUrl = options.togglePopupUrl;
this.formTokens = options.formTokens;
subscriptions = options.subscriptions;
ajaxErrorTitle = options.ajaxErrorTitle;
vapidPublicKey = options.vapidPublicKey;
subscribeButton = document.querySelector('#subscribe_webpush');
unsubscribeButton = document.querySelector('#unsubscribe_webpush');
togglePopupButton = document.querySelector('#toggle_popup_prompt');
// Set up toggle popup button handler if it exists (on UCP settings page)
if (togglePopupButton) {
togglePopupButton.addEventListener('click', togglePopupHandler);
}
// Service workers are only supported in secure context
if (window.isSecureContext !== true) {
setDisabledState();
return;
}
if ('serviceWorker' in navigator && 'PushManager' in window) {
navigator.serviceWorker.register(serviceWorkerUrl)
.then(async() => {
subscribeButton.addEventListener('click', subscribeButtonHandler);
unsubscribeButton.addEventListener('click', unsubscribeButtonHandler);
await updateButtonState();
initPopupPrompt();
})
.catch(error => {
console.info(error);
// Service worker could not be registered
setDisabledState();
});
} else {
setDisabledState();
}
};
/**
* If subscribing is disabled, hide the dropdown toggle and update the subscription button text
*
* @returns {void}
*/
function setDisabledState() {
subscribeButton.disabled = true;
const notificationList = document.getElementById('notification_list');
const subscribeToggle = notificationList.querySelector('.wpn-notification-dropdown-footer');
if (subscribeToggle) {
subscribeToggle.style.display = 'none';
}
if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) {
subscribeButton.value = subscribeButton.getAttribute('data-l-unsupported');
}
}
/**
* Update button state depending on the notification state
*
* @returns {Promise<void>}
*/
async function updateButtonState() {
setSubscriptionState(false);
if (Notification.permission !== 'granted') {
return;
}
try {
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
if (typeof registration === 'undefined') {
return;
}
const subscription = await registration.pushManager.getSubscription();
if (!subscription) {
return;
}
if (shouldRefreshSubscription(subscription)) {
await refreshSubscription(registration, subscription);
return;
}
setSubscriptionState(true);
} catch (error) {
console.error('Failed to update Web Push subscription state:', error);
}
}
/**
* Initialize popup prompt
*/
function initPopupPrompt() {
const popup = document.getElementById('wpn_popup_prompt');
if (!popup || Notification.permission === 'denied') {
return;
}
// Check if user denied prompt on this browser
if (promptDenied.get() === 'true') {
return;
}
// Check if this browser already has a subscription
navigator.serviceWorker.getRegistration(serviceWorkerUrl)
.then(registration => {
if (typeof registration === 'undefined') {
showPopup(popup);
return;
}
registration.pushManager.getSubscription()
.then(subscription => {
if (shouldRefreshSubscription(subscription)) {
showPopup(popup);
}
});
});
}
/**
* Show popup with event handlers
*
* @param {HTMLElement} popup
*/
function showPopup(popup) {
setTimeout(() => {
popup.style.display = 'flex';
}, 1000);
const allowBtn = document.getElementById('wpn_popup_allow');
const denyBtn = document.getElementById('wpn_popup_deny');
const overlay = document.getElementById('wpn_popup_prompt');
if (allowBtn) {
allowBtn.addEventListener('click', (event) => {
event.stopPropagation();
hidePopup(popup);
subscribeButtonHandler(event).catch(error => {
console.error('Subscription handler error:', error);
});
});
}
if (denyBtn) {
denyBtn.addEventListener('click', (event) => {
event.stopPropagation();
hidePopup(popup);
promptDenied.set();
});
}
if (overlay) {
overlay.addEventListener('click', (event) => {
if (event.target === overlay) {
hidePopup(popup);
promptDenied.set();
}
});
popupEscapeHandler = (event) => {
if (event.key === 'Escape') {
hidePopup(popup);
promptDenied.set();
}
};
document.addEventListener('keydown', popupEscapeHandler);
}
}
/**
* Hide popup
*
* @param {HTMLElement|null} popup
*/
function hidePopup(popup) {
if (popup) {
popup.style.display = 'none';
}
document.removeEventListener('keydown', popupEscapeHandler);
popupEscapeHandler = null;
}
/**
* Check whether a subscription is valid
*
* @param {PushSubscription} subscription
* @returns {boolean}
*/
const isValidSubscription = subscription => {
if (!subscription) {
return false;
}
if (subscription.expirationTime && subscription.expirationTime <= Date.now()) {
return false;
}
for (const curSubscription of subscriptions) {
if (subscription.endpoint === curSubscription.endpoint) {
return true;
}
}
// Subscription is not in valid subscription list for user
return false;
};
/**
* Check whether the current browser subscription uses the configured VAPID key
*
* @param {PushSubscription} subscription
* @returns {boolean}
*/
const hasCurrentVapidKey = subscription => {
if (!subscription || !subscription.options || !subscription.options.applicationServerKey) {
return true;
}
return uint8ArrayToUrlB64(new Uint8Array(subscription.options.applicationServerKey)) === vapidPublicKey;
};
/**
* Check whether a subscription should be recreated in the browser and backend
*
* @param {PushSubscription} subscription
* @returns {boolean}
*/
const shouldRefreshSubscription = subscription => !isValidSubscription(subscription) || !hasCurrentVapidKey(subscription);
/**
* Remove a cached subscription entry
*
* @param {string} endpoint
*/
function removeStoredSubscription(endpoint) {
if (!endpoint) {
return;
}
subscriptions = subscriptions.filter(subscription => subscription.endpoint !== endpoint);
}
/**
* Update cached subscriptions with the newest server state
*
* @param {PushSubscription} subscription
* @param {string} previousEndpoint
*/
function storeSubscription(subscription, previousEndpoint = '') {
removeStoredSubscription(previousEndpoint);
removeStoredSubscription(subscription.endpoint);
subscriptions.push({
endpoint: subscription.endpoint,
expirationTime: subscription.expirationTime || 0,
});
}
/**
* Convert a PushSubscription to the payload expected by the backend
*
* @param {PushSubscription} subscription
* @param {string} previousEndpoint
* @returns {Object}
*/
function getSubscriptionPayload(subscription, previousEndpoint = '') {
const payload = subscription.toJSON();
if (previousEndpoint) {
payload.previous_endpoint = previousEndpoint;
}
return payload;
}
/**
* Set subscription state for buttons
*
* @param {boolean} subscribed True if subscribed, false if not
*/
function setSubscriptionState(subscribed) {
if (subscribed) {
subscribeButton.classList.add('hidden');
unsubscribeButton.classList.remove('hidden');
} else {
subscribeButton.classList.remove('hidden');
unsubscribeButton.classList.add('hidden');
}
}
/**
* Persist a browser subscription to the backend
*
* @param {PushSubscription} subscription
* @param {string} previousEndpoint
* @returns {Promise<Object>}
*/
async function persistSubscription(subscription, previousEndpoint = '') {
const loadingIndicator = phpbb.loadingIndicator();
try {
const response = await fetch(subscribeUrl, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body: getFormData(getSubscriptionPayload(subscription, previousEndpoint)),
});
const data = await response.json();
if (!data.success) {
throw new Error(data.message || subscribeButton.getAttribute('data-l-unsupported'));
}
handleSubscribe(data, subscription, previousEndpoint);
return data;
} finally {
loadingIndicator.fadeOut(phpbb.alertTime);
}
}
/**
* Create a fresh browser subscription and store it in the backend
*
* @param {ServiceWorkerRegistration} registration
* @param {PushSubscription|null} previousSubscription
* @returns {Promise<PushSubscription>}
*/
async function refreshSubscription(registration, previousSubscription = null) {
const previousEndpoint = previousSubscription ? previousSubscription.endpoint : '';
if (previousSubscription) {
await previousSubscription.unsubscribe();
removeStoredSubscription(previousEndpoint);
}
const newSubscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(vapidPublicKey),
});
try {
await persistSubscription(newSubscription, previousEndpoint);
return newSubscription;
} catch (error) {
newSubscription.unsubscribe().catch(console.error);
throw error;
}
}
/**
* Handler for pushing subscribe button
*
* @param {Event} event Subscribe button push event
* @returns {Promise<void>}
*/
async function subscribeButtonHandler(event) {
event.preventDefault();
subscribeButton.removeEventListener('click', subscribeButtonHandler);
try {
// Prevent the user from clicking the subscribe button multiple times.
const result = await Notification.requestPermission();
if (result === 'denied') {
phpbb.alert(subscribeButton.getAttribute('data-l-err'), subscribeButton.getAttribute('data-l-msg'));
return;
}
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
// We might already have a subscription that is unknown to this instance of phpBB.
// Unsubscribe before trying to subscribe again.
if (typeof registration === 'undefined') {
throw new Error(subscribeButton.getAttribute('data-l-unsupported'));
}
try {
const subscribed = await registration.pushManager.getSubscription();
await refreshSubscription(registration, subscribed);
} catch (error) {
promptDenied.set();
hidePopup(document.getElementById('wpn_popup_prompt'));
phpbb.alert(ajaxErrorTitle, error.message || subscribeButton.getAttribute('data-l-unsupported'));
}
} catch (error) {
promptDenied.set(); // deny the prompt on error to prevent repeated prompting
hidePopup(document.getElementById('wpn_popup_prompt'));
console.error('Push subscription error:', error);
phpbb.alert(subscribeButton.getAttribute('data-l-err'), error.message || subscribeButton.getAttribute('data-l-unsupported'));
} finally {
subscribeButton.addEventListener('click', subscribeButtonHandler);
}
}
/**
* Handler for pushing unsubscribe button
*
* @param {Event} event Unsubscribe button push event
* @returns {Promise<void>}
*/
async function unsubscribeButtonHandler(event) {
event.preventDefault();
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
if (typeof registration === 'undefined') {
return;
}
const subscription = await registration.pushManager.getSubscription();
if (!subscription) {
setSubscriptionState(false);
return;
}
const loadingIndicator = phpbb.loadingIndicator();
fetch(unsubscribeUrl, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body: getFormData({ endpoint: subscription.endpoint }),
})
.then(async(response) => {
let data = null;
try {
data = await response.json();
} catch (e) {
// Ignore JSON parsing failures and fall back below.
}
if (!response.ok || !data || !data.success) {
throw new Error(data && data.message ? data.message : 'Unsubscribe failed.');
}
const unsubscribed = await subscription.unsubscribe();
if (unsubscribed) {
removeStoredSubscription(subscription.endpoint);
setSubscriptionState(false);
}
})
.catch(error => {
phpbb.alert(ajaxErrorTitle, error.message || error);
})
.finally(() => {
loadingIndicator.fadeOut(phpbb.alertTime);
});
}
/**
* Handler for toggle popup prompt button
*
* @param {Event} event Toggle button push event
*/
function togglePopupHandler(event) {
event.preventDefault();
const loadingIndicator = phpbb.loadingIndicator();
const formData = new FormData();
formData.append('form_token', phpbb.webpush.formTokens.formToken);
formData.append('creation_time', phpbb.webpush.formTokens.creationTime.toString());
fetch(togglePopupUrl, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body: formData,
})
.then(response => response.json())
.then(data => {
loadingIndicator.fadeOut(phpbb.alertTime);
if (data.success) {
// Update button text based on new state
const button = document.getElementById('toggle_popup_prompt');
if (button) {
button.value = data.disabled ? button.getAttribute('data-l-enable') : button.getAttribute('data-l-disable');
}
if ('form_tokens' in data) {
updateFormTokens(data.form_tokens);
}
}
})
.catch(error => {
loadingIndicator.fadeOut(phpbb.alertTime);
phpbb.alert(ajaxErrorTitle, error);
});
}
/**
* Handle subscribe response
*
* @param {{success: boolean, form_tokens?: Object}} response Response from subscription endpoint
* @param {PushSubscription} subscription Browser subscription
* @param {string} previousEndpoint Previous endpoint for refreshed subscriptions
*/
function handleSubscribe(response, subscription, previousEndpoint = '') {
if (response.success) {
storeSubscription(subscription, previousEndpoint);
setSubscriptionState(true);
if ('form_tokens' in response) {
updateFormTokens(response.form_tokens);
}
promptDenied.remove();
hidePopup(document.getElementById('wpn_popup_prompt'));
}
}
/**
* Get form data object including form tokens
*
* @param {Object} data Data to create form data from
* @returns {FormData} Form data
*/
function getFormData(data) {
const formData = new FormData();
formData.append('form_token', phpbb.webpush.formTokens.formToken);
formData.append('creation_time', phpbb.webpush.formTokens.creationTime.toString());
formData.append('data', JSON.stringify(data));
return formData;
}
/**
* Update form tokens with supplied ones
*
* @param {{creation_time: number, form_token: string}} formTokens
*/
function updateFormTokens(formTokens) {
phpbb.webpush.formTokens.creationTime = formTokens.creation_time;
phpbb.webpush.formTokens.formToken = formTokens.form_token;
}
/**
* Convert a base64 string to Uint8Array
*
* @param {string} base64String
* @returns {Uint8Array}
*/
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
/**
* Convert a Uint8Array to a URL-safe base64 string
*
* @param {Uint8Array} value
* @returns {string}
*/
function uint8ArrayToUrlB64(value) {
let stringValue = '';
for (let i = 0; i < value.length; i++) {
stringValue += String.fromCharCode(value[i]);
}
return window.btoa(stringValue)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/u, '');
}
const promptDenied = {
key: 'wpn_popup_denied',
set() {
localStorage.setItem(this.key, 'true');
},
get() {
return localStorage.getItem(this.key);
},
remove() {
localStorage.removeItem(this.key);
}
};
}
function domReady(callBack) {
'use strict';
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callBack);
} else {
callBack();
}
}
phpbb.webpush = new PhpbbWebpush();
domReady(() => {
'use strict';
/* global phpbbWebpushOptions */
phpbb.webpush.init(phpbbWebpushOptions);
});