Skip to content

Commit a284177

Browse files
authored
Merge pull request #3426 from element-hq/feature/fga/fix_self_verification_flow
Feature/fga/fix self verification flow
2 parents fc0bb64 + a7ab8ee commit a284177

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

features/verifysession/impl/src/main/kotlin/io/element/android/features/verifysession/impl/VerifySelfSessionView.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,9 @@ fun VerifySelfSessionView(
7070
onSuccessLogout: (String?) -> Unit,
7171
modifier: Modifier = Modifier,
7272
) {
73-
fun resetFlow() {
74-
state.eventSink(VerifySelfSessionViewEvents.Reset)
75-
}
76-
77-
val latestOnFinish by rememberUpdatedState(newValue = onFinish)
78-
LaunchedEffect(state.verificationFlowStep, latestOnFinish) {
79-
if (state.verificationFlowStep is FlowStep.Skipped) {
80-
latestOnFinish()
81-
}
82-
}
83-
BackHandler {
73+
fun cancelOrResetFlow() {
8474
when (state.verificationFlowStep) {
85-
is FlowStep.Canceled -> resetFlow()
75+
is FlowStep.Canceled -> state.eventSink(VerifySelfSessionViewEvents.Reset)
8676
is FlowStep.AwaitingOtherDeviceResponse, FlowStep.Ready -> state.eventSink(VerifySelfSessionViewEvents.Cancel)
8777
is FlowStep.Verifying -> {
8878
if (!state.verificationFlowStep.state.isLoading()) {
@@ -92,6 +82,16 @@ fun VerifySelfSessionView(
9282
else -> Unit
9383
}
9484
}
85+
86+
val latestOnFinish by rememberUpdatedState(newValue = onFinish)
87+
LaunchedEffect(state.verificationFlowStep, latestOnFinish) {
88+
if (state.verificationFlowStep is FlowStep.Skipped) {
89+
latestOnFinish()
90+
}
91+
}
92+
BackHandler {
93+
cancelOrResetFlow()
94+
}
9595
val verificationFlowStep = state.verificationFlowStep
9696

9797
if (state.verificationFlowStep is FlowStep.Loading ||
@@ -133,9 +133,9 @@ fun VerifySelfSessionView(
133133
footer = {
134134
BottomMenu(
135135
screenState = state,
136-
goBack = ::resetFlow,
136+
onCancelClick = ::cancelOrResetFlow,
137137
onEnterRecoveryKey = onEnterRecoveryKey,
138-
onFinish = onFinish,
138+
onContinueClick = onFinish,
139139
onResetKey = onResetKey,
140140
)
141141
}
@@ -268,8 +268,8 @@ private fun BottomMenu(
268268
screenState: VerifySelfSessionState,
269269
onEnterRecoveryKey: () -> Unit,
270270
onResetKey: () -> Unit,
271-
goBack: () -> Unit,
272-
onFinish: () -> Unit,
271+
onCancelClick: () -> Unit,
272+
onContinueClick: () -> Unit,
273273
) {
274274
val verificationViewState = screenState.verificationFlowStep
275275
val eventSink = screenState.eventSink
@@ -316,7 +316,7 @@ private fun BottomMenu(
316316
TextButton(
317317
modifier = Modifier.fillMaxWidth(),
318318
text = stringResource(CommonStrings.action_cancel),
319-
onClick = goBack,
319+
onClick = onCancelClick,
320320
)
321321
}
322322
}
@@ -330,7 +330,7 @@ private fun BottomMenu(
330330
TextButton(
331331
modifier = Modifier.fillMaxWidth(),
332332
text = stringResource(CommonStrings.action_cancel),
333-
onClick = goBack,
333+
onClick = onCancelClick,
334334
)
335335
}
336336
}
@@ -375,7 +375,7 @@ private fun BottomMenu(
375375
Button(
376376
modifier = Modifier.fillMaxWidth(),
377377
text = stringResource(CommonStrings.action_continue),
378-
onClick = onFinish,
378+
onClick = onContinueClick,
379379
)
380380
// Placeholder so the 1st button keeps its vertical position
381381
Spacer(modifier = Modifier.height(48.dp))

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/verification/RustSessionVerificationService.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import kotlinx.coroutines.flow.map
2626
import kotlinx.coroutines.flow.onEach
2727
import kotlinx.coroutines.flow.stateIn
2828
import kotlinx.coroutines.launch
29+
import kotlinx.coroutines.sync.Mutex
30+
import kotlinx.coroutines.sync.withLock
2931
import kotlinx.coroutines.withTimeout
3032
import org.matrix.rustcomponents.sdk.Client
3133
import org.matrix.rustcomponents.sdk.Encryption
@@ -95,18 +97,19 @@ class RustSessionVerificationService(
9597
updateVerificationStatus()
9698
}
9799
}
98-
.launchIn(sessionCoroutineScope)
100+
.launchIn(sessionCoroutineScope)
99101
}
100102

101103
override suspend fun requestVerification() = tryOrFail {
102-
if (!this::verificationController.isInitialized) {
103-
verificationController = client.getSessionVerificationController()
104-
verificationController.setDelegate(this)
105-
}
104+
initVerificationControllerIfNeeded()
106105
verificationController.requestVerification()
107106
}
108107

109-
override suspend fun cancelVerification() = tryOrFail { verificationController.cancelVerification() }
108+
override suspend fun cancelVerification() = tryOrFail {
109+
verificationController.cancelVerification()
110+
// We need to manually set the state to canceled, as the Rust SDK doesn't always call `didCancel` when it should
111+
didCancel()
112+
}
110113

111114
override suspend fun approveVerification() = tryOrFail { verificationController.approveVerification() }
112115

@@ -193,6 +196,16 @@ class RustSessionVerificationService(
193196
}
194197
}
195198

199+
private var initControllerMutex = Mutex()
200+
201+
private suspend fun initVerificationControllerIfNeeded() = initControllerMutex.withLock {
202+
if (!this::verificationController.isInitialized) {
203+
tryOrFail {
204+
verificationController = client.getSessionVerificationController()
205+
verificationController.setDelegate(this)
206+
}
207+
}
208+
}
196209
private suspend fun updateVerificationStatus() {
197210
if (verificationFlowState.value == VerificationFlowState.Finished) {
198211
// Calling `encryptionService.verificationState()` performs a network call and it will deadlock if there is no network
@@ -212,10 +225,7 @@ class RustSessionVerificationService(
212225
// Otherwise, just check the current verification status from the session verification controller instead
213226
Timber.d("Updating verification status: flow is pending or was finished some time ago")
214227
runCatching {
215-
if (!this@RustSessionVerificationService::verificationController.isInitialized) {
216-
verificationController = client.getSessionVerificationController()
217-
verificationController.setDelegate(this@RustSessionVerificationService)
218-
}
228+
initVerificationControllerIfNeeded()
219229
_sessionVerifiedStatus.value = if (verificationController.isVerified()) {
220230
SessionVerifiedStatus.Verified
221231
} else {

0 commit comments

Comments
 (0)