diff --git a/changelog.d/19782.misc b/changelog.d/19782.misc new file mode 100644 index 00000000000..7f3e374278a --- /dev/null +++ b/changelog.d/19782.misc @@ -0,0 +1 @@ +Fix [MSC3912](https://github.com/matrix-org/matrix-spec-proposals/pull/3912) (relation based redaction) for room versions greater than 10. Contributed by @famedly. diff --git a/synapse/handlers/relations.py b/synapse/handlers/relations.py index ee4f8d672ee..593ea10b44b 100644 --- a/synapse/handlers/relations.py +++ b/synapse/handlers/relations.py @@ -38,6 +38,7 @@ from synapse.logging.opentracing import trace from synapse.storage.databases.main.relations import ThreadsNextBatch, _RelatedEvent from synapse.streams.config import PaginationConfig +from synapse.synapse_rust.room_versions import RoomVersion from synapse.types import JsonDict, Requester, UserID from synapse.util.async_helpers import gather_results from synapse.visibility import filter_and_transform_events_for_client @@ -211,6 +212,7 @@ async def redact_events_related_to( event_id: str, initial_redaction_event: EventBase, relation_types: list[str], + room_version: RoomVersion, ) -> None: """Redacts all events related to the given event ID with one of the given relation types. @@ -228,6 +230,8 @@ async def redact_events_related_to( event_id. relation_types: The types of relations to look for. If "*" is in the list, all related events will be redacted regardless of the type. + room_version: The RoomVersion of the room, for deciding where the 'redacts' + key should go in the event dict Raises: ShadowBanError if the requester is shadow-banned @@ -244,16 +248,24 @@ async def redact_events_related_to( ) for related_event_id in related_event_ids: + # Depending on the room version involved, the "redacts" key can go in one of + # two places. If we only use what was provided in the initial event, it will + # only target an event that was already redacted and nothing will happen. + new_redaction_content = dict(initial_redaction_event.content) + event_dict: JsonDict = { + "type": EventTypes.Redaction, + "content": new_redaction_content, + "room_id": initial_redaction_event.room_id, + "sender": requester.user.to_string(), + } + if room_version.updated_redaction_rules: + event_dict["content"].update({"redacts": related_event_id}) + else: + event_dict["redacts"] = related_event_id try: await self._event_creation_handler.create_and_send_nonmember_event( requester, - { - "type": EventTypes.Redaction, - "content": initial_redaction_event.content, - "room_id": initial_redaction_event.room_id, - "sender": requester.user.to_string(), - "redacts": related_event_id, - }, + event_dict, ratelimit=False, ) except SynapseError as e: diff --git a/synapse/rest/client/room.py b/synapse/rest/client/room.py index 48ef42c7d6f..2a9613a7e03 100644 --- a/synapse/rest/client/room.py +++ b/synapse/rest/client/room.py @@ -1438,6 +1438,7 @@ async def _do( event_id=event_id, initial_redaction_event=event, relation_types=with_relations, + room_version=room_version, ) event_id = event.event_id