Skip to content

Commit

Permalink
Merge branch 'develop' into chore/upgrade-to-kotlin-1.9.22
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugods authored Mar 6, 2024
2 parents 3d6f877 + 72d393f commit b890d87
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,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 @@ -1011,7 +1011,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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class TeamRepositoryTest {
nonQualifiedUserId = "teamMember1"
)

val teamMembersList = TeamsApi.TeamMemberList(
val teamMembersList = TeamsApi.TeamMemberListPaginated(
hasMore = false,
members = listOf(
teamMember
Expand Down Expand Up @@ -173,7 +173,7 @@ class TeamRepositoryTest {
nonQualifiedUserId = "teamMember1"
)

val teamMembersList = TeamsApi.TeamMemberList(
val teamMembersList = TeamsApi.TeamMemberListPaginated(
hasMore = true,
members = listOf(
teamMember
Expand Down Expand Up @@ -523,7 +523,7 @@ class TeamRepositoryTest {
.thenReturn(Either.Right(Unit))
}

fun withGetTeamMembers(result: NetworkResponse<TeamsApi.TeamMemberList>) = apply {
fun withGetTeamMembers(result: NetworkResponse<TeamsApi.TeamMemberListPaginated>) = apply {
given(teamsApi)
.suspendFunction(teamsApi::getTeamMembers)
.whenInvokedWith(any(), any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ class UserRepositoryTest {
given(teamsApi)
.suspendFunction(teamsApi::getTeamMembersByIds)
.whenInvokedWith(any(), any())
.thenReturn(NetworkResponse.Success(TeamsApi.TeamMemberList(false, result, "A=="), mapOf(), 200))
.thenReturn(NetworkResponse.Success(TeamsApi.TeamMemberListNonPaginated(false, result), mapOf(), 200))
}

fun withSuccessfulGetUsersByQualifiedIdList(knownUserEntities: List<UserDetailsEntity>) = apply {
Expand Down
Loading

0 comments on commit b890d87

Please sign in to comment.