Disable push notification when not logged in (#19272)
This commit is contained in:
		
					parent
					
						
							
								37eaa7fc01
							
						
					
				
			
			
				commit
				
					
						216dbaedaf
					
				
			
		
					 5 changed files with 74 additions and 47 deletions
				
			
		|  | @ -1,19 +1,5 @@ | |||
| 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 => { | ||||
|  | @ -21,3 +7,11 @@ export function changeAlerts(path, value) { | |||
|     dispatch(saveSettings()); | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| export { | ||||
|   CLEAR_SUBSCRIPTION, | ||||
|   SET_BROWSER_SUPPORT, | ||||
|   SET_SUBSCRIPTION, | ||||
|   SET_ALERTS, | ||||
| } from './setter'; | ||||
| export { register } from './registerer'; | ||||
|  |  | |||
|  | @ -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()'); | ||||
| 
 | ||||
|  | @ -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'); | ||||
|       const [{ Workbox }, { me }] = await Promise.all([ | ||||
|         import('workbox-window'), | ||||
|         import('mastodon/initial_state'), | ||||
|       ]); | ||||
| 
 | ||||
|           return wb.register(); | ||||
|         }) | ||||
|         .then(() => { | ||||
|           store.dispatch(registerPushNotifications.register()); | ||||
|         }) | ||||
|         .catch(err => { | ||||
|           console.error(err); | ||||
|         }); | ||||
|       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()'); | ||||
|   }); | ||||
| } | ||||
|  |  | |||
|  | @ -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); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue