2022-09-11 11:42:40 -07:00
|
|
|
import { get, post } from '@rails/request.js';
|
|
|
|
import I18n from "retrospring/i18n";
|
|
|
|
import { showNotification } from "utilities/notifications";
|
2023-01-21 11:50:06 -08:00
|
|
|
import { Buffer } from "buffer";
|
2022-09-11 11:42:40 -07:00
|
|
|
|
|
|
|
export function enableHandler (event: Event): void {
|
|
|
|
event.preventDefault();
|
2022-10-23 07:00:21 -07:00
|
|
|
const sender = event.target as HTMLButtonElement;
|
2022-09-11 11:42:40 -07:00
|
|
|
|
|
|
|
try {
|
2023-02-23 07:52:33 -08:00
|
|
|
getServiceWorker()
|
2022-09-11 11:42:40 -07:00
|
|
|
.then(subscribe)
|
|
|
|
.then(async subscription => {
|
|
|
|
return Notification.requestPermission().then(permission => {
|
|
|
|
if (permission != "granted") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-20 10:02:12 -07:00
|
|
|
post('/ajax/webpush', {
|
2022-09-11 11:42:40 -07:00
|
|
|
body: {
|
|
|
|
subscription
|
|
|
|
},
|
|
|
|
contentType: 'application/json'
|
|
|
|
}).then(async response => {
|
|
|
|
const data = await response.json;
|
|
|
|
|
|
|
|
if (data.success) {
|
|
|
|
new Notification(I18n.translate("frontend.push_notifications.subscribe.success.title"), {
|
|
|
|
body: I18n.translate("frontend.push_notifications.subscribe.success.body")
|
|
|
|
});
|
2022-10-23 07:00:21 -07:00
|
|
|
|
|
|
|
document.querySelectorAll<HTMLButtonElement>('button[data-action="push-disable"], button[data-action="push-remove-all"]')
|
|
|
|
.forEach(button => button.classList.remove('d-none'));
|
|
|
|
|
|
|
|
sender.classList.add('d-none');
|
2023-01-02 04:45:24 -08:00
|
|
|
document.querySelector<HTMLDivElement>('.push-settings')?.classList.add('d-none');
|
|
|
|
localStorage.setItem('dismiss-push-settings-prompt', 'true');
|
2022-10-23 07:00:21 -07:00
|
|
|
|
|
|
|
document.getElementById('subscription-count').textContent = data.message;
|
2022-09-11 11:42:40 -07:00
|
|
|
} else {
|
|
|
|
new Notification(I18n.translate("frontend.push_notifications.fail.title"), {
|
|
|
|
body: I18n.translate("frontend.push_notifications.fail.body")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to set up push notifications", error);
|
|
|
|
showNotification(I18n.translate("frontend.push_notifications.setup_fail"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 07:52:33 -08:00
|
|
|
async function getServiceWorker(): Promise<ServiceWorkerRegistration> {
|
|
|
|
return navigator.serviceWorker.getRegistration("/");
|
2022-09-11 11:42:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getServerKey(): Promise<Buffer> {
|
|
|
|
const response = await get("/ajax/webpush/key");
|
|
|
|
const data = await response.json;
|
|
|
|
return Buffer.from(data.key, 'base64');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function subscribe(registration: ServiceWorkerRegistration): Promise<PushSubscription> {
|
|
|
|
const key = await getServerKey();
|
|
|
|
return await registration.pushManager.subscribe({
|
|
|
|
userVisibleOnly: true,
|
|
|
|
applicationServerKey: key
|
|
|
|
});
|
|
|
|
}
|