From abc7fab03e9b6005508dad7856c6aac6fdd38bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Tue, 5 Nov 2024 10:54:40 +0100 Subject: [PATCH] Try waiting for the encryption service to be ready before getting the user identity in `RustSessionVerificationService` --- .../RustSessionVerificationService.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt index 76fb3d9b17..666cbcb442 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt @@ -231,6 +231,10 @@ class RustSessionVerificationService( private suspend fun initVerificationControllerIfNeeded() = initControllerMutex.withLock { if (!this::verificationController.isInitialized) { tryOrFail { + // We may have a race condition if we try to get the user identity before the encryption service is ready + withTimeout(5.seconds) { + encryptionService.waitForE2eeInitializationTasks() + } verificationController = client.getSessionVerificationController() verificationController.setDelegate(this) } @@ -245,11 +249,7 @@ class RustSessionVerificationService( runCatching { encryptionService.waitForE2eeInitializationTasks() }.onSuccess { - _sessionVerifiedStatus.value = when (encryptionService.verificationState()) { - VerificationState.UNKNOWN -> SessionVerifiedStatus.Unknown - VerificationState.VERIFIED -> SessionVerifiedStatus.Verified - VerificationState.UNVERIFIED -> SessionVerifiedStatus.NotVerified - } + _sessionVerifiedStatus.value = encryptionService.verificationState().map() Timber.d("New verification status: ${_sessionVerifiedStatus.value}") } } else { @@ -257,17 +257,21 @@ class RustSessionVerificationService( Timber.d("Updating verification status: flow is pending or was finished some time ago") runCatching { initVerificationControllerIfNeeded() - _sessionVerifiedStatus.value = if (encryptionService.verificationState() == VerificationState.VERIFIED) { - SessionVerifiedStatus.Verified - } else { - SessionVerifiedStatus.NotVerified - } + _sessionVerifiedStatus.value = encryptionService.verificationState().map() Timber.d("New verification status: ${_sessionVerifiedStatus.value}") } } } } +private fun VerificationState.map(): SessionVerifiedStatus { + return when (this) { + VerificationState.UNKNOWN -> SessionVerifiedStatus.Unknown + VerificationState.VERIFIED -> SessionVerifiedStatus.Verified + VerificationState.UNVERIFIED -> SessionVerifiedStatus.NotVerified + } +} + private fun RustSessionVerificationData.map(): SessionVerificationData { return use { sessionVerificationData -> when (sessionVerificationData) {