Skip to content

Commit

Permalink
Disable push notification when not logged in (mastodon#19272)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Oct 3, 2022
1 parent 37eaa7f commit 216dbae
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 48 deletions.
26 changes: 10 additions & 16 deletions app/javascript/mastodon/actions/push_notifications/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import {
SET_BROWSER_SUPPORT,
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,
};
import { setAlerts } from './setter';
import { saveSettings } from './registerer';

export function changeAlerts(path, value) {
return dispatch => {
dispatch(setAlerts(path, value));
dispatch(saveSettings());
};
}

export {
CLEAR_SUBSCRIPTION,
SET_BROWSER_SUPPORT,
SET_SUBSCRIPTION,
SET_ALERTS,
} from './setter';
export { register } from './registerer';
41 changes: 26 additions & 15 deletions app/javascript/mastodon/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import * as registerPushNotifications from 'mastodon/actions/push_notifications';
import { setupBrowserNotifications } from 'mastodon/actions/notifications';
import Mastodon, { store } from 'mastodon/containers/mastodon';
import ready from 'mastodon/ready';

const perf = require('./performance');
const perf = require('mastodon/performance');

/**
* @returns {Promise<void>}
*/
function main() {
perf.start('main()');

Expand All @@ -18,27 +20,36 @@ function main() {
}
}

ready(() => {
return ready(async () => {
const mountNode = document.getElementById('mastodon');
const props = JSON.parse(mountNode.getAttribute('data-props'));

ReactDOM.render(<Mastodon {...props} />, mountNode);
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('mastodon/actions/push_notifications');

store.dispatch(registerPushNotifications.register());
}
}

perf.stop('main()');
});
}
Expand Down
37 changes: 31 additions & 6 deletions app/javascript/mastodon/ready.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
export default function ready(loaded) {
if (['interactive', 'complete'].includes(document.readyState)) {
loaded();
} else {
document.addEventListener('DOMContentLoaded', 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)) {
loaded();
} else {
document.addEventListener('DOMContentLoaded', loaded);
}
});
}
6 changes: 4 additions & 2 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { start } from '../mastodon/common';

start();

loadPolyfills().then(() => {
require('../mastodon/main').default();
loadPolyfills().then(async () => {
const { default: main } = import('mastodon/main');

return main();
}).catch(e => {
console.error(e);
});
13 changes: 4 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3211,15 +3211,10 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001219:
version "1.0.30001310"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001310.tgz#da02cd07432c9eece6992689d1b84ca18139eea8"
integrity sha512-cb9xTV8k9HTIUA3GnPUJCk0meUnrHL5gy5QePfDjxHyNBcnzPzrHFv5GqfP7ue5b1ZyzZL0RJboD6hQlPXjhjg==

caniuse-lite@^1.0.30001400:
version "1.0.30001406"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001406.tgz#d0146e7919635479f873b4507517b627f66ab269"
integrity sha512-bWTlaXUy/rq0BBtYShc/jArYfBPjEV95euvZ8JVtO43oQExEN/WquoqpufFjNu4kSpi5cy5kMbNvzztWDfv1Jg==
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001400:
version "1.0.30001414"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001414.tgz"
integrity sha512-t55jfSaWjCdocnFdKQoO+d2ct9C59UZg4dY3OnUlSZ447r8pUtIKdp0hpAzrGFultmTC+Us+KpKi4GZl/LXlFg==

chalk@^1.1.3:
version "1.1.3"
Expand Down

0 comments on commit 216dbae

Please sign in to comment.