Skip to content

Commit

Permalink
state.otherUserMxItem cannot be null anymore.
Browse files Browse the repository at this point in the history
Ensure the User is retrieved from the network, or fallback to a default User object.
  • Loading branch information
bmarty committed Sep 14, 2022
1 parent 6d2a9ec commit 92d7391
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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),
)
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -160,7 +162,6 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(

setState {
copy(
otherUserMxItem = userItem?.toMatrixItem(),
sasTransactionState = sasTx?.state,
qrTransactionState = qrTx?.state,
transactionId = pr?.transactionId ?: initialState.verificationId,
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 -> {
Expand All @@ -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()))
}
}
}
Expand Down

0 comments on commit 92d7391

Please sign in to comment.