2017-07-10 19:47:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
|
|
|
const storage = require("./localStorage");
|
|
|
|
const socket = require("./socket");
|
|
|
|
|
2017-11-07 20:22:16 +00:00
|
|
|
let pushNotificationsButton;
|
2017-07-10 19:47:03 +00:00
|
|
|
let clientSubscribed = null;
|
|
|
|
let applicationServerKey;
|
|
|
|
|
2017-12-24 14:06:23 +00:00
|
|
|
if ("serviceWorker" in navigator) {
|
|
|
|
navigator.serviceWorker.addEventListener("message", (event) => {
|
|
|
|
if (event.data && event.data.type === "open") {
|
2018-01-30 09:38:33 +00:00
|
|
|
$("#sidebar").find(`.chan[data-target="#${event.data.channel}"]`).trigger("click");
|
2017-12-24 14:06:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-28 08:53:32 +00:00
|
|
|
module.exports.hasServiceWorker = false;
|
|
|
|
|
2017-07-10 19:47:03 +00:00
|
|
|
module.exports.configurePushNotifications = (subscribedOnServer, key) => {
|
|
|
|
applicationServerKey = key;
|
|
|
|
|
|
|
|
// If client has push registration but the server knows nothing about it,
|
|
|
|
// this subscription is broken and client has to register again
|
|
|
|
if (clientSubscribed === true && subscribedOnServer === false) {
|
2018-01-30 09:38:33 +00:00
|
|
|
pushNotificationsButton.prop("disabled", true);
|
2017-07-10 19:47:03 +00:00
|
|
|
|
2017-09-28 08:53:32 +00:00
|
|
|
navigator.serviceWorker.ready
|
2017-07-10 19:47:03 +00:00
|
|
|
.then((registration) => registration.pushManager.getSubscription())
|
|
|
|
.then((subscription) => subscription && subscription.unsubscribe())
|
|
|
|
.then((successful) => {
|
|
|
|
if (successful) {
|
2018-01-30 09:38:33 +00:00
|
|
|
alternatePushButton().prop("disabled", false);
|
2017-07-10 19:47:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-07 20:22:16 +00:00
|
|
|
module.exports.initialize = () => {
|
|
|
|
pushNotificationsButton = $("#pushNotifications");
|
|
|
|
|
|
|
|
if (!isAllowedServiceWorkersHost()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-10 19:47:03 +00:00
|
|
|
$("#pushNotificationsHttps").hide();
|
|
|
|
|
|
|
|
if ("serviceWorker" in navigator) {
|
|
|
|
navigator.serviceWorker.register("service-worker.js").then((registration) => {
|
2017-09-28 08:53:32 +00:00
|
|
|
module.exports.hasServiceWorker = true;
|
|
|
|
|
2017-07-10 19:47:03 +00:00
|
|
|
if (!registration.pushManager) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return registration.pushManager.getSubscription().then((subscription) => {
|
|
|
|
$("#pushNotificationsUnsupported").hide();
|
|
|
|
|
|
|
|
pushNotificationsButton
|
2018-01-30 09:38:33 +00:00
|
|
|
.prop("disabled", false)
|
2017-07-10 19:47:03 +00:00
|
|
|
.on("click", onPushButton);
|
|
|
|
|
|
|
|
clientSubscribed = !!subscription;
|
|
|
|
|
|
|
|
if (clientSubscribed) {
|
|
|
|
alternatePushButton();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
$("#pushNotificationsUnsupported span").text(err);
|
|
|
|
});
|
|
|
|
}
|
2017-11-07 20:22:16 +00:00
|
|
|
};
|
2017-07-10 19:47:03 +00:00
|
|
|
|
|
|
|
function onPushButton() {
|
2018-01-30 09:38:33 +00:00
|
|
|
pushNotificationsButton.prop("disabled", true);
|
2017-07-10 19:47:03 +00:00
|
|
|
|
2017-12-24 20:04:27 +00:00
|
|
|
navigator.serviceWorker.ready.then((registration) =>
|
2017-07-10 19:47:03 +00:00
|
|
|
registration.pushManager.getSubscription().then((existingSubscription) => {
|
|
|
|
if (existingSubscription) {
|
|
|
|
socket.emit("push:unregister");
|
|
|
|
|
|
|
|
return existingSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
return registration.pushManager.subscribe({
|
|
|
|
applicationServerKey: urlBase64ToUint8Array(applicationServerKey),
|
2017-11-15 06:35:15 +00:00
|
|
|
userVisibleOnly: true,
|
2017-07-10 19:47:03 +00:00
|
|
|
}).then((subscription) => {
|
|
|
|
const rawKey = subscription.getKey ? subscription.getKey("p256dh") : "";
|
2017-11-22 06:39:32 +00:00
|
|
|
const key = rawKey ? window.btoa(String.fromCharCode(...new Uint8Array(rawKey))) : "";
|
2017-07-10 19:47:03 +00:00
|
|
|
const rawAuthSecret = subscription.getKey ? subscription.getKey("auth") : "";
|
2017-11-22 06:39:32 +00:00
|
|
|
const authSecret = rawAuthSecret ? window.btoa(String.fromCharCode(...new Uint8Array(rawAuthSecret))) : "";
|
2017-07-10 19:47:03 +00:00
|
|
|
|
|
|
|
socket.emit("push:register", {
|
|
|
|
token: storage.get("token"),
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
p256dh: key,
|
2017-11-15 06:35:15 +00:00
|
|
|
auth: authSecret,
|
|
|
|
},
|
2017-07-10 19:47:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}).then((successful) => {
|
|
|
|
if (successful) {
|
2018-01-30 09:38:33 +00:00
|
|
|
alternatePushButton().prop("disabled", false);
|
2017-07-10 19:47:03 +00:00
|
|
|
}
|
2017-12-24 20:04:27 +00:00
|
|
|
})
|
|
|
|
).catch((err) => {
|
|
|
|
$("#pushNotificationsUnsupported")
|
2018-03-16 22:44:19 +00:00
|
|
|
.find("span").text(`An error has occurred: ${err}`).end()
|
2017-12-24 20:04:27 +00:00
|
|
|
.show();
|
2017-07-10 19:47:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function alternatePushButton() {
|
|
|
|
const text = pushNotificationsButton.text();
|
|
|
|
|
|
|
|
return pushNotificationsButton
|
|
|
|
.text(pushNotificationsButton.data("text-alternate"))
|
|
|
|
.data("text-alternate", text);
|
|
|
|
}
|
|
|
|
|
|
|
|
function urlBase64ToUint8Array(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAllowedServiceWorkersHost() {
|
|
|
|
return location.protocol === "https:" || location.hostname === "localhost" || location.hostname === "127.0.0.1";
|
|
|
|
}
|