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

Commit 37d8462

Browse files
committed
Avoid unnecessary database access.
1 parent eaa7916 commit 37d8462

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

synapse/handlers/relations.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,28 @@ async def _get_threads_for_events(
273273
# Fetch thread summaries.
274274
summaries = await self._main_store.get_thread_summaries(event_ids)
275275

276-
# Only fetch participated for a limited selection based on what had
276+
# Only fetch whether the current user has participated in a thread based
277+
# on which returned for a limited selection based on what had
277278
# summaries.
278279
thread_event_ids = [
279280
event_id for event_id, summary in summaries.items() if summary
280281
]
281-
participated = await self._main_store.get_threads_participated(
282-
thread_event_ids, user_id
282+
283+
# Preseed thread participation with whether the requester send the event.
284+
participated = {
285+
event_id: events_by_id[event_id].sender == user_id
286+
for event_id in thread_event_ids
287+
}
288+
# Check other events against the database.
289+
participated.update(
290+
await self._main_store.get_threads_participated(
291+
[
292+
event_id
293+
for event_id in thread_event_ids
294+
if not participated[event_id]
295+
],
296+
user_id,
297+
)
283298
)
284299

285300
# Then subtract off the results for any ignored users.

tests/rest/client/test_relations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ def assert_thread(bundled_aggregations: JsonDict) -> None:
10731073
return assert_thread
10741074

10751075
# A user which has sent the root event or replied has participated.
1076-
self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(True), 10)
1076+
self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(True), 9)
10771077
# Note that this re-uses some cached values, so the total number of
10781078
# queries is much smaller.
10791079
self._test_bundled_aggregations(
@@ -1131,7 +1131,7 @@ def assert_thread(bundled_aggregations: JsonDict) -> None:
11311131
bundled_aggregations["latest_event"].get("unsigned"),
11321132
)
11331133

1134-
self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 10)
1134+
self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 9)
11351135

11361136
def test_nested_thread(self) -> None:
11371137
"""

0 commit comments

Comments
 (0)