Skip to content

Commit

Permalink
Try waiting for the encryption service to be ready before getting the…
Browse files Browse the repository at this point in the history
… user identity in `RustSessionVerificationService`
  • Loading branch information
jmartinesp committed Nov 5, 2024
1 parent 8abb1dd commit abc7fab
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -245,29 +249,29 @@ 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 {
// Otherwise, just check the current verification status from the session verification controller instead
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) {
Expand Down

0 comments on commit abc7fab

Please sign in to comment.