thelounge/client/js/webpush.js

107 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-07-10 19:47:03 +00:00
"use strict";
2019-11-16 17:24:03 +00:00
import socket from "./socket";
import store from "./store";
import {switchToChannel} from "./router";
export default {togglePushSubscription};
2017-07-10 19:47:03 +00:00
if ("serviceWorker" in navigator) {
navigator.serviceWorker.addEventListener("message", (event) => {
if (event.data && event.data.type === "open") {
const id = parseInt(event.data.channel.substr(5), 10); // remove "chan-" prefix
2019-11-07 09:29:04 +00:00
const channelTarget = store.getters.findChannel(id);
2019-11-04 14:11:17 +00:00
if (channelTarget) {
switchToChannel(channelTarget.channel);
2019-11-04 14:11:17 +00:00
}
}
});
}
2019-11-12 20:03:59 +00:00
socket.once("push:issubscribed", function(hasSubscriptionOnServer) {
2017-11-07 20:22:16 +00:00
if (!isAllowedServiceWorkersHost()) {
store.commit("pushNotificationState", "nohttps");
2017-11-07 20:22:16 +00:00
return;
}
2019-11-12 20:03:59 +00:00
if (!("serviceWorker" in navigator)) {
return;
}
2019-11-12 20:03:59 +00:00
navigator.serviceWorker
.register("service-worker.js")
.then((registration) => {
2019-11-16 17:24:03 +00:00
store.commit("hasServiceWorker");
2017-07-10 19:47:03 +00:00
2019-11-12 20:03:59 +00:00
if (!registration.pushManager) {
return;
}
2017-07-10 19:47:03 +00:00
2019-11-12 20:03:59 +00:00
return registration.pushManager.getSubscription().then((subscription) => {
// If client has push registration but the server knows nothing about it,
// this subscription is broken and client has to register again
if (subscription && hasSubscriptionOnServer === false) {
subscription.unsubscribe().then((successful) => {
store.commit(
"pushNotificationState",
successful ? "supported" : "unsupported"
);
});
} else {
2019-11-04 14:11:17 +00:00
store.commit(
"pushNotificationState",
2019-11-12 20:03:59 +00:00
subscription ? "subscribed" : "supported"
2019-11-04 14:11:17 +00:00
);
2019-11-12 20:03:59 +00:00
}
});
2019-11-12 20:03:59 +00:00
})
.catch((err) => {
store.commit("pushNotificationState", "unsupported");
console.error(err); // eslint-disable-line no-console
});
});
2017-07-10 19:47:03 +00:00
2019-11-16 17:24:03 +00:00
function togglePushSubscription() {
2019-11-04 14:11:17 +00:00
store.commit("pushNotificationState", "loading");
2017-07-10 19:47:03 +00:00
2019-07-17 09:33:59 +00:00
navigator.serviceWorker.ready
.then((registration) =>
2019-11-04 14:11:17 +00:00
registration.pushManager.getSubscription().then((existingSubscription) => {
if (existingSubscription) {
socket.emit("push:unregister");
2019-11-12 20:03:59 +00:00
return existingSubscription.unsubscribe().then((successful) => {
store.commit(
"pushNotificationState",
successful ? "supported" : "unsupported"
);
2019-11-04 14:11:17 +00:00
});
}
return registration.pushManager
.subscribe({
2019-11-12 20:03:59 +00:00
applicationServerKey: store.state.serverConfiguration.applicationServerKey,
2019-11-04 14:11:17 +00:00
userVisibleOnly: true,
})
.then((subscription) => {
2019-11-12 20:03:59 +00:00
socket.emit("push:register", subscription.toJSON());
2019-11-04 14:11:17 +00:00
store.commit("pushNotificationState", "subscribed");
});
})
2019-07-17 09:33:59 +00:00
)
2019-11-12 20:03:59 +00:00
.catch((err) => {
store.commit("pushNotificationState", "unsupported");
2019-11-12 20:03:59 +00:00
console.error(err); // eslint-disable-line no-console
2019-07-17 09:33:59 +00:00
});
2019-11-16 17:24:03 +00:00
}
2017-07-10 19:47:03 +00:00
function isAllowedServiceWorkersHost() {
2019-07-17 09:33:59 +00:00
return (
location.protocol === "https:" ||
location.hostname === "localhost" ||
location.hostname === "127.0.0.1"
);
2017-07-10 19:47:03 +00:00
}