Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mls-client): skip MLSClient registration if registered client is mlsCapable (WPB-10723) 🍒 #2988

Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ class VerifyExistingClientUseCaseTest {
assertIs<VerifyExistingClientResult.Failure.E2EICertificateRequired>(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<VerifyExistingClientResult.Success>(result)
coVerify {
arrangement.registerMLSClientUseCase(any())
}.wasNotInvoked()
assertEquals(client, result.client)
}

@Test
fun givenRegisteredClientIdAndMLSAllowed_whenRegisterMLSSucceed_thenReturnSuccess() = runTest {
val clientId = ClientId("clientId")
Expand Down
Loading