From 66b94778e0af020cf7ba21701189b89b8fc6c2a7 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Tue, 1 Aug 2023 16:30:34 +1000 Subject: [PATCH] Fixed the build issues and a bug where a new legacy group wasn't subscribile --- .../PushRegistrationManager.swift | 9 +++---- Session/Notifications/SyncPushTokensJob.swift | 9 +++---- .../MessageSender+ClosedGroups.swift | 25 +++++++++++++------ .../ShareNavController.swift | 4 ++- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Session/Notifications/PushRegistrationManager.swift b/Session/Notifications/PushRegistrationManager.swift index 04d4a4e0b..a8ef2c345 100644 --- a/Session/Notifications/PushRegistrationManager.swift +++ b/Session/Notifications/PushRegistrationManager.swift @@ -128,10 +128,7 @@ public enum PushRegistrationError: Error { return true } - // FIXME: Might be nice to try to avoid having this required to run on the main thread (follow a similar approach to the 'SyncPushTokensJob' & `Atomic`?) private func registerForVanillaPushToken() -> AnyPublisher { - AssertIsOnMainThread() - // Use the existing publisher if it exists if let vanillaTokenPublisher: AnyPublisher = self.vanillaTokenPublisher { return vanillaTokenPublisher @@ -139,15 +136,17 @@ public enum PushRegistrationError: Error { .eraseToAnyPublisher() } - UIApplication.shared.registerForRemoteNotifications() - // No pending vanilla token yet; create a new publisher let publisher: AnyPublisher = Deferred { Future { self.vanillaTokenResolver = $0 } } + .shareReplay(1) .eraseToAnyPublisher() self.vanillaTokenPublisher = publisher + // Tell the device to register for remote notifications + DispatchQueue.main.sync { UIApplication.shared.registerForRemoteNotifications() } + return publisher .timeout( .seconds(10), diff --git a/Session/Notifications/SyncPushTokensJob.swift b/Session/Notifications/SyncPushTokensJob.swift index 2df9657b3..0420bdb6a 100644 --- a/Session/Notifications/SyncPushTokensJob.swift +++ b/Session/Notifications/SyncPushTokensJob.swift @@ -58,7 +58,7 @@ public enum SyncPushTokensJob: JobExecutor { guard isUsingFullAPNs else { Just(Storage.shared[.lastRecordedPushToken]) .setFailureType(to: Error.self) - .flatMap { lastRecordedPushToken in + .flatMap { lastRecordedPushToken -> AnyPublisher in if let existingToken: String = lastRecordedPushToken { SNLog("[SyncPushTokensJob] Unregister using last recorded push token: \(redact(existingToken))") return Just(existingToken) @@ -71,7 +71,7 @@ public enum SyncPushTokensJob: JobExecutor { .map { token, _ in token } .eraseToAnyPublisher() } - .flatMap { pushToken in PushNotificationAPI.unregister(Data(hex: pushToken)) } + .flatMap { pushToken in PushNotificationAPI.unsubscribe(token: Data(hex: pushToken)) } .map { // Tell the device to unregister for remote notifications (essentially try to invalidate // the token if needed @@ -102,9 +102,8 @@ public enum SyncPushTokensJob: JobExecutor { PushRegistrationManager.shared.requestPushTokens() .flatMap { (pushToken: String, voipToken: String) -> AnyPublisher in PushNotificationAPI - .register( - with: Data(hex: pushToken), - publicKey: getUserHexEncodedPublicKey(), + .subscribe( + token: Data(hex: pushToken), isForcedUpdate: true ) .retry(3) diff --git a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift index 5cecfca71..c94831b18 100644 --- a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift +++ b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift @@ -16,7 +16,7 @@ extension MessageSender { members: Set ) -> AnyPublisher { Storage.shared - .writePublisher { db -> (String, SessionThread, [MessageSender.PreparedSendData]) in + .writePublisher { db -> (String, SessionThread, [MessageSender.PreparedSendData], Set) in let userPublicKey: String = getUserHexEncodedPublicKey(db) var members: Set = members @@ -111,21 +111,30 @@ extension MessageSender { interactionId: nil ) } + let allActiveLegacyGroupIds: Set = try ClosedGroup + .select(.threadId) + .filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%")) + .joining( + required: ClosedGroup.members + .filter(GroupMember.Columns.profileId == userPublicKey) + ) + .asRequest(of: String.self) + .fetchSet(db) + .inserting(groupPublicKey) // Insert the new key just to be sure - return (userPublicKey, thread, memberSendData) + return (userPublicKey, thread, memberSendData, allActiveLegacyGroupIds) } - .flatMap { userPublicKey, thread, memberSendData in + .flatMap { userPublicKey, thread, memberSendData, allActiveLegacyGroupIds in Publishers .MergeMany( // Send a closed group update message to all members individually memberSendData .map { MessageSender.sendImmediate(preparedSendData: $0) } .appending( - // Notify the PN server - PushNotificationAPI.performOperation( - .subscribe, - for: thread.id, - publicKey: userPublicKey + // Resubscribe to all legacy groups + PushNotificationAPI.subscribeToLegacyGroups( + currentUserPublicKey: userPublicKey, + legacyGroupIds: allActiveLegacyGroupIds ) ) ) diff --git a/SessionShareExtension/ShareNavController.swift b/SessionShareExtension/ShareNavController.swift index 7b0c29ed5..07c698ccb 100644 --- a/SessionShareExtension/ShareNavController.swift +++ b/SessionShareExtension/ShareNavController.swift @@ -9,6 +9,7 @@ import SignalCoreKit final class ShareNavController: UINavigationController, ShareViewDelegate { public static var attachmentPrepPublisher: AnyPublisher<[SignalAttachment], Error>? + private let versionMigrationsComplete: Atomic = Atomic(false) // MARK: - Error @@ -84,7 +85,7 @@ final class ShareNavController: UINavigationController, ShareViewDelegate { /// results in the `AppSetup` not actually running (and the UI not actually being loaded correctly) - in order to avoid this /// we call `checkIsAppReady` explicitly here assuming that either the `AppSetup` _hasn't_ complete or won't ever /// get run - checkIsAppReady() + checkIsAppReady(migrationsCompleted: versionMigrationsComplete.wrappedValue) } override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { @@ -107,6 +108,7 @@ final class ShareNavController: UINavigationController, ShareViewDelegate { } } + versionMigrationsComplete.mutate { $0 = true } checkIsAppReady(migrationsCompleted: true) }