From b3cad3e709587c346d7f7d037b33551f21d4b041 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Tue, 20 Jun 2023 12:31:03 +1000 Subject: [PATCH] Added in the new legacy endpoint --- Session.xcodeproj/project.pbxproj | 4 + .../Models/LegacyGroupOnlyRequest.swift | 12 +++ .../Notifications/PushNotificationAPI.swift | 77 ++++++++----------- .../Types/PushNotificationAPIEndpoint.swift | 5 +- .../General/SNUserDefaults.swift | 1 - 5 files changed, 50 insertions(+), 49 deletions(-) create mode 100644 SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyGroupOnlyRequest.swift diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 69124a8f7..3aeee3f0b 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -813,6 +813,7 @@ FDC438CD27BC641200C60D73 /* Set+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC438CC27BC641200C60D73 /* Set+Utilities.swift */; }; FDC6D6F32860607300B04575 /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDF0B7542807C4BB004C14C5 /* Environment.swift */; }; FDC6D7602862B3F600B04575 /* Dependencies.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC6D75F2862B3F600B04575 /* Dependencies.swift */; }; + FDCD2E032A41294E00964D6A /* LegacyGroupOnlyRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDCD2E022A41294E00964D6A /* LegacyGroupOnlyRequest.swift */; }; FDCDB8DE2810F73B00352A0C /* Differentiable+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDCDB8DD2810F73B00352A0C /* Differentiable+Utilities.swift */; }; FDCDB8E02811007F00352A0C /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDCDB8DF2811007F00352A0C /* HomeViewModel.swift */; }; FDD2506E283711D600198BDA /* DifferenceKit+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD2506D283711D600198BDA /* DifferenceKit+Utilities.swift */; }; @@ -1959,6 +1960,7 @@ FDC438CA27BB7DB100C60D73 /* UpdateMessageRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateMessageRequest.swift; sourceTree = ""; }; FDC438CC27BC641200C60D73 /* Set+Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Set+Utilities.swift"; sourceTree = ""; }; FDC6D75F2862B3F600B04575 /* Dependencies.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dependencies.swift; sourceTree = ""; }; + FDCD2E022A41294E00964D6A /* LegacyGroupOnlyRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyGroupOnlyRequest.swift; sourceTree = ""; }; FDCDB8DD2810F73B00352A0C /* Differentiable+Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Differentiable+Utilities.swift"; sourceTree = ""; }; FDCDB8DF2811007F00352A0C /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = ""; }; FDD2506D283711D600198BDA /* DifferenceKit+Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DifferenceKit+Utilities.swift"; sourceTree = ""; }; @@ -4240,6 +4242,7 @@ FDC13D572A17207D007267C7 /* UnsubscribeResponse.swift */, FD6E4C892A1AEE4700C7C243 /* LegacyUnsubscribeRequest.swift */, FDC13D532A16FF29007267C7 /* LegacyGroupRequest.swift */, + FDCD2E022A41294E00964D6A /* LegacyGroupOnlyRequest.swift */, FDC4382E27B383AF00C60D73 /* LegacyPushServerResponse.swift */, FDC13D592A1721C5007267C7 /* LegacyNotifyRequest.swift */, FDFBB74C2A1F3C4E00CA7350 /* NotificationMetadata.swift */, @@ -5825,6 +5828,7 @@ FD8ECF8B2935DB4B00C0D1BB /* SharedConfigMessage.swift in Sources */, FD09798727FD1B7800936362 /* GroupMember.swift in Sources */, FDB4BBC92839BEF000B7C95D /* ProfileManagerError.swift in Sources */, + FDCD2E032A41294E00964D6A /* LegacyGroupOnlyRequest.swift in Sources */, FD3E0C84283B5835002A425C /* SessionThreadViewModel.swift in Sources */, FD09C5EC282B8F18000CE219 /* AttachmentError.swift in Sources */, FD17D79927F40AB800122BE0 /* _003_YDBToGRDBMigration.swift in Sources */, diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyGroupOnlyRequest.swift b/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyGroupOnlyRequest.swift new file mode 100644 index 000000000..1a87dcf8e --- /dev/null +++ b/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyGroupOnlyRequest.swift @@ -0,0 +1,12 @@ +// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. + +import Foundation + +extension PushNotificationAPI { + struct LegacyGroupOnlyRequest: Codable { + let token: String + let pubKey: String + let device: String + let legacyGroupPublicKeys: Set + } +} diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift index 66e28ae9a..befa6c45a 100644 --- a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift +++ b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift @@ -115,54 +115,39 @@ public enum PushNotificationAPI { } } ) - .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( + .map { _ in () } + .eraseToAnyPublisher(), // FIXME: Remove this once legacy groups are deprecated - contentsOf: legacyGroupIds - .map { legacyGroupId in - PushNotificationAPI.subscribeToLegacyGroup( - legacyGroupId: legacyGroupId, - currentUserPublicKey: currentUserPublicKey, - using: dependencies + PushNotificationAPI + .send( + request: PushNotificationAPIRequest( + endpoint: .legacyGroupsOnlySubscribe, + body: LegacyGroupOnlyRequest( + token: hexEncodedToken, + pubKey: currentUserPublicKey, + device: "ios", + legacyGroupPublicKeys: legacyGroupIds + ) ) - } - ) + ) + .decoded(as: LegacyPushServerResponse.self, using: dependencies) + .retry(maxRetryCount) + .handleEvents( + receiveOutput: { _, response in + guard response.code != 0 else { + return SNLog("Couldn't subscribe for legacy groups due to error: \(response.message ?? "nil").") + } + }, + receiveCompletion: { result in + switch result { + case .finished: break + case .failure: SNLog("Couldn't subscribe for legacy groups.") + } + } + ) + .map { _ in () } + .eraseToAnyPublisher() + ] ) .collect() .map { _ in () } diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/Types/PushNotificationAPIEndpoint.swift b/SessionMessagingKit/Sending & Receiving/Notifications/Types/PushNotificationAPIEndpoint.swift index cc3068395..072abcc60 100644 --- a/SessionMessagingKit/Sending & Receiving/Notifications/Types/PushNotificationAPIEndpoint.swift +++ b/SessionMessagingKit/Sending & Receiving/Notifications/Types/PushNotificationAPIEndpoint.swift @@ -12,6 +12,7 @@ public extension PushNotificationAPI { case legacyNotify = "notify" case legacyRegister = "register" case legacyUnregister = "unregister" + case legacyGroupsOnlySubscribe = "register_legacy_groups_only" case legacyGroupSubscribe = "subscribe_closed_group" case legacyGroupUnsubscribe = "unsubscribe_closed_group" @@ -20,7 +21,7 @@ public extension PushNotificationAPI { var server: String { switch self { case .legacyNotify, .legacyRegister, .legacyUnregister, - .legacyGroupSubscribe, .legacyGroupUnsubscribe: + .legacyGroupsOnlySubscribe, .legacyGroupSubscribe, .legacyGroupUnsubscribe: return PushNotificationAPI.legacyServer default: return PushNotificationAPI.server @@ -30,7 +31,7 @@ public extension PushNotificationAPI { var serverPublicKey: String { switch self { case .legacyNotify, .legacyRegister, .legacyUnregister, - .legacyGroupSubscribe, .legacyGroupUnsubscribe: + .legacyGroupsOnlySubscribe, .legacyGroupSubscribe, .legacyGroupUnsubscribe: return PushNotificationAPI.legacyServerPublicKey default: return PushNotificationAPI.serverPublicKey diff --git a/SessionUtilitiesKit/General/SNUserDefaults.swift b/SessionUtilitiesKit/General/SNUserDefaults.swift index 910461a3d..82f18bd64 100644 --- a/SessionUtilitiesKit/General/SNUserDefaults.swift +++ b/SessionUtilitiesKit/General/SNUserDefaults.swift @@ -31,7 +31,6 @@ public enum SNUserDefaults { case hasSeenCallIPExposureWarning case hasSeenCallMissedTips case isUsingFullAPNs - case hasUnregisteredForLegacyPushNotifications case wasUnlinked case isMainAppActive case isCallOngoing