Skip to content

Commit

Permalink
feat: support Proteus federation if MLS not supported by backend (WPB…
Browse files Browse the repository at this point in the history
…-14456) (#3668)
  • Loading branch information
yamilmedina authored Nov 29, 2024
1 parent 7720cdd commit aed7ea8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.wire.android.di.KaliumCoreLogic
import com.wire.kalium.logic.CoreLogic
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.search.FederatedSearchParser
import com.wire.kalium.logic.feature.search.IsFederationSearchAllowedUseCase
import com.wire.kalium.logic.feature.search.SearchByHandleUseCase
import com.wire.kalium.logic.feature.search.SearchScope
import com.wire.kalium.logic.feature.search.SearchUsersUseCase
Expand Down Expand Up @@ -53,4 +54,9 @@ class SearchModule {
@ViewModelScoped
@Provides
fun provideFederatedSearchParser(searchScope: SearchScope): FederatedSearchParser = searchScope.federatedSearchParser

@ViewModelScoped
@Provides
fun provideIsFederationSearchAllowedUseCase(searchScope: SearchScope): IsFederationSearchAllowedUseCase =
searchScope.isFederationSearchAllowedUseCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ import com.wire.android.mapper.ContactMapper
import com.wire.android.ui.home.newconversation.model.Contact
import com.wire.android.ui.navArgs
import com.wire.android.util.EMPTY
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.user.SupportedProtocol
import com.wire.kalium.logic.feature.auth.ValidateUserHandleResult
import com.wire.kalium.logic.feature.auth.ValidateUserHandleUseCase
import com.wire.kalium.logic.feature.conversation.GetConversationProtocolInfoUseCase
import com.wire.kalium.logic.feature.search.FederatedSearchParser
import com.wire.kalium.logic.feature.search.IsFederationSearchAllowedUseCase
import com.wire.kalium.logic.feature.search.SearchByHandleUseCase
import com.wire.kalium.logic.feature.search.SearchUserResult
import com.wire.kalium.logic.feature.search.SearchUsersUseCase
import com.wire.kalium.logic.feature.user.GetDefaultProtocolUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableSet
Expand All @@ -59,8 +56,7 @@ class SearchUserViewModel @Inject constructor(
private val contactMapper: ContactMapper,
private val federatedSearchParser: FederatedSearchParser,
private val validateUserHandle: ValidateUserHandleUseCase,
private val getDefaultProtocol: GetDefaultProtocolUseCase,
private val getConversationProtocolInfo: GetConversationProtocolInfoUseCase,
private val isFederationSearchAllowed: IsFederationSearchAllowedUseCase,
savedStateHandle: SavedStateHandle
) : ViewModel() {

Expand All @@ -78,17 +74,7 @@ class SearchUserViewModel @Inject constructor(

init {
viewModelScope.launch {
val isProteusTeam = getDefaultProtocol() == SupportedProtocol.PROTEUS

val isOtherDomainAllowed: Boolean = addMembersSearchNavArgs?.conversationId?.let { conversationId ->
when (val result = getConversationProtocolInfo(conversationId)) {
is GetConversationProtocolInfoUseCase.Result.Failure -> !isProteusTeam

is GetConversationProtocolInfoUseCase.Result.Success ->
!isProteusTeam && result.protocolInfo !is Conversation.ProtocolInfo.Proteus
}
} ?: !isProteusTeam

val isOtherDomainAllowed = isFederationSearchAllowed(addMembersSearchNavArgs?.conversationId)
state = state.copy(isOtherDomainAllowed = isOtherDomainAllowed)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ import com.wire.kalium.logic.data.id.GroupID
import com.wire.kalium.logic.data.mls.CipherSuite
import com.wire.kalium.logic.data.publicuser.model.UserSearchDetails
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.data.user.SupportedProtocol
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.type.UserType
import com.wire.kalium.logic.feature.auth.ValidateUserHandleResult
import com.wire.kalium.logic.feature.auth.ValidateUserHandleUseCase
import com.wire.kalium.logic.feature.conversation.GetConversationProtocolInfoUseCase
import com.wire.kalium.logic.feature.search.FederatedSearchParser
import com.wire.kalium.logic.feature.search.IsFederationSearchAllowedUseCase
import com.wire.kalium.logic.feature.search.SearchByHandleUseCase
import com.wire.kalium.logic.feature.search.SearchUserResult
import com.wire.kalium.logic.feature.search.SearchUsersUseCase
import com.wire.kalium.logic.feature.user.GetDefaultProtocolUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
Expand Down Expand Up @@ -138,10 +136,9 @@ class SearchUserViewModelTest {
fun `given Proteus conversation and MLS team, when calling the searchUseCase, then otherDomain is not allowed`() =
runTest {
val conversationId = ConversationId("id", "domain")
val (arrangement, viewModel) = Arrangement()
val (_, viewModel) = Arrangement()
.withAddMembersSearchNavArgs(AddMembersSearchNavArgs(conversationId, true))
.withConversationProtocolInfo(GetConversationProtocolInfoUseCase.Result.Success(Conversation.ProtocolInfo.Proteus))
.withDefaultProtocol(SupportedProtocol.MLS)
.withIsFederationSearchAllowedResult(false)
.withIsValidHandleResult(ValidateUserHandleResult.Valid(""))
.withFederatedSearchParserResult(
FederatedSearchParser.Result(
Expand All @@ -164,10 +161,9 @@ class SearchUserViewModelTest {
fun `given MLS conversation and Proteus team, when calling the searchUseCase, then otherDomain is not allowed`() =
runTest {
val conversationId = ConversationId("id", "domain")
val (arrangement, viewModel) = Arrangement()
val (_, viewModel) = Arrangement()
.withAddMembersSearchNavArgs(AddMembersSearchNavArgs(conversationId, true))
.withConversationProtocolInfo(GetConversationProtocolInfoUseCase.Result.Success(mlsProtocol))
.withDefaultProtocol(SupportedProtocol.PROTEUS)
.withIsFederationSearchAllowedResult(false)
.withIsValidHandleResult(ValidateUserHandleResult.Valid(""))
.withFederatedSearchParserResult(
FederatedSearchParser.Result(
Expand All @@ -190,10 +186,9 @@ class SearchUserViewModelTest {
fun `given MLS conversation and MLS team, when calling the searchUseCase, then otherDomain is allowed`() =
runTest {
val conversationId = ConversationId("id", "domain")
val (arrangement, viewModel) = Arrangement()
val (_, viewModel) = Arrangement()
.withAddMembersSearchNavArgs(AddMembersSearchNavArgs(conversationId, true))
.withConversationProtocolInfo(GetConversationProtocolInfoUseCase.Result.Success(mlsProtocol))
.withDefaultProtocol(SupportedProtocol.MLS)
.withIsFederationSearchAllowedResult(true)
.withIsValidHandleResult(ValidateUserHandleResult.Valid(""))
.withFederatedSearchParserResult(
FederatedSearchParser.Result(
Expand Down Expand Up @@ -360,21 +355,15 @@ class SearchUserViewModelTest {
lateinit var searchByHandleUseCase: SearchByHandleUseCase

@MockK
lateinit var getDefaultProtocolUseCase: GetDefaultProtocolUseCase

@MockK
lateinit var getConversationProtocolInfo: GetConversationProtocolInfoUseCase
lateinit var isFederationSearchAllowedUseCase: IsFederationSearchAllowedUseCase

init {
MockKAnnotations.init(this, relaxUnitFun = true)
every { contactMapper.fromSearchUserResult(any()) } answers {
val user = args.get(0) as UserSearchDetails
fromSearchUserResult(user)
}
every { getDefaultProtocolUseCase() } returns SupportedProtocol.PROTEUS
coEvery {
getConversationProtocolInfo(any())
} returns GetConversationProtocolInfoUseCase.Result.Success(Conversation.ProtocolInfo.Proteus)
withIsFederationSearchAllowedResult(false)
}

fun fromSearchUserResult(user: UserSearchDetails): Contact {
Expand Down Expand Up @@ -427,12 +416,8 @@ class SearchUserViewModelTest {
coEvery { searchByHandleUseCase(any(), any(), any()) } returns result
}

suspend fun withConversationProtocolInfo(result: GetConversationProtocolInfoUseCase.Result) = apply {
coEvery { getConversationProtocolInfo(any()) } returns result
}

fun withDefaultProtocol(protocol: SupportedProtocol) = apply {
every { getDefaultProtocolUseCase() } returns protocol
fun withIsFederationSearchAllowedResult(isAllowed: Boolean = true) = apply {
coEvery { isFederationSearchAllowedUseCase(any()) } returns isAllowed
}

private lateinit var searchUserViewModel: SearchUserViewModel
Expand All @@ -444,8 +429,7 @@ class SearchUserViewModelTest {
contactMapper = contactMapper,
federatedSearchParser = federatedSearchParser,
validateUserHandle = validateUserHandle,
getConversationProtocolInfo = getConversationProtocolInfo,
getDefaultProtocol = getDefaultProtocolUseCase,
isFederationSearchAllowed = isFederationSearchAllowedUseCase,
savedStateHandle = savedStateHandle
)
}.run {
Expand Down

0 comments on commit aed7ea8

Please sign in to comment.