handling edits with rich text (kind of~)

This commit is contained in:
Adam Brown 2022-10-24 22:47:35 +01:00
parent 87f25e6e66
commit e79c5c9a27
3 changed files with 12 additions and 6 deletions

View File

@ -58,7 +58,7 @@ internal class DefaultSyncService(
roomMembersService, roomMembersService,
roomDataSource, roomDataSource,
TimelineEventsProcessor( TimelineEventsProcessor(
RoomEventCreator(roomMembersService, errorTracker, RoomEventFactory(roomMembersService, richMessageParser)), RoomEventCreator(roomMembersService, errorTracker, RoomEventFactory(roomMembersService, richMessageParser), richMessageParser),
roomEventsDecrypter, roomEventsDecrypter,
eventDecrypter, eventDecrypter,
EventLookupUseCase(roomStore) EventLookupUseCase(roomStore)

View File

@ -19,6 +19,7 @@ internal class RoomEventCreator(
private val roomMembersService: RoomMembersService, private val roomMembersService: RoomMembersService,
private val errorTracker: ErrorTracker, private val errorTracker: ErrorTracker,
private val roomEventFactory: RoomEventFactory, private val roomEventFactory: RoomEventFactory,
private val richMessageParser: RichMessageParser,
) { ) {
suspend fun ApiTimelineEvent.Encrypted.toRoomEvent(roomId: RoomId): RoomEvent? { 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? { 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 userCredentials: UserCredentials,
private val roomId: RoomId, private val roomId: RoomId,
private val roomEventFactory: RoomEventFactory, private val roomEventFactory: RoomEventFactory,
private val richMessageParser: RichMessageParser,
) { ) {
suspend fun mapToRoomEvent(event: ApiTimelineEvent.TimelineMessage, lookup: Lookup): RoomEvent? { suspend fun mapToRoomEvent(event: ApiTimelineEvent.TimelineMessage, lookup: Lookup): RoomEvent? {
@ -138,7 +140,7 @@ internal class TimelineEventMapper(
is ApiTimelineEvent.TimelineMessage.Content.Text -> original.toTextMessage( is ApiTimelineEvent.TimelineMessage.Content.Text -> original.toTextMessage(
utcTimestamp = incomingEdit.utcTimestamp, utcTimestamp = incomingEdit.utcTimestamp,
content = incomingEdit.asTextContent().body?.removePrefix(" * ")?.trim() ?: "redacted", content = incomingEdit.asTextContent().let { it.formattedBody ?: it.body }?.removePrefix(" * ") ?: "redacted",
edited = true, edited = true,
) )
@ -147,8 +149,8 @@ internal class TimelineEventMapper(
} }
} }
// TODO handle edits
private fun RoomEvent.Message.edited(edit: ApiTimelineEvent.TimelineMessage) = this.copy( 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, utcTimestamp = edit.utcTimestamp,
edited = true, edited = true,
) )
@ -158,7 +160,7 @@ internal class TimelineEventMapper(
is ApiTimelineEvent.TimelineMessage.Content.Image -> source.toImageMessage(userCredentials, roomId) is ApiTimelineEvent.TimelineMessage.Content.Image -> source.toImageMessage(userCredentials, roomId)
is ApiTimelineEvent.TimelineMessage.Content.Text -> source.toTextMessage( is ApiTimelineEvent.TimelineMessage.Content.Text -> source.toTextMessage(
roomId, roomId,
content = source.asTextContent().formattedBody ?: source.content.body ?: "" content = source.asTextContent().formattedBody ?: source.content.body ?: "redacted"
) )
ApiTimelineEvent.TimelineMessage.Content.Ignored -> throw IllegalStateException() ApiTimelineEvent.TimelineMessage.Content.Ignored -> throw IllegalStateException()

View File

@ -33,7 +33,11 @@ internal class RoomEventCreatorTest {
private val fakeRoomMembersService = FakeRoomMembersService() 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 @Test
fun `given Megolm encrypted event then maps to encrypted room message`() = runTest { fun `given Megolm encrypted event then maps to encrypted room message`() = runTest {