From 44355109d7f8d861876c4aebc3ee9c727bdad22b Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Mon, 2 Nov 2020 10:55:39 +0100 Subject: [PATCH] Improve direct chat handling We show less information in direct chats in dual chat bubble layout, as we assume that the senders know themselves, and see the chat partner from the action bar at the top. However, there are some limitations: - A directChat might actually include more members, due to helper bots, multiple accounts, or misconfigurations - Even if a direct chat has exactly two members, there might have been some membership changes in the past To account for this, we do the following: - Only enable the direct message layout if there are exactly two members in the room currently (in addition to the isDirect-flag being set) - Before hiding sender information, check if the sender is actually the expected chat partner. If not, fallback to showing avatar and name for this message. Possible improvement for the future: if we have a message in a direct chat by a non-member, following messages by the dm chat partner might benefit from showing member name either way. Change-Id: Ie4a204510990301175339e60469048b06669d36b --- .../helper/MessageInformationDataFactory.kt | 26 ++++++++++++++++++- .../detail/timeline/item/AbsMessageItem.kt | 5 ++-- .../timeline/item/MessageInformationData.kt | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt index 76faad279f..163296e71c 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt @@ -35,6 +35,8 @@ import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toModel +import org.matrix.android.sdk.api.session.room.members.roomMemberQueryParams +import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.ReferencesAggregatedContent import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationRequestContent import org.matrix.android.sdk.api.session.room.send.SendState @@ -76,6 +78,27 @@ class MessageInformationDataFactory @Inject constructor(private val session: Ses val time = dateFormatter.format(event.root.originServerTs, DateFormatKind.MESSAGE_SIMPLE) val e2eDecoration = getE2EDecoration(event) + var isEffectivelyDirect = false + var dmOtherMemberId: String? = null + if ((roomSummaryHolder.roomSummary?.isDirect == true) && event.root.roomId != null) { + val members = session.getRoom(event.root.roomId!!) + ?.getRoomMembers(roomMemberQueryParams { memberships = listOf(Membership.JOIN) }) + ?.map { it.userId } + .orEmpty() + .toSet() + if (members.size == 2) { + var foundSelf = false + for (member in members) { + if (member == session.myUserId) { + foundSelf = true + } else { + dmOtherMemberId = member + } + } + isEffectivelyDirect = foundSelf && (dmOtherMemberId != null) + } + } + return MessageInformationData( eventId = eventId, senderId = event.root.senderId ?: "", @@ -127,7 +150,8 @@ class MessageInformationDataFactory @Inject constructor(private val session: Ses } else { AnonymousReadReceipt.PROCESSING }, - isDirect = roomSummaryHolder.roomSummary?.isDirect ?: false, + isDirect = isEffectivelyDirect, + dmChatPartnerId = dmOtherMemberId, e2eDecoration = e2eDecoration ) } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt index 0fb0cf9ffb..e068d0d345 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/AbsMessageItem.kt @@ -242,7 +242,7 @@ abstract class AbsMessageItem : AbsBaseMessageItem } override fun ignoreMessageGuideline(context: Context): Boolean { - return infoInBubbles(context) && (attributes.informationData.sentByMe || attributes.informationData.isDirect) + return infoInBubbles(context) && canHideAvatars() } open fun getViewStubMinimumWidth(holder: H, contentInBubble: Boolean, showInformation: Boolean): Int { @@ -303,7 +303,8 @@ abstract class AbsMessageItem : AbsBaseMessageItem } open fun canHideAvatars(): Boolean { - return attributes.informationData.sentByMe || attributes.informationData.isDirect + return attributes.informationData.sentByMe || + (attributes.informationData.isDirect && attributes.informationData.senderId == attributes.informationData.dmChatPartnerId) } override fun setBubbleLayout(holder: H, bubbleStyle: String, bubbleStyleSetting: String, reverseBubble: Boolean) { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageInformationData.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageInformationData.kt index 307b91dee6..6354b201f5 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageInformationData.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageInformationData.kt @@ -44,6 +44,7 @@ data class MessageInformationData( val sentByMe : Boolean, val readReceiptAnonymous: AnonymousReadReceipt, val isDirect: Boolean, + val dmChatPartnerId: String?, val e2eDecoration: E2EDecoration = E2EDecoration.NONE ) : Parcelable {