diff --git a/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt b/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt index 68f252b..b3c903f 100644 --- a/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt +++ b/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt @@ -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, diff --git a/core/src/main/kotlin/app/dapk/st/core/RichText.kt b/core/src/main/kotlin/app/dapk/st/core/RichText.kt index 8cff054..18206d0 100644 --- a/core/src/main/kotlin/app/dapk/st/core/RichText.kt +++ b/core/src/main/kotlin/app/dapk/st/core/RichText.kt @@ -7,8 +7,17 @@ data class RichText(val parts: Set) { 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 = "") \ No newline at end of file +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 + } +} \ No newline at end of file diff --git a/design-library/src/main/kotlin/app/dapk/st/design/components/Bubble.kt b/design-library/src/main/kotlin/app/dapk/st/design/components/Bubble.kt index 91506a4..3a8c5e7 100644 --- a/design-library/src/main/kotlin/app/dapk/st/design/components/Bubble.kt +++ b/design-library/src/main/kotlin/app/dapk/st/design/components/Bubble.kt @@ -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("@")}") } } } diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt index 5c7b5d2..e6ddb29 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreator.kt @@ -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) } diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt index bfae621..c8b1f10 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt @@ -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("")?.let { - this.substring(it + "".length) - } ?: this - } - .trim() - .replaceLinks() - .removeTag("p") - .removeTag("em") - .removeTag("strong") - .removeTag("code") - .removeTag("pre") - .replace(""", "\"") - .replace("'", "'") - .replace("
", "\n") - .replace("
", "\n") - -private fun String.removeTag(name: String) = this.replace("<$name>", "").replace("/$name>", "") - -private fun String.replaceLinks(): String { - return this.indexOfOrNull("")!! - val end = indexOfOrNull("")!! - val content = this.substring(openTagClose + "\">".length, end) - this.replaceRange(start, end + "".length, content) - } ?: this -} - -private fun ApiTimelineEvent.TimelineMessage.asTextContent() = this.content as ApiTimelineEvent.TimelineMessage.Content.Text