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: do not remove MLS from the set of supported protocols [WPB-16254] 🍒 #3313

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
*/
package com.wire.kalium.logic.feature.user

import com.wire.kalium.logger.KaliumLogger
import com.wire.kalium.common.error.CoreFailure
import com.wire.kalium.common.error.StorageFailure
import com.wire.kalium.common.functional.Either
import com.wire.kalium.common.functional.flatMap
import com.wire.kalium.common.functional.flatMapLeft
import com.wire.kalium.common.functional.map
import com.wire.kalium.logger.KaliumLogger
import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.data.client.Client
import com.wire.kalium.logic.data.client.ClientRepository
Expand All @@ -32,10 +36,6 @@ import com.wire.kalium.logic.data.user.SupportedProtocol
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.feature.mlsmigration.hasMigrationEnded
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.common.functional.Either
import com.wire.kalium.common.functional.flatMap
import com.wire.kalium.common.functional.flatMapLeft
import com.wire.kalium.common.functional.map
import kotlinx.datetime.Instant

/**
Expand All @@ -60,12 +60,19 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
Either.Right(false)
} else {
(userRepository.getSelfUser().flatMap { selfUser ->
selfSupportedProtocols().flatMap { newSupportedProtocols ->
selfSupportedProtocols().flatMap { calculatedSupportedProtocols ->
val finalizedSupportedProtocols = if (selfUser.supportedProtocols?.contains(SupportedProtocol.MLS) == true) {
calculatedSupportedProtocols + SupportedProtocol.MLS
} else {
calculatedSupportedProtocols
}
logger.i(
"Updating supported protocols = $newSupportedProtocols previously = ${selfUser.supportedProtocols}"
"Updating supported protocols = $calculatedSupportedProtocols " +
"previously = ${selfUser.supportedProtocols}, " +
"finalized = $finalizedSupportedProtocols"
)
if (newSupportedProtocols != selfUser.supportedProtocols) {
userRepository.updateSupportedProtocols(newSupportedProtocols).map { true }
if (finalizedSupportedProtocols != selfUser.supportedProtocols) {
userRepository.updateSupportedProtocols(finalizedSupportedProtocols).map { true }
} else {
Either.Right(false)
}
Expand All @@ -81,7 +88,7 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
else -> Either.Left(it)
}
}
})
} ?: Either.Left(StorageFailure.DataNotFound))
}
}

Expand All @@ -97,13 +104,23 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
}
}
.flatMap { migrationConfiguration ->
userConfigRepository.getSupportedProtocols().map { supportedProtocols ->
userConfigRepository.getSupportedProtocols().map { teamSupportedProtocols ->
val selfSupportedProtocols = mutableSetOf<SupportedProtocol>()
if (proteusIsSupported(supportedProtocols, migrationConfiguration)) {
if (proteusIsSupported(
teamSettingsSupportedProtocols = teamSupportedProtocols,
migrationConfiguration = migrationConfiguration
)
) {
selfSupportedProtocols.add(SupportedProtocol.PROTEUS)
}

if (mlsIsSupported(supportedProtocols, migrationConfiguration, selfClients, currentClientId)) {
if (mlsIsSupported(
teamSettingsSupportedProtocols = teamSupportedProtocols,
migrationConfiguration = migrationConfiguration,
selfClients = selfClients,
selfClientId = currentClientId
)
) {
selfSupportedProtocols.add(SupportedProtocol.MLS)
}
selfSupportedProtocols
Expand All @@ -113,12 +130,12 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
}

private fun mlsIsSupported(
supportedProtocols: Set<SupportedProtocol>,
teamSettingsSupportedProtocols: Set<SupportedProtocol>,
migrationConfiguration: MLSMigrationModel,
selfClients: List<Client>,
selfClientId: ClientId
): Boolean {
val mlsIsSupported = supportedProtocols.contains(SupportedProtocol.MLS)
val mlsIsSupported = teamSettingsSupportedProtocols.contains(SupportedProtocol.MLS)
val mlsMigrationHasEnded = migrationConfiguration.hasMigrationEnded()
val allSelfClientsAreMLSCapable = selfClients
.filter { it.isActive || it.id == selfClientId }
Expand All @@ -136,10 +153,10 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
}

private fun proteusIsSupported(
supportedProtocols: Set<SupportedProtocol>,
teamSettingsSupportedProtocols: Set<SupportedProtocol>,
migrationConfiguration: MLSMigrationModel
): Boolean {
val proteusIsSupported = supportedProtocols.contains(SupportedProtocol.PROTEUS)
val proteusIsSupported = teamSettingsSupportedProtocols.contains(SupportedProtocol.PROTEUS)
val mlsMigrationHasEnded = migrationConfiguration.hasMigrationEnded()
logger.d(
"proteus is supported = $proteusIsSupported, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful(supportedProtocols = setOf(SupportedProtocol.PROTEUS))
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.PROTEUS))
withTeamSupportedProtocol(setOf(SupportedProtocol.PROTEUS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(clients = emptyList())
withUpdateSupportedProtocolsSuccessful()
Expand All @@ -94,7 +94,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.PROTEUS))
withTeamSupportedProtocol(setOf(SupportedProtocol.PROTEUS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(clients = emptyList())
withUpdateSupportedProtocolsSuccessful()
Expand All @@ -114,7 +114,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(clients = emptyList())
withUpdateSupportedProtocolsSuccessful()
Expand All @@ -134,7 +134,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(COMPLETED_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(clients = emptyList())
withUpdateSupportedProtocolsSuccessful()
Expand All @@ -154,7 +154,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(
clients = listOf(
Expand All @@ -178,7 +178,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(
clients = listOf(
Expand All @@ -203,7 +203,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(
clients = listOf(
Expand All @@ -228,7 +228,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(COMPLETED_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(
clients = listOf(
Expand All @@ -253,7 +253,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.PROTEUS, SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.PROTEUS, SupportedProtocol.MLS))
withGetMigrationConfigurationFailing(StorageFailure.DataNotFound)
withGetSelfClientsSuccessful(
clients = listOf(
Expand All @@ -277,7 +277,7 @@ class UpdateSupportedProtocolsUseCaseTest {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful()
withGetSupportedProtocolsSuccessful(setOf(SupportedProtocol.PROTEUS))
withTeamSupportedProtocol(setOf(SupportedProtocol.PROTEUS))
withGetMigrationConfigurationSuccessful(DISABLED_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(
clients = listOf(
Expand Down Expand Up @@ -314,6 +314,31 @@ class UpdateSupportedProtocolsUseCaseTest {
}.wasNotInvoked()
}

@Test
fun givenSelfSupportedProtocolsContainsMLS_whenCalculatedDoesNotContainsMLS_thenUpdateSupportedProtocolsContainsMLS() = runTest {
val (arrangement, useCase) = Arrangement()
.arrange {
withCurrentClientIdSuccess(ClientId("1"))
withIsMLSSupported(true)
withGetSelfUserSuccessful(supportedProtocols = setOf(SupportedProtocol.MLS))
withTeamSupportedProtocol(setOf(SupportedProtocol.PROTEUS, SupportedProtocol.MLS))
withGetMigrationConfigurationSuccessful(ONGOING_MIGRATION_CONFIGURATION)
withGetSelfClientsSuccessful(
clients = listOf(
TestClient.CLIENT.copy(isMLSCapable = true, lastActive = Clock.System.now()),
TestClient.CLIENT.copy(isMLSCapable = false, lastActive = Instant.DISTANT_PAST)
)
)
withUpdateSupportedProtocolsSuccessful()
}

useCase()

coVerify {
arrangement.userRepository.updateSupportedProtocols(matches { it.contains(SupportedProtocol.MLS) && it.contains(SupportedProtocol.PROTEUS) })
}.wasInvoked(exactly = once)
}

private class Arrangement : CurrentClientIdProviderArrangement by CurrentClientIdProviderArrangementImpl() {
@Mock
val clientRepository = mock(ClientRepository::class)
Expand Down Expand Up @@ -358,7 +383,7 @@ class UpdateSupportedProtocolsUseCaseTest {
}.returns(Either.Left(failure))
}

suspend fun withGetSupportedProtocolsSuccessful(supportedProtocols: Set<SupportedProtocol>) = apply {
suspend fun withTeamSupportedProtocol(supportedProtocols: Set<SupportedProtocol>) = apply {
coEvery {
userConfigRepository.getSupportedProtocols()
}.returns(Either.Right(supportedProtocols))
Expand Down
Loading