From cb844e4c1c28fe6613c848c08fd97a3afe48b368 Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski Date: Thu, 27 Mar 2025 21:46:36 +0100 Subject: [PATCH 1/3] Fix redacted messages not being correctly processed during sync --- .../client/room/TimelineEventHandler.kt | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt b/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt index 7c26c9077..7eb75b595 100644 --- a/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt +++ b/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt @@ -233,13 +233,14 @@ class TimelineEventHandlerImpl( when (this) { is MessageEvent -> { redactRelation(this) - val eventType = - api.eventContentSerializerMappings.message - .find { it.kClass.isInstance(content) }?.type - ?: "UNKNOWN" - val newContent = RedactedEventContent(eventType) + val redactedContent = content as? RedactedEventContent + ?: RedactedEventContent( + api.eventContentSerializerMappings.message + .find { it.kClass.isInstance(content) }?.type + ?: "UNKNOWN" + ) MessageEvent( - newContent, + redactedContent, id, sender, roomId, @@ -253,14 +254,15 @@ class TimelineEventHandlerImpl( is RoomEvent.StateEvent -> { // TODO should update state to last known (maybe not needed with sync v3) - val eventType = - api.eventContentSerializerMappings.state - .find { it.kClass.isInstance(content) }?.type - ?: "UNKNOWN" - val newContent = RedactedEventContent(eventType) + val redactedContent = content as? RedactedEventContent + ?: RedactedEventContent( + api.eventContentSerializerMappings.state + .find { it.kClass.isInstance(content) }?.type + ?: "UNKNOWN" + ) RoomEvent.StateEvent( // TODO should keep some fields and change state: https://spec.matrix.org/v1.10/rooms/v9/#redactions - newContent, + redactedContent, id, sender, roomId, -- GitLab From be20ef39e5150ae389bce9a4b0fd544c738e60da Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski Date: Thu, 27 Mar 2025 21:49:23 +0100 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81effa883..e1baf1fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fix redacted messages not being correctly processed during sync + ### Security ## 4.13.5 -- GitLab From 5ba4044c636235891ec30f29b94ee795fb67610f Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski Date: Thu, 27 Mar 2025 22:03:02 +0100 Subject: [PATCH 3/3] Add test for double redaction behaviour --- .../client/room/TimelineEventHandler.kt | 2 +- .../client/room/TimelineEventHandlerTest.kt | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt b/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt index 7eb75b595..80f17658d 100644 --- a/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt +++ b/trixnity-client/src/commonMain/kotlin/net/folivo/trixnity/client/room/TimelineEventHandler.kt @@ -352,7 +352,7 @@ class TimelineEventHandlerImpl( events?.distinctBy { it.id } ?.filter { get(it.id, it.roomId).first() == null } - private suspend fun RoomTimelineStore.addEventsToTimeline( + internal suspend fun RoomTimelineStore.addEventsToTimeline( startEvent: TimelineEvent, roomId: RoomId, previousToken: String?, diff --git a/trixnity-client/src/commonTest/kotlin/net/folivo/trixnity/client/room/TimelineEventHandlerTest.kt b/trixnity-client/src/commonTest/kotlin/net/folivo/trixnity/client/room/TimelineEventHandlerTest.kt index adf034428..04e5f519d 100644 --- a/trixnity-client/src/commonTest/kotlin/net/folivo/trixnity/client/room/TimelineEventHandlerTest.kt +++ b/trixnity-client/src/commonTest/kotlin/net/folivo/trixnity/client/room/TimelineEventHandlerTest.kt @@ -146,6 +146,68 @@ class TimelineEventHandlerTest : ShouldSpec({ nextEventId shouldBe event3.id } } + should("not redact room event twice") { + val messageEventId = EventId("\$message") + val redactionEventId = EventId("\$redact") + + val redactionEvent = MessageEvent( + content = RedactionEventContent(redacts = messageEventId), + id = redactionEventId, + sender = alice, + roomId = room, + originTimestamp = 2 + ) + + val messageEvent = MessageEvent( + content = RedactedEventContent("m.room.message"), + id = messageEventId, + sender = alice, + roomId = room, + originTimestamp = 1, + UnsignedRoomEventData.UnsignedMessageEventData( + redactedBecause = redactionEvent + ) + ) + + with(cut) { + val events = listOf( + messageEvent, + redactionEvent + ).handleRedactions() + roomTimelineStore.addEventsToTimeline( + startEvent = TimelineEvent( + event = events.first(), + previousEventId = null, + nextEventId = null, + gap = null + ), + roomId = room, + previousToken = null, + previousHasGap = true, + previousEvent = null, + previousEventChunk = null, + nextToken = "token", + nextHasGap = true, + nextEvent = null, + nextEventChunk = events.drop(1), + ) + } + assertSoftly(roomTimelineStore.get(messageEvent.id, room).first().shouldNotBeNull()) { + event shouldBe MessageEvent( + content = RedactedEventContent("m.room.message"), + id = EventId("\$message"), + sender = alice, + roomId = room, + originTimestamp = 1, + UnsignedRoomEventData.UnsignedMessageEventData( + redactedBecause = redactionEvent + ) + ) + content shouldBe Result.success(RedactedEventContent("m.room.message")) + roomId shouldBe room + eventId shouldBe messageEvent.id + } + } should("redact state event") { val event1 = nameEvent(1) val event2 = nameEvent(2) -- GitLab