From 3aa30e5f15c1eea0bd1fc6911a21693d38b8151d Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 10 Jul 2019 18:05:43 +0200 Subject: [PATCH] Fix reply of reply --- .../room/send/LocalEchoEventFactory.kt | 10 ++++-- .../internal/session/room/send/TextContent.kt | 33 +++++++++++++++++++ .../api/pushrules/PushrulesConditionTest.kt | 3 +- .../home/room/detail/RoomDetailFragment.kt | 7 ++-- .../riotx/features/html/EventHtmlRenderer.kt | 4 +++ 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt index cc449018..85776860 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt @@ -201,7 +201,8 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials type = MessageType.MSGTYPE_FILE, body = attachment.name ?: "file", info = FileInfo( - mimeType = attachment.mimeType.takeIf { it.isNotBlank() } ?: "application/octet-stream", + mimeType = attachment.mimeType.takeIf { it.isNotBlank() } + ?: "application/octet-stream", size = attachment.size ), url = attachment.path @@ -287,14 +288,17 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials MessageType.MSGTYPE_EMOTE, MessageType.MSGTYPE_TEXT, MessageType.MSGTYPE_NOTICE -> { - //If we already have formatted body, return it? var formattedText: String? = null if (content is MessageTextContent) { if (content.format == MessageType.FORMAT_MATRIX_HTML) { formattedText = content.formattedBody } } - return TextContent(content.body, formattedText) + val isReply = content.relatesTo?.inReplyTo?.eventId != null + return if (isReply) + TextContent(content.body, formattedText).removeInReplyFallbacks() + else + TextContent(content.body, formattedText) } MessageType.MSGTYPE_FILE -> return TextContent(stringProvider.getString(R.string.reply_to_a_file)) MessageType.MSGTYPE_AUDIO -> return TextContent(stringProvider.getString(R.string.reply_to_an_audio_file)) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt index f1f29c20..9e2c785c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt @@ -39,3 +39,36 @@ fun TextContent.toMessageTextContent(): MessageTextContent { formattedBody = formattedText ) } + +fun TextContent.removeInReplyFallbacks(): TextContent { + return copy( + text = extractUsefulTextFromReply(this.text), + formattedText = this.formattedText?.let { extractUsefulTextFromHtmlReply(it) } + ) +} + +private fun extractUsefulTextFromReply(repliedBody: String): String { + val lines = repliedBody.lines() + var wellFormed = repliedBody.startsWith(">") + var endOfPreviousFound = false + val usefullines = ArrayList() + lines.forEach { + if (it == "") { + endOfPreviousFound = true + return@forEach + } + if (!endOfPreviousFound) { + wellFormed = wellFormed && it.startsWith(">") + } else { + usefullines.add(it) + } + } + return usefullines.joinToString("\n").takeIf { wellFormed } ?: repliedBody +} + +private fun extractUsefulTextFromHtmlReply(repliedBody: String): String { + if (repliedBody.startsWith("")) { + return repliedBody.substring(repliedBody.lastIndexOf("") + "".length).trim() + } + return repliedBody +} diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt index 17faee35..ca3d9215 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt @@ -38,7 +38,6 @@ import org.junit.Test class PushrulesConditionTest { - @Test fun test_eventmatch_type_condition() { val condition = EventMatchCondition("type", "m.room.message") @@ -286,7 +285,7 @@ class PushrulesConditionTest { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun replyToMessage(eventReplied: Event, replyText: String): Cancelable? { + override fun replyToMessage(eventReplied: Event, replyText: String, autoMarkdown: Boolean): Cancelable? { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt index 62ffefea..e849925b 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt @@ -91,6 +91,7 @@ import im.vector.riotx.features.home.room.detail.timeline.action.MessageMenuView import im.vector.riotx.features.home.room.detail.timeline.action.ViewReactionBottomSheet import im.vector.riotx.features.home.room.detail.timeline.helper.EndlessRecyclerViewScrollListener import im.vector.riotx.features.home.room.detail.timeline.item.MessageInformationData +import im.vector.riotx.features.html.EventHtmlRenderer import im.vector.riotx.features.html.PillImageSpan import im.vector.riotx.features.invite.VectorInviteView import im.vector.riotx.features.media.ImageContentRenderer @@ -105,8 +106,6 @@ import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_room_detail.* import kotlinx.android.synthetic.main.merge_composer_layout.view.* import org.commonmark.parser.Parser -import ru.noties.markwon.Markwon -import ru.noties.markwon.html.HtmlPlugin import timber.log.Timber import java.io.File import javax.inject.Inject @@ -179,6 +178,7 @@ class RoomDetailFragment : @Inject lateinit var errorFormatter: ErrorFormatter private lateinit var scrollOnNewMessageCallback: ScrollOnNewMessageCallback private lateinit var scrollOnHighlightedEventCallback: ScrollOnHighlightedEventCallback + @Inject lateinit var eventHtmlRenderer: EventHtmlRenderer override fun getLayoutResId() = R.layout.fragment_room_detail @@ -259,8 +259,7 @@ class RoomDetailFragment : val parser = Parser.builder().build() val document = parser.parse(messageContent.formattedBody ?: messageContent.body) - formattedBody = Markwon.builder(requireContext()) - .usePlugin(HtmlPlugin.create()).build().render(document) + formattedBody = eventHtmlRenderer.render(document) } composerLayout.composerRelatedMessageContent.text = formattedBody ?: nonFormattedBody diff --git a/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt index 37cc0cc3..476a70e7 100644 --- a/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt @@ -50,6 +50,10 @@ class EventHtmlRenderer @Inject constructor(context: Context, return markwon.toMarkdown(text) } + fun render(node: Node) : CharSequence { + return markwon.render(node) + } + } private class MatrixPlugin private constructor(private val glideRequests: GlideRequests,