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: missing non-mls conversation when app supports MLS but backend doesn't #2579

Merged
merged 4 commits into from
Mar 4, 2024
Merged
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 @@ -104,4 +104,4 @@ data class UpdateClientCapabilitiesParam(
* the `INACTIVE_DURATION`.
*/
val Client.isActive: Boolean
get() = lastActive?.let { (Clock.System.now() - it) < Client.INACTIVE_DURATION } ?: false
get() = (lastActive ?: registrationTime)?.let { (Clock.System.now() - it) < Client.INACTIVE_DURATION } ?: false
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,9 @@ class UserSessionScope internal constructor(
clientRepository,
userRepository,
userConfigRepository,
featureSupport
featureSupport,
clientIdProvider,
userScopedLogger
)

private val updateSupportedProtocolsAndResolveOneOnOnes: UpdateSupportedProtocolsAndResolveOneOnOnesUseCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
*/
package com.wire.kalium.logic.feature.user

import com.wire.kalium.logger.KaliumLogger
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.data.client.Client
import com.wire.kalium.logic.data.client.ClientRepository
import com.wire.kalium.logic.data.client.isActive
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.featureConfig.MLSMigrationModel
import com.wire.kalium.logic.data.featureConfig.Status
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.user.SupportedProtocol
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.feature.mlsmigration.hasMigrationEnded
Expand All @@ -33,7 +36,6 @@ import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.flatMapLeft
import com.wire.kalium.logic.functional.map
import com.wire.kalium.logic.kaliumLogger
import kotlinx.datetime.Instant

/**
Expand All @@ -47,17 +49,19 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
private val clientsRepository: ClientRepository,
private val userRepository: UserRepository,
private val userConfigRepository: UserConfigRepository,
private val featureSupport: FeatureSupport
private val featureSupport: FeatureSupport,
private val currentClientIdProvider: CurrentClientIdProvider,
private val logger: KaliumLogger
) : UpdateSelfUserSupportedProtocolsUseCase {

override suspend operator fun invoke(): Either<CoreFailure, Boolean> {
return if (!featureSupport.isMLSSupported) {
kaliumLogger.d("Skip updating supported protocols, since MLS is not supported.")
logger.d("Skip updating supported protocols, since MLS is not supported.")
Either.Right(false)
} else {
(userRepository.getSelfUser()?.let { selfUser ->
selfSupportedProtocols().flatMap { newSupportedProtocols ->
kaliumLogger.i(
logger.i(
"Updating supported protocols = $newSupportedProtocols previously = ${selfUser.supportedProtocols}"
)
if (newSupportedProtocols != selfUser.supportedProtocols) {
Expand All @@ -68,11 +72,12 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
}.flatMapLeft {
when (it) {
is StorageFailure.DataNotFound -> {
kaliumLogger.w(
logger.w(
"Skip updating supported protocols since additional protocols are not configured"
)
Either.Right(false)
}

else -> Either.Left(it)
}
}
Expand All @@ -81,33 +86,48 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
}

private suspend fun selfSupportedProtocols(): Either<CoreFailure, Set<SupportedProtocol>> =
clientsRepository.selfListOfClients().flatMap { selfClients ->
userConfigRepository.getMigrationConfiguration()
.flatMapLeft { if (it is StorageFailure.DataNotFound) Either.Right(MIGRATION_CONFIGURATION_DISABLED) else Either.Left(it) }
.flatMap { migrationConfiguration ->
userConfigRepository.getSupportedProtocols().map { supportedProtocols ->
val selfSupportedProtocols = mutableSetOf<SupportedProtocol>()
if (proteusIsSupported(supportedProtocols, migrationConfiguration)) {
selfSupportedProtocols.add(SupportedProtocol.PROTEUS)
currentClientIdProvider().flatMap { currentClientId ->
clientsRepository.selfListOfClients().flatMap { selfClients ->
userConfigRepository.getMigrationConfiguration()
.flatMapLeft {
if (it is StorageFailure.DataNotFound) {
Either.Right(MIGRATION_CONFIGURATION_DISABLED)
} else {
Either.Left(it)
}
}
.flatMap { migrationConfiguration ->
userConfigRepository.getSupportedProtocols().map { supportedProtocols ->
val selfSupportedProtocols = mutableSetOf<SupportedProtocol>()
if (proteusIsSupported(supportedProtocols, migrationConfiguration)) {
selfSupportedProtocols.add(SupportedProtocol.PROTEUS)
}

if (mlsIsSupported(supportedProtocols, migrationConfiguration, selfClients)) {
selfSupportedProtocols.add(SupportedProtocol.MLS)
if (mlsIsSupported(supportedProtocols, migrationConfiguration, selfClients, currentClientId)) {
selfSupportedProtocols.add(SupportedProtocol.MLS)
}
selfSupportedProtocols
}
selfSupportedProtocols
}
}
}
}

private fun mlsIsSupported(
supportedProtocols: Set<SupportedProtocol>,
migrationConfiguration: MLSMigrationModel,
selfClients: List<Client>
selfClients: List<Client>,
selfClientId: ClientId
): Boolean {
val mlsIsSupported = supportedProtocols.contains(SupportedProtocol.MLS)
val mlsMigrationHasEnded = migrationConfiguration.hasMigrationEnded()
val allSelfClientsAreMLSCapable = selfClients.filter { it.isActive }.all { it.isMLSCapable }
kaliumLogger.d(
val allSelfClientsAreMLSCapable = selfClients
.filter { it.isActive || it.id == selfClientId }
.ifEmpty {
logger.w("user has 0 active clients")
emptyList()
}.all { it.isMLSCapable }

logger.d(
"mls is supported = $mlsIsSupported, " +
"all active self clients are mls capable = $allSelfClientsAreMLSCapable " +
"migration has ended = $mlsMigrationHasEnded"
Expand All @@ -121,7 +141,7 @@ internal class UpdateSelfUserSupportedProtocolsUseCaseImpl(
): Boolean {
val proteusIsSupported = supportedProtocols.contains(SupportedProtocol.PROTEUS)
val mlsMigrationHasEnded = migrationConfiguration.hasMigrationEnded()
kaliumLogger.d(
logger.d(
"proteus is supported = $proteusIsSupported, " +
"migration has ended = $mlsMigrationHasEnded"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,31 @@ class ClientTest {
assertTrue(client.isActive)
}

@Test
fun givenLastActiveIsNull_whenRegistrationTRimeIsNotOld_thenIsActiveIsTrue() {
val client = TestClient.CLIENT.copy(
lastActive = null,
registrationTime = Clock.System.now() - (Client.INACTIVE_DURATION - 1.days)
)
assertTrue(client.isActive)
}

@Test
fun givenLastActiveIsNull_whenRegistrationTRimeIsOld_thenIsActiveIsFalse() {
val client = TestClient.CLIENT.copy(
lastActive = null,
registrationTime = Clock.System.now() - (Client.INACTIVE_DURATION + 1.days)
)
assertFalse(client.isActive)
}

@Test
fun givenLastActiveIsNull_whenRegistrationTRimeIsNull_thenIsActiveIsFalse() {
val client = TestClient.CLIENT.copy(
lastActive = null,
registrationTime = null
)
assertFalse(client.isActive)
}

}
Loading
Loading