From 1cd6af1236a9fbfb4a12e6be05f396bc246ae8b3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 11 Apr 2024 12:58:43 -0400 Subject: [PATCH] Remove existing push subscriptions when unregistering from proxy --- .../DisabledPushManager.swift | 4 ++++ .../PushNotifications/PushManager.swift | 1 + .../PushNotifications/PushManagerImpl.swift | 2 +- .../NotificationsPrefsView.swift | 19 +++++++++++++++++++ .../PushInstanceSettingsView.swift | 7 +++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift b/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift index f55e9eb2..28af0f5f 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift @@ -17,6 +17,10 @@ class DisabledPushManager: _PushManager { nil } + var subscriptions: [PushSubscription] { + [] + } + func createSubscription(account: UserAccountInfo) throws -> PushSubscription { throw Disabled() } diff --git a/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift b/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift index ebbc86a9..fcd364ff 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift @@ -45,6 +45,7 @@ public protocol _PushManager { var enabled: Bool { get } var proxyRegistration: PushProxyRegistration? { get } + var subscriptions: [PushSubscription] { get } func createSubscription(account: UserAccountInfo) throws -> PushSubscription func removeSubscription(account: UserAccountInfo) func updateSubscription(account: UserAccountInfo, alerts: PushSubscription.Alerts, policy: PushSubscription.Policy) diff --git a/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift b/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift index 04a1afe6..6305b6d3 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift @@ -40,7 +40,7 @@ class PushManagerImpl: _PushManager { defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration") } } - private(set) var subscriptions: [PushSubscription] { + public private(set) var subscriptions: [PushSubscription] { get { if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] { return array.compactMap(PushSubscription.init(defaultsDict:)) diff --git a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift index 69f6c4c8..995aa383 100644 --- a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift +++ b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift @@ -96,6 +96,21 @@ struct NotificationsPrefsView: View { do { try await PushManager.shared.unregister() pushProxyRegistration = nil + for subscription in PushManager.shared.subscriptions { + if let account = UserAccountsManager.shared.getAccount(id: subscription.accountID) { + let mastodonController = MastodonController.getForAccount(account) + do { + try await mastodonController.deletePushSubscription() + PushManager.shared.removeSubscription(account: account) + PushManager.logger.debug("Push subscription removed on \(account.instanceURL)") + // this is a bit of a hack. the PushInstanceSettingsViews need to know to update + // their @State variables after we remove the subscription + NotificationCenter.default.post(name: .pushSubscriptionRemoved, object: account.id) + } catch { + PushManager.logger.error("Erroring removing push subscription: \(String(describing: error))") + } + } + } return true } catch { self.error = .unregistering(error) @@ -120,3 +135,7 @@ private enum NotificationsSetupError: LocalizedError { } } } + +extension Notification.Name { + static let pushSubscriptionRemoved = Notification.Name("Tusker.pushSubscriptionRemoved") +} diff --git a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift index 63b38b8d..e43acf58 100644 --- a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift +++ b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift @@ -52,6 +52,13 @@ struct PushInstanceSettingsView: View { } message: { Text("You must grant permission on \(account.instanceURL.host!) to turn on push notifications.") } + .onReceive(NotificationCenter.default + .publisher(for: .pushSubscriptionRemoved) + .filter { ($0.object as? String) == account.id } + ) { _ in + mode = .off + subscription = nil + } } private func updateNotificationsEnabled(enabled: Bool) async -> Bool {