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

Commit 4b3aebe

Browse files
committed
Include aggregations in results.
1 parent bbd10e3 commit 4b3aebe

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

synapse/config/experimental.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def read_config(self, config: JsonDict, **kwargs):
2828
self.msc1849_enabled = config.get("experimental_msc1849_support_enabled", True)
2929
# MSC3440 (thread relation)
3030
self.msc3440_enabled: bool = experimental.get("msc3440_enabled", False)
31+
# MSC3666: including bundled relations in /search.
32+
self.msc3666_enabled: bool = experimental.get("msc3666_enabled", False)
3133

3234
# MSC3026 (busy presence state)
3335
self.msc3026_enabled: bool = experimental.get("msc3026_enabled", False)

synapse/handlers/search.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def __init__(self, hs: "HomeServer"):
4343
self.state_store = self.storage.state
4444
self.auth = hs.get_auth()
4545

46+
self._msc3666_enabled = hs.config.experimental.msc3666_enabled
47+
4648
async def get_old_rooms_from_upgraded_room(self, room_id: str) -> Iterable[str]:
4749
"""Retrieves room IDs of old rooms in the history of an upgraded room.
4850
@@ -418,12 +420,29 @@ async def search(
418420

419421
time_now = self.clock.time_msec()
420422

423+
aggregations = None
424+
if self._msc3666_enabled:
425+
aggregations = await self.store.get_bundled_aggregations(
426+
# Generate an iterable of EventBase for all the events that will be
427+
# returned, including contextual events.
428+
itertools.chain(
429+
# The events_before and events_after for each context.
430+
itertools.chain.from_iterable(
431+
itertools.chain(context["events_before"], context["events_after"]) # type: ignore[arg-type]
432+
for context in contexts.values()
433+
),
434+
# The returned events.
435+
allowed_events,
436+
),
437+
user.to_string(),
438+
)
439+
421440
for context in contexts.values():
422441
context["events_before"] = self._event_serializer.serialize_events(
423-
context["events_before"], time_now # type: ignore[arg-type]
442+
context["events_before"], time_now, bundle_aggregations=aggregations # type: ignore[arg-type]
424443
)
425444
context["events_after"] = self._event_serializer.serialize_events(
426-
context["events_after"], time_now # type: ignore[arg-type]
445+
context["events_after"], time_now, bundle_aggregations=aggregations # type: ignore[arg-type]
427446
)
428447

429448
state_results = {}
@@ -440,7 +459,9 @@ async def search(
440459
results.append(
441460
{
442461
"rank": rank_map[e.event_id],
443-
"result": self._event_serializer.serialize_event(e, time_now),
462+
"result": self._event_serializer.serialize_event(
463+
e, time_now, bundle_aggregations=aggregations
464+
),
444465
"context": contexts.get(e.event_id, {}),
445466
}
446467
)

tests/rest/client/test_relations.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ def test_aggregation_must_be_annotation(self):
453453
)
454454
self.assertEquals(400, channel.code, channel.json_body)
455455

456-
@unittest.override_config({"experimental_features": {"msc3440_enabled": True}})
456+
@unittest.override_config(
457+
{"experimental_features": {"msc3440_enabled": True, "msc3666_enabled": True}}
458+
)
457459
def test_bundled_aggregations(self):
458460
"""
459461
Test that annotations, references, and threads get correctly bundled.
@@ -579,6 +581,23 @@ def assert_bundle(event_json: JsonDict) -> None:
579581
self.assertTrue(room_timeline["limited"])
580582
assert_bundle(self._find_event_in_chunk(room_timeline["events"]))
581583

584+
# Request search.
585+
channel = self.make_request(
586+
"POST",
587+
"/search",
588+
# Search term matches the parent message.
589+
content={"search_categories": {"room_events": {"search_term": "Hi"}}},
590+
access_token=self.user_token,
591+
)
592+
self.assertEquals(200, channel.code, channel.json_body)
593+
chunk = [
594+
result["result"]
595+
for result in channel.json_body["search_categories"]["room_events"][
596+
"results"
597+
]
598+
]
599+
assert_bundle(self._find_event_in_chunk(chunk))
600+
582601
def test_aggregation_get_event_for_annotation(self):
583602
"""Test that annotations do not get bundled aggregations included
584603
when directly requested.
@@ -759,6 +778,7 @@ def test_ignore_invalid_room(self):
759778
self.assertEquals(200, channel.code, channel.json_body)
760779
self.assertNotIn("m.relations", channel.json_body["unsigned"])
761780

781+
@unittest.override_config({"experimental_features": {"msc3666_enabled": True}})
762782
def test_edit(self):
763783
"""Test that a simple edit works."""
764784

@@ -825,6 +845,23 @@ def assert_bundle(event_json: JsonDict) -> None:
825845
self.assertTrue(room_timeline["limited"])
826846
assert_bundle(self._find_event_in_chunk(room_timeline["events"]))
827847

848+
# Request search.
849+
channel = self.make_request(
850+
"POST",
851+
"/search",
852+
# Search term matches the parent message.
853+
content={"search_categories": {"room_events": {"search_term": "Hi"}}},
854+
access_token=self.user_token,
855+
)
856+
self.assertEquals(200, channel.code, channel.json_body)
857+
chunk = [
858+
result["result"]
859+
for result in channel.json_body["search_categories"]["room_events"][
860+
"results"
861+
]
862+
]
863+
assert_bundle(self._find_event_in_chunk(chunk))
864+
828865
def test_multi_edit(self):
829866
"""Test that multiple edits, including attempts by people who
830867
shouldn't be allowed, are correctly handled.

0 commit comments

Comments
 (0)