removing previous tag stripping

This commit is contained in:
Adam Brown 2022-10-24 22:30:22 +01:00
parent de8c476ed5
commit 87f25e6e66
5 changed files with 20 additions and 41 deletions

View File

@ -23,7 +23,7 @@ fun aRoomOverview(
fun anEncryptedRoomMessageEvent(
eventId: EventId = anEventId(),
utcTimestamp: Long = 0L,
content: String = "encrypted-content",
content: RichText = RichText.of("encrypted-content"),
author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer,
edited: Boolean = false,
@ -47,7 +47,7 @@ fun aRoomReplyMessageEvent(
fun aRoomMessageEvent(
eventId: EventId = anEventId(),
utcTimestamp: Long = 0L,
content: String = "message-content",
content: RichText = RichText.of("message-content"),
author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer,
edited: Boolean = false,

View File

@ -7,8 +7,17 @@ data class RichText(val parts: Set<Part>) {
data class Bold(val content: String) : Part
data class Italic(val content: String) : Part
data class BoldItalic(val content: String) : Part
data class Person(val userId: String) : Part
data class Person(val displayName: String) : Part
}
}
fun RichText.asString() = parts.joinToString(separator = "")
fun RichText.asString() = parts.joinToString(separator = "") {
when(it) {
is RichText.Part.Bold -> it.content
is RichText.Part.BoldItalic -> it.content
is RichText.Part.Italic -> it.content
is RichText.Part.Link -> it.label
is RichText.Part.Normal -> it.content
is RichText.Part.Person -> it.displayName
}
}

View File

@ -280,7 +280,7 @@ fun RichText.toAnnotatedText() = buildAnnotatedString {
is RichText.Part.Normal -> append(it.content)
is RichText.Part.Person -> withStyle(nameStyle) {
append("@${it.userId.substringBefore(':').removePrefix("@")}")
append("@${it.displayName.substringBefore(':').removePrefix("@")}")
}
}
}

View File

@ -156,13 +156,17 @@ internal class TimelineEventMapper(
private suspend fun RoomEventFactory.mapToRoomEvent(source: ApiTimelineEvent.TimelineMessage): RoomEvent {
return when (source.content) {
is ApiTimelineEvent.TimelineMessage.Content.Image -> source.toImageMessage(userCredentials, roomId)
is ApiTimelineEvent.TimelineMessage.Content.Text -> source.toTextMessage(roomId, content = source.asTextContent().formattedBody ?: source.content.body ?: "")
is ApiTimelineEvent.TimelineMessage.Content.Text -> source.toTextMessage(
roomId,
content = source.asTextContent().formattedBody ?: source.content.body ?: ""
)
ApiTimelineEvent.TimelineMessage.Content.Ignored -> throw IllegalStateException()
}
}
private suspend fun ApiTimelineEvent.TimelineMessage.toTextMessage(
content: String = this.asTextContent().formattedBody?.stripTags() ?: this.asTextContent().body ?: "redacted",
content: String = this.asTextContent().formattedBody ?: this.asTextContent().body ?: "redacted",
edited: Boolean = false,
utcTimestamp: Long = this.utcTimestamp,
) = with(roomEventFactory) { toTextMessage(roomId, content, edited, utcTimestamp) }

View File

@ -53,37 +53,3 @@ internal class RoomEventFactory(
)
}
}
private fun String.indexOfOrNull(string: String) = this.indexOf(string).takeIf { it != -1 }
fun String.stripTags() = this
.run {
this.indexOfOrNull("</mx-reply>")?.let {
this.substring(it + "</mx-reply>".length)
} ?: this
}
.trim()
.replaceLinks()
.removeTag("p")
.removeTag("em")
.removeTag("strong")
.removeTag("code")
.removeTag("pre")
.replace("&quot;", "\"")
.replace("&#39;", "'")
.replace("<br />", "\n")
.replace("<br/>", "\n")
private fun String.removeTag(name: String) = this.replace("<$name>", "").replace("/$name>", "")
private fun String.replaceLinks(): String {
return this.indexOfOrNull("<a href=")?.let { start ->
val openTagClose = indexOfOrNull("\">")!!
val end = indexOfOrNull("</a>")!!
val content = this.substring(openTagClose + "\">".length, end)
this.replaceRange(start, end + "</a>".length, content)
} ?: this
}
private fun ApiTimelineEvent.TimelineMessage.asTextContent() = this.content as ApiTimelineEvent.TimelineMessage.Content.Text