Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7687739

Browse files
committed
For backwards compat, populate the top-level redacts field.
1 parent 0d6714d commit 7687739

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

synapse/events/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,15 @@ def serialize_event(
473473
if config.as_client_event:
474474
d = config.event_format(d)
475475

476+
# If the event is a redaction, copy the redacts field from the content to
477+
# top-level for backwards compatibility.
478+
if (
479+
e.type == EventTypes.Redaction
480+
and e.room_version.updated_redaction_rules
481+
and e.redacts is not None
482+
):
483+
d["redacts"] = e.redacts
484+
476485
only_event_fields = config.only_event_fields
477486
if only_event_fields:
478487
if not isinstance(only_event_fields, list) or not all(

tests/rest/client/test_redactions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from synapse.rest import admin
2121
from synapse.rest.client import login, room, sync
2222
from synapse.server import HomeServer
23+
from synapse.storage._base import db_to_json
24+
from synapse.storage.database import LoggingTransaction
2325
from synapse.types import JsonDict
2426
from synapse.util import Clock
2527

@@ -597,5 +599,20 @@ def test_content_redaction(self) -> None:
597599
redact_event = timeline[-1]
598600
self.assertEqual(redact_event["type"], EventTypes.Redaction)
599601
# The redacts key should be in the content.
600-
self.assertNotIn("redacts", redact_event)
601602
self.assertEquals(redact_event["content"]["redacts"], event_id)
603+
604+
# It should also be copied as the top-level redacts field for backwards
605+
# compatibility.
606+
self.assertEquals(redact_event["redacts"], event_id)
607+
608+
# But it isn't actually part of the event.
609+
def get_event(txn: LoggingTransaction) -> JsonDict:
610+
return db_to_json(
611+
main_datastore._fetch_event_rows(txn, [event_id])[event_id].json
612+
)
613+
614+
main_datastore = self.hs.get_datastores().main
615+
event_json = self.get_success(
616+
main_datastore.db_pool.runInteraction("get_event", get_event)
617+
)
618+
self.assertNotIn("redacts", event_json)

0 commit comments

Comments
 (0)