forked from shadowfacts/Tusker
Remove existing push subscriptions when unregistering from proxy
This commit is contained in:
parent
9f6910ba73
commit
1cd6af1236
|
@ -17,6 +17,10 @@ class DisabledPushManager: _PushManager {
|
|||
nil
|
||||
}
|
||||
|
||||
var subscriptions: [PushSubscription] {
|
||||
[]
|
||||
}
|
||||
|
||||
func createSubscription(account: UserAccountInfo) throws -> PushSubscription {
|
||||
throw Disabled()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:))
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue