2019-11-16 17:24:03 +00:00
|
|
|
import socket from "./socket";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {store} from "./store";
|
2019-11-16 17:24:03 +00:00
|
|
|
|
|
|
|
export default {togglePushSubscription};
|
2017-07-10 19:47:03 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
socket.once("push:issubscribed", function (hasSubscriptionOnServer) {
|
2017-11-07 20:22:16 +00:00
|
|
|
if (!isAllowedServiceWorkersHost()) {
|
2019-11-02 19:40:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-09-28 08:53:32 +00:00
|
|
|
|
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) {
|
2022-06-19 00:25:21 +00:00
|
|
|
void subscription.unsubscribe().then((successful) => {
|
2019-11-12 20:03:59 +00:00
|
|
|
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-08-02 12:53:31 +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({
|
2022-06-19 00:25:21 +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");
|
2020-07-15 13:08:36 +00:00
|
|
|
store.commit("refreshDesktopNotificationState");
|
2019-11-04 14:11:17 +00:00
|
|
|
});
|
|
|
|
})
|
2019-07-17 09:33:59 +00:00
|
|
|
)
|
2019-11-12 20:03:59 +00:00
|
|
|
.catch((err) => {
|
2019-11-02 19:40:59 +00:00
|
|
|
store.commit("pushNotificationState", "unsupported");
|
2020-07-15 13:08:36 +00:00
|
|
|
store.commit("refreshDesktopNotificationState");
|
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
|
|
|
}
|