From 26e2b86ab6d434f00c3ce567514bcaf541cf69ec Mon Sep 17 00:00:00 2001 From: OwsleyJr Date: Thu, 14 Aug 2025 22:47:09 -0400 Subject: [PATCH] fix: delete endpoint on push notification disable fix: add endpoint deletion on disable --- server/routes/user/index.ts | 11 +++-- .../UserNotificationsWebPush/index.tsx | 47 ++++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/server/routes/user/index.ts b/server/routes/user/index.ts index 9dd8ca03b..9b50b26ee 100644 --- a/server/routes/user/index.ts +++ b/server/routes/user/index.ts @@ -269,16 +269,19 @@ 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 (no content) + 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..55f112be1 100644 --- a/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx +++ b/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx @@ -8,7 +8,6 @@ import DeviceItem from '@app/components/UserProfile/UserSettings/UserNotificatio import useSettings from '@app/hooks/useSettings'; import { useUser } from '@app/hooks/useUser'; import globalMessages from '@app/i18n/globalMessages'; -import defineMessages from '@app/utils/defineMessages'; import { getPushSubscription, subscribeToPushNotifications, @@ -25,31 +24,28 @@ import axios from 'axios'; import { Form, Formik } from 'formik'; import { useRouter } from 'next/router'; import { useEffect, useMemo, useState } from 'react'; -import { useIntl } from 'react-intl'; +import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR, { mutate } from 'swr'; -const messages = defineMessages( - 'components.UserProfile.UserSettings.UserNotificationSettings.UserNotificationsWebPush', - { - webpushsettingssaved: 'Web push notification settings saved successfully!', - webpushsettingsfailed: 'Web push notification settings failed to save.', - enablewebpush: 'Enable web push', - disablewebpush: 'Disable web push', - managedevices: 'Manage Devices', - type: 'type', - created: 'Created', - device: 'Device', - subscriptiondeleted: 'Subscription deleted.', - subscriptiondeleteerror: - 'Something went wrong while deleting the user subscription.', - nodevicestoshow: 'You have no web push subscriptions to show.', - webpushhasbeenenabled: 'Web push has been enabled.', - webpushhasbeendisabled: 'Web push has been disabled.', - enablingwebpusherror: 'Something went wrong while enabling web push.', - disablingwebpusherror: 'Something went wrong while disabling web push.', - } -); +const messages = defineMessages({ + webpushsettingssaved: 'Web push notification settings saved successfully!', + webpushsettingsfailed: 'Web push notification settings failed to save.', + enablewebpush: 'Enable web push', + disablewebpush: 'Disable web push', + managedevices: 'Manage Devices', + type: 'type', + created: 'Created', + device: 'Device', + subscriptiondeleted: 'Subscription deleted.', + subscriptiondeleteerror: + 'Something went wrong while deleting the user subscription.', + nodevicestoshow: 'You have no web push subscriptions to show.', + webpushhasbeenenabled: 'Web push has been enabled.', + webpushhasbeendisabled: 'Web push has been disabled.', + enablingwebpusherror: 'Something went wrong while enabling web push.', + disablingwebpusherror: 'Something went wrong while disabling web push.', +}); const UserWebPushSettings = () => { const intl = useIntl(); @@ -111,6 +107,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), {