diff --git a/package.json b/package.json index dd8faa242..57f587317 100644 --- a/package.json +++ b/package.json @@ -132,7 +132,7 @@ "multiselect-react-dropdown": "^2.0.25", "object-to-formdata": "^4.5.1", "path-browserify": "^1.0.1", - "pl-api": "^0.0.14", + "pl-api": "^0.0.15", "postcss": "^8.4.29", "process": "^0.11.10", "punycode": "^2.1.1", diff --git a/src/actions/push-notifications/registerer.ts b/src/actions/push-notifications/registerer.ts index 5e2dfd9c7..1c25c7105 100644 --- a/src/actions/push-notifications/registerer.ts +++ b/src/actions/push-notifications/registerer.ts @@ -5,6 +5,7 @@ import { decode as decodeBase64 } from 'soapbox/utils/base64'; import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; +import type { WebPushSubscription } from 'pl-api'; import type { AppDispatch, RootState } from 'soapbox/store'; import type { Me } from 'soapbox/types/soapbox'; @@ -54,7 +55,7 @@ const sendSubscriptionToBackend = (subscription: PushSubscription, me: Me) => } } - return dispatch(createPushSubscription(params) as any); + return dispatch(createPushSubscription(params)); }; // Last one checks for payload support: https://web-push-book.gauntface.com/chapter-06/01-non-standards-browsers/#no-payload @@ -81,10 +82,7 @@ const register = () => getRegistration() .then(getPushSubscription) // @ts-ignore - .then(({ registration, subscription }: { - registration: ServiceWorkerRegistration; - subscription: PushSubscription | null; - }) => { + .then(({ registration, subscription }) => { if (subscription !== null) { // We have a subscription, check if it is still valid const currentServerKey = (new Uint8Array(subscription.options.applicationServerKey!)).toString(); @@ -97,22 +95,25 @@ const register = () => return { subscription }; } else { // Something went wrong, try to subscribe again - return unsubscribe({ registration, subscription }).then((registration: ServiceWorkerRegistration) => { + return unsubscribe({ registration, subscription }).then((registration) => { return subscribe(registration, getState); }).then( - (subscription: PushSubscription) => dispatch(sendSubscriptionToBackend(subscription, me) as any)); + (subscription) => dispatch(sendSubscriptionToBackend(subscription, me))); } } // No subscription, try to subscribe return subscribe(registration, getState) - .then(subscription => dispatch(sendSubscriptionToBackend(subscription, me) as any)); + .then(async (pushSubscription) => { + const subscription = await dispatch(sendSubscriptionToBackend(pushSubscription, me)); + return { subscription }; + }); }) - .then(({ subscription }: { subscription: PushSubscription | Record }) => { + .then(({ subscription }: { subscription: WebPushSubscription | PushSubscription | null }) => { // If we got a PushSubscription (and not a subscription object from the backend) // it means that the backend subscription is valid (and was set during hydration) - if (!(subscription instanceof PushSubscription)) { - dispatch(setSubscription(subscription as PushSubscription)); + if (subscription !== null && !(subscription instanceof PushSubscription)) { + dispatch(setSubscription(subscription)); if (me) { pushNotificationsSetting.set(me, { alerts: subscription.alerts }); } @@ -142,7 +143,7 @@ const register = () => const saveSettings = () => (dispatch: AppDispatch, getState: () => RootState) => { const state = getState().push_notifications; - const alerts = state.alerts; + const alerts = state.alerts.toJS(); const data = { alerts }; const me = getState().me; diff --git a/src/actions/push-notifications/setter.ts b/src/actions/push-notifications/setter.ts index fd81914b0..046a18fb3 100644 --- a/src/actions/push-notifications/setter.ts +++ b/src/actions/push-notifications/setter.ts @@ -1,3 +1,5 @@ +import type { WebPushSubscription } from 'pl-api'; + const SET_BROWSER_SUPPORT = 'PUSH_NOTIFICATIONS_SET_BROWSER_SUPPORT' as const; const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION' as const; const CLEAR_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_CLEAR_SUBSCRIPTION' as const; @@ -8,7 +10,7 @@ const setBrowserSupport = (value: boolean) => ({ value, }); -const setSubscription = (subscription: PushSubscription) => ({ +const setSubscription = (subscription: WebPushSubscription) => ({ type: SET_SUBSCRIPTION, subscription, }); diff --git a/src/actions/push-subscriptions.ts b/src/actions/push-subscriptions.ts index 01a64d244..311a7517e 100644 --- a/src/actions/push-subscriptions.ts +++ b/src/actions/push-subscriptions.ts @@ -1,78 +1,25 @@ import { getClient } from '../api'; -import type { CreatePushNotificationsSubscriptionParams } from 'pl-api'; +import type { CreatePushNotificationsSubscriptionParams, UpdatePushNotificationsSubscriptionParams } from 'pl-api'; import type { AppDispatch, RootState } from 'soapbox/store'; -const PUSH_SUBSCRIPTION_CREATE_REQUEST = 'PUSH_SUBSCRIPTION_CREATE_REQUEST' as const; -const PUSH_SUBSCRIPTION_CREATE_SUCCESS = 'PUSH_SUBSCRIPTION_CREATE_SUCCESS' as const; -const PUSH_SUBSCRIPTION_CREATE_FAIL = 'PUSH_SUBSCRIPTION_CREATE_FAIL' as const; - -const PUSH_SUBSCRIPTION_FETCH_REQUEST = 'PUSH_SUBSCRIPTION_FETCH_REQUEST' as const; -const PUSH_SUBSCRIPTION_FETCH_SUCCESS = 'PUSH_SUBSCRIPTION_FETCH_SUCCESS' as const; -const PUSH_SUBSCRIPTION_FETCH_FAIL = 'PUSH_SUBSCRIPTION_FETCH_FAIL' as const; - -const PUSH_SUBSCRIPTION_UPDATE_REQUEST = 'PUSH_SUBSCRIPTION_UPDATE_REQUEST' as const; -const PUSH_SUBSCRIPTION_UPDATE_SUCCESS = 'PUSH_SUBSCRIPTION_UPDATE_SUCCESS' as const; -const PUSH_SUBSCRIPTION_UPDATE_FAIL = 'PUSH_SUBSCRIPTION_UPDATE_FAIL' as const; - -const PUSH_SUBSCRIPTION_DELETE_REQUEST = 'PUSH_SUBSCRIPTION_DELETE_REQUEST' as const; -const PUSH_SUBSCRIPTION_DELETE_SUCCESS = 'PUSH_SUBSCRIPTION_DELETE_SUCCESS' as const; -const PUSH_SUBSCRIPTION_DELETE_FAIL = 'PUSH_SUBSCRIPTION_DELETE_FAIL' as const; - const createPushSubscription = (params: CreatePushNotificationsSubscriptionParams) => - (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: PUSH_SUBSCRIPTION_CREATE_REQUEST, params }); - return getClient(getState).pushNotifications.createSubscription(params) - .then((subscription) => - dispatch({ type: PUSH_SUBSCRIPTION_CREATE_SUCCESS, params, subscription }), - ).catch(error => - dispatch({ type: PUSH_SUBSCRIPTION_CREATE_FAIL, params, error }), - ); - }; + (dispatch: AppDispatch, getState: () => RootState) => + getClient(getState).pushNotifications.createSubscription(params); const fetchPushSubscription = () => - (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: PUSH_SUBSCRIPTION_FETCH_REQUEST }); - return getClient(getState).pushNotifications.getSubscription().then((subscription) => - dispatch({ type: PUSH_SUBSCRIPTION_FETCH_SUCCESS, subscription }), - ).catch(error => - dispatch({ type: PUSH_SUBSCRIPTION_FETCH_FAIL, error }), - ); - }; + (dispatch: AppDispatch, getState: () => RootState) => + getClient(getState).pushNotifications.getSubscription(); -const updatePushSubscription = (params: Record) => - (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: PUSH_SUBSCRIPTION_UPDATE_REQUEST, params }); - return getClient(getState).pushNotifications.updateSubscription(params).then((subscription) => - dispatch({ type: PUSH_SUBSCRIPTION_UPDATE_SUCCESS, params, subscription }), - ).catch(error => - dispatch({ type: PUSH_SUBSCRIPTION_UPDATE_FAIL, params, error }), - ); - }; +const updatePushSubscription = (params: UpdatePushNotificationsSubscriptionParams) => + (dispatch: AppDispatch, getState: () => RootState) => + getClient(getState).pushNotifications.updateSubscription(params); const deletePushSubscription = () => - (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: PUSH_SUBSCRIPTION_DELETE_REQUEST }); - return getClient(getState).pushNotifications.deleteSubscription().then(() => - dispatch({ type: PUSH_SUBSCRIPTION_DELETE_SUCCESS }), - ).catch(error => - dispatch({ type: PUSH_SUBSCRIPTION_DELETE_FAIL, error }), - ); - }; + (dispatch: AppDispatch, getState: () => RootState) => + getClient(getState).pushNotifications.deleteSubscription(); export { - PUSH_SUBSCRIPTION_CREATE_REQUEST, - PUSH_SUBSCRIPTION_CREATE_SUCCESS, - PUSH_SUBSCRIPTION_CREATE_FAIL, - PUSH_SUBSCRIPTION_FETCH_REQUEST, - PUSH_SUBSCRIPTION_FETCH_SUCCESS, - PUSH_SUBSCRIPTION_FETCH_FAIL, - PUSH_SUBSCRIPTION_UPDATE_REQUEST, - PUSH_SUBSCRIPTION_UPDATE_SUCCESS, - PUSH_SUBSCRIPTION_UPDATE_FAIL, - PUSH_SUBSCRIPTION_DELETE_REQUEST, - PUSH_SUBSCRIPTION_DELETE_SUCCESS, - PUSH_SUBSCRIPTION_DELETE_FAIL, createPushSubscription, fetchPushSubscription, updatePushSubscription, diff --git a/src/reducers/push-notifications.ts b/src/reducers/push-notifications.ts index ed12522af..637227371 100644 --- a/src/reducers/push-notifications.ts +++ b/src/reducers/push-notifications.ts @@ -5,7 +5,7 @@ import { SET_BROWSER_SUPPORT, SET_SUBSCRIPTION, CLEAR_SUBSCRIPTION, SET_ALERTS } import type { SetterAction } from 'soapbox/actions/push-notifications/setter'; const SubscriptionRecord = ImmutableRecord({ - id: '', + id: 0, endpoint: '', }); diff --git a/yarn.lock b/yarn.lock index 5aeb626ff..044e9978e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8385,10 +8385,10 @@ pkg-types@^1.0.3: mlly "^1.2.0" pathe "^1.1.0" -pl-api@^0.0.14: - version "0.0.14" - resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.0.14.tgz#71466b336482dc04739150b88b912bffa648d984" - integrity sha512-dRztsnvQDWlz3P3Rtpgi1fbMbt3SUJOwC2Pn+BnoZIc5lV84KbXQ0/LI5kfGFHATJkH3iTPtqSjVBnIxCYtTVw== +pl-api@^0.0.15: + version "0.0.15" + resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.0.15.tgz#93d68907227768b7a33715202e26c3ab769db55c" + integrity sha512-ullWk5opBVhG6XgCMhCX5CdNj8Nn2nC+lh+615MXOIhtmWmi6j46lrkS1S8fqk/kPCde8jGsET+cWgAkc0/gQQ== dependencies: blurhash "^2.0.5" http-link-header "^1.1.3"