diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index 0aa14ab8d7..6f0e80dc8b 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -695,6 +695,7 @@ 5079F3C22DA47554008430EC /* NotificationPreconditionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5079F3C02DA47554008430EC /* NotificationPreconditionTest.swift */; }; 507B69112C503FA800F1C6D7 /* CallLinkTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50552C302BAC079A00815474 /* CallLinkTest.swift */; }; 507B69122C5044F800F1C6D7 /* LinkPreviewGroupLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34A95517271B510400B05242 /* LinkPreviewGroupLink.swift */; }; + 507C6AF52ED10BD900A7C74C /* RegisteredState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 507C6AF42ED10BD900A7C74C /* RegisteredState.swift */; }; 507CD5E529660D5100E47DAC /* ServiceId.swift in Sources */ = {isa = PBXBuildFile; fileRef = 507CD5E429660D5100E47DAC /* ServiceId.swift */; }; 507E1BDF2A0E13B100650611 /* NSKeyedUnarchiver+SSK.swift in Sources */ = {isa = PBXBuildFile; fileRef = 507E1BDE2A0E13B100650611 /* NSKeyedUnarchiver+SSK.swift */; }; 508622AD2D026F5200931BF9 /* CanonicalPhoneNumberTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508622AC2D026F5200931BF9 /* CanonicalPhoneNumberTest.swift */; }; @@ -4871,6 +4872,7 @@ 50791B1C2D037A9800D747F8 /* PhoneNumberCountryTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhoneNumberCountryTest.swift; sourceTree = ""; }; 5079F3B52DA4713C008430EC /* NotificationPrecondition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationPrecondition.swift; sourceTree = ""; }; 5079F3C02DA47554008430EC /* NotificationPreconditionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationPreconditionTest.swift; sourceTree = ""; }; + 507C6AF42ED10BD900A7C74C /* RegisteredState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisteredState.swift; sourceTree = ""; }; 507CD5E429660D5100E47DAC /* ServiceId.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceId.swift; sourceTree = ""; }; 507D614B2BE433EE00DA7BA3 /* be */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = be; path = translations/be.lproj/InfoPlist.strings; sourceTree = ""; }; 507D614C2BE433EE00DA7BA3 /* be */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = be; path = translations/be.lproj/Localizable.strings; sourceTree = ""; }; @@ -14741,6 +14743,7 @@ D9CAF74F2A0ACFF20049193A /* PniDistributionParameterBuilder.swift */, C18E3C712A9FF65D003D1CF1 /* PniDistributionSyncMessage.swift */, 50D146CC2D1F4E5800D628DE /* ProfileKey.swift */, + 507C6AF42ED10BD900A7C74C /* RegisteredState.swift */, C1C2185C2E70BA27008567A1 /* RegistrationIdMismatchManager.swift */, 507CD5E429660D5100E47DAC /* ServiceId.swift */, ); @@ -19356,6 +19359,7 @@ 66EB029B2BAB9F1C004F0580 /* ReferencedAttachment.swift in Sources */, F9C5CE18289453B400548EEE /* Refinery.swift in Sources */, 720547F62B9C985300E2CF2F /* RefreshEvent.swift in Sources */, + 507C6AF52ED10BD900A7C74C /* RegisteredState.swift in Sources */, C13EDC3F2D94636000670597 /* Registration.pb.swift in Sources */, 661170C12ABA459D00A1B16D /* RegistrationIdGenerator.swift in Sources */, C1C2185D2E70BA27008567A1 /* RegistrationIdMismatchManager.swift in Sources */, diff --git a/Signal/AppLaunch/AppDelegate.swift b/Signal/AppLaunch/AppDelegate.swift index d80425c7c8..de454cf102 100644 --- a/Signal/AppLaunch/AppDelegate.swift +++ b/Signal/AppLaunch/AppDelegate.swift @@ -794,16 +794,16 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { } let recipientDatabaseTable = DependenciesBridge.shared.recipientDatabaseTable - let tsRegistrationState: TSRegistrationState = SSKEnvironment.shared.databaseStorageRef.read { tx in - let registrationState = tsAccountManager.registrationState(tx: tx) - if registrationState.isRegistered, let localIdentifiers = tsAccountManager.localIdentifiers(tx: tx) { + let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() + SSKEnvironment.shared.databaseStorageRef.read { tx in + if let registeredState { + let localIdentifiers = registeredState.localIdentifiers let deviceId = tsAccountManager.storedDeviceId(tx: tx) let localRecipient = recipientDatabaseTable.fetchRecipient(serviceId: localIdentifiers.aci, transaction: tx) let deviceCount = localRecipient?.deviceIds.count ?? 0 let linkedDeviceMessage = deviceCount > 1 ? "\(deviceCount) devices including the primary" : "no linked devices" Logger.info("localAci: \(localIdentifiers.aci), deviceId: \(deviceId) (\(linkedDeviceMessage))") } - return registrationState } let profileManager = SSKEnvironment.shared.profileManagerRef @@ -852,12 +852,12 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { self.refreshConnection(isAppActive: false, shouldRunCron: false) } - if tsRegistrationState.isRegistered { + if registeredState != nil { // This should happen at any launch, background or foreground. SyncPushTokensJob.run() } - if tsRegistrationState.isRegistered { + if registeredState != nil { Task { do { try await APNSRotationStore.rotateIfNeededOnAppLaunchAndReadiness( @@ -878,7 +878,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { AppVersionImpl.shared.mainAppLaunchDidComplete() scheduleBgAppRefresh() - Self.updateApplicationShortcutItems(isRegistered: tsRegistrationState.isRegistered) + Self.updateApplicationShortcutItems(isRegistered: registeredState != nil) let notificationCenter = NotificationCenter.default notificationCenter.addObserver( @@ -888,7 +888,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { object: nil ) - checkDatabaseIntegrityIfNecessary(isRegistered: tsRegistrationState.isRegistered) + checkDatabaseIntegrityIfNecessary(isRegistered: registeredState != nil) SignalApp.shared.showLaunchInterface( launchInterface, @@ -1388,13 +1388,14 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { Logger.info("Activated.") } - let tsRegistrationState: TSRegistrationState = DependenciesBridge.shared.db.read { tx in + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() + + if registeredState != nil { + DependenciesBridge.shared.db.read { tx in // Always check prekeys after app launches, and sometimes check on app activation. - let registrationState = DependenciesBridge.shared.tsAccountManager.registrationState(tx: tx) - if registrationState.isRegistered { DependenciesBridge.shared.preKeyManager.checkPreKeysIfNecessary(tx: tx) } - return registrationState } if !hasActivated { @@ -1406,7 +1407,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { // cleaning in the background. SSKEnvironment.shared.disappearingMessagesJobRef.startIfNecessary() - if !tsRegistrationState.isRegistered { + if registeredState == nil { // Unregistered user should have no unread messages. e.g. if you delete your account. SSKEnvironment.shared.notificationPresenterRef.clearAllNotifications() } @@ -1415,7 +1416,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { refreshConnection(isAppActive: true, shouldRunCron: true) // Every time we become active... - if tsRegistrationState.isRegistered { + if registeredState != nil { // TODO: Should we run this immediately even if we would like to process already decrypted envelopes handed to us by the NSE? Task { await SSKEnvironment.shared.groupMessageProcessorManagerRef.startAllProcessors() @@ -1639,10 +1640,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { break case .notHandled: let tsAccountManager = DependenciesBridge.shared.tsAccountManager - guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - Logger.info("Ignoring remote notification; user is not registered.") - return - } + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() let backgroundMessageFetcher = DependenciesBridge.shared.backgroundMessageFetcherFactory.buildFetcher() await backgroundMessageFetcher.start() @@ -1838,15 +1836,11 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { scheduleBgAppRefresh() let tsAccountManager = DependenciesBridge.shared.tsAccountManager - let isRegistered = tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered - if isRegistered { - appReadiness.runNowOrWhenAppDidBecomeReadySync { - SSKEnvironment.shared.databaseStorageRef.write { transaction in - let localAddress = tsAccountManager.localIdentifiers(tx: transaction)?.aciAddress - Logger.info("localAddress: \(String(describing: localAddress))") - - ExperienceUpgradeFinder.markAllCompleteForNewUser(transaction: transaction) - } + let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() + if let registeredState { + Logger.info("localAci: \(registeredState.localIdentifiers.aci)") + SSKEnvironment.shared.databaseStorageRef.write { transaction in + ExperienceUpgradeFinder.markAllCompleteForNewUser(transaction: transaction) } DependenciesBridge.shared.attachmentDownloadManager.beginDownloadingIfNecessary() Task { @@ -1859,7 +1853,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { } } - Self.updateApplicationShortcutItems(isRegistered: isRegistered) + Self.updateApplicationShortcutItems(isRegistered: registeredState != nil) } // MARK: - Shortcut Items diff --git a/Signal/Calls/CallLinkStateUpdater.swift b/Signal/Calls/CallLinkStateUpdater.swift index aa1eefeedf..60c78aa512 100644 --- a/Signal/Calls/CallLinkStateUpdater.swift +++ b/Signal/Calls/CallLinkStateUpdater.swift @@ -89,13 +89,11 @@ actor CallLinkStateUpdater { } } - guard let localIdentifiers = tsAccountManager.localIdentifiersWithMaybeSneakyTransaction else { - throw OWSGenericError("Not registered.") - } + let registeredState = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() let oldRecord = try db.read { tx -> CallLinkRecord? in return try callLinkStore.fetch(roomId: roomId, tx: tx) } - let authCredential = try await authCredentialManager.fetchCallLinkAuthCredential(localIdentifiers: localIdentifiers) + let authCredential = try await authCredentialManager.fetchCallLinkAuthCredential(localIdentifiers: registeredState.localIdentifiers) let updateResult = await Result { try await updateAndFetch(authCredential) } let updateAction: UpdateAction diff --git a/Signal/Calls/UserInterface/CallsListViewController.swift b/Signal/Calls/UserInterface/CallsListViewController.swift index 75cef5e089..237505e69d 100644 --- a/Signal/Calls/UserInterface/CallsListViewController.swift +++ b/Signal/Calls/UserInterface/CallsListViewController.swift @@ -1223,10 +1223,10 @@ class CallsListViewController: OWSViewController, HomeTabViewController, CallSer } private static nonisolated func peekCallLink(rootKey: CallLinkRootKey, deps: Dependencies) async throws { - guard let localIdentifiers = deps.tsAccountManager.localIdentifiersWithMaybeSneakyTransaction else { - throw OWSGenericError("Not registered.") - } - let authCredential = try await deps.callService.authCredentialManager.fetchCallLinkAuthCredential(localIdentifiers: localIdentifiers) + let registeredState = try deps.tsAccountManager.registeredStateWithMaybeSneakyTransaction() + let authCredential = try await deps.callService.authCredentialManager.fetchCallLinkAuthCredential( + localIdentifiers: registeredState.localIdentifiers, + ) let eraId: String? do { eraId = try await deps.callService.callLinkManager.peekCallLink(rootKey: rootKey, authCredential: authCredential) diff --git a/Signal/URLs/UrlOpener.swift b/Signal/URLs/UrlOpener.swift index 5b21c63582..4e2e11409a 100644 --- a/Signal/URLs/UrlOpener.swift +++ b/Signal/URLs/UrlOpener.swift @@ -123,11 +123,9 @@ class UrlOpener { @MainActor func openUrl(_ parsedUrl: ParsedUrl, in window: UIWindow) { - guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - return owsFailDebug("Ignoring URL; not registered.") - } guard let rootViewController = window.rootViewController else { - return owsFailDebug("Ignoring URL; no root view controller.") + owsFailDebug("Ignoring URL; no root view controller.") + return } if shouldDismiss(for: parsedUrl.openableUrl) && rootViewController.presentedViewController != nil { rootViewController.dismiss(animated: false, completion: { @@ -147,11 +145,22 @@ class UrlOpener { @MainActor private func openUrlAfterDismissing(_ openableUrl: OpenableUrl, rootViewController: UIViewController) { + do throws(NotRegisteredError) { + try _openUrlAfterDismissing(openableUrl, rootViewController: rootViewController) + } catch { + Logger.warn("Ignoring url because we're not registered") + } + } + + @MainActor + private func _openUrlAfterDismissing(_ openableUrl: OpenableUrl, rootViewController: UIViewController) throws(NotRegisteredError) { switch openableUrl { case .phoneNumberLink(let url): + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() SignalDotMePhoneNumberLink.openChat(url: url, fromViewController: rootViewController) case .usernameLink(let link): + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() Task { guard let (_, aci) = await UsernameQuerier().queryForUsernameLink( link: link, @@ -167,18 +176,21 @@ class UrlOpener { } case .stickerPack(let stickerPackInfo): + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() let stickerPackViewController = StickerPackViewController(stickerPackInfo: stickerPackInfo) stickerPackViewController.present(from: rootViewController, animated: false) case .groupInvite(let url): + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() GroupInviteLinksUI.openGroupInviteLink(url, fromViewController: rootViewController) case .signalProxy(let url): rootViewController.present(ProxyLinkSheetViewController(url: url)!, animated: true) case .linkDevice: - guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegisteredPrimaryDevice else { - owsFailDebug("Ignoring URL; not primary device.") + let registeredState = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() + guard registeredState.isPrimary else { + Logger.warn("Ignoring URL; not primary device.") return } @@ -203,9 +215,9 @@ class UrlOpener { rootViewController.presentActionSheet(linkDeviceWarningActionSheet) case .quickRestore(let url): - - guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegisteredPrimaryDevice else { - owsFailDebug("Ignoring URL; not primary device.") + let registeredState = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() + guard registeredState.isPrimary else { + Logger.warn("Ignoring URL; not primary device.") return } @@ -219,6 +231,7 @@ class UrlOpener { } case .completeIDEALDonation(let donationType): + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() Task { [appReadiness, databaseStorage] in let handled = await DonationViewsUtil.attemptToContinueActiveIDEALDonation( type: donationType, @@ -245,6 +258,7 @@ class UrlOpener { } case .callLink(let callLink): + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() GroupCallViewController.presentLobby(for: callLink) } } diff --git a/Signal/src/ViewControllers/AppSettings/Account/AccountSettingsViewController.swift b/Signal/src/ViewControllers/AppSettings/Account/AccountSettingsViewController.swift index 5e760e46a4..06601c0dfa 100644 --- a/Signal/src/ViewControllers/AppSettings/Account/AccountSettingsViewController.swift +++ b/Signal/src/ViewControllers/AppSettings/Account/AccountSettingsViewController.swift @@ -269,8 +269,8 @@ class AccountSettingsViewController: OWSTableViewController2 { private func changeNumberState() -> ChangeNumberState { return SSKEnvironment.shared.databaseStorageRef.read { transaction -> ChangeNumberState in let tsAccountManager = DependenciesBridge.shared.tsAccountManager - let tsRegistrationState = tsAccountManager.registrationState(tx: transaction) - guard tsRegistrationState.isRegistered else { + let registeredState = try? tsAccountManager.registeredState(tx: transaction) + guard let registeredState else { return .disallowed } let loader = RegistrationCoordinatorLoaderImpl(dependencies: .from(self)) @@ -282,8 +282,7 @@ class AccountSettingsViewController: OWSTableViewController2 { return .disallowed } guard - let localIdentifiers = tsAccountManager.localIdentifiers(tx: transaction), - let localE164 = E164(localIdentifiers.phoneNumber), + let localE164 = E164(registeredState.localIdentifiers.phoneNumber), let authToken = tsAccountManager.storedServerAuthToken(tx: transaction), let localDeviceId = tsAccountManager.storedDeviceId(tx: transaction).ifValid else { @@ -293,7 +292,7 @@ class AccountSettingsViewController: OWSTableViewController2 { return .allowed(RegistrationMode.ChangeNumberParams( oldE164: localE164, oldAuthToken: authToken, - localAci: localIdentifiers.aci, + localAci: registeredState.localIdentifiers.aci, localDeviceId: localDeviceId )) } diff --git a/SignalServiceKit/Account/IdentityKeyMismatchManager.swift b/SignalServiceKit/Account/IdentityKeyMismatchManager.swift index f4a571dec1..154323d788 100644 --- a/SignalServiceKit/Account/IdentityKeyMismatchManager.swift +++ b/SignalServiceKit/Account/IdentityKeyMismatchManager.swift @@ -151,13 +151,9 @@ class IdentityKeyMismatchManagerImpl: IdentityKeyMismatchManager { let localIdentifier: ServiceId do { let loadLocalIdentifiers = { [db, tsAccountManager] () throws -> LocalIdentifiers in - let localIdentifiers = db.read { tx in - return tsAccountManager.localIdentifiers(tx: tx) + return try db.read { tx in + return try tsAccountManager.registeredState(tx: tx).localIdentifiers } - guard let localIdentifiers else { - throw OWSGenericError("not registered") - } - return localIdentifiers } switch identity { diff --git a/SignalServiceKit/Account/RegisteredState.swift b/SignalServiceKit/Account/RegisteredState.swift new file mode 100644 index 0000000000..0eac5a9dc5 --- /dev/null +++ b/SignalServiceKit/Account/RegisteredState.swift @@ -0,0 +1,20 @@ +// +// Copyright 2025 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only +// + +import Foundation + +public struct RegisteredState { + public let isPrimary: Bool + public let localIdentifiers: LocalIdentifiers + + init(registrationState: TSRegistrationState, localIdentifiers: LocalIdentifiers?) throws(NotRegisteredError) { + guard registrationState.isRegistered else { + throw NotRegisteredError() + } + // These are both valid when we're registered. + self.isPrimary = registrationState.isPrimaryDevice! + self.localIdentifiers = localIdentifiers! + } +} diff --git a/SignalServiceKit/Account/RegistrationIdMismatchManager.swift b/SignalServiceKit/Account/RegistrationIdMismatchManager.swift index d4062c78a0..460eb13ad2 100644 --- a/SignalServiceKit/Account/RegistrationIdMismatchManager.swift +++ b/SignalServiceKit/Account/RegistrationIdMismatchManager.swift @@ -34,26 +34,19 @@ public class RegistrationIdMismatchManagerImpl: RegistrationIdMismatchManager { return } - guard db.read(block: { - tsAccountManager.registrationState(tx: $0).isRegistered + guard let registeredState = db.read(block: { tx in + return try? tsAccountManager.registeredState(tx: tx) }) else { Logger.warn("Attempting to check registrationId while unregistered.") return } - guard let localIdentifiers = db.read(block: { - tsAccountManager.localIdentifiers(tx: $0) - }) else { - owsFailDebug("Missing local identifiers during registrationId check") - return - } - do { // Check ACI - try await _checkRegistrationIdMatches(identity: .aci, serviceId: localIdentifiers.aci) + try await _checkRegistrationIdMatches(identity: .aci, serviceId: registeredState.localIdentifiers.aci) // Check PNI - if let pni = localIdentifiers.pni { + if let pni = registeredState.localIdentifiers.pni { try await _checkRegistrationIdMatches(identity: .pni, serviceId: pni) } else { owsFailDebug("Missing PNI during registrationId check") diff --git a/SignalServiceKit/Account/TSAccountManager/TSAccountManager.swift b/SignalServiceKit/Account/TSAccountManager/TSAccountManager.swift index 189943bcc8..55fafa87df 100644 --- a/SignalServiceKit/Account/TSAccountManager/TSAccountManager.swift +++ b/SignalServiceKit/Account/TSAccountManager/TSAccountManager.swift @@ -111,6 +111,20 @@ public enum LocalDeviceId: CustomStringConvertible { } extension TSAccountManager { + public func registeredStateWithMaybeSneakyTransaction() throws(NotRegisteredError) -> RegisteredState { + return try RegisteredState( + registrationState: self.registrationStateWithMaybeSneakyTransaction, + localIdentifiers: self.localIdentifiersWithMaybeSneakyTransaction, + ) + } + + public func registeredState(tx: DBReadTransaction) throws(NotRegisteredError) -> RegisteredState { + return try RegisteredState( + registrationState: self.registrationState(tx: tx), + localIdentifiers: self.localIdentifiers(tx: tx), + ) + } + public func localIdentifiersWithMaybeSneakyTransaction(authedAccount: AuthedAccount) throws -> LocalIdentifiers { switch authedAccount.info { case .explicit(let info): diff --git a/SignalServiceKit/Backups/Archiving/Archivers/Recipient/BackupArchiveAvatarFetcher.swift b/SignalServiceKit/Backups/Archiving/Archivers/Recipient/BackupArchiveAvatarFetcher.swift index f44b4fc79a..80bc41cdfc 100644 --- a/SignalServiceKit/Backups/Archiving/Archivers/Recipient/BackupArchiveAvatarFetcher.swift +++ b/SignalServiceKit/Backups/Archiving/Archivers/Recipient/BackupArchiveAvatarFetcher.swift @@ -173,10 +173,7 @@ public class BackupArchiveAvatarFetcher { record: Record, loader: TaskQueueLoader ) async -> TaskRecordResult { - guard - tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered, - let localIdentifiers = tsAccountManager.localIdentifiersWithMaybeSneakyTransaction - else { + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { try? await loader.stop() return .cancelled } @@ -198,7 +195,7 @@ public class BackupArchiveAvatarFetcher { } do { - if localIdentifiers.contains(serviceId: serviceId) { + if registeredState.localIdentifiers.contains(serviceId: serviceId) { _ = try await profileManager.fetchLocalUsersProfile( authedAccount: .implicit() ) diff --git a/SignalServiceKit/Backups/Attachments/BackupAttachmentUploadQueueRunner.swift b/SignalServiceKit/Backups/Attachments/BackupAttachmentUploadQueueRunner.swift index df5acce2dd..d5814fe3be 100644 --- a/SignalServiceKit/Backups/Attachments/BackupAttachmentUploadQueueRunner.swift +++ b/SignalServiceKit/Backups/Attachments/BackupAttachmentUploadQueueRunner.swift @@ -4,6 +4,7 @@ // import Foundation +import LibSignalClient public protocol BackupAttachmentUploadQueueRunner { @@ -415,17 +416,18 @@ class BackupAttachmentUploadQueueRunnerImpl: BackupAttachmentUploadQueueRunner { break } - let (localAci, isRegisteredPrimaryDevice) = db.read { tx in - return ( - self.tsAccountManager.localIdentifiers(tx: tx)?.aci, - self.tsAccountManager.registrationState(tx: tx).isRegisteredPrimaryDevice, - ) + let localAci: Aci + let isPrimary: Bool + do throws(NotRegisteredError) { + let registeredState = try self.tsAccountManager.registeredStateWithMaybeSneakyTransaction() + localAci = registeredState.localIdentifiers.aci + isPrimary = registeredState.isPrimary + } catch { + try? await loader.stop(reason: error) + return .retryableError(error) } - guard - isRegisteredPrimaryDevice, - let localAci - else { - let error = OWSAssertionError("Not registered!") + guard isPrimary else { + let error = OWSAssertionError("not primary") try? await loader.stop(reason: error) return .retryableError(error) } diff --git a/SignalServiceKit/Contacts/OWSSyncManager.swift b/SignalServiceKit/Contacts/OWSSyncManager.swift index f75625f7d7..8e86a054a2 100644 --- a/SignalServiceKit/Contacts/OWSSyncManager.swift +++ b/SignalServiceKit/Contacts/OWSSyncManager.swift @@ -82,15 +82,11 @@ extension OWSSyncManager: SyncManagerProtocolObjc { private func _sendConfigurationSyncMessage(tx: DBWriteTransaction) { let tsAccountManager = DependenciesBridge.shared.tsAccountManager - guard tsAccountManager.registrationState(tx: tx).isRegistered else { - return - } - - guard let thread = TSContactThread.getOrCreateLocalThread(transaction: tx) else { - owsFailDebug("Missing thread.") + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return } + let thread = TSContactThread.getOrCreateThread(withContactAddress: registeredState.localIdentifiers.aciAddress, transaction: tx) let linkPreviews = DependenciesBridge.shared.linkPreviewSettingStore.areLinkPreviewsEnabled(tx: tx) let readReceipts = OWSReceiptManager.areReadReceiptsEnabled(transaction: tx) let sealedSenderIndicators = SSKEnvironment.shared.preferencesRef.shouldShowUnidentifiedDeliveryIndicators(transaction: tx) @@ -140,9 +136,8 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { } private func _sendAllSyncRequestMessages(onlyIfNecessary: Bool) async throws { - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - throw OWSAssertionError("Unexpectedly tried to send sync request before registration.") - } + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() let databaseStorage = SSKEnvironment.shared.databaseStorageRef let completionGuarantees = await databaseStorage.awaitableWrite { (transaction) -> [Guarantee] in @@ -186,16 +181,19 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { public func sendKeysSyncMessage() { Logger.info("") - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - return owsFailDebug("Unexpectedly tried to send sync request before registration.") + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { + owsFailDebug("Unexpectedly tried to send sync request before registration.") + return } - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isPrimaryDevice ?? false else { - return owsFailDebug("Keys sync should only be initiated from the primary device") + guard registeredState.isPrimary else { + owsFailDebug("Keys sync should only be initiated from the primary device") + return } - SSKEnvironment.shared.databaseStorageRef.asyncWrite { [weak self] transaction in - self?.sendKeysSyncMessage(tx: transaction) + SSKEnvironment.shared.databaseStorageRef.asyncWrite { transaction in + self.sendKeysSyncMessage(tx: transaction) } } @@ -360,12 +358,14 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { public func sendMessageRequestResponseSyncMessage(thread: TSThread, responseType: OWSSyncMessageRequestResponseType) { Logger.info("") - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - return owsFailDebug("Unexpectedly tried to send sync message before registration.") + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { + owsFailDebug("Unexpectedly tried to send sync message before registration.") + return } - SSKEnvironment.shared.databaseStorageRef.asyncWrite { [weak self] transaction in - self?.sendMessageRequestResponseSyncMessage(thread: thread, responseType: responseType, transaction: transaction) + SSKEnvironment.shared.databaseStorageRef.asyncWrite { transaction in + self.sendMessageRequestResponseSyncMessage(thread: thread, responseType: responseType, transaction: transaction) } } @@ -376,15 +376,17 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { ) { Logger.info("") - guard DependenciesBridge.shared.tsAccountManager.registrationState(tx: transaction).isRegistered else { - return owsFailDebug("Unexpectedly tried to send sync message before registration.") - } - - guard let localThread = TSContactThread.getOrCreateLocalThread(transaction: transaction) else { - owsFailDebug("Couldn't get localThread.") + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: transaction) else { + owsFailDebug("Unexpectedly tried to send sync message before registration.") return } + let localThread = TSContactThread.getOrCreateThread( + withContactAddress: registeredState.localIdentifiers.aciAddress, + transaction: transaction, + ) + let syncMessageRequestResponse = OWSSyncMessageRequestResponseMessage( localThread: localThread, messageRequestThread: thread, @@ -465,9 +467,8 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { let tsAccountManager = DependenciesBridge.shared.tsAccountManager let (localIdentifiers, hasAnyLinkedDevice) = try SSKEnvironment.shared.databaseStorageRef.read { tx in - guard let localIdentifiers = tsAccountManager.localIdentifiers(tx: tx) else { - throw OWSError(error: .contactSyncFailed, description: "Not registered.", isRetryable: false) - } + let registeredState = try tsAccountManager.registeredState(tx: tx) + let localIdentifiers = registeredState.localIdentifiers let localRecipient = recipientDatabaseTable.fetchRecipient(serviceId: localIdentifiers.aci, transaction: tx) return (localIdentifiers, (localRecipient?.deviceIds ?? []).count >= 2) } @@ -599,15 +600,15 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { private func _sendFetchLatestSyncMessage(type: OWSSyncFetchType, tx: DBWriteTransaction) { let tsAccountManager = DependenciesBridge.shared.tsAccountManager - guard tsAccountManager.registrationState(tx: tx).isRegistered else { + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { owsFailDebug("Tried to send sync message before registration.") return } - guard let thread = TSContactThread.getOrCreateLocalThread(transaction: tx) else { - owsFailDebug("Missing thread.") - return - } + let thread = TSContactThread.getOrCreateThread( + withContactAddress: registeredState.localIdentifiers.aciAddress, + transaction: tx, + ) let fetchLatestSyncMessage = OWSSyncFetchLatestMessage(localThread: thread, fetchType: type, transaction: tx) let preparedMessage = PreparedOutgoingMessage.preprepared( @@ -660,7 +661,8 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { public func sendInitialSyncRequestsAwaitingCreatedThreadOrdering(timeoutSeconds: TimeInterval) -> Promise<[String]> { Logger.info("") - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { return Promise(error: OWSAssertionError("Unexpectedly tried to send sync request before registration.")) } @@ -698,17 +700,21 @@ extension OWSSyncManager: SyncManagerProtocol, SyncManagerProtocolSwift { Logger.info("keys") } - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - return owsFailDebug("Unexpectedly tried to send sync request before registration.") + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { + owsFailDebug("Unexpectedly tried to send sync request before registration.") + return } - guard !DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegisteredPrimaryDevice else { - return owsFailDebug("Sync request should only be sent from a linked device") + guard !registeredState.isPrimary else { + owsFailDebug("Sync request should only be sent from a linked device") + return } - guard let thread = TSContactThread.getOrCreateLocalThread(transaction: transaction) else { - return owsFailDebug("Missing thread") - } + let thread = TSContactThread.getOrCreateThread( + withContactAddress: registeredState.localIdentifiers.aciAddress, + transaction: transaction, + ) let syncRequestMessage = OWSSyncRequestMessage(localThread: thread, requestType: requestType.rawValue, transaction: transaction) let preparedMessage = PreparedOutgoingMessage.preprepared( diff --git a/SignalServiceKit/Devices/LinkAndSyncManager.swift b/SignalServiceKit/Devices/LinkAndSyncManager.swift index c0ec277a07..322ac2061b 100644 --- a/SignalServiceKit/Devices/LinkAndSyncManager.swift +++ b/SignalServiceKit/Devices/LinkAndSyncManager.swift @@ -166,20 +166,15 @@ public class LinkAndSyncManagerImpl: LinkAndSyncManager { tokenId: DeviceProvisioningTokenId, progress: OWSSequentialProgressRootSink ) async throws(PrimaryLinkNSyncError) { - let (localIdentifiers, registrationState) = db.read { tx in - return ( - tsAccountManager.localIdentifiers(tx: tx), - tsAccountManager.registrationState(tx: tx) - ) - } - guard let localIdentifiers else { - owsFailDebug("Not registered!") - return - } - guard registrationState.isPrimaryDevice == true else { - owsFailDebug("Non-primary device waiting for secondary linking") + let registeredState: RegisteredState + do throws(NotRegisteredError) { + registeredState = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() + } catch { + // TODO: Throw an error to indicate this failed because we're not registered. + Logger.warn("Couldn't wait for linking because we're no longer registered") return } + owsPrecondition(registeredState.isPrimary, "Can't wait for linking unless we're a primary") let blockObject = DeviceSleepBlockObject(blockReason: Constants.sleepBlockingDescription) await deviceSleepManager?.addBlock(blockObject: blockObject) @@ -235,7 +230,7 @@ public class LinkAndSyncManagerImpl: LinkAndSyncManager { backupMetadata = try await generateBackup( waitForDeviceToLinkResponse: waitForLinkResponse, ephemeralBackupKey: ephemeralBackupKey, - localIdentifiers: localIdentifiers, + localIdentifiers: registeredState.localIdentifiers, progress: progress.child(for: .exportingBackup) ) } catch let error { diff --git a/SignalServiceKit/Groups/GroupManager.swift b/SignalServiceKit/Groups/GroupManager.swift index 38dee5177a..9fd8781402 100644 --- a/SignalServiceKit/Groups/GroupManager.swift +++ b/SignalServiceKit/Groups/GroupManager.swift @@ -1280,21 +1280,17 @@ public class GroupManager: NSObject { } private static func _ensureLocalProfileHasCommitmentIfNecessary() async throws { - let accountManager = DependenciesBridge.shared.tsAccountManager + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + let registeredState = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() - func hasProfileKeyCredential() throws -> Bool { - return try SSKEnvironment.shared.databaseStorageRef.read { tx in - guard accountManager.registrationState(tx: tx).isRegistered else { - return false - } - guard let localAci = accountManager.localIdentifiers(tx: tx)?.aci else { - throw OWSAssertionError("Missing localAci.") - } + func hasProfileKeyCredential() -> Bool { + return SSKEnvironment.shared.databaseStorageRef.read { tx in + let localAci = registeredState.localIdentifiers.aci return SSKEnvironment.shared.groupsV2Ref.hasProfileKeyCredential(for: localAci, transaction: tx) } } - guard try !hasProfileKeyCredential() else { + guard !hasProfileKeyCredential() else { return } @@ -1303,16 +1299,11 @@ public class GroupManager: NSObject { // would get as part of fetching our local profile). _ = try await SSKEnvironment.shared.profileManagerRef.fetchLocalUsersProfile(authedAccount: .implicit()) - guard try !hasProfileKeyCredential() else { + guard !hasProfileKeyCredential() else { return } - guard - CurrentAppContext().isMainApp, - SSKEnvironment.shared.databaseStorageRef.read(block: { tx in - accountManager.registrationState(tx: tx).isRegisteredPrimaryDevice - }) - else { + guard registeredState.isPrimary, CurrentAppContext().isMainApp else { Logger.warn("Skipping upload of local profile key commitment, not in main app!") return } diff --git a/SignalServiceKit/Jobs/DonationReceiptCredentialRedemptionJobQueue.swift b/SignalServiceKit/Jobs/DonationReceiptCredentialRedemptionJobQueue.swift index 1c9c4b762f..1a8b48226b 100644 --- a/SignalServiceKit/Jobs/DonationReceiptCredentialRedemptionJobQueue.swift +++ b/SignalServiceKit/Jobs/DonationReceiptCredentialRedemptionJobQueue.swift @@ -525,20 +525,11 @@ private class DonationReceiptCredentialRedemptionJobRunner: JobRunner { // Now that we know what type of job we are, suffix the logger. logger = logger.suffixed(with: "[\(configuration.paymentType)]") - let ( - registrationState, - badgesSnapshotBeforeJob, - ) = db.read { tx in - return ( - tsAccountManager.registrationState(tx: tx), - // In order to properly show the "you have a new badge" UI after this job - // succeeds, we need to know what badges we had beforehand. - ProfileBadgesSnapshot.forLocalProfile(profileManager: profileManager, tx: tx), - ) - } - - guard registrationState.isRegistered else { - throw OWSAssertionError("Attempting to redeem a donation, but not registered!") + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() + let badgesSnapshotBeforeJob = db.read { tx in + // In order to properly show the "you have a new badge" UI after this job + // succeeds, we need to know what badges we had beforehand. + return ProfileBadgesSnapshot.forLocalProfile(profileManager: profileManager, tx: tx) } logger.info("Running job.") diff --git a/SignalServiceKit/Messages/BlockingManager.swift b/SignalServiceKit/Messages/BlockingManager.swift index 5eeec6872d..43b5145d7d 100644 --- a/SignalServiceKit/Messages/BlockingManager.swift +++ b/SignalServiceKit/Messages/BlockingManager.swift @@ -427,13 +427,12 @@ public class BlockingManager { } let tsAccountManager = DependenciesBridge.shared.tsAccountManager - guard tsAccountManager.registrationState(tx: tx).isRegistered else { - throw OWSGenericError("Not registered.") - } + let registeredState = try tsAccountManager.registeredState(tx: tx) - guard let localThread = TSContactThread.getOrCreateLocalThread(transaction: tx) else { - throw OWSAssertionError("Missing thread.") - } + let localThread = TSContactThread.getOrCreateThread( + withContactAddress: registeredState.localIdentifiers.aciAddress, + transaction: tx, + ) let recipientDatabaseTable = DependenciesBridge.shared.recipientDatabaseTable let blockedRecipients = try blockedRecipientStore.blockedRecipientIds(tx: tx).compactMap { diff --git a/SignalServiceKit/Messages/MessageProcessor.swift b/SignalServiceKit/Messages/MessageProcessor.swift index f8d6c6571e..1261132b7f 100644 --- a/SignalServiceKit/Messages/MessageProcessor.swift +++ b/SignalServiceKit/Messages/MessageProcessor.swift @@ -159,7 +159,6 @@ public class MessageProcessor { private func drainPendingEnvelopes() { guard CurrentAppContext().shouldProcessIncomingMessages else { return } guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { return } - guard SSKEnvironment.shared.messagePipelineSupervisorRef.isMessageProcessingPermitted else { return } queueForProcessing.async { diff --git a/SignalServiceKit/Messages/MessageSender.swift b/SignalServiceKit/Messages/MessageSender.swift index 7615bfc2e0..e34cf97289 100644 --- a/SignalServiceKit/Messages/MessageSender.swift +++ b/SignalServiceKit/Messages/MessageSender.swift @@ -604,9 +604,7 @@ public class MessageSender { throw AppExpiredError() } let tsAccountManager = DependenciesBridge.shared.tsAccountManager - if !tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered { - throw NotRegisteredError() - } + _ = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() if message.shouldBeSaved { let latestCopy = SSKEnvironment.shared.databaseStorageRef.read { tx in TSInteraction.anyFetch(uniqueId: message.uniqueId, transaction: tx) as? TSOutgoingMessage diff --git a/SignalServiceKit/Messages/OWSIdentityManager.swift b/SignalServiceKit/Messages/OWSIdentityManager.swift index c46001bd6a..79e5e403c9 100644 --- a/SignalServiceKit/Messages/OWSIdentityManager.swift +++ b/SignalServiceKit/Messages/OWSIdentityManager.swift @@ -593,13 +593,10 @@ public class OWSIdentityManagerImpl: OWSIdentityManager { } private func syncQueuedVerificationStates() { - guard tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - return - } - guard let thread = TSContactThread.getOrCreateLocalThreadWithSneakyTransaction() else { - owsFailDebug("Missing thread.") + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { return } + let thread = TSContactThread.getOrCreateThread(contactAddress: registeredState.localIdentifiers.aciAddress) let allKeys = db.read { tx in queuedVerificationStateSyncMessagesKeyValueStore.allKeys(transaction: tx) } // We expect very few keys in practice, and each key triggers multiple // database write transactions. If we do end up with thousands of keys, diff --git a/SignalServiceKit/Messages/Stickers/StickerManager.swift b/SignalServiceKit/Messages/Stickers/StickerManager.swift index e4615e18fd..2f4506fe1a 100644 --- a/SignalServiceKit/Messages/Stickers/StickerManager.swift +++ b/SignalServiceKit/Messages/Stickers/StickerManager.swift @@ -1096,13 +1096,14 @@ public class StickerManager: NSObject { packs: [StickerPackInfo], transaction: DBWriteTransaction ) { - guard DependenciesBridge.shared.tsAccountManager.registrationState(tx: transaction).isRegistered else { - return - } - guard let thread = TSContactThread.getOrCreateLocalThread(transaction: transaction) else { - owsFailDebug("Missing thread.") + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: transaction) else { return } + let thread = TSContactThread.getOrCreateThread( + withContactAddress: registeredState.localIdentifiers.aciAddress, + transaction: transaction, + ) let message = OWSStickerPackSyncMessage( localThread: thread, packs: packs, diff --git a/SignalServiceKit/Messages/UD/OWSUDManager.swift b/SignalServiceKit/Messages/UD/OWSUDManager.swift index 295bf0ced3..e93ecdc8e5 100644 --- a/SignalServiceKit/Messages/UD/OWSUDManager.swift +++ b/SignalServiceKit/Messages/UD/OWSUDManager.swift @@ -296,10 +296,7 @@ public class OWSUDManagerImpl: OWSUDManager { } private func _fetchSenderCertificates(forceRefresh: Bool) async throws -> SenderCertificates { - guard self.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - // We don't want to assert but we should log and fail. - throw OWSGenericError("Not registered and ready.") - } + _ = try self.tsAccountManager.registeredStateWithMaybeSneakyTransaction() async let defaultCert = fetchSenderCertificate(aciOnly: false, forceRefresh: forceRefresh) async let aciOnlyCert = fetchSenderCertificate(aciOnly: true, forceRefresh: forceRefresh) return SenderCertificates( diff --git a/SignalServiceKit/Payments/PaymentsHelperImpl.swift b/SignalServiceKit/Payments/PaymentsHelperImpl.swift index ad79a5e2a6..c034f42ee0 100644 --- a/SignalServiceKit/Payments/PaymentsHelperImpl.swift +++ b/SignalServiceKit/Payments/PaymentsHelperImpl.swift @@ -29,12 +29,11 @@ public class PaymentsHelperImpl: PaymentsHelperSwift, PaymentsHelper { } public var hasValidPhoneNumberForPayments: Bool { - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegistered else { - return false - } - guard let localNumber = DependenciesBridge.shared.tsAccountManager.localIdentifiersWithMaybeSneakyTransaction?.phoneNumber else { + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { return false } + let localNumber = registeredState.localIdentifiers.phoneNumber let paymentsDisabledRegions = RemoteConfig.current.paymentsDisabledRegions if paymentsDisabledRegions.isEmpty { return Self.isValidPhoneNumberForPayments_fixedAllowlist(localNumber) diff --git a/SignalServiceKit/Profiles/OWSProfileManager.swift b/SignalServiceKit/Profiles/OWSProfileManager.swift index 60284e511b..b9efc86d7d 100644 --- a/SignalServiceKit/Profiles/OWSProfileManager.swift +++ b/SignalServiceKit/Profiles/OWSProfileManager.swift @@ -707,8 +707,10 @@ extension OWSProfileManager: ProfileManager { triggers: [RotateProfileKeyTrigger], authedAccount: AuthedAccount ) async throws -> Bool { - guard DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction.isRegisteredPrimaryDevice else { - throw OWSAssertionError("tsAccountManager.isRegistered was unexpectedly false") + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + let registeredState = try tsAccountManager.registeredStateWithMaybeSneakyTransaction() + guard registeredState.isPrimary else { + throw OWSAssertionError("not a primary device") } Logger.info("Beginning profile key rotation.") @@ -738,9 +740,7 @@ extension OWSProfileManager: ProfileManager { } try await uploadPromise.awaitable() - guard let localAci = DependenciesBridge.shared.tsAccountManager.localIdentifiersWithMaybeSneakyTransaction?.aci else { - throw OWSAssertionError("Missing localAci.") - } + let localAci = registeredState.localIdentifiers.aci Logger.info("Persisting rotated profile key and kicking off subsequent operations.") @@ -1489,11 +1489,11 @@ extension OWSProfileManager: ProfileManager { /// /// - Parameter tx: The transaction to use for this operation. func rotateProfileKeyUponRecipientHideObjC(tx: DBWriteTransaction) { - let tsRegistrationState = DependenciesBridge.shared.tsAccountManager.registrationState(tx: tx) - guard tsRegistrationState.isRegistered else { + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return } - guard tsRegistrationState.isPrimaryDevice ?? false else { + guard registeredState.isPrimary else { return } // We schedule in the NSE by writing state; the actual rotation @@ -1503,11 +1503,11 @@ extension OWSProfileManager: ProfileManager { } fileprivate func _forceRotateLocalProfileKeyForGroupDeparture(tx: DBWriteTransaction) { - let tsRegistrationState = DependenciesBridge.shared.tsAccountManager.registrationState(tx: tx) - guard tsRegistrationState.isRegistered else { + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return } - guard tsRegistrationState.isPrimaryDevice ?? false else { + guard registeredState.isPrimary else { return } // We schedule in the NSE by writing state; the actual rotation diff --git a/SignalServiceKit/SecureValueRecovery/MasterKeySyncManager.swift b/SignalServiceKit/SecureValueRecovery/MasterKeySyncManager.swift index 9e9f58d13d..12c01f525d 100644 --- a/SignalServiceKit/SecureValueRecovery/MasterKeySyncManager.swift +++ b/SignalServiceKit/SecureValueRecovery/MasterKeySyncManager.swift @@ -47,18 +47,14 @@ class MasterKeySyncManagerImpl: MasterKeySyncManager { } func runStartupJobs(tx: DBWriteTransaction) { - switch tsAccountManager.registrationState(tx: tx) { - case .registered: - runStartupJobsForPrimaryDevice(tx: tx) - case .provisioned: - runStartupJobsForLinkedDevice(tx: tx) - case .delinked, .deregistered, .unregistered, .transferred, - .transferringIncoming, .transferringLinkedOutgoing, - .transferringPrimaryOutgoing, - .reregistering, .relinking: - logger.info("Skipping; not registered") + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return } + if registeredState.isPrimary { + runStartupJobsForPrimaryDevice(tx: tx) + } else { + runStartupJobsForLinkedDevice(tx: tx) + } } private func runStartupJobsForPrimaryDevice(tx: DBWriteTransaction) { diff --git a/SignalServiceKit/StorageService/StorageServiceManager.swift b/SignalServiceKit/StorageService/StorageServiceManager.swift index a3f9245a51..6696ac1079 100644 --- a/SignalServiceKit/StorageService/StorageServiceManager.swift +++ b/SignalServiceKit/StorageService/StorageServiceManager.swift @@ -447,8 +447,8 @@ public class StorageServiceManagerImpl: NSObject, StorageServiceManager { case .implicit: // Under the new reg flow, we will sync kbs keys before being fully ready with // ts account manager auth set up. skip if so. - let registrationState = DependenciesBridge.shared.tsAccountManager.registrationStateWithMaybeSneakyTransaction - guard registrationState.isRegistered else { + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { Logger.info("Skipping storage service operation with implicit auth during registration.") return nil } @@ -460,11 +460,7 @@ public class StorageServiceManagerImpl: NSObject, StorageServiceManager { return nil } localIdentifiers = implicitLocalIdentifiers - guard let implicitIsPrimaryDevice = registrationState.isPrimaryDevice else { - owsFailDebug("Trying to perform storage service operation without isPrimaryDevice.") - return nil - } - isPrimaryDevice = implicitIsPrimaryDevice + isPrimaryDevice = registeredState.isPrimary } return { diff --git a/SignalServiceKit/StorageService/StorageServiceUnknownFieldMigrator.swift b/SignalServiceKit/StorageService/StorageServiceUnknownFieldMigrator.swift index 39bcadfe91..4ee9424775 100644 --- a/SignalServiceKit/StorageService/StorageServiceUnknownFieldMigrator.swift +++ b/SignalServiceKit/StorageService/StorageServiceUnknownFieldMigrator.swift @@ -346,8 +346,8 @@ public class StorageServiceUnknownFieldMigrator { return } - guard let isPrimaryDevice = DependenciesBridge.shared.tsAccountManager.registrationState(tx: tx).isPrimaryDevice else { - // Not registered! + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return } @@ -356,7 +356,7 @@ public class StorageServiceUnknownFieldMigrator { migration: Migration ) { let filteredRecords = records.compactMap { $0 as? Migration.RecordType } - migration.mergeUnknownFields(filteredRecords, isPrimaryDevice, tx) + migration.mergeUnknownFields(filteredRecords, registeredState.isPrimary, tx) } necessaryMigrations.forEach { migrationId in @@ -378,8 +378,8 @@ public class StorageServiceUnknownFieldMigrator { return record } - guard let isPrimaryDevice = DependenciesBridge.shared.tsAccountManager.registrationState(tx: tx).isPrimaryDevice else { - // Not registered! + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return record } @@ -393,7 +393,7 @@ public class StorageServiceUnknownFieldMigrator { } let builder = _builder ?? record.asBuilder() var typecastBuilder = builder as! Migration.RecordType.Builder - migration.interceptRemoteManifest(record as! Migration.RecordType, &typecastBuilder, isPrimaryDevice, tx) + migration.interceptRemoteManifest(record as! Migration.RecordType, &typecastBuilder, registeredState.isPrimary, tx) _builder = (typecastBuilder as! RecordType.Builder) } @@ -421,8 +421,8 @@ public class StorageServiceUnknownFieldMigrator { return record } - guard let isPrimaryDevice = DependenciesBridge.shared.tsAccountManager.registrationState(tx: tx).isPrimaryDevice else { - // Not registered! + let tsAccountManager = DependenciesBridge.shared.tsAccountManager + guard let registeredState = try? tsAccountManager.registeredState(tx: tx) else { return record } @@ -436,7 +436,7 @@ public class StorageServiceUnknownFieldMigrator { } let builder = _builder ?? record.asBuilder() var typecastBuilder = builder as! Migration.RecordType.Builder - migration.interceptLocalManifest(record as! Migration.RecordType, &typecastBuilder, isPrimaryDevice, tx) + migration.interceptLocalManifest(record as! Migration.RecordType, &typecastBuilder, registeredState.isPrimary, tx) _builder = (typecastBuilder as! RecordType.Builder) } diff --git a/SignalServiceKit/Subscriptions/Donations/DonationUtilities.swift b/SignalServiceKit/Subscriptions/Donations/DonationUtilities.swift index 955357096b..8edd7f9b03 100644 --- a/SignalServiceKit/Subscriptions/Donations/DonationUtilities.swift +++ b/SignalServiceKit/Subscriptions/Donations/DonationUtilities.swift @@ -153,17 +153,14 @@ public class DonationUtilities { inMode donationMode: DonationMode, tsAccountManager: TSAccountManager, ) -> Bool { - let registrationState = tsAccountManager.registrationStateWithMaybeSneakyTransaction - let localNumber = tsAccountManager.localIdentifiersWithMaybeSneakyTransaction?.phoneNumber - - guard registrationState.isRegistered else { + guard let registeredState = try? tsAccountManager.registeredStateWithMaybeSneakyTransaction() else { // Don't allow donations if unregistered. return false } return !supportedDonationPaymentMethods( forDonationMode: donationMode, - localNumber: localNumber + localNumber: registeredState.localIdentifiers.phoneNumber, ).isEmpty }