hardlounge/client/js/webpush.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

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