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

Commit

Permalink
Avoid unnecessary database access.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed May 17, 2022
1 parent eaa7916 commit 37d8462
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions synapse/handlers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,28 @@ async def _get_threads_for_events(
# Fetch thread summaries.
summaries = await self._main_store.get_thread_summaries(event_ids)

# Only fetch participated for a limited selection based on what had
# Only fetch whether the current user has participated in a thread based
# on which returned for a limited selection based on what had
# summaries.
thread_event_ids = [
event_id for event_id, summary in summaries.items() if summary
]
participated = await self._main_store.get_threads_participated(
thread_event_ids, user_id

# Preseed thread participation with whether the requester send the event.
participated = {
event_id: events_by_id[event_id].sender == user_id
for event_id in thread_event_ids
}
# Check other events against the database.
participated.update(
await self._main_store.get_threads_participated(
[
event_id
for event_id in thread_event_ids
if not participated[event_id]
],
user_id,
)
)

# Then subtract off the results for any ignored users.
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def assert_thread(bundled_aggregations: JsonDict) -> None:
return assert_thread

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

self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 10)
self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 9)

def test_nested_thread(self) -> None:
"""
Expand Down

0 comments on commit 37d8462

Please sign in to comment.