diff --git a/src/actions/push-notifications/registerer.ts b/src/actions/push-notifications/registerer.ts index 9937fd938..c521534c3 100644 --- a/src/actions/push-notifications/registerer.ts +++ b/src/actions/push-notifications/registerer.ts @@ -1,6 +1,5 @@ import { createPushSubscription, updatePushSubscription } from 'soapbox/actions/push-subscriptions'; import { pushNotificationsSetting } from 'soapbox/settings'; -import { getVapidKey } from 'soapbox/utils/auth'; import { decode as decodeBase64 } from 'soapbox/utils/base64'; import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; @@ -30,10 +29,10 @@ const getPushSubscription = (registration: ServiceWorkerRegistration) => registration.pushManager.getSubscription() .then(subscription => ({ registration, subscription })); -const subscribe = (registration: ServiceWorkerRegistration, getState: () => RootState) => +const subscribe = (vapidKey: string, registration: ServiceWorkerRegistration) => registration.pushManager.subscribe({ userVisibleOnly: true, - applicationServerKey: urlBase64ToUint8Array(getVapidKey(getState())), + applicationServerKey: urlBase64ToUint8Array(vapidKey), }); const unsubscribe = ({ registration, subscription }: { @@ -61,10 +60,9 @@ const sendSubscriptionToBackend = (subscription: PushSubscription, me: Me) => // eslint-disable-next-line compat/compat const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype); -const register = () => +const register = (vapidKey: string) => (dispatch: AppDispatch, getState: () => RootState) => { const me = getState().me; - const vapidKey = getVapidKey(getState()); dispatch(setBrowserSupport(supportsPushNotifications)); @@ -98,14 +96,14 @@ const register = () => } else { // Something went wrong, try to subscribe again return unsubscribe({ registration, subscription }).then((registration: ServiceWorkerRegistration) => { - return subscribe(registration, getState); + return subscribe(vapidKey, registration); }).then( (subscription: PushSubscription) => dispatch(sendSubscriptionToBackend(subscription, me) as any)); } } // No subscription, try to subscribe - return subscribe(registration, getState) + return subscribe(vapidKey, registration) .then(subscription => dispatch(sendSubscriptionToBackend(subscription, me) as any)); }) .then(({ subscription }: { subscription: PushSubscription | Record }) => { diff --git a/src/features/ui/index.tsx b/src/features/ui/index.tsx index 711f14289..ca65bc492 100644 --- a/src/features/ui/index.tsx +++ b/src/features/ui/index.tsx @@ -34,7 +34,6 @@ import ProfilePage from 'soapbox/pages/profile-page'; import RemoteInstancePage from 'soapbox/pages/remote-instance-page'; import SearchPage from 'soapbox/pages/search-page'; import StatusPage from 'soapbox/pages/status-page'; -import { getVapidKey } from 'soapbox/utils/auth'; import BackgroundShapes from './components/background-shapes'; import FloatingActionButton from './components/floating-action-button'; @@ -388,7 +387,7 @@ const UI: React.FC = ({ children }) => { const { account } = useOwnAccount(); const instance = useInstance(); const features = useFeatures(); - const vapidKey = useAppSelector(state => getVapidKey(state)); + const vapidKey = instance.instance.configuration.vapid.public_key; const dropdownMenuIsOpen = useAppSelector(state => state.dropdown_menu.isOpen); @@ -470,7 +469,9 @@ const UI: React.FC = ({ children }) => { }, [!!account]); useEffect(() => { - dispatch(registerPushNotifications()); + if (vapidKey) { + dispatch(registerPushNotifications(vapidKey)); + } }, [vapidKey]); const shouldHideFAB = (): boolean => { diff --git a/src/utils/auth.ts b/src/utils/auth.ts index b5d9fb120..73757fb80 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -62,8 +62,4 @@ export const getAuthUserUrl = (state: RootState) => { ].filter(url => url)).find(isURL); }; -/** Get the VAPID public key. */ -export const getVapidKey = (state: RootState) => - state.auth.app.vapid_key || state.instance.pleroma.vapid_public_key; - export const getMeUrl = (state: RootState) => selectOwnAccount(state)?.url; \ No newline at end of file