state.otherUserMxItem cannot be null anymore.

Ensure the User is retrieved from the network, or fallback to a default User object.
This commit is contained in:
Benoit Marty 2022-09-14 20:25:20 +02:00
parent 6d2a9ec9d5
commit 92d7391232
5 changed files with 54 additions and 37 deletions

View File

@ -152,29 +152,25 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
}
override fun invalidate() = withState(viewModel) { state ->
state.otherUserMxItem?.let { matrixItem ->
if (state.isMe) {
avatarRenderer.render(matrixItem, views.otherUserAvatarImageView)
if (state.sasTransactionState == VerificationTxState.Verified ||
state.qrTransactionState == VerificationTxState.Verified ||
state.verifiedFromPrivateKeys) {
views.otherUserShield.render(RoomEncryptionTrustLevel.Trusted)
} else {
views.otherUserShield.render(RoomEncryptionTrustLevel.Warning)
}
views.otherUserNameText.text = getString(
if (state.selfVerificationMode) R.string.crosssigning_verify_this_session else R.string.crosssigning_verify_session
)
avatarRenderer.render(state.otherUserMxItem, views.otherUserAvatarImageView)
if (state.isMe) {
if (state.sasTransactionState == VerificationTxState.Verified ||
state.qrTransactionState == VerificationTxState.Verified ||
state.verifiedFromPrivateKeys) {
views.otherUserShield.render(RoomEncryptionTrustLevel.Trusted)
} else {
avatarRenderer.render(matrixItem, views.otherUserAvatarImageView)
if (state.sasTransactionState == VerificationTxState.Verified || state.qrTransactionState == VerificationTxState.Verified) {
views.otherUserNameText.text = getString(R.string.verification_verified_user, matrixItem.getBestName())
views.otherUserShield.render(RoomEncryptionTrustLevel.Trusted)
} else {
views.otherUserNameText.text = getString(R.string.verification_verify_user, matrixItem.getBestName())
views.otherUserShield.render(null)
}
views.otherUserShield.render(RoomEncryptionTrustLevel.Warning)
}
views.otherUserNameText.text = getString(
if (state.selfVerificationMode) R.string.crosssigning_verify_this_session else R.string.crosssigning_verify_session
)
} else {
if (state.sasTransactionState == VerificationTxState.Verified || state.qrTransactionState == VerificationTxState.Verified) {
views.otherUserNameText.text = getString(R.string.verification_verified_user, state.otherUserMxItem.getBestName())
views.otherUserShield.render(RoomEncryptionTrustLevel.Trusted)
} else {
views.otherUserNameText.text = getString(R.string.verification_verify_user, state.otherUserMxItem.getBestName())
views.otherUserShield.render(null)
}
}
@ -271,7 +267,7 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
VerificationQRWaitingFragment::class,
VerificationQRWaitingFragment.Args(
isMe = state.isMe,
otherUserName = state.otherUserMxItem?.getBestName() ?: state.otherUserId
otherUserName = state.otherUserMxItem.getBestName()
)
)
return@withState

View File

@ -37,6 +37,7 @@ import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.crosssigning.KEYBACKUP_SECRET_SSSS_NAME
@ -70,7 +71,7 @@ data class VerificationBottomSheetViewState(
val roomId: String?,
// true when we display the loading and we wait for the other (incoming request)
val selfVerificationMode: Boolean,
val otherUserMxItem: MatrixItem? = null,
val otherUserMxItem: MatrixItem,
val pendingRequest: Async<PendingVerificationRequest> = Uninitialized,
val pendingLocalId: String? = null,
val sasTransactionState: VerificationTxState? = null,
@ -92,7 +93,8 @@ data class VerificationBottomSheetViewState(
otherUserId = args.otherUserId,
verificationId = args.verificationId,
roomId = args.roomId,
selfVerificationMode = args.selfVerificationMode
selfVerificationMode = args.selfVerificationMode,
otherUserMxItem = MatrixItem.UserItem(args.otherUserId),
)
}
@ -126,7 +128,7 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(
}
}
val userItem = session.getUser(initialState.otherUserId)
fetchOtherUserProfile(initialState.otherUserId)
var autoReady = false
val pr = if (initialState.selfVerificationMode) {
@ -160,7 +162,6 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(
setState {
copy(
otherUserMxItem = userItem?.toMatrixItem(),
sasTransactionState = sasTx?.state,
qrTransactionState = qrTx?.state,
transactionId = pr?.transactionId ?: initialState.verificationId,
@ -183,6 +184,28 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(
}
}
private fun fetchOtherUserProfile(otherUserId: String) {
session.getUser(otherUserId)?.toMatrixItem()?.let {
setState {
copy(
otherUserMxItem = it
)
}
}
// Always fetch the latest User data
viewModelScope.launch {
tryOrNull { session.userService().resolveUser(otherUserId) }
?.toMatrixItem()
?.let {
setState {
copy(
otherUserMxItem = it
)
}
}
}
}
override fun onCleared() {
session.cryptoService().verificationService().removeListener(this)
super.onCleared()

View File

@ -26,6 +26,7 @@ import im.vector.app.core.utils.colorizeMatchingText
import im.vector.app.features.crypto.verification.VerificationBottomSheetViewState
import im.vector.app.features.crypto.verification.epoxy.bottomSheetVerificationActionItem
import im.vector.app.features.crypto.verification.epoxy.bottomSheetVerificationNoticeItem
import im.vector.app.features.displayname.getBestName
import im.vector.lib.core.utils.epoxy.charsequence.EpoxyCharSequence
import im.vector.lib.core.utils.epoxy.charsequence.toEpoxyCharSequence
import javax.inject.Inject
@ -61,7 +62,7 @@ class VerificationCancelController @Inject constructor(
}
} else {
val otherUserID = state.otherUserId
val otherDisplayName = state.otherUserMxItem?.displayName ?: state.otherUserId
val otherDisplayName = state.otherUserMxItem.getBestName()
bottomSheetVerificationNoticeItem {
id("notice")
notice(

View File

@ -54,7 +54,7 @@ class VerificationQrScannedByOtherController @Inject constructor(
if (state.isMe) {
notice(host.stringProvider.getString(R.string.qr_code_scanned_self_verif_notice).toEpoxyCharSequence())
} else {
val name = state.otherUserMxItem?.getBestName() ?: state.otherUserId
val name = state.otherUserMxItem.getBestName()
notice(host.stringProvider.getString(R.string.qr_code_scanned_by_other_notice, name).toEpoxyCharSequence())
}
}

View File

@ -52,7 +52,6 @@ class VerificationRequestController @Inject constructor(
override fun buildModels() {
val state = viewState ?: return
val matrixItem = state.otherUserMxItem ?: return
val host = this
if (state.selfVerificationMode) {
@ -107,11 +106,9 @@ class VerificationRequestController @Inject constructor(
if (state.isMe) {
stringProvider.getString(R.string.verify_new_session_notice)
} else {
matrixItem.let {
stringProvider.getString(R.string.verification_request_notice, it.id)
.toSpannable()
.colorizeMatchingText(it.id, colorProvider.getColorFromAttribute(R.attr.vctr_notice_text_color))
}
stringProvider.getString(R.string.verification_request_notice, state.otherUserId)
.toSpannable()
.colorizeMatchingText(state.otherUserId, colorProvider.getColorFromAttribute(R.attr.vctr_notice_text_color))
}
bottomSheetVerificationNoticeItem {
@ -138,7 +135,7 @@ class VerificationRequestController @Inject constructor(
is Loading -> {
bottomSheetVerificationWaitingItem {
id("waiting")
title(host.stringProvider.getString(R.string.verification_request_waiting_for, matrixItem.getBestName()))
title(host.stringProvider.getString(R.string.verification_request_waiting_for, state.otherUserMxItem.getBestName()))
}
}
is Success -> {
@ -151,7 +148,7 @@ class VerificationRequestController @Inject constructor(
} else {
bottomSheetVerificationWaitingItem {
id("waiting")
title(host.stringProvider.getString(R.string.verification_request_waiting_for, matrixItem.getBestName()))
title(host.stringProvider.getString(R.string.verification_request_waiting_for, state.otherUserMxItem.getBestName()))
}
}
}