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

Commit 4df10d3

Browse files
authored
Do not consider events by ignored users for relations (#12285)
Filter the events returned from `/relations` for the requester's ignored users in a similar way to `/messages` (and `/sync`).
1 parent 5436b01 commit 4df10d3

File tree

7 files changed

+90
-4
lines changed

7 files changed

+90
-4
lines changed

changelog.d/12227.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where events from ignored users were still considered for relations.

changelog.d/12227.misc

-1
This file was deleted.

changelog.d/12232.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where events from ignored users were still considered for relations.

changelog.d/12232.misc

-1
This file was deleted.

changelog.d/12285.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where events from ignored users were still considered for relations.

synapse/handlers/relations.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from synapse.api.errors import SynapseError
2222
from synapse.events import EventBase
2323
from synapse.types import JsonDict, Requester, StreamToken
24+
from synapse.visibility import filter_events_for_client
2425

2526
if TYPE_CHECKING:
2627
from synapse.server import HomeServer
@@ -62,6 +63,7 @@ def __bool__(self) -> bool:
6263
class RelationsHandler:
6364
def __init__(self, hs: "HomeServer"):
6465
self._main_store = hs.get_datastores().main
66+
self._storage = hs.get_storage()
6567
self._auth = hs.get_auth()
6668
self._clock = hs.get_clock()
6769
self._event_handler = hs.get_event_handler()
@@ -103,7 +105,8 @@ async def get_relations(
103105

104106
user_id = requester.user.to_string()
105107

106-
await self._auth.check_user_in_room_or_world_readable(
108+
# TODO Properly handle a user leaving a room.
109+
(_, member_event_id) = await self._auth.check_user_in_room_or_world_readable(
107110
room_id, user_id, allow_departed_users=True
108111
)
109112

@@ -130,6 +133,10 @@ async def get_relations(
130133
[c["event_id"] for c in pagination_chunk.chunk]
131134
)
132135

136+
events = await filter_events_for_client(
137+
self._storage, user_id, events, is_peeking=(member_event_id is None)
138+
)
139+
133140
now = self._clock.time_msec()
134141
# Do not bundle aggregations when retrieving the original event because
135142
# we want the content before relations are applied to it.

tests/rest/client/test_relations.py

+79-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from twisted.test.proto_helpers import MemoryReactor
2222

23-
from synapse.api.constants import EventTypes, RelationTypes
23+
from synapse.api.constants import AccountDataTypes, EventTypes, RelationTypes
2424
from synapse.rest import admin
2525
from synapse.rest.client import login, register, relations, room, sync
2626
from synapse.server import HomeServer
@@ -1324,6 +1324,84 @@ def test_bundled_aggregations_with_filter(self) -> None:
13241324
self.assertIn("m.relations", parent_event["unsigned"])
13251325

13261326

1327+
class RelationIgnoredUserTestCase(BaseRelationsTestCase):
1328+
"""Relations sent from an ignored user should be ignored."""
1329+
1330+
def _test_ignored_user(
1331+
self, allowed_event_ids: List[str], ignored_event_ids: List[str]
1332+
) -> None:
1333+
"""
1334+
Fetch the relations and ensure they're all there, then ignore user2, and
1335+
repeat.
1336+
"""
1337+
# Get the relations.
1338+
event_ids = self._get_related_events()
1339+
self.assertCountEqual(event_ids, allowed_event_ids + ignored_event_ids)
1340+
1341+
# Ignore user2 and re-do the requests.
1342+
self.get_success(
1343+
self.store.add_account_data_for_user(
1344+
self.user_id,
1345+
AccountDataTypes.IGNORED_USER_LIST,
1346+
{"ignored_users": {self.user2_id: {}}},
1347+
)
1348+
)
1349+
1350+
# Get the relations.
1351+
event_ids = self._get_related_events()
1352+
self.assertCountEqual(event_ids, allowed_event_ids)
1353+
1354+
def test_annotation(self) -> None:
1355+
"""Annotations should ignore"""
1356+
# Send 2 from us, 2 from the to be ignored user.
1357+
allowed_event_ids = []
1358+
ignored_event_ids = []
1359+
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
1360+
allowed_event_ids.append(channel.json_body["event_id"])
1361+
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="b")
1362+
allowed_event_ids.append(channel.json_body["event_id"])
1363+
channel = self._send_relation(
1364+
RelationTypes.ANNOTATION,
1365+
"m.reaction",
1366+
key="a",
1367+
access_token=self.user2_token,
1368+
)
1369+
ignored_event_ids.append(channel.json_body["event_id"])
1370+
channel = self._send_relation(
1371+
RelationTypes.ANNOTATION,
1372+
"m.reaction",
1373+
key="c",
1374+
access_token=self.user2_token,
1375+
)
1376+
ignored_event_ids.append(channel.json_body["event_id"])
1377+
1378+
self._test_ignored_user(allowed_event_ids, ignored_event_ids)
1379+
1380+
def test_reference(self) -> None:
1381+
"""Annotations should ignore"""
1382+
channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
1383+
allowed_event_ids = [channel.json_body["event_id"]]
1384+
1385+
channel = self._send_relation(
1386+
RelationTypes.REFERENCE, "m.room.test", access_token=self.user2_token
1387+
)
1388+
ignored_event_ids = [channel.json_body["event_id"]]
1389+
1390+
self._test_ignored_user(allowed_event_ids, ignored_event_ids)
1391+
1392+
def test_thread(self) -> None:
1393+
"""Annotations should ignore"""
1394+
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
1395+
allowed_event_ids = [channel.json_body["event_id"]]
1396+
1397+
channel = self._send_relation(
1398+
RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
1399+
)
1400+
ignored_event_ids = [channel.json_body["event_id"]]
1401+
1402+
self._test_ignored_user(allowed_event_ids, ignored_event_ids)
1403+
1404+
13271405
class RelationRedactionTestCase(BaseRelationsTestCase):
13281406
"""
13291407
Test the behaviour of relations when the parent or child event is redacted.

0 commit comments

Comments
 (0)