Skip to content

Commit

Permalink
fix: update common protocol resolution (#3208)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamilmedina committed Jan 9, 2025
1 parent 43fbe32 commit 8768576
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ 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
import com.wire.kalium.logic.functional.getOrNull
import com.wire.kalium.logic.kaliumLogger

internal interface OneOnOneProtocolSelector {
suspend fun getProtocolForUser(userId: UserId): Either<CoreFailure, SupportedProtocol>
Expand All @@ -41,13 +42,17 @@ internal class OneOnOneProtocolSelectorImpl(
return@flatMap Either.Left(CoreFailure.Unknown(error))
}

val teamDefaultProtocol = userConfigRepository.getDefaultProtocol().getOrNull()
val selfUserProtocols = selfUser.supportedProtocols.orEmpty()
val otherUserProtocols = otherUser.supportedProtocols.orEmpty()
val commonProtocols = userConfigRepository.getDefaultProtocol().fold({
selfUserProtocols.intersect(otherUserProtocols)
}, {
selfUserProtocols.intersect(listOf(it).toSet()).intersect(otherUserProtocols)
})
val commonProtocols = selfUserProtocols.intersect(otherUserProtocols)

kaliumLogger.withTextTag(TAG).d(
"teamDefaultProtocol = $teamDefaultProtocol, " +
"selfUserProtocols = $selfUserProtocols, " +
"otherUserProtocols = $otherUserProtocols, " +
"commonProtocols = $commonProtocols"
)

return when {
commonProtocols.contains(SupportedProtocol.MLS) -> Either.Right(SupportedProtocol.MLS)
Expand All @@ -56,4 +61,8 @@ internal class OneOnOneProtocolSelectorImpl(
else -> Either.Left(CoreFailure.NoCommonProtocolFound.SelfNeedToUpdate)
}
}

private companion object {
const val TAG = "OneOnOneProtocolSelector"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,48 +166,6 @@ class OneOnOneProtocolSelectorTest {
}
}

@Test
fun givenUsersHaveProtocolInCommonButDiffersWithDefaultProtocol_thenShouldReturnNoCommonProtocol() = runTest {
val (_, oneOnOneProtocolSelector) = arrange {
withSelfUserReturning(TestUser.SELF.copy(supportedProtocols = setOf(SupportedProtocol.MLS)))
withUserByIdReturning(Either.Right(TestUser.OTHER.copy(supportedProtocols = setOf(SupportedProtocol.MLS))))
withGetDefaultProtocolReturning(SupportedProtocol.PROTEUS.right())
}

oneOnOneProtocolSelector.getProtocolForUser(TestUser.USER_ID)
.shouldFail {
assertIs<CoreFailure.NoCommonProtocolFound>(it)
}
}

@Test
fun givenSelfUserSupportsDefaultProtocolButOtherUserDoesnt_thenShouldReturnNoCommonProtocol() = runTest {
val (_, oneOnOneProtocolSelector) = arrange {
withSelfUserReturning(TestUser.SELF.copy(supportedProtocols = setOf(SupportedProtocol.MLS, SupportedProtocol.PROTEUS)))
withUserByIdReturning(Either.Right(TestUser.OTHER.copy(supportedProtocols = setOf(SupportedProtocol.MLS))))
withGetDefaultProtocolReturning(SupportedProtocol.PROTEUS.right())
}

oneOnOneProtocolSelector.getProtocolForUser(TestUser.USER_ID)
.shouldFail {
assertIs<CoreFailure.NoCommonProtocolFound>(it)
}
}

@Test
fun givenSelfUserDoesntSupportsDefaultProtocolButOtherUserDoes_thenShouldReturnNoCommonProtocol() = runTest {
val (_, oneOnOneProtocolSelector) = arrange {
withSelfUserReturning(TestUser.SELF.copy(supportedProtocols = setOf(SupportedProtocol.MLS)))
withUserByIdReturning(Either.Right(TestUser.OTHER.copy(supportedProtocols = setOf(SupportedProtocol.MLS, SupportedProtocol.PROTEUS))))
withGetDefaultProtocolReturning(SupportedProtocol.PROTEUS.right())
}

oneOnOneProtocolSelector.getProtocolForUser(TestUser.USER_ID)
.shouldFail {
assertIs<CoreFailure.NoCommonProtocolFound>(it)
}
}

@Test
fun givenUsersHaveProtocolInCommonIncludingDefaultProtocol_thenShouldReturnDefaultProtocolAsCommonProtocol() = runTest {
val (_, oneOnOneProtocolSelector) = arrange {
Expand Down

0 comments on commit 8768576

Please sign in to comment.