thelounge/client/js/webpush.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-07-10 21:47:03 +02:00
"use strict";
2019-11-16 18:24:03 +01:00
import socket from "./socket";
import store from "./store";
export default {togglePushSubscription};
2017-07-10 21:47:03 +02:00
socket.once("push:issubscribed", function (hasSubscriptionOnServer) {
2017-11-07 21:22:16 +01:00
if (!isAllowedServiceWorkersHost()) {
store.commit("pushNotificationState", "nohttps");
2017-11-07 21:22:16 +01:00
return;
}
2019-11-12 21:03:59 +01:00
if (!("serviceWorker" in navigator)) {
return;
}
2019-11-12 21:03:59 +01:00
navigator.serviceWorker
.register("service-worker.js")
.then((registration) => {
2019-11-16 18:24:03 +01:00
store.commit("hasServiceWorker");
2017-07-10 21:47:03 +02:00
2019-11-12 21:03:59 +01:00
if (!registration.pushManager) {
return;
}
2017-07-10 21:47:03 +02:00
2019-11-12 21:03:59 +01: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 15:11:17 +01:00
store.commit(
"pushNotificationState",
2019-11-12 21:03:59 +01:00
subscription ? "subscribed" : "supported"
2019-11-04 15:11:17 +01:00
);
2019-11-12 21:03:59 +01:00
}
});
2019-11-12 21:03:59 +01:00
})
.catch((err) => {
store.commit("pushNotificationState", "unsupported");
console.error(err); // eslint-disable-line no-console
});
});
2017-07-10 21:47:03 +02:00
2019-11-16 18:24:03 +01:00
function togglePushSubscription() {
2019-11-04 15:11:17 +01:00
store.commit("pushNotificationState", "loading");
2017-07-10 21:47:03 +02:00
2019-07-17 11:33:59 +02:00
navigator.serviceWorker.ready
.then((registration) =>
2019-11-04 15:11:17 +01:00
registration.pushManager.getSubscription().then((existingSubscription) => {
if (existingSubscription) {
socket.emit("push:unregister");
2019-11-12 21:03:59 +01:00
return existingSubscription.unsubscribe().then((successful) => {
store.commit(
"pushNotificationState",
successful ? "supported" : "unsupported"
);
2019-11-04 15:11:17 +01:00
});
}
return registration.pushManager
.subscribe({
2019-11-12 21:03:59 +01:00
applicationServerKey: store.state.serverConfiguration.applicationServerKey,
2019-11-04 15:11:17 +01:00
userVisibleOnly: true,
})
.then((subscription) => {
2019-11-12 21:03:59 +01:00
socket.emit("push:register", subscription.toJSON());
2019-11-04 15:11:17 +01:00
store.commit("pushNotificationState", "subscribed");
store.commit("refreshDesktopNotificationState");
2019-11-04 15:11:17 +01:00
});
})
2019-07-17 11:33:59 +02:00
)
2019-11-12 21:03:59 +01:00
.catch((err) => {
store.commit("pushNotificationState", "unsupported");
store.commit("refreshDesktopNotificationState");
2019-11-12 21:03:59 +01:00
console.error(err); // eslint-disable-line no-console
2019-07-17 11:33:59 +02:00
});
2019-11-16 18:24:03 +01:00
}
2017-07-10 21:47:03 +02:00
function isAllowedServiceWorkersHost() {
2019-07-17 11:33:59 +02:00
return (
location.protocol === "https:" ||
location.hostname === "localhost" ||
location.hostname === "127.0.0.1"
);
2017-07-10 21:47:03 +02:00
}