diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCase.kt index 4850adf0fca..6c393915427 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCase.kt @@ -24,8 +24,6 @@ import com.wire.kalium.logic.data.client.ClientRepository import com.wire.kalium.logic.data.conversation.ClientId import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.functional.fold -import com.wire.kalium.logic.functional.getOrElse -import com.wire.kalium.logic.functional.map import com.wire.kalium.util.DelicateKaliumApi /** @@ -52,36 +50,22 @@ internal class VerifyExistingClientUseCaseImpl @OptIn(DelicateKaliumApi::class) return clientRepository.selfListOfClients() .fold({ VerifyExistingClientResult.Failure.Generic(it) - }, { listOfClients -> - val client = listOfClients.firstOrNull { it.id == clientId } + }, { listOfRegisteredClients -> + val registeredClient = listOfRegisteredClients.firstOrNull { it.id == clientId } when { - (client == null) -> VerifyExistingClientResult.Failure.ClientNotRegistered + registeredClient == null -> VerifyExistingClientResult.Failure.ClientNotRegistered - isAllowedToRegisterMLSClient() -> { - registerMLSClientUseCase.invoke(clientId = client.id).map { - if (it is RegisterMLSClientResult.E2EICertificateRequired) - VerifyExistingClientResult.Failure.E2EICertificateRequired(client, selfUserId) - else VerifyExistingClientResult.Success(client) - }.getOrElse { VerifyExistingClientResult.Failure.Generic(it) } - } - - else -> VerifyExistingClientResult.Success(client) - } - - if (client != null) { - if (isAllowedToRegisterMLSClient()) { - registerMLSClientUseCase.invoke(clientId = client.id).fold({ + !registeredClient.isMLSCapable && isAllowedToRegisterMLSClient() -> { + registerMLSClientUseCase.invoke(clientId = registeredClient.id).fold({ VerifyExistingClientResult.Failure.Generic(it) - }) { + }, { if (it is RegisterMLSClientResult.E2EICertificateRequired) - VerifyExistingClientResult.Failure.E2EICertificateRequired(client, selfUserId) - else VerifyExistingClientResult.Success(client) - } - } else { - VerifyExistingClientResult.Success(client) + VerifyExistingClientResult.Failure.E2EICertificateRequired(registeredClient, selfUserId) + else VerifyExistingClientResult.Success(registeredClient) + }) } - } else { - VerifyExistingClientResult.Failure.ClientNotRegistered + + else -> VerifyExistingClientResult.Success(registeredClient) } }) } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCaseTest.kt index 7c62632d6ef..91d914628f7 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/VerifyExistingClientUseCaseTest.kt @@ -79,6 +79,23 @@ class VerifyExistingClientUseCaseTest { assertIs(result) } + @Test + fun givenRegisteredClientIdAndMLSAllowedAndExistClientIsMLSCapable_whenRegisterMLSSucceed_thenReturnSuccessAndSkipMLSRegistration() = + runTest { + val clientId = ClientId("clientId") + val client = TestClient.CLIENT.copy(id = clientId, isMLSCapable = true) + val (arrangement, useCase) = arrange { + withSelfClientsResult(Either.Right(listOf(client))) + withIsAllowedToRegisterMLSClient(true) + } + val result = useCase.invoke(clientId) + assertIs(result) + coVerify { + arrangement.registerMLSClientUseCase(any()) + }.wasNotInvoked() + assertEquals(client, result.client) + } + @Test fun givenRegisteredClientIdAndMLSAllowed_whenRegisterMLSSucceed_thenReturnSuccess() = runTest { val clientId = ClientId("clientId")