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: update common protocol resolution (WPB-15191) #3208

Merged
merged 1 commit into from
Jan 3, 2025
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 @@ -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
Loading