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

Commit e0f2429

Browse files
author
Mathieu Velten
authored
Add a catch-all * to the supported relation types when redacting (#15705)
This is an update to MSC3912 implementation
1 parent 30a5076 commit e0f2429

File tree

4 files changed

+143
-8
lines changed

4 files changed

+143
-8
lines changed

changelog.d/15705.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a catch-all * to the supported relation types when redacting an event and its related events. This is an update to [MSC3912](https://github.com/matrix-org/matrix-spec-proposals/pull/3861) implementation.

synapse/handlers/relations.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,22 @@ async def redact_events_related_to(
205205
event_id: The event IDs to look and redact relations of.
206206
initial_redaction_event: The redaction for the event referred to by
207207
event_id.
208-
relation_types: The types of relations to look for.
208+
relation_types: The types of relations to look for. If "*" is in the list,
209+
all related events will be redacted regardless of the type.
209210
210211
Raises:
211212
ShadowBanError if the requester is shadow-banned
212213
"""
213-
related_event_ids = (
214-
await self._main_store.get_all_relations_for_event_with_types(
215-
event_id, relation_types
214+
if "*" in relation_types:
215+
related_event_ids = await self._main_store.get_all_relations_for_event(
216+
event_id
217+
)
218+
else:
219+
related_event_ids = (
220+
await self._main_store.get_all_relations_for_event_with_types(
221+
event_id, relation_types
222+
)
216223
)
217-
)
218224

219225
for related_event_id in related_event_ids:
220226
try:

synapse/storage/databases/main/relations.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,36 @@ def get_all_relation_ids_for_event_with_types_txn(
365365
func=get_all_relation_ids_for_event_with_types_txn,
366366
)
367367

368+
async def get_all_relations_for_event(
369+
self,
370+
event_id: str,
371+
) -> List[str]:
372+
"""Get the event IDs of all events that have a relation to the given event.
373+
374+
Args:
375+
event_id: The event for which to look for related events.
376+
377+
Returns:
378+
A list of the IDs of the events that relate to the given event.
379+
"""
380+
381+
def get_all_relation_ids_for_event_txn(
382+
txn: LoggingTransaction,
383+
) -> List[str]:
384+
rows = self.db_pool.simple_select_list_txn(
385+
txn=txn,
386+
table="event_relations",
387+
keyvalues={"relates_to_id": event_id},
388+
retcols=["event_id"],
389+
)
390+
391+
return [row["event_id"] for row in rows]
392+
393+
return await self.db_pool.runInteraction(
394+
desc="get_all_relation_ids_for_event",
395+
func=get_all_relation_ids_for_event_txn,
396+
)
397+
368398
async def event_includes_relation(self, event_id: str) -> bool:
369399
"""Check if the given event relates to another event.
370400

tests/rest/client/test_redactions.py

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ def test_redact_event_as_moderator_ratelimit(self) -> None:
217217
self._redact_event(self.mod_access_token, self.room_id, msg_id)
218218

219219
@override_config({"experimental_features": {"msc3912_enabled": True}})
220-
def test_redact_relations(self) -> None:
221-
"""Tests that we can redact the relations of an event at the same time as the
222-
event itself.
220+
def test_redact_relations_with_types(self) -> None:
221+
"""Tests that we can redact the relations of an event of specific types
222+
at the same time as the event itself.
223223
"""
224224
# Send a root event.
225225
res = self.helper.send_event(
@@ -317,6 +317,104 @@ def test_redact_relations(self) -> None:
317317
)
318318
self.assertNotIn("redacted_because", event_dict, event_dict)
319319

320+
@override_config({"experimental_features": {"msc3912_enabled": True}})
321+
def test_redact_all_relations(self) -> None:
322+
"""Tests that we can redact all the relations of an event at the same time as the
323+
event itself.
324+
"""
325+
# Send a root event.
326+
res = self.helper.send_event(
327+
room_id=self.room_id,
328+
type=EventTypes.Message,
329+
content={"msgtype": "m.text", "body": "hello"},
330+
tok=self.mod_access_token,
331+
)
332+
root_event_id = res["event_id"]
333+
334+
# Send an edit to this root event.
335+
res = self.helper.send_event(
336+
room_id=self.room_id,
337+
type=EventTypes.Message,
338+
content={
339+
"body": " * hello world",
340+
"m.new_content": {
341+
"body": "hello world",
342+
"msgtype": "m.text",
343+
},
344+
"m.relates_to": {
345+
"event_id": root_event_id,
346+
"rel_type": RelationTypes.REPLACE,
347+
},
348+
"msgtype": "m.text",
349+
},
350+
tok=self.mod_access_token,
351+
)
352+
edit_event_id = res["event_id"]
353+
354+
# Also send a threaded message whose root is the same as the edit's.
355+
res = self.helper.send_event(
356+
room_id=self.room_id,
357+
type=EventTypes.Message,
358+
content={
359+
"msgtype": "m.text",
360+
"body": "message 1",
361+
"m.relates_to": {
362+
"event_id": root_event_id,
363+
"rel_type": RelationTypes.THREAD,
364+
},
365+
},
366+
tok=self.mod_access_token,
367+
)
368+
threaded_event_id = res["event_id"]
369+
370+
# Also send a reaction, again with the same root.
371+
res = self.helper.send_event(
372+
room_id=self.room_id,
373+
type=EventTypes.Reaction,
374+
content={
375+
"m.relates_to": {
376+
"rel_type": RelationTypes.ANNOTATION,
377+
"event_id": root_event_id,
378+
"key": "👍",
379+
}
380+
},
381+
tok=self.mod_access_token,
382+
)
383+
reaction_event_id = res["event_id"]
384+
385+
# Redact the root event, specifying that we also want to delete all events that
386+
# relate to it.
387+
self._redact_event(
388+
self.mod_access_token,
389+
self.room_id,
390+
root_event_id,
391+
with_relations=["*"],
392+
)
393+
394+
# Check that the root event got redacted.
395+
event_dict = self.helper.get_event(
396+
self.room_id, root_event_id, self.mod_access_token
397+
)
398+
self.assertIn("redacted_because", event_dict, event_dict)
399+
400+
# Check that the edit got redacted.
401+
event_dict = self.helper.get_event(
402+
self.room_id, edit_event_id, self.mod_access_token
403+
)
404+
self.assertIn("redacted_because", event_dict, event_dict)
405+
406+
# Check that the threaded message got redacted.
407+
event_dict = self.helper.get_event(
408+
self.room_id, threaded_event_id, self.mod_access_token
409+
)
410+
self.assertIn("redacted_because", event_dict, event_dict)
411+
412+
# Check that the reaction got redacted.
413+
event_dict = self.helper.get_event(
414+
self.room_id, reaction_event_id, self.mod_access_token
415+
)
416+
self.assertIn("redacted_because", event_dict, event_dict)
417+
320418
@override_config({"experimental_features": {"msc3912_enabled": True}})
321419
def test_redact_relations_no_perms(self) -> None:
322420
"""Tests that, when redacting a message along with its relations, if not all

0 commit comments

Comments
 (0)