From 05b8f7d3750540ddd2978be675852fbacf16e71b Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Tue, 4 Oct 2022 23:54:05 +0200 Subject: [PATCH] Create TimelineEvent.getVectorLastMessageContent() in vector module --- .../vector/app/core/extensions/TimelineEvent.kt | 17 +++++++++++++++++ .../detail/composer/MessageComposerFragment.kt | 4 ++-- .../detail/composer/MessageComposerViewModel.kt | 4 ++-- .../timeline/action/MessageActionsViewModel.kt | 6 +++--- .../timeline/factory/MessageItemFactory.kt | 17 ++--------------- .../format/DisplayableEventFormatter.kt | 4 ++-- .../helper/MessageInformationDataFactory.kt | 10 +++++++--- .../style/TimelineMessageLayoutFactory.kt | 8 ++++---- .../notifications/NotifiableEventResolver.kt | 4 ++-- .../features/poll/create/CreatePollViewModel.kt | 4 ++-- 10 files changed, 43 insertions(+), 35 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/extensions/TimelineEvent.kt b/vector/src/main/java/im/vector/app/core/extensions/TimelineEvent.kt index 28c1587b1a..cdb84387ce 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/TimelineEvent.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/TimelineEvent.kt @@ -16,9 +16,14 @@ package im.vector.app.core.extensions +import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO +import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent 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.model.message.MessageContent import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent +import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent fun TimelineEvent.canReact(): Boolean { // Only event of type EventType.MESSAGE, EventType.STICKER and EventType.POLL_START are supported for the moment @@ -26,3 +31,15 @@ fun TimelineEvent.canReact(): Boolean { root.sendState == SendState.SYNCED && !root.isRedacted() } + +/** + * Get last MessageContent, after a possible edition. + * This method iterate on the vector event types and fallback to [getLastMessageContent] from the matrix sdk for the other types. + */ +fun TimelineEvent.getVectorLastMessageContent(): MessageContent? { + // Iterate on event types which are not part of the matrix sdk, otherwise fallback to the sdk method + return when (root.getClearType()) { + STATE_ROOM_VOICE_BROADCAST_INFO -> (annotations?.editSummary?.latestContent ?: root.getClearContent()).toModel() + else -> getLastMessageContent() + } +} diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt index 509e8cc4af..3f58a4a184 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt @@ -48,6 +48,7 @@ import com.vanniktech.emoji.EmojiPopup import dagger.hilt.android.AndroidEntryPoint import im.vector.app.R import im.vector.app.core.error.fatalError +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.extensions.showKeyboard import im.vector.app.core.glide.GlideApp @@ -103,7 +104,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageFormat import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent import org.matrix.android.sdk.api.session.room.model.message.MessageTextContent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.util.MatrixItem import org.matrix.android.sdk.api.util.toMatrixItem import reactivecircus.flowbinding.android.view.focusChanges @@ -356,7 +356,7 @@ class MessageComposerFragment : VectorBaseFragment(), A setTextColor(matrixItemColorProvider.getColor(MatrixItem.UserItem(event.root.senderId ?: "@"))) } - val messageContent: MessageContent? = event.getLastMessageContent() + val messageContent: MessageContent? = event.getVectorLastMessageContent() val nonFormattedBody = when (messageContent) { is MessageAudioContent -> getAudioContentBodyText(messageContent) is MessagePollContent -> messageContent.getBestPollCreationInfo()?.question?.getBestQuestion() diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt index afdd01ba46..c83f818ac8 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt @@ -23,6 +23,7 @@ import dagger.assisted.AssistedInject import im.vector.app.R import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.resources.StringProvider import im.vector.app.features.analytics.AnalyticsTracker @@ -62,7 +63,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageType import org.matrix.android.sdk.api.session.room.model.relation.shouldRenderInThread import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper import org.matrix.android.sdk.api.session.room.send.UserDraft -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.session.room.timeline.getRelationContent import org.matrix.android.sdk.api.session.room.timeline.getTextEditableContent import org.matrix.android.sdk.api.session.space.CreateSpaceParams @@ -513,7 +513,7 @@ class MessageComposerViewModel @AssistedInject constructor( room.relationService().editReply(state.sendMode.timelineEvent, it, action.text.toString()) } } else { - val messageContent = state.sendMode.timelineEvent.getLastMessageContent() + val messageContent = state.sendMode.timelineEvent.getVectorLastMessageContent() val existingBody = messageContent?.body ?: "" if (existingBody != action.text) { room.relationService().editTextMessage( diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt index 3dfb6744e0..0c44ee386d 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt @@ -25,6 +25,7 @@ import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.error.ErrorFormatter import im.vector.app.core.extensions.canReact +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.resources.StringProvider @@ -60,7 +61,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageWithAttachme import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.session.room.timeline.hasBeenEdited import org.matrix.android.sdk.api.session.room.timeline.isPoll import org.matrix.android.sdk.api.session.room.timeline.isRootThread @@ -187,7 +187,7 @@ class MessageActionsViewModel @AssistedInject constructor( when (timelineEvent.root.getClearType()) { EventType.MESSAGE, EventType.STICKER -> { - val messageContent: MessageContent? = timelineEvent.getLastMessageContent() + val messageContent: MessageContent? = timelineEvent.getVectorLastMessageContent() if (messageContent is MessageTextContent && messageContent.format == MessageFormat.FORMAT_MATRIX_HTML) { val html = messageContent.formattedBody ?.takeIf { it.isNotBlank() } @@ -253,7 +253,7 @@ class MessageActionsViewModel @AssistedInject constructor( } private fun actionsForEvent(timelineEvent: TimelineEvent, actionPermissions: ActionPermissions): List { - val messageContent = timelineEvent.getLastMessageContent() + val messageContent = timelineEvent.getVectorLastMessageContent() val msgType = messageContent?.msgType return arrayListOf().apply { 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 0fbd1033c7..12fd286054 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 @@ -28,6 +28,7 @@ import dagger.Lazy import im.vector.app.R import im.vector.app.core.epoxy.ClickListener import im.vector.app.core.epoxy.VectorEpoxyModel +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.files.LocalFilesHelper import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.StringProvider @@ -79,7 +80,6 @@ import im.vector.app.features.media.ImageContentRenderer import im.vector.app.features.media.VideoContentRenderer import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.voice.AudioWaveformView -import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent import im.vector.lib.core.utils.epoxy.charsequence.toEpoxyCharSequence import me.gujun.android.span.span @@ -106,8 +106,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageVerification import org.matrix.android.sdk.api.session.room.model.message.MessageVideoContent import org.matrix.android.sdk.api.session.room.model.message.getFileUrl import org.matrix.android.sdk.api.session.room.model.message.getThumbnailUrl -import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.settings.LightweightSettingsStorage import org.matrix.android.sdk.api.util.MimeTypes import javax.inject.Inject @@ -168,7 +166,7 @@ class MessageItemFactory @Inject constructor( return buildRedactedItem(attributes, highlight) } - val messageContent = getLastMessageContent(event) + val messageContent = event.getVectorLastMessageContent() if (messageContent == null) { val malformedText = stringProvider.getString(R.string.malformed_message) return defaultItemFactory.create(malformedText, informationData, highlight, callback) @@ -210,17 +208,6 @@ class MessageItemFactory @Inject constructor( } } - private fun getLastMessageContent(event: TimelineEvent): MessageContent? { - return with(event) { - // Iterate on event types which are not part of the matrix sdk, otherwise fallback to the sdk method - when (root.getClearType()) { - STATE_ROOM_VOICE_BROADCAST_INFO -> - (annotations?.editSummary?.latestContent ?: root.getClearContent()).toModel() - else -> event.getLastMessageContent() - } - } - } - private fun buildLocationItem( locationContent: MessageLocationContent, informationData: MessageInformationData, diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt index 7b9bd4530b..eb531b6f1b 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt @@ -19,6 +19,7 @@ package im.vector.app.features.home.room.detail.timeline.format import dagger.Lazy import im.vector.app.EmojiSpanify import im.vector.app.R +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.StringProvider import im.vector.app.features.html.EventHtmlRenderer @@ -34,7 +35,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageTextContent import org.matrix.android.sdk.api.session.room.model.message.MessageType import org.matrix.android.sdk.api.session.room.model.relation.ReactionContent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.session.room.timeline.getTextDisplayableContent import javax.inject.Inject @@ -60,7 +60,7 @@ class DisplayableEventFormatter @Inject constructor( return when (timelineEvent.root.getClearType()) { EventType.MESSAGE -> { - timelineEvent.getLastMessageContent()?.let { messageContent -> + timelineEvent.getVectorLastMessageContent()?.let { messageContent -> when (messageContent.msgType) { MessageType.MSGTYPE_TEXT -> { val body = messageContent.getTextDisplayableContent() 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 ddb98c42c6..50b4366e98 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 @@ -18,6 +18,7 @@ package im.vector.app.features.home.room.detail.timeline.helper import im.vector.app.core.date.DateFormatKind import im.vector.app.core.date.VectorDateFormatter +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.extensions.localDateTime import im.vector.app.features.home.room.detail.timeline.factory.TimelineItemFactoryParams import im.vector.app.features.home.room.detail.timeline.item.E2EDecoration @@ -41,7 +42,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageType import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationRequestContent import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.session.room.timeline.hasBeenEdited import javax.inject.Inject @@ -123,7 +123,11 @@ class MessageInformationDataFactory @Inject constructor( isLastFromThisSender = isLastFromThisSender, e2eDecoration = e2eDecoration, sendStateDecoration = sendStateDecoration, - messageType = if (event.root.isSticker()) { MessageType.MSGTYPE_STICKER_LOCAL } else { event.root.getMsgType() } + messageType = if (event.root.isSticker()) { + MessageType.MSGTYPE_STICKER_LOCAL + } else { + event.root.getMsgType() + } ) } @@ -230,7 +234,7 @@ class MessageInformationDataFactory @Inject constructor( EventType.KEY_VERIFICATION_DONE, EventType.KEY_VERIFICATION_CANCEL -> true EventType.MESSAGE -> { - event.getLastMessageContent() is MessageVerificationRequestContent + event.getVectorLastMessageContent() is MessageVerificationRequestContent } else -> false } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/style/TimelineMessageLayoutFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/style/TimelineMessageLayoutFactory.kt index 14a02c7172..379e5b3b91 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/style/TimelineMessageLayoutFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/style/TimelineMessageLayoutFactory.kt @@ -18,6 +18,7 @@ package im.vector.app.features.home.room.detail.timeline.style import android.content.res.Resources import im.vector.app.R +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.extensions.localDateTime import im.vector.app.core.resources.LocaleProvider import im.vector.app.core.resources.isRTL @@ -29,7 +30,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessageType import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationRequestContent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.session.room.timeline.isEdition import org.matrix.android.sdk.api.session.room.timeline.isRootThread import javax.inject.Inject @@ -126,7 +126,7 @@ class TimelineMessageLayoutFactory @Inject constructor( isLastFromThisSender = isLastFromThisSender ) - val messageContent = event.getLastMessageContent() + val messageContent = event.getVectorLastMessageContent() TimelineMessageLayout.Bubble( showAvatar = showInformation && !isSentByMe, showDisplayName = showInformation && !isSentByMe, @@ -167,7 +167,7 @@ class TimelineMessageLayoutFactory @Inject constructor( private fun TimelineEvent.shouldBuildBubbleLayout(): Boolean { val type = root.getClearType() if (type in EVENT_TYPES_WITH_BUBBLE_LAYOUT) { - val messageContent = getLastMessageContent() + val messageContent = getVectorLastMessageContent() return messageContent?.msgType !in MSG_TYPES_WITHOUT_BUBBLE_LAYOUT } return false @@ -212,7 +212,7 @@ class TimelineMessageLayoutFactory @Inject constructor( EventType.KEY_VERIFICATION_DONE, EventType.KEY_VERIFICATION_CANCEL -> true EventType.MESSAGE -> { - event.getLastMessageContent() is MessageVerificationRequestContent + event.getVectorLastMessageContent() is MessageVerificationRequestContent } else -> false } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt index 90138fd495..4ee7da4b64 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt @@ -17,6 +17,7 @@ package im.vector.app.features.notifications import android.net.Uri import im.vector.app.R +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.extensions.takeAs import im.vector.app.core.resources.BuildMeta import im.vector.app.core.resources.StringProvider @@ -45,7 +46,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageWithAttachme import org.matrix.android.sdk.api.session.room.sender.SenderInfo import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.getEditedEventId -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent import org.matrix.android.sdk.api.util.toMatrixItem import timber.log.Timber import java.util.UUID @@ -231,7 +231,7 @@ class NotifiableEventResolver @Inject constructor( private suspend fun TimelineEvent.downloadAndExportImage(session: Session): Uri? { return kotlin.runCatching { - getLastMessageContent()?.takeAs()?.let { imageMessage -> + getVectorLastMessageContent()?.takeAs()?.let { imageMessage -> val fileService = session.fileService() fileService.downloadFile(imageMessage) fileService.getTemporarySharableURI(imageMessage) diff --git a/vector/src/main/java/im/vector/app/features/poll/create/CreatePollViewModel.kt b/vector/src/main/java/im/vector/app/features/poll/create/CreatePollViewModel.kt index ec064877a9..1b2f0d7d08 100644 --- a/vector/src/main/java/im/vector/app/features/poll/create/CreatePollViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/poll/create/CreatePollViewModel.kt @@ -22,6 +22,7 @@ import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory +import im.vector.app.core.extensions.getVectorLastMessageContent import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.poll.PollMode import org.matrix.android.sdk.api.session.Session @@ -29,7 +30,6 @@ import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.room.getTimelineEvent import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent import org.matrix.android.sdk.api.session.room.model.message.PollType -import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent class CreatePollViewModel @AssistedInject constructor( @Assisted private val initialState: CreatePollViewState, @@ -72,7 +72,7 @@ class CreatePollViewModel @AssistedInject constructor( private fun initializeEditedPoll(eventId: String) { val event = room.getTimelineEvent(eventId) ?: return - val content = event.getLastMessageContent() as? MessagePollContent ?: return + val content = event.getVectorLastMessageContent() as? MessagePollContent ?: return val pollCreationInfo = content.getBestPollCreationInfo() val pollType = pollCreationInfo?.kind ?: PollType.DISCLOSED_UNSTABLE