From cc93cd5d6cf302140d9df56e64dc6e902ce56708 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Tue, 4 Oct 2022 01:15:47 +0900 Subject: [Glitch] Disable push notification when not logged in Port 216dbaedaf587db834cfdd53b896e9c7e1284d9c to glitch-soc Signed-off-by: Claire --- app/javascript/flavours/glitch/util/main.js | 41 ++++++++++++++++++----------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'app/javascript/flavours/glitch/util/main.js') diff --git a/app/javascript/flavours/glitch/util/main.js b/app/javascript/flavours/glitch/util/main.js index 9e76774b6..b4e6bc151 100644 --- a/app/javascript/flavours/glitch/util/main.js +++ b/app/javascript/flavours/glitch/util/main.js @@ -1,12 +1,14 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import * as registerPushNotifications from 'flavours/glitch/actions/push_notifications'; import { setupBrowserNotifications } from 'flavours/glitch/actions/notifications'; import Mastodon, { store } from 'flavours/glitch/containers/mastodon'; import ready from 'flavours/glitch/util/ready'; -const perf = require('./performance'); +const perf = require('flavours/glitch/util/performance'); +/** + * @returns {Promise} + */ function main() { perf.start('main()'); @@ -18,7 +20,7 @@ function main() { } } - ready(() => { + return ready(async () => { const mountNode = document.getElementById('mastodon'); const props = JSON.parse(mountNode.getAttribute('data-props')); @@ -26,19 +28,28 @@ function main() { store.dispatch(setupBrowserNotifications()); if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - import('workbox-window') - .then(({ Workbox }) => { - const wb = new Workbox('/sw.js'); - - return wb.register(); - }) - .then(() => { - store.dispatch(registerPushNotifications.register()); - }) - .catch(err => { - console.error(err); - }); + const [{ Workbox }, { me }] = await Promise.all([ + import('workbox-window'), + import('mastodon/initial_state'), + ]); + + const wb = new Workbox('/sw.js'); + + try { + await wb.register(); + } catch (err) { + console.error(err); + + return; + } + + if (me) { + const registerPushNotifications = await import('flavours/glitch/actions/push_notifications'); + + store.dispatch(registerPushNotifications.register()); + } } + perf.stop('main()'); }); } -- cgit