Fix reply of reply

This commit is contained in:
Valere 2019-07-10 18:05:43 +02:00
parent 9a57a02996
commit 3aa30e5f15
5 changed files with 48 additions and 9 deletions

View File

@ -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))

View File

@ -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<String>()
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("<mx-reply>")) {
return repliedBody.substring(repliedBody.lastIndexOf("</mx-reply>") + "</mx-reply>".length).trim()
}
return repliedBody
}

View File

@ -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.
}


View File

@ -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

View File

@ -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,