Compare commits

...

2 Commits

7 changed files with 34 additions and 6 deletions

View File

@ -17,6 +17,10 @@ class DisabledPushManager: _PushManager {
nil nil
} }
var subscriptions: [PushSubscription] {
[]
}
func createSubscription(account: UserAccountInfo) throws -> PushSubscription { func createSubscription(account: UserAccountInfo) throws -> PushSubscription {
throw Disabled() throw Disabled()
} }

View File

@ -24,15 +24,13 @@ public struct PushManager {
@MainActor @MainActor
private static func createPushManager() -> any _PushManager { private static func createPushManager() -> any _PushManager {
guard let info = Bundle.main.object(forInfoDictionaryKey: "TuskerInfo") as? [String: Any], guard let info = Bundle.main.object(forInfoDictionaryKey: "TuskerInfo") as? [String: Any],
let scheme = info["PushProxyScheme"] as? String,
let host = info["PushProxyHost"] as? String, let host = info["PushProxyHost"] as? String,
!scheme.isEmpty,
!host.isEmpty else { !host.isEmpty else {
logger.debug("Missing proxy info, push disabled") logger.debug("Missing proxy info, push disabled")
return DisabledPushManager() return DisabledPushManager()
} }
var endpoint = URLComponents() var endpoint = URLComponents()
endpoint.scheme = scheme endpoint.scheme = "https"
endpoint.host = host endpoint.host = host
let url = endpoint.url! let url = endpoint.url!
logger.debug("Push notifications enabled with proxy \(url.absoluteString, privacy: .public)") logger.debug("Push notifications enabled with proxy \(url.absoluteString, privacy: .public)")
@ -45,6 +43,7 @@ public protocol _PushManager {
var enabled: Bool { get } var enabled: Bool { get }
var proxyRegistration: PushProxyRegistration? { get } var proxyRegistration: PushProxyRegistration? { get }
var subscriptions: [PushSubscription] { get }
func createSubscription(account: UserAccountInfo) throws -> PushSubscription func createSubscription(account: UserAccountInfo) throws -> PushSubscription
func removeSubscription(account: UserAccountInfo) func removeSubscription(account: UserAccountInfo)
func updateSubscription(account: UserAccountInfo, alerts: PushSubscription.Alerts, policy: PushSubscription.Policy) func updateSubscription(account: UserAccountInfo, alerts: PushSubscription.Alerts, policy: PushSubscription.Policy)

View File

@ -40,7 +40,7 @@ class PushManagerImpl: _PushManager {
defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration") defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration")
} }
} }
private(set) var subscriptions: [PushSubscription] { public private(set) var subscriptions: [PushSubscription] {
get { get {
if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] { if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] {
return array.compactMap(PushSubscription.init(defaultsDict:)) return array.compactMap(PushSubscription.init(defaultsDict:))

View File

@ -3,3 +3,4 @@
DEVELOPMENT_TEAM = YOUR_TEAM_ID DEVELOPMENT_TEAM = YOUR_TEAM_ID
BUNDLE_ID_PREFIX = com.example BUNDLE_ID_PREFIX = com.example
TUSKER_PUSH_PROXY_HOST =

View File

@ -105,8 +105,6 @@
<dict> <dict>
<key>PushProxyHost</key> <key>PushProxyHost</key>
<string>$(TUSKER_PUSH_PROXY_HOST)</string> <string>$(TUSKER_PUSH_PROXY_HOST)</string>
<key>PushProxyScheme</key>
<string>$(TUSKER_PUSH_PROXY_SCHEME)</string>
<key>SentryDSN</key> <key>SentryDSN</key>
<string>$(SENTRY_DSN)</string> <string>$(SENTRY_DSN)</string>
</dict> </dict>

View File

@ -96,6 +96,21 @@ struct NotificationsPrefsView: View {
do { do {
try await PushManager.shared.unregister() try await PushManager.shared.unregister()
pushProxyRegistration = nil 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 return true
} catch { } catch {
self.error = .unregistering(error) self.error = .unregistering(error)
@ -120,3 +135,7 @@ private enum NotificationsSetupError: LocalizedError {
} }
} }
} }
extension Notification.Name {
static let pushSubscriptionRemoved = Notification.Name("Tusker.pushSubscriptionRemoved")
}

View File

@ -52,6 +52,13 @@ struct PushInstanceSettingsView: View {
} message: { } message: {
Text("You must grant permission on \(account.instanceURL.host!) to turn on push notifications.") 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 { private func updateNotificationsEnabled(enabled: Bool) async -> Bool {