kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Pull VAPID key from V2 instance
rodzic
80ed516f82
commit
76a7cd5f0a
|
@ -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<string, any> }) => {
|
||||
|
|
|
@ -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<IUI> = ({ 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<IUI> = ({ children }) => {
|
|||
}, [!!account]);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(registerPushNotifications());
|
||||
if (vapidKey) {
|
||||
dispatch(registerPushNotifications(vapidKey));
|
||||
}
|
||||
}, [vapidKey]);
|
||||
|
||||
const shouldHideFAB = (): boolean => {
|
||||
|
|
|
@ -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;
|
Ładowanie…
Reference in New Issue