kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Merge branch 'v2-vapid' into 'main'
Pull VAPID key from V2 instance See merge request soapbox-pub/soapbox!3159environments/review-main-yi2y9f/deployments/4916
commit
b754eb62c2
|
@ -1,6 +1,5 @@
|
||||||
import { createPushSubscription, updatePushSubscription } from 'soapbox/actions/push-subscriptions';
|
import { createPushSubscription, updatePushSubscription } from 'soapbox/actions/push-subscriptions';
|
||||||
import { pushNotificationsSetting } from 'soapbox/settings';
|
import { pushNotificationsSetting } from 'soapbox/settings';
|
||||||
import { getVapidKey } from 'soapbox/utils/auth';
|
|
||||||
import { decode as decodeBase64 } from 'soapbox/utils/base64';
|
import { decode as decodeBase64 } from 'soapbox/utils/base64';
|
||||||
|
|
||||||
import { setBrowserSupport, setSubscription, clearSubscription } from './setter';
|
import { setBrowserSupport, setSubscription, clearSubscription } from './setter';
|
||||||
|
@ -30,10 +29,10 @@ const getPushSubscription = (registration: ServiceWorkerRegistration) =>
|
||||||
registration.pushManager.getSubscription()
|
registration.pushManager.getSubscription()
|
||||||
.then(subscription => ({ registration, subscription }));
|
.then(subscription => ({ registration, subscription }));
|
||||||
|
|
||||||
const subscribe = (registration: ServiceWorkerRegistration, getState: () => RootState) =>
|
const subscribe = (vapidKey: string, registration: ServiceWorkerRegistration) =>
|
||||||
registration.pushManager.subscribe({
|
registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
applicationServerKey: urlBase64ToUint8Array(getVapidKey(getState())),
|
applicationServerKey: urlBase64ToUint8Array(vapidKey),
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsubscribe = ({ registration, subscription }: {
|
const unsubscribe = ({ registration, subscription }: {
|
||||||
|
@ -61,10 +60,9 @@ const sendSubscriptionToBackend = (subscription: PushSubscription, me: Me) =>
|
||||||
// eslint-disable-next-line compat/compat
|
// eslint-disable-next-line compat/compat
|
||||||
const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype);
|
const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype);
|
||||||
|
|
||||||
const register = () =>
|
const register = (vapidKey: string) =>
|
||||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||||
const me = getState().me;
|
const me = getState().me;
|
||||||
const vapidKey = getVapidKey(getState());
|
|
||||||
|
|
||||||
dispatch(setBrowserSupport(supportsPushNotifications));
|
dispatch(setBrowserSupport(supportsPushNotifications));
|
||||||
|
|
||||||
|
@ -98,14 +96,14 @@ const register = () =>
|
||||||
} else {
|
} else {
|
||||||
// Something went wrong, try to subscribe again
|
// Something went wrong, try to subscribe again
|
||||||
return unsubscribe({ registration, subscription }).then((registration: ServiceWorkerRegistration) => {
|
return unsubscribe({ registration, subscription }).then((registration: ServiceWorkerRegistration) => {
|
||||||
return subscribe(registration, getState);
|
return subscribe(vapidKey, registration);
|
||||||
}).then(
|
}).then(
|
||||||
(subscription: PushSubscription) => dispatch(sendSubscriptionToBackend(subscription, me) as any));
|
(subscription: PushSubscription) => dispatch(sendSubscriptionToBackend(subscription, me) as any));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No subscription, try to subscribe
|
// No subscription, try to subscribe
|
||||||
return subscribe(registration, getState)
|
return subscribe(vapidKey, registration)
|
||||||
.then(subscription => dispatch(sendSubscriptionToBackend(subscription, me) as any));
|
.then(subscription => dispatch(sendSubscriptionToBackend(subscription, me) as any));
|
||||||
})
|
})
|
||||||
.then(({ subscription }: { subscription: PushSubscription | Record<string, 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 RemoteInstancePage from 'soapbox/pages/remote-instance-page';
|
||||||
import SearchPage from 'soapbox/pages/search-page';
|
import SearchPage from 'soapbox/pages/search-page';
|
||||||
import StatusPage from 'soapbox/pages/status-page';
|
import StatusPage from 'soapbox/pages/status-page';
|
||||||
import { getVapidKey } from 'soapbox/utils/auth';
|
|
||||||
|
|
||||||
import BackgroundShapes from './components/background-shapes';
|
import BackgroundShapes from './components/background-shapes';
|
||||||
import FloatingActionButton from './components/floating-action-button';
|
import FloatingActionButton from './components/floating-action-button';
|
||||||
|
@ -388,7 +387,7 @@ const UI: React.FC<IUI> = ({ children }) => {
|
||||||
const { account } = useOwnAccount();
|
const { account } = useOwnAccount();
|
||||||
const instance = useInstance();
|
const instance = useInstance();
|
||||||
const features = useFeatures();
|
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);
|
const dropdownMenuIsOpen = useAppSelector(state => state.dropdown_menu.isOpen);
|
||||||
|
|
||||||
|
@ -470,7 +469,9 @@ const UI: React.FC<IUI> = ({ children }) => {
|
||||||
}, [!!account]);
|
}, [!!account]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch(registerPushNotifications());
|
if (vapidKey) {
|
||||||
|
dispatch(registerPushNotifications(vapidKey));
|
||||||
|
}
|
||||||
}, [vapidKey]);
|
}, [vapidKey]);
|
||||||
|
|
||||||
const shouldHideFAB = (): boolean => {
|
const shouldHideFAB = (): boolean => {
|
||||||
|
|
|
@ -62,8 +62,4 @@ export const getAuthUserUrl = (state: RootState) => {
|
||||||
].filter(url => url)).find(isURL);
|
].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;
|
export const getMeUrl = (state: RootState) => selectOwnAccount(state)?.url;
|
Ładowanie…
Reference in New Issue