[Glitch] Disable push notification when not logged in

Port 216dbaedaf to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
main
Yamagishi Kazutoshi 2022-10-04 01:15:47 +09:00 committed by Claire
parent d86cb4cab8
commit cc93cd5d6c
4 changed files with 70 additions and 38 deletions

View File

@ -1,19 +1,5 @@
import { import { setAlerts } from './setter';
SET_BROWSER_SUPPORT, import { saveSettings } from './registerer';
SET_SUBSCRIPTION,
CLEAR_SUBSCRIPTION,
SET_ALERTS,
setAlerts,
} from './setter';
import { register, saveSettings } from './registerer';
export {
SET_BROWSER_SUPPORT,
SET_SUBSCRIPTION,
CLEAR_SUBSCRIPTION,
SET_ALERTS,
register,
};
export function changeAlerts(path, value) { export function changeAlerts(path, value) {
return dispatch => { return dispatch => {
@ -21,3 +7,11 @@ export function changeAlerts(path, value) {
dispatch(saveSettings()); dispatch(saveSettings());
}; };
} }
export {
CLEAR_SUBSCRIPTION,
SET_BROWSER_SUPPORT,
SET_SUBSCRIPTION,
SET_ALERTS,
} from './setter';
export { register } from './registerer';

View File

@ -1,8 +1,10 @@
import 'packs/public-path'; import 'packs/public-path';
import loadPolyfills from 'flavours/glitch/util/load_polyfills'; import loadPolyfills from 'flavours/glitch/util/load_polyfills';
loadPolyfills().then(() => { loadPolyfills().then(async () => {
require('flavours/glitch/util/main').default(); const { default: main } = import('flavours/glitch/util/main');
return main();
}).catch(e => { }).catch(e => {
console.error(e); console.error(e);
}); });

View File

@ -1,12 +1,14 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import * as registerPushNotifications from 'flavours/glitch/actions/push_notifications';
import { setupBrowserNotifications } from 'flavours/glitch/actions/notifications'; import { setupBrowserNotifications } from 'flavours/glitch/actions/notifications';
import Mastodon, { store } from 'flavours/glitch/containers/mastodon'; import Mastodon, { store } from 'flavours/glitch/containers/mastodon';
import ready from 'flavours/glitch/util/ready'; import ready from 'flavours/glitch/util/ready';
const perf = require('./performance'); const perf = require('flavours/glitch/util/performance');
/**
* @returns {Promise<void>}
*/
function main() { function main() {
perf.start('main()'); perf.start('main()');
@ -18,7 +20,7 @@ function main() {
} }
} }
ready(() => { return ready(async () => {
const mountNode = document.getElementById('mastodon'); const mountNode = document.getElementById('mastodon');
const props = JSON.parse(mountNode.getAttribute('data-props')); const props = JSON.parse(mountNode.getAttribute('data-props'));
@ -26,19 +28,28 @@ function main() {
store.dispatch(setupBrowserNotifications()); store.dispatch(setupBrowserNotifications());
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
import('workbox-window') const [{ Workbox }, { me }] = await Promise.all([
.then(({ Workbox }) => { import('workbox-window'),
import('mastodon/initial_state'),
]);
const wb = new Workbox('/sw.js'); const wb = new Workbox('/sw.js');
return wb.register(); try {
}) await wb.register();
.then(() => { } catch (err) {
store.dispatch(registerPushNotifications.register());
})
.catch(err => {
console.error(err); console.error(err);
});
return;
} }
if (me) {
const registerPushNotifications = await import('flavours/glitch/actions/push_notifications');
store.dispatch(registerPushNotifications.register());
}
}
perf.stop('main()'); perf.stop('main()');
}); });
} }

View File

@ -1,7 +1,32 @@
export default function ready(loaded) { // @ts-check
/**
* @param {(() => void) | (() => Promise<void>)} callback
* @returns {Promise<void>}
*/
export default function ready(callback) {
return new Promise((resolve, reject) => {
function loaded() {
let result;
try {
result = callback();
} catch (err) {
reject(err);
return;
}
if (typeof result?.then === 'function') {
result.then(resolve).catch(reject);
} else {
resolve();
}
}
if (['interactive', 'complete'].includes(document.readyState)) { if (['interactive', 'complete'].includes(document.readyState)) {
loaded(); loaded();
} else { } else {
document.addEventListener('DOMContentLoaded', loaded); document.addEventListener('DOMContentLoaded', loaded);
} }
});
} }