Skip to content

Commit

Permalink
fix: handle NoCommonProtocol error while starting conversation cherry…
Browse files Browse the repository at this point in the history
…-pick (#3138)
  • Loading branch information
borichellow authored Jun 28, 2024
1 parent bac2e63 commit 91bb89e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import com.wire.android.R
import com.wire.android.di.hiltViewModelScoped
import com.wire.android.model.ClickBlockParams
Expand All @@ -52,6 +53,7 @@ import com.wire.android.ui.theme.WireTheme
import com.wire.android.ui.theme.wireTypography
import com.wire.android.util.ui.PreviewMultipleThemes
import com.wire.android.util.ui.stringWithStyledArgs
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.data.user.UserId
Expand Down Expand Up @@ -105,7 +107,7 @@ fun ConnectionActionButton(
loading = viewModel.actionableState().isPerformingAction,
onClick = {
viewModel.onOpenConversation(onOpenConversation) {
unableStartConversationDialogState.show(UnableStartConversationDialogState(fullName))
unableStartConversationDialogState.show(UnableStartConversationDialogState(fullName, it))
}
},
)
Expand Down Expand Up @@ -196,16 +198,60 @@ fun ConnectionActionButton(
@Composable
fun UnableStartConversationDialogContent(dialogState: VisibilityState<UnableStartConversationDialogState>) {
VisibilityState(dialogState) { state ->
val title: String
val text: AnnotatedString

when (state.error) {
is CoreFailure.MissingKeyPackages -> {
title = stringResource(id = R.string.missing_keypackage_dialog_title)
text = LocalContext.current.resources.stringWithStyledArgs(
R.string.missing_keypackage_dialog_body,
MaterialTheme.wireTypography.body01,
MaterialTheme.wireTypography.body02,
colorsScheme().onBackground,
colorsScheme().onBackground,
state.userName
)
}

is CoreFailure.NoCommonProtocolFound.SelfNeedToUpdate -> {
title = stringResource(id = R.string.missing_keypackage_dialog_title)
text = LocalContext.current.resources.stringWithStyledArgs(
R.string.no_common_protocol_dialog_body_self_need_update,
MaterialTheme.wireTypography.body01,
MaterialTheme.wireTypography.body02,
colorsScheme().onBackground,
colorsScheme().onBackground,
state.userName
)
}

is CoreFailure.NoCommonProtocolFound.OtherNeedToUpdate -> {
title = stringResource(id = R.string.missing_keypackage_dialog_title)
text = LocalContext.current.resources.stringWithStyledArgs(
R.string.no_common_protocol_dialog_body_other_need_update,
MaterialTheme.wireTypography.body01,
MaterialTheme.wireTypography.body02,
colorsScheme().onBackground,
colorsScheme().onBackground,
state.userName
)
}

else -> {
title = stringResource(id = R.string.error_unknown_title)
text = LocalContext.current.resources.stringWithStyledArgs(
R.string.error_unknown_message,
MaterialTheme.wireTypography.body01,
MaterialTheme.wireTypography.body02,
colorsScheme().onBackground,
colorsScheme().onBackground
)
}
}
WireDialog(
title = stringResource(id = R.string.missing_keypackage_dialog_title),
text = LocalContext.current.resources.stringWithStyledArgs(
R.string.missing_keypackage_dialog_body,
MaterialTheme.wireTypography.body01,
MaterialTheme.wireTypography.body02,
colorsScheme().onBackground,
colorsScheme().onBackground,
state.userName
),
title = title,
text = text,
onDismiss = dialogState::dismiss,
optionButton1Properties = WireDialogButtonProperties(
onClick = dialogState::dismiss,
Expand All @@ -216,7 +262,7 @@ fun UnableStartConversationDialogContent(dialogState: VisibilityState<UnableStar
}
}

data class UnableStartConversationDialogState(val userName: String)
data class UnableStartConversationDialogState(val userName: String, val error: CoreFailure)

@Composable
@PreviewMultipleThemes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.R
import com.wire.android.appLogger
import com.wire.android.di.scopedArgs
import com.wire.android.di.ViewModelScopedPreview
import com.wire.android.di.scopedArgs
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.ui.UIText
import com.wire.kalium.logger.obfuscateId
Expand Down Expand Up @@ -58,6 +58,7 @@ import javax.inject.Inject
interface ConnectionActionButtonViewModel {
val infoMessage: SharedFlow<UIText>
get() = MutableSharedFlow()

fun actionableState(): ConnectionActionState = ConnectionActionState()
fun onSendConnectionRequest() {}
fun onCancelConnectionRequest() {}
Expand All @@ -66,7 +67,7 @@ interface ConnectionActionButtonViewModel {
fun onUnblockUser() {}
fun onOpenConversation(onSuccess: (conversationId: ConversationId) -> Unit) {}
fun onMissingLegalHoldConsentDismissed() {}
fun onOpenConversation(onSuccess: (conversationId: ConversationId) -> Unit, onMissingKeyPackages: () -> Unit) {}
fun onOpenConversation(onSuccess: (conversationId: ConversationId) -> Unit, onFailure: (CoreFailure) -> Unit) {}
}

@Suppress("LongParameterList", "TooManyFunctions")
Expand Down Expand Up @@ -192,14 +193,17 @@ class ConnectionActionButtonViewModelImpl @Inject constructor(
}
}

override fun onOpenConversation(onSuccess: (conversationId: ConversationId) -> Unit, onMissingKeyPackages: () -> Unit) {
override fun onOpenConversation(
onSuccess: (conversationId: ConversationId) -> Unit,
onFailure: (error: CoreFailure) -> Unit
) {
viewModelScope.launch {
state = state.performAction()
when (val result = withContext(dispatchers.io()) { getOrCreateOneToOneConversation(userId) }) {
is CreateConversationResult.Failure -> {
appLogger.d(("Couldn't retrieve or create the conversation"))
appLogger.d(("Couldn't retrieve or create the conversation. Error ${result.coreFailure}"))
state = state.finishAction()
if (result.coreFailure is CoreFailure.MissingKeyPackages) onMissingKeyPackages()
onFailure(result.coreFailure)
}

is CreateConversationResult.Success -> onSuccess(result.conversation.id)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,10 @@
<!-- Missing keyPackages dialog -->
<string name="missing_keypackage_dialog_title">Unable to start conversation</string>
<string name="missing_keypackage_dialog_body">You can’t start the conversation with %1$s right now. %1$s needs to open Wire or log in again first. Please try again later.</string>
<!-- No Common protocol dialog -->
<string name="no_common_protocol_dialog_title">Unable to start conversation</string>
<string name="no_common_protocol_dialog_body_self_need_update">You can’t communicate with %1$s, as your device doesn’t support the suitable protocol. Download the latest MLS Wire version, to call, and send messages and files.</string>
<string name="no_common_protocol_dialog_body_other_need_update">You can’t communicate with %1$s, as you two use different protocols. When %1$s gets an update, you can call and send messages and files.</string>
<!-- Gallery -->
<string name="media_gallery_default_title_name">Media Gallery</string>
<string name="media_gallery_on_image_downloaded">Saved to Downloads folder</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ class ConnectionActionButtonViewModelTest {
.arrange()

// when
viewModel.onOpenConversation(arrangement.onOpenConversation, arrangement.onMissingKeyPackages)
viewModel.onOpenConversation(arrangement.onOpenConversation, arrangement.onStartConversationError)

// then
coVerify {
arrangement.getOrCreateOneToOneConversation(TestUser.USER_ID)
}
verify { arrangement.onOpenConversation(any()) }
verify { arrangement.onMissingKeyPackages wasNot Called }
verify { arrangement.onStartConversationError wasNot Called }
}

@Test
Expand All @@ -286,33 +286,34 @@ class ConnectionActionButtonViewModelTest {
.arrange()

// when
viewModel.onOpenConversation(arrangement.onOpenConversation, arrangement.onMissingKeyPackages)
viewModel.onOpenConversation(arrangement.onOpenConversation, arrangement.onStartConversationError)

// then
coVerify {
arrangement.getOrCreateOneToOneConversation(TestUser.USER_ID)
}
verify { arrangement.onOpenConversation wasNot Called }
verify { arrangement.onMissingKeyPackages wasNot Called }
verify { arrangement.onStartConversationError(eq(failure)) }
}

@Test
fun `given a conversationId, when trying to open the conversation and fails with MissingKeyPackages, then call MissingKeyPackage()`() =
runTest {
// given
val errorResult = CoreFailure.MissingKeyPackages(setOf())
val (arrangement, viewModel) = ConnectionActionButtonHiltArrangement()
.withGetOneToOneConversation(CreateConversationResult.Failure(CoreFailure.MissingKeyPackages(setOf())))
.withGetOneToOneConversation(CreateConversationResult.Failure(errorResult))
.arrange()

// when
viewModel.onOpenConversation(arrangement.onOpenConversation, arrangement.onMissingKeyPackages)
viewModel.onOpenConversation(arrangement.onOpenConversation, arrangement.onStartConversationError)

// then
coVerify {
arrangement.getOrCreateOneToOneConversation(TestUser.USER_ID)
}
verify { arrangement.onOpenConversation wasNot Called }
verify { arrangement.onMissingKeyPackages() }
verify { arrangement.onStartConversationError(eq(errorResult)) }
}

companion object {
Expand Down Expand Up @@ -356,7 +357,7 @@ internal class ConnectionActionButtonHiltArrangement {
lateinit var onOpenConversation: (conversationId: ConversationId) -> Unit

@MockK(relaxed = true)
lateinit var onMissingKeyPackages: () -> Unit
lateinit var onStartConversationError: (CoreFailure) -> Unit

private val viewModel by lazy {
ConnectionActionButtonViewModelImpl(
Expand Down
2 changes: 1 addition & 1 deletion kalium
Submodule kalium updated 296 files

0 comments on commit 91bb89e

Please sign in to comment.