mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-03 16:10:00 +00:00
Self verification : makes sure only one controller is created
This commit is contained in:
+14
-8
@@ -26,6 +26,8 @@ import kotlinx.coroutines.flow.map
|
|||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withTimeout
|
import kotlinx.coroutines.withTimeout
|
||||||
import org.matrix.rustcomponents.sdk.Client
|
import org.matrix.rustcomponents.sdk.Client
|
||||||
import org.matrix.rustcomponents.sdk.Encryption
|
import org.matrix.rustcomponents.sdk.Encryption
|
||||||
@@ -99,10 +101,7 @@ class RustSessionVerificationService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun requestVerification() = tryOrFail {
|
override suspend fun requestVerification() = tryOrFail {
|
||||||
if (!this::verificationController.isInitialized) {
|
initVerificationControllerIfNeeded()
|
||||||
verificationController = client.getSessionVerificationController()
|
|
||||||
verificationController.setDelegate(this)
|
|
||||||
}
|
|
||||||
verificationController.requestVerification()
|
verificationController.requestVerification()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,6 +192,16 @@ class RustSessionVerificationService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var initControllerMutex = Mutex()
|
||||||
|
|
||||||
|
private suspend fun initVerificationControllerIfNeeded() = initControllerMutex.withLock {
|
||||||
|
if (!this::verificationController.isInitialized) {
|
||||||
|
tryOrFail {
|
||||||
|
verificationController = client.getSessionVerificationController()
|
||||||
|
verificationController.setDelegate(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
private suspend fun updateVerificationStatus() {
|
private suspend fun updateVerificationStatus() {
|
||||||
if (verificationFlowState.value == VerificationFlowState.Finished) {
|
if (verificationFlowState.value == VerificationFlowState.Finished) {
|
||||||
// Calling `encryptionService.verificationState()` performs a network call and it will deadlock if there is no network
|
// Calling `encryptionService.verificationState()` performs a network call and it will deadlock if there is no network
|
||||||
@@ -212,10 +221,7 @@ class RustSessionVerificationService(
|
|||||||
// Otherwise, just check the current verification status from the session verification controller instead
|
// 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")
|
Timber.d("Updating verification status: flow is pending or was finished some time ago")
|
||||||
runCatching {
|
runCatching {
|
||||||
if (!this@RustSessionVerificationService::verificationController.isInitialized) {
|
initVerificationControllerIfNeeded()
|
||||||
verificationController = client.getSessionVerificationController()
|
|
||||||
verificationController.setDelegate(this@RustSessionVerificationService)
|
|
||||||
}
|
|
||||||
_sessionVerifiedStatus.value = if (verificationController.isVerified()) {
|
_sessionVerifiedStatus.value = if (verificationController.isVerified()) {
|
||||||
SessionVerifiedStatus.Verified
|
SessionVerifiedStatus.Verified
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user