From 5f35926ce6cdeca71cca9f3b4c3364ab040d084b Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Mon, 17 Oct 2022 11:21:15 +0200 Subject: [PATCH] Voice Broadcast - Hide related voice message events --- .../android/sdk/api/session/events/model/Event.kt | 5 +++++ .../detail/timeline/factory/MessageItemFactory.kt | 7 +++++-- .../detail/timeline/helper/TimelineEventsGroups.kt | 11 +++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/Event.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/Event.kt index f5d2c0d9a0..71daf4cc4f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/Event.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/Event.kt @@ -31,6 +31,7 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent import org.matrix.android.sdk.api.session.room.model.message.MessageStickerContent import org.matrix.android.sdk.api.session.room.model.message.MessageType +import org.matrix.android.sdk.api.session.room.model.message.asMessageAudioEvent import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent import org.matrix.android.sdk.api.session.room.model.relation.shouldRenderInThread import org.matrix.android.sdk.api.session.room.send.SendState @@ -357,6 +358,10 @@ fun Event.isAudioMessage(): Boolean { } } +fun Event.isVoiceMessage(): Boolean { + return this.asMessageAudioEvent()?.content?.voiceMessageIndicator != null +} + fun Event.isFileMessage(): Boolean { return when (getMsgType()) { MessageType.MSGTYPE_FILE -> true diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt index 06da69fc1a..d39f933363 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt @@ -323,7 +323,10 @@ class MessageItemFactory @Inject constructor( informationData: MessageInformationData, highlight: Boolean, attributes: AbsMessageItem.Attributes - ): MessageVoiceItem { + ): MessageVoiceItem? { + val eventsGroup = params.eventsGroup?.let { VoiceBroadcastEventsGroup(it) } + if (eventsGroup != null && eventsGroup.getLastDisplayableEvent().eventId != params.event.eventId) return null + val fileUrl = getAudioFileUrl(messageContent, informationData) val playbackControlButtonClickListener = createOnPlaybackButtonClickListener(messageContent, informationData, params) @@ -722,7 +725,7 @@ class MessageItemFactory @Inject constructor( ): MessageVoiceBroadcastItem? { if (messageContent.voiceBroadcastState != VoiceBroadcastState.STARTED) return null val voiceBroadcastEventsGroup = eventsGroup?.let { VoiceBroadcastEventsGroup(it) } ?: return null - val mostRecentEvent = voiceBroadcastEventsGroup.getLastEvent() + val mostRecentEvent = voiceBroadcastEventsGroup.getLastDisplayableEvent() val mostRecentMessageContent = (mostRecentEvent.getVectorLastMessageContent() as? MessageVoiceBroadcastInfoContent) ?: return null return MessageVoiceBroadcastItem_() .attributes(attributes) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/TimelineEventsGroups.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/TimelineEventsGroups.kt index 13de456e84..ccfdac8f15 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/TimelineEventsGroups.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/TimelineEventsGroups.kt @@ -22,6 +22,9 @@ import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.events.model.EventType +import org.matrix.android.sdk.api.session.events.model.RelationType +import org.matrix.android.sdk.api.session.events.model.getRelationContent +import org.matrix.android.sdk.api.session.events.model.isVoiceMessage import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.room.model.call.CallInviteContent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent @@ -61,6 +64,10 @@ class TimelineEventsGroups { EventType.isCallEvent(type) -> (content?.get("call_id") as? String) type == VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO -> root.asVoiceBroadcastEvent()?.reference?.eventId type == EventType.STATE_ROOM_WIDGET || type == EventType.STATE_ROOM_WIDGET_LEGACY -> root.stateKey + type == EventType.MESSAGE && root.isVoiceMessage() -> { + // Group voice messages with a reference to an eventId + root.getRelationContent()?.takeIf { it.type == RelationType.REFERENCE }?.eventId + } else -> { null } @@ -134,8 +141,8 @@ class CallSignalingEventsGroup(private val group: TimelineEventsGroup) { } class VoiceBroadcastEventsGroup(private val group: TimelineEventsGroup) { - fun getLastEvent(): TimelineEvent { + fun getLastDisplayableEvent(): TimelineEvent { return group.events.find { it.root.asVoiceBroadcastEvent()?.content?.voiceBroadcastState == VoiceBroadcastState.STOPPED } - ?: group.events.maxBy { it.root.originServerTs ?: 0L } + ?: group.events.filter { it.root.type == VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO }.maxBy { it.root.originServerTs ?: 0L } } }