From 1b41a72e72166e4750d3612470f715ee89e2f81f Mon Sep 17 00:00:00 2001 From: ariskotsomitopoulos Date: Mon, 10 Jan 2022 14:14:11 +0200 Subject: [PATCH] Fix Quote from within a thread --- .../sdk/api/session/room/send/SendService.kt | 2 +- .../session/room/send/DefaultSendService.kt | 10 +++- .../room/send/LocalEchoEventFactory.kt | 17 ++++++- .../composer/MessageComposerViewModel.kt | 51 +++++-------------- 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/send/SendService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/send/SendService.kt index a132d9ff10..f9a775589c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/send/SendService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/send/SendService.kt @@ -63,7 +63,7 @@ interface SendService { * @param autoMarkdown If true, the SDK will generate a formatted HTML message from the body text if markdown syntax is present * @return a [Cancelable] */ - fun sendQuotedTextMessage(quotedEvent: TimelineEvent, text: String, autoMarkdown: Boolean): Cancelable + fun sendQuotedTextMessage(quotedEvent: TimelineEvent, text: String, autoMarkdown: Boolean, rootThreadEventId: String? = null): Cancelable /** * Method to send a media asynchronously. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt index 14b66b5377..23322b081c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt @@ -97,8 +97,14 @@ internal class DefaultSendService @AssistedInject constructor( .let { sendEvent(it) } } - override fun sendQuotedTextMessage(quotedEvent: TimelineEvent, text: String, autoMarkdown: Boolean): Cancelable { - return localEchoEventFactory.createQuotedTextEvent(roomId, quotedEvent, text, autoMarkdown) + override fun sendQuotedTextMessage(quotedEvent: TimelineEvent, text: String, autoMarkdown: Boolean, rootThreadEventId: String?): Cancelable { + return localEchoEventFactory.createQuotedTextEvent( + roomId = roomId, + quotedEvent = quotedEvent, + text = text, + autoMarkdown = autoMarkdown, + rootThreadEventId = rootThreadEventId + ) .also { createLocalEcho(it) } .let { sendEvent(it) } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt index 930327b41f..86da186222 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/LocalEchoEventFactory.kt @@ -476,6 +476,7 @@ internal class LocalEchoEventFactory @Inject constructor( newBodyFormatted ) } + private fun buildReplyFallback(body: TextContent, originalSenderId: String?, newBodyText: String): String { return buildString { append("> <") @@ -564,11 +565,25 @@ internal class LocalEchoEventFactory @Inject constructor( quotedEvent: TimelineEvent, text: String, autoMarkdown: Boolean, + rootThreadEventId: String? ): Event { val messageContent = quotedEvent.getLastMessageContent() val textMsg = messageContent?.body val quoteText = legacyRiotQuoteText(textMsg, text) - return createFormattedTextEvent(roomId, markdownParser.parse(quoteText, force = true, advanced = autoMarkdown), MessageType.MSGTYPE_TEXT) + + return if (rootThreadEventId != null) { + createMessageEvent( + roomId, + markdownParser + .parse(quoteText, force = true, advanced = autoMarkdown) + .toThreadTextContent(rootThreadEventId, MessageType.MSGTYPE_TEXT) + ) + } else { + createFormattedTextEvent( + roomId, + markdownParser.parse(quoteText, force = true, advanced = autoMarkdown), + MessageType.MSGTYPE_TEXT) + } } private fun legacyRiotQuoteText(quotedText: String?, myText: String): String { 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 ea89313131..c15c026e9d 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 @@ -242,7 +242,7 @@ class MessageComposerViewModel @AssistedInject constructor( handleJoinToAnotherRoomSlashCommand(slashCommandResult) popDraft() } - is ParsedCommand.PartRoom -> { + is ParsedCommand.PartRoom -> { handlePartSlashCommand(slashCommandResult) } is ParsedCommand.SendEmote -> { @@ -325,7 +325,7 @@ class MessageComposerViewModel @AssistedInject constructor( is ParsedCommand.ChangeAvatarForRoom -> { handleChangeAvatarForRoomSlashCommand(slashCommandResult) } - is ParsedCommand.ShowUser -> { + is ParsedCommand.ShowUser -> { _viewEvents.post(MessageComposerViewEvents.SlashCommandResultOk()) handleWhoisSlashCommand(slashCommandResult) popDraft() @@ -343,7 +343,7 @@ class MessageComposerViewModel @AssistedInject constructor( ) } } - is ParsedCommand.CreateSpace -> { + is ParsedCommand.CreateSpace -> { _viewEvents.post(MessageComposerViewEvents.SlashCommandLoading) viewModelScope.launch(Dispatchers.IO) { try { @@ -367,7 +367,7 @@ class MessageComposerViewModel @AssistedInject constructor( } Unit } - is ParsedCommand.AddToSpace -> { + is ParsedCommand.AddToSpace -> { _viewEvents.post(MessageComposerViewEvents.SlashCommandLoading) viewModelScope.launch(Dispatchers.IO) { try { @@ -386,7 +386,7 @@ class MessageComposerViewModel @AssistedInject constructor( } Unit } - is ParsedCommand.JoinSpace -> { + is ParsedCommand.JoinSpace -> { _viewEvents.post(MessageComposerViewEvents.SlashCommandLoading) viewModelScope.launch(Dispatchers.IO) { try { @@ -399,7 +399,7 @@ class MessageComposerViewModel @AssistedInject constructor( } Unit } - is ParsedCommand.LeaveRoom -> { + is ParsedCommand.LeaveRoom -> { viewModelScope.launch(Dispatchers.IO) { try { session.getRoom(slashCommandResult.roomId)?.leave(null) @@ -411,7 +411,7 @@ class MessageComposerViewModel @AssistedInject constructor( } Unit } - is ParsedCommand.UpgradeRoom -> { + is ParsedCommand.UpgradeRoom -> { _viewEvents.post( MessageComposerViewEvents.ShowRoomUpgradeDialog( slashCommandResult.newVersion, @@ -446,39 +446,12 @@ class MessageComposerViewModel @AssistedInject constructor( _viewEvents.post(MessageComposerViewEvents.MessageSent) popDraft() } -// is SendMode.Quote -> { -// val messageContent = state.sendMode.timelineEvent.getLastMessageContent() -// val textMsg = messageContent?.body -// -// val finalText = legacyRiotQuoteText(textMsg, action.text.toString()) -// -// // TODO check for pills? -// -// // TODO Refactor this, just temporary for quotes -// val parser = Parser.builder().build() -// val document = parser.parse(finalText) -// val renderer = HtmlRenderer.builder().build() -// val htmlText = renderer.render(document) -// -// if (finalText == htmlText) { -// state.rootThreadEventId?.let { -// room.replyInThread( -// rootThreadEventId = it, -// replyInThreadText = finalText) -// } ?: room.sendTextMessage(finalText) -// } else { -// state.rootThreadEventId?.let { -// room.replyInThread( -// rootThreadEventId = it, -// replyInThreadText = finalText, -// formattedText = htmlText) -// } ?: room.sendFormattedTextMessage(finalText, htmlText) -// } -// _viewEvents.post(MessageComposerViewEvents.MessageSent) -// popDraft() -// } is SendMode.Quote -> { - room.sendQuotedTextMessage(state.sendMode.timelineEvent, action.text.toString(), action.autoMarkdown) + room.sendQuotedTextMessage( + quotedEvent = state.sendMode.timelineEvent, + text = action.text.toString(), + autoMarkdown = action.autoMarkdown, + rootThreadEventId = state.rootThreadEventId) _viewEvents.post(MessageComposerViewEvents.MessageSent) popDraft() }