Skip to content

Commit

Permalink
fix(mls): respect default protocol in one-on-one conversation initial…
Browse files Browse the repository at this point in the history
…isation (WPB-8975) (#2768)
  • Loading branch information
mchenani authored May 16, 2024
1 parent 0c76a4d commit ab0156b
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ class UserSessionScope internal constructor(

internal val mlsMigrationManager: MLSMigrationManager = MLSMigrationManagerImpl(
kaliumConfigs,
featureSupport,
isMLSEnabled,
incrementalSyncRepository,
lazy { clientRepository },
lazy { users.timestampKeyRepository },
Expand Down Expand Up @@ -1611,7 +1611,8 @@ class UserSessionScope internal constructor(

private val oneOnOneProtocolSelector: OneOnOneProtocolSelector
get() = OneOnOneProtocolSelectorImpl(
userRepository
userRepository,
userConfigRepository
)

private val acmeCertificatesSyncWorker: ACMECertificatesSyncWorker by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.wire.kalium.logic.data.sync.IncrementalSyncRepository
import com.wire.kalium.logic.data.sync.IncrementalSyncStatus
import com.wire.kalium.logic.feature.TimestampKeyRepository
import com.wire.kalium.logic.feature.TimestampKeys
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.logic.feature.user.IsMLSEnabledUseCase
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
Expand All @@ -46,11 +46,10 @@ import kotlinx.datetime.Clock
* Orchestrates the migration from proteus to MLS.
*/
internal interface MLSMigrationManager

@Suppress("LongParameterList")
internal class MLSMigrationManagerImpl(
private val kaliumConfigs: KaliumConfigs,
private val featureSupport: FeatureSupport,
private val isMLSEnabledUseCase: IsMLSEnabledUseCase,
private val incrementalSyncRepository: IncrementalSyncRepository,
private val clientRepository: Lazy<ClientRepository>,
private val timestampKeyRepository: Lazy<TimestampKeyRepository>,
Expand All @@ -73,7 +72,7 @@ internal class MLSMigrationManagerImpl(
incrementalSyncRepository.incrementalSyncState.collect { syncState ->
ensureActive()
if (syncState is IncrementalSyncStatus.Live &&
featureSupport.isMLSSupported &&
isMLSEnabledUseCase() &&
clientRepository.value.hasRegisteredMLSClient().getOrElse(false)
) {
updateMigration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class MLSMigrationWorkerImpl(
override suspend fun runMigration() =
syncMigrationConfigurations().flatMap {
userConfigRepository.getMigrationConfiguration().getOrNull()?.let { configuration ->
if (configuration.hasMigrationStarted()) {
if (configuration.status.toBoolean() && configuration.hasMigrationStarted()) {
kaliumLogger.i("Running proteus to MLS migration")
mlsMigrator.migrateProteusConversations().flatMap {
if (configuration.hasMigrationEnded()) {
Expand All @@ -57,7 +57,6 @@ internal class MLSMigrationWorkerImpl(
}
} ?: Either.Right(Unit)
}

private suspend fun syncMigrationConfigurations(): Either<CoreFailure, Unit> =
featureConfigRepository.getFeatureConfigs().flatMap { configurations ->
mlsConfigHandler.handle(configurations.mlsModel, duringSlowSync = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
package com.wire.kalium.logic.feature.protocol

import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.data.user.SupportedProtocol
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.fold

internal interface OneOnOneProtocolSelector {
suspend fun getProtocolForUser(userId: UserId): Either<CoreFailure, SupportedProtocol>
}

internal class OneOnOneProtocolSelectorImpl(
private val userRepository: UserRepository
private val userRepository: UserRepository,
private val userConfigRepository: UserConfigRepository
) : OneOnOneProtocolSelector {
override suspend fun getProtocolForUser(userId: UserId): Either<CoreFailure, SupportedProtocol> =
userRepository.userById(userId).flatMap { otherUser ->
Expand All @@ -40,8 +43,11 @@ internal class OneOnOneProtocolSelectorImpl(

val selfUserProtocols = selfUser.supportedProtocols.orEmpty()
val otherUserProtocols = otherUser.supportedProtocols.orEmpty()

val commonProtocols = selfUserProtocols.intersect(otherUserProtocols)
val commonProtocols = userConfigRepository.getDefaultProtocol().fold({
selfUserProtocols.intersect(otherUserProtocols)
}, {
selfUserProtocols.intersect(listOf(it).toSet()).intersect(otherUserProtocols)
})

return when {
commonProtocols.contains(SupportedProtocol.MLS) -> Either.Right(SupportedProtocol.MLS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.wire.kalium.logic.data.sync.IncrementalSyncRepository
import com.wire.kalium.logic.data.sync.IncrementalSyncStatus
import com.wire.kalium.logic.feature.TimestampKeyRepository
import com.wire.kalium.logic.feature.TimestampKeys
import com.wire.kalium.logic.featureFlags.FeatureSupport
import com.wire.kalium.logic.feature.user.IsMLSEnabledUseCase
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.test_util.TestKaliumDispatcher
Expand Down Expand Up @@ -123,7 +123,7 @@ class MLSMigrationManagerTest {
val clientRepository = mock(classOf<ClientRepository>())

@Mock
val featureSupport = mock(classOf<FeatureSupport>())
val isMLSEnabledUseCase = mock(classOf<IsMLSEnabledUseCase>())

@Mock
val timestampKeyRepository = mock(classOf<TimestampKeyRepository>())
Expand Down Expand Up @@ -153,8 +153,8 @@ class MLSMigrationManagerTest {
}

fun withIsMLSSupported(supported: Boolean) = apply {
given(featureSupport)
.invocation { featureSupport.isMLSSupported }
given(isMLSEnabledUseCase)
.invocation { isMLSEnabledUseCase.invoke() }
.thenReturn(supported)
}

Expand All @@ -167,7 +167,7 @@ class MLSMigrationManagerTest {

fun arrange() = this to MLSMigrationManagerImpl(
kaliumConfigs,
featureSupport,
isMLSEnabledUseCase,
incrementalSyncRepository,
lazy { clientRepository },
lazy { timestampKeyRepository },
Expand Down
Loading

0 comments on commit ab0156b

Please sign in to comment.