From 95a1cd89bca9cda15ff10598bd2d494f25d9a547 Mon Sep 17 00:00:00 2001 From: Max Radermacher Date: Thu, 17 Jul 2025 17:46:51 -0500 Subject: [PATCH] Asyncify updateAccountAttributes --- .../RegistrationCoordinatorImpl+Service.swift | 37 +++++++++---------- .../RegistrationCoordinatorImpl.swift | 12 ++++-- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Signal/Registration/RegistrationCoordinatorImpl+Service.swift b/Signal/Registration/RegistrationCoordinatorImpl+Service.swift index 8ee02c4d6f..f93bfc1266 100644 --- a/Signal/Registration/RegistrationCoordinatorImpl+Service.swift +++ b/Signal/Registration/RegistrationCoordinatorImpl+Service.swift @@ -292,37 +292,34 @@ extension RegistrationCoordinatorImpl { } } - /// Returns nil error if success. public static func makeUpdateAccountAttributesRequest( _ attributes: AccountAttributes, auth: ChatServiceAuth, signalService: OWSSignalServiceProtocol, retriesLeft: Int = RegistrationCoordinatorImpl.Constants.networkErrorRetries - ) -> Guarantee { + ) async throws { let request = RegistrationRequestFactory.updatePrimaryDeviceAccountAttributesRequest( attributes, auth: auth ) - return signalService.urlSessionForMainSignalService().promiseForTSRequest(request) - .map(on: SyncScheduler()) { response in - guard response.responseStatusCode >= 200, response.responseStatusCode < 300 else { - // Errors are undifferentiated; the only actual error we can get is an unauthenticated - // one and there isn't any way to handle that as different from a, say server 500. - return OWSAssertionError("Got unexpected response code from update attributes request: \(response.responseStatusCode).") - } - return nil + do { + let response = try await signalService.urlSessionForMainSignalService().performRequest(request) + guard response.responseStatusCode >= 200, response.responseStatusCode < 300 else { + // Errors are undifferentiated; the only actual error we can get is an unauthenticated + // one and there isn't any way to handle that as different from a, say server 500. + throw OWSAssertionError("Got unexpected response code from update attributes request: \(response.responseStatusCode).") } - .recover(on: SyncScheduler()) { error in - if error.isNetworkFailureOrTimeout, retriesLeft > 0 { - return makeUpdateAccountAttributesRequest( - attributes, - auth: auth, - signalService: signalService, - retriesLeft: retriesLeft - 1 - ) - } - return .value(error) + } catch { + if error.isNetworkFailureOrTimeout, retriesLeft > 0 { + return try await makeUpdateAccountAttributesRequest( + attributes, + auth: auth, + signalService: signalService, + retriesLeft: retriesLeft - 1 + ) } + throw error + } } enum WhoAmIResponse { diff --git a/Signal/Registration/RegistrationCoordinatorImpl.swift b/Signal/Registration/RegistrationCoordinatorImpl.swift index f9107fa6e4..9a226f3584 100644 --- a/Signal/Registration/RegistrationCoordinatorImpl.swift +++ b/Signal/Registration/RegistrationCoordinatorImpl.swift @@ -1443,7 +1443,7 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator { ) -> Guarantee { Logger.info("") - return updateAccountAttributes(accountIdentity) + return Guarantee.wrapAsync { await self.updateAccountAttributes(accountIdentity) } .then(on: DispatchQueue.main) { [weak self] error -> Guarantee in guard let self else { return unretainedSelfError() @@ -3886,10 +3886,10 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator { inMemoryState.udAccessKey = udAccessKey } - private func updateAccountAttributes(_ accountIdentity: AccountIdentity) -> Guarantee { + private func updateAccountAttributes(_ accountIdentity: AccountIdentity) async -> Error? { Logger.info("") - return Service - .makeUpdateAccountAttributesRequest( + do { + try await Service.makeUpdateAccountAttributesRequest( makeAccountAttributes( isManualMessageFetchEnabled: inMemoryState.isManualMessageFetchEnabled, twoFAMode: self.attributes2FAMode(e164: accountIdentity.e164) @@ -3897,6 +3897,10 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator { auth: accountIdentity.chatServiceAuth, signalService: deps.signalService, ) + return nil + } catch { + return error + } } private func updatePhoneNumberDiscoverability(accountIdentity: AccountIdentity, phoneNumberDiscoverability: PhoneNumberDiscoverability) {