From 61ad85b97b16b6a6dc627205cc4dce5be839fd72 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Mon, 22 May 2023 10:57:16 +1000 Subject: [PATCH] Added logic to unsubscribe for legacy one-to-one PNs --- .../Models/LegacyUnsubscribeRequest.swift | 14 +++++++ .../Notifications/PushNotificationAPI.swift | 37 ++++++++++++++++++- .../General/SNUserDefaults.swift | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift b/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift new file mode 100644 index 000000000..663bafb17 --- /dev/null +++ b/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift @@ -0,0 +1,14 @@ +// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. + +import Foundation +import SessionSnodeKit + +extension PushNotificationAPI { + struct LegacyUnsubscribeRequest: Codable { + private let token: String + + init(token: String) { + self.token = token + } + } +} diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift index 4d1c0148a..bb5d6ce8d 100644 --- a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift +++ b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift @@ -115,7 +115,42 @@ public enum PushNotificationAPI { } } ) - .map { _ in () } + .flatMap { _ in + guard UserDefaults.standard[.hasUnregisteredForLegacyPushNotifications] != true else { + return Just(()) + .setFailureType(to: Error.self) + .eraseToAnyPublisher() + } + + return PushNotificationAPI + .send( + request: PushNotificationAPIRequest( + endpoint: .legacyUnregister, + body: LegacyUnsubscribeRequest( + token: hexEncodedToken + ) + ) + ) + .retry(maxRetryCount) + .handleEvents( + receiveCompletion: { result in + switch result { + case .finished: + /// Save that we've already unsubscribed + /// + /// **Note:** The server can return an error (`response.code != 0`) but + /// that means the server properly processed the request and the error is likely + /// due to the device not actually being previously subscribed for notifications + /// rather than actually failing to unsubscribe + UserDefaults.standard[.hasUnregisteredForLegacyPushNotifications] = true + + case .failure: SNLog("Couldn't unsubscribe for legacy notifications.") + } + } + ) + .map { _ in () } + .eraseToAnyPublisher() + } .eraseToAnyPublisher() ].appending( // FIXME: Remove this once legacy groups are deprecated diff --git a/SessionUtilitiesKit/General/SNUserDefaults.swift b/SessionUtilitiesKit/General/SNUserDefaults.swift index 82f18bd64..910461a3d 100644 --- a/SessionUtilitiesKit/General/SNUserDefaults.swift +++ b/SessionUtilitiesKit/General/SNUserDefaults.swift @@ -31,6 +31,7 @@ public enum SNUserDefaults { case hasSeenCallIPExposureWarning case hasSeenCallMissedTips case isUsingFullAPNs + case hasUnregisteredForLegacyPushNotifications case wasUnlinked case isMainAppActive case isCallOngoing