Matrix rust sdk : update SessionVerificationService to suspend (Async-Uniffi)
This commit is contained in:
parent
669ff1fe2f
commit
bca3f58062
4 changed files with 57 additions and 28 deletions
|
|
@ -15,16 +15,18 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@file:Suppress("WildcardImport")
|
@file:Suppress("WildcardImport")
|
||||||
|
|
||||||
package io.element.android.features.verifysession.impl
|
package io.element.android.features.verifysession.impl
|
||||||
|
|
||||||
import io.element.android.libraries.statemachine.createStateMachine
|
|
||||||
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
|
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
|
||||||
import io.element.android.libraries.matrix.api.verification.VerificationEmoji
|
import io.element.android.libraries.matrix.api.verification.VerificationEmoji
|
||||||
import io.element.android.libraries.matrix.api.verification.VerificationFlowState
|
import io.element.android.libraries.matrix.api.verification.VerificationFlowState
|
||||||
|
import io.element.android.libraries.statemachine.createStateMachine
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class VerifySelfSessionStateMachine(
|
class VerifySelfSessionStateMachine(
|
||||||
coroutineScope: CoroutineScope,
|
coroutineScope: CoroutineScope,
|
||||||
|
|
@ -37,13 +39,20 @@ class VerifySelfSessionStateMachine(
|
||||||
on<Event.StartSasVerification>(State.StartingSasVerification)
|
on<Event.StartSasVerification>(State.StartingSasVerification)
|
||||||
}
|
}
|
||||||
addState<State.RequestingVerification> {
|
addState<State.RequestingVerification> {
|
||||||
onEnter { sessionVerificationService.requestVerification() }
|
onEnter {
|
||||||
|
coroutineScope.launch {
|
||||||
|
sessionVerificationService.requestVerification()
|
||||||
|
}
|
||||||
|
}
|
||||||
on<Event.DidAcceptVerificationRequest>(State.VerificationRequestAccepted)
|
on<Event.DidAcceptVerificationRequest>(State.VerificationRequestAccepted)
|
||||||
on<Event.DidFail>(State.Initial)
|
on<Event.DidFail>(State.Initial)
|
||||||
}
|
}
|
||||||
addState<State.StartingSasVerification> {
|
addState<State.StartingSasVerification> {
|
||||||
onEnter { sessionVerificationService.startVerification() }
|
onEnter {
|
||||||
|
coroutineScope.launch {
|
||||||
|
sessionVerificationService.startVerification()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
addState<State.VerificationRequestAccepted> {
|
addState<State.VerificationRequestAccepted> {
|
||||||
on<Event.StartSasVerification>(State.StartingSasVerification)
|
on<Event.StartSasVerification>(State.StartingSasVerification)
|
||||||
|
|
@ -60,16 +69,22 @@ class VerifySelfSessionStateMachine(
|
||||||
}
|
}
|
||||||
addState<State.Verifying.Replying> {
|
addState<State.Verifying.Replying> {
|
||||||
onEnter { state ->
|
onEnter { state ->
|
||||||
if (state.accept) {
|
coroutineScope.launch {
|
||||||
sessionVerificationService.approveVerification()
|
if (state.accept) {
|
||||||
} else {
|
sessionVerificationService.approveVerification()
|
||||||
sessionVerificationService.declineVerification()
|
} else {
|
||||||
|
sessionVerificationService.declineVerification()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
on<Event.DidAcceptChallenge>(State.Completed)
|
on<Event.DidAcceptChallenge>(State.Completed)
|
||||||
}
|
}
|
||||||
addState<State.Canceling> {
|
addState<State.Canceling> {
|
||||||
onEnter { sessionVerificationService.cancelVerification() }
|
onEnter {
|
||||||
|
coroutineScope.launch {
|
||||||
|
sessionVerificationService.cancelVerification()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
on<Event.DidStartSasVerification>(State.SasVerificationStarted)
|
on<Event.DidStartSasVerification>(State.SasVerificationStarted)
|
||||||
on<Event.Cancel>(State.Canceling)
|
on<Event.Cancel>(State.Canceling)
|
||||||
|
|
@ -134,10 +149,13 @@ class VerifySelfSessionStateMachine(
|
||||||
/** Replying to a verification challenge. */
|
/** Replying to a verification challenge. */
|
||||||
data class Replying(override val emojis: List<VerificationEmoji>, val accept: Boolean) : Verifying(emojis)
|
data class Replying(override val emojis: List<VerificationEmoji>, val accept: Boolean) : Verifying(emojis)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The verification is being canceled. */
|
/** The verification is being canceled. */
|
||||||
object Canceling : State
|
object Canceling : State
|
||||||
|
|
||||||
/** The verification has been canceled, remotely or locally. */
|
/** The verification has been canceled, remotely or locally. */
|
||||||
object Canceled : State
|
object Canceled : State
|
||||||
|
|
||||||
/** Verification successful. */
|
/** Verification successful. */
|
||||||
object Completed : State
|
object Completed : State
|
||||||
}
|
}
|
||||||
|
|
@ -145,26 +163,37 @@ class VerifySelfSessionStateMachine(
|
||||||
sealed interface Event {
|
sealed interface Event {
|
||||||
/** Request verification. */
|
/** Request verification. */
|
||||||
object RequestVerification : Event
|
object RequestVerification : Event
|
||||||
|
|
||||||
/** The current verification request has been accepted. */
|
/** The current verification request has been accepted. */
|
||||||
object DidAcceptVerificationRequest : Event
|
object DidAcceptVerificationRequest : Event
|
||||||
|
|
||||||
/** Start a SaS verification flow. */
|
/** Start a SaS verification flow. */
|
||||||
object StartSasVerification : Event
|
object StartSasVerification : Event
|
||||||
|
|
||||||
/** Started a SaS verification flow. */
|
/** Started a SaS verification flow. */
|
||||||
object DidStartSasVerification : Event
|
object DidStartSasVerification : Event
|
||||||
|
|
||||||
/** Has received emojis. */
|
/** Has received emojis. */
|
||||||
data class DidReceiveChallenge(val emojis: List<VerificationEmoji>) : Event
|
data class DidReceiveChallenge(val emojis: List<VerificationEmoji>) : Event
|
||||||
|
|
||||||
/** Emojis match. */
|
/** Emojis match. */
|
||||||
object AcceptChallenge : Event
|
object AcceptChallenge : Event
|
||||||
|
|
||||||
/** Emojis do not match. */
|
/** Emojis do not match. */
|
||||||
object DeclineChallenge : Event
|
object DeclineChallenge : Event
|
||||||
|
|
||||||
/** Remote accepted challenge. */
|
/** Remote accepted challenge. */
|
||||||
object DidAcceptChallenge : Event
|
object DidAcceptChallenge : Event
|
||||||
|
|
||||||
/** Request cancellation. */
|
/** Request cancellation. */
|
||||||
object Cancel : Event
|
object Cancel : Event
|
||||||
|
|
||||||
/** Verification cancelled. */
|
/** Verification cancelled. */
|
||||||
object DidCancel : Event
|
object DidCancel : Event
|
||||||
|
|
||||||
/** Request failed. */
|
/** Request failed. */
|
||||||
object DidFail : Event
|
object DidFail : Event
|
||||||
|
|
||||||
/** Restart the verification flow. */
|
/** Restart the verification flow. */
|
||||||
object Restart : Event
|
object Restart : Event
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,32 +40,32 @@ interface SessionVerificationService {
|
||||||
/**
|
/**
|
||||||
* Request verification of the current session.
|
* Request verification of the current session.
|
||||||
*/
|
*/
|
||||||
fun requestVerification()
|
suspend fun requestVerification()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cancels the current verification attempt.
|
* Cancels the current verification attempt.
|
||||||
*/
|
*/
|
||||||
fun cancelVerification()
|
suspend fun cancelVerification()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Approves the current verification. This must happen on both devices to successfully verify a session.
|
* Approves the current verification. This must happen on both devices to successfully verify a session.
|
||||||
*/
|
*/
|
||||||
fun approveVerification()
|
suspend fun approveVerification()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Declines the verification attempt because the user could not verify or does not trust the other side of the verification.
|
* Declines the verification attempt because the user could not verify or does not trust the other side of the verification.
|
||||||
*/
|
*/
|
||||||
fun declineVerification()
|
suspend fun declineVerification()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the verification of the unverified session from another device.
|
* Starts the verification of the unverified session from another device.
|
||||||
*/
|
*/
|
||||||
fun startVerification()
|
suspend fun startVerification()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the verification service state to the initial step.
|
* Returns the verification service state to the initial step.
|
||||||
*/
|
*/
|
||||||
fun reset()
|
suspend fun reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Verification status of the current session. */
|
/** Verification status of the current session. */
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@ package io.element.android.libraries.matrix.impl.verification
|
||||||
|
|
||||||
import io.element.android.libraries.core.data.tryOrNull
|
import io.element.android.libraries.core.data.tryOrNull
|
||||||
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
|
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
|
||||||
import io.element.android.libraries.matrix.api.verification.VerificationFlowState
|
|
||||||
import io.element.android.libraries.matrix.api.verification.SessionVerifiedStatus
|
import io.element.android.libraries.matrix.api.verification.SessionVerifiedStatus
|
||||||
import io.element.android.libraries.matrix.api.verification.VerificationEmoji
|
import io.element.android.libraries.matrix.api.verification.VerificationEmoji
|
||||||
|
import io.element.android.libraries.matrix.api.verification.VerificationFlowState
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
|
@ -52,21 +52,21 @@ class RustSessionVerificationService @Inject constructor() : SessionVerification
|
||||||
private val _sessionVerifiedStatus = MutableStateFlow<SessionVerifiedStatus>(SessionVerifiedStatus.Unknown)
|
private val _sessionVerifiedStatus = MutableStateFlow<SessionVerifiedStatus>(SessionVerifiedStatus.Unknown)
|
||||||
override val sessionVerifiedStatus: StateFlow<SessionVerifiedStatus> = _sessionVerifiedStatus.asStateFlow()
|
override val sessionVerifiedStatus: StateFlow<SessionVerifiedStatus> = _sessionVerifiedStatus.asStateFlow()
|
||||||
|
|
||||||
override fun requestVerification() = tryOrFail {
|
override suspend fun requestVerification() = tryOrFail {
|
||||||
verificationController?.requestVerification()
|
verificationController?.requestVerification()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancelVerification() = tryOrFail { verificationController?.cancelVerification() }
|
override suspend fun cancelVerification() = tryOrFail { verificationController?.cancelVerification() }
|
||||||
|
|
||||||
override fun approveVerification() = tryOrFail { verificationController?.approveVerification() }
|
override suspend fun approveVerification() = tryOrFail { verificationController?.approveVerification() }
|
||||||
|
|
||||||
override fun declineVerification() = tryOrFail { verificationController?.declineVerification() }
|
override suspend fun declineVerification() = tryOrFail { verificationController?.declineVerification() }
|
||||||
|
|
||||||
override fun startVerification() = tryOrFail {
|
override suspend fun startVerification() = tryOrFail {
|
||||||
verificationController?.startSasVerification()
|
verificationController?.startSasVerification()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun tryOrFail(block: () -> Unit) {
|
private suspend fun tryOrFail(block: suspend () -> Unit) {
|
||||||
runCatching {
|
runCatching {
|
||||||
block()
|
block()
|
||||||
}.onFailure { didFail() }
|
}.onFailure { didFail() }
|
||||||
|
|
@ -107,7 +107,7 @@ class RustSessionVerificationService @Inject constructor() : SessionVerification
|
||||||
|
|
||||||
// end-region
|
// end-region
|
||||||
|
|
||||||
override fun reset() {
|
override suspend fun reset() {
|
||||||
if (isReady.value) {
|
if (isReady.value) {
|
||||||
// Cancel any pending verification attempt
|
// Cancel any pending verification attempt
|
||||||
tryOrNull { verificationController?.cancelVerification() }
|
tryOrNull { verificationController?.cancelVerification() }
|
||||||
|
|
|
||||||
|
|
@ -37,17 +37,17 @@ class FakeSessionVerificationService : SessionVerificationService {
|
||||||
|
|
||||||
override val isReady: StateFlow<Boolean> = _isReady
|
override val isReady: StateFlow<Boolean> = _isReady
|
||||||
|
|
||||||
override fun requestVerification() {
|
override suspend fun requestVerification() {
|
||||||
_verificationFlowState.value = VerificationFlowState.AcceptedVerificationRequest
|
_verificationFlowState.value = VerificationFlowState.AcceptedVerificationRequest
|
||||||
_verificationFlowState.value = VerificationFlowState.StartedSasVerification
|
_verificationFlowState.value = VerificationFlowState.StartedSasVerification
|
||||||
_verificationFlowState.value = VerificationFlowState.ReceivedVerificationData(emojiList)
|
_verificationFlowState.value = VerificationFlowState.ReceivedVerificationData(emojiList)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancelVerification() {
|
override suspend fun cancelVerification() {
|
||||||
_verificationFlowState.value = VerificationFlowState.Canceled
|
_verificationFlowState.value = VerificationFlowState.Canceled
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun approveVerification() {
|
override suspend fun approveVerification() {
|
||||||
if (!shouldFail) {
|
if (!shouldFail) {
|
||||||
_verificationFlowState.value = VerificationFlowState.Finished
|
_verificationFlowState.value = VerificationFlowState.Finished
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -55,7 +55,7 @@ class FakeSessionVerificationService : SessionVerificationService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun declineVerification() {
|
override suspend fun declineVerification() {
|
||||||
if (!shouldFail) {
|
if (!shouldFail) {
|
||||||
_verificationFlowState.value = VerificationFlowState.Canceled
|
_verificationFlowState.value = VerificationFlowState.Canceled
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -63,7 +63,7 @@ class FakeSessionVerificationService : SessionVerificationService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startVerification() {
|
override suspend fun startVerification() {
|
||||||
_verificationFlowState.value = VerificationFlowState.StartedSasVerification
|
_verificationFlowState.value = VerificationFlowState.StartedSasVerification
|
||||||
_verificationFlowState.value = VerificationFlowState.ReceivedVerificationData(emojiList)
|
_verificationFlowState.value = VerificationFlowState.ReceivedVerificationData(emojiList)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue