diff --git a/server/routes/user/index.ts b/server/routes/user/index.ts index 9dd8ca03b..e3a5fd074 100644 --- a/server/routes/user/index.ts +++ b/server/routes/user/index.ts @@ -269,16 +269,20 @@ router.delete<{ userId: number; endpoint: string }>( try { const userPushSubRepository = getRepository(UserPushSubscription); - const userPushSub = await userPushSubRepository.findOneOrFail({ - relations: { - user: true, - }, + const userPushSub = await userPushSubRepository.findOne({ + relations: { user: true }, where: { user: { id: req.params.userId }, endpoint: req.params.endpoint, }, }); + // If not found, just return 204 to prevent push disable failure + // (rare scenario where user push sub does not exist) + if (!userPushSub) { + return res.status(204).send(); + } + await userPushSubRepository.remove(userPushSub); return res.status(204).send(); } catch (e) { diff --git a/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx b/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx index fbe03f7cc..cf054673f 100644 --- a/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx +++ b/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx @@ -111,6 +111,11 @@ const UserWebPushSettings = () => { try { await unsubscribeToPushNotifications(user?.id, endpoint); + // Delete from backend if endpoint is available + if (subEndpoint) { + await deletePushSubscriptionFromBackend(subEndpoint); + } + localStorage.setItem('pushNotificationsEnabled', 'false'); setWebPushEnabled(false); addToast(intl.formatMessage(messages.webpushhasbeendisabled), {