From e79c5c9a27282b2c0cf7af6088c937b55f6de97e Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 24 Oct 2022 22:47:35 +0100 Subject: [PATCH] handling edits with rich text (kind of~) --- .../dapk/st/matrix/sync/internal/DefaultSyncService.kt | 2 +- .../st/matrix/sync/internal/sync/RoomEventCreator.kt | 10 ++++++---- .../matrix/sync/internal/sync/RoomEventCreatorTest.kt | 6 +++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt index bb446d1..907388c 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/DefaultSyncService.kt @@ -58,7 +58,7 @@ internal class DefaultSyncService( roomMembersService, roomDataSource, TimelineEventsProcessor( - RoomEventCreator(roomMembersService, errorTracker, RoomEventFactory(roomMembersService, richMessageParser)), + RoomEventCreator(roomMembersService, errorTracker, RoomEventFactory(roomMembersService, richMessageParser), richMessageParser), roomEventsDecrypter, eventDecrypter, EventLookupUseCase(roomStore) 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 e6ddb29..72d9a46 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 @@ -19,6 +19,7 @@ internal class RoomEventCreator( private val roomMembersService: RoomMembersService, private val errorTracker: ErrorTracker, private val roomEventFactory: RoomEventFactory, + private val richMessageParser: RichMessageParser, ) { suspend fun ApiTimelineEvent.Encrypted.toRoomEvent(roomId: RoomId): RoomEvent? { @@ -44,7 +45,7 @@ internal class RoomEventCreator( } suspend fun ApiTimelineEvent.TimelineMessage.toRoomEvent(userCredentials: UserCredentials, roomId: RoomId, lookup: Lookup): RoomEvent? { - return TimelineEventMapper(userCredentials, roomId, roomEventFactory).mapToRoomEvent(this, lookup) + return TimelineEventMapper(userCredentials, roomId, roomEventFactory, richMessageParser).mapToRoomEvent(this, lookup) } } @@ -52,6 +53,7 @@ internal class TimelineEventMapper( private val userCredentials: UserCredentials, private val roomId: RoomId, private val roomEventFactory: RoomEventFactory, + private val richMessageParser: RichMessageParser, ) { suspend fun mapToRoomEvent(event: ApiTimelineEvent.TimelineMessage, lookup: Lookup): RoomEvent? { @@ -138,7 +140,7 @@ internal class TimelineEventMapper( is ApiTimelineEvent.TimelineMessage.Content.Text -> original.toTextMessage( utcTimestamp = incomingEdit.utcTimestamp, - content = incomingEdit.asTextContent().body?.removePrefix(" * ")?.trim() ?: "redacted", + content = incomingEdit.asTextContent().let { it.formattedBody ?: it.body }?.removePrefix(" * ") ?: "redacted", edited = true, ) @@ -147,8 +149,8 @@ internal class TimelineEventMapper( } } - // TODO handle edits private fun RoomEvent.Message.edited(edit: ApiTimelineEvent.TimelineMessage) = this.copy( + content = richMessageParser.parse(edit.asTextContent().let { it.formattedBody ?: it.body }?.removePrefix(" * ") ?: "redacted"), utcTimestamp = edit.utcTimestamp, edited = true, ) @@ -158,7 +160,7 @@ internal class TimelineEventMapper( is ApiTimelineEvent.TimelineMessage.Content.Image -> source.toImageMessage(userCredentials, roomId) is ApiTimelineEvent.TimelineMessage.Content.Text -> source.toTextMessage( roomId, - content = source.asTextContent().formattedBody ?: source.content.body ?: "" + content = source.asTextContent().formattedBody ?: source.content.body ?: "redacted" ) ApiTimelineEvent.TimelineMessage.Content.Ignored -> throw IllegalStateException() diff --git a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt index 7328471..a2d5cc7 100644 --- a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt +++ b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventCreatorTest.kt @@ -33,7 +33,11 @@ internal class RoomEventCreatorTest { private val fakeRoomMembersService = FakeRoomMembersService() - private val roomEventCreator = RoomEventCreator(fakeRoomMembersService, FakeErrorTracker(), RoomEventFactory(fakeRoomMembersService, RichMessageParser())) + private val richMessageParser = RichMessageParser() + private val roomEventCreator = RoomEventCreator( + fakeRoomMembersService, FakeErrorTracker(), RoomEventFactory(fakeRoomMembersService, richMessageParser), + richMessageParser + ) @Test fun `given Megolm encrypted event then maps to encrypted room message`() = runTest {