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