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

Remove an unnecessary class from the relations code. #12338

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12338.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor relations code to remove an unnecessary class.
35 changes: 23 additions & 12 deletions synapse/handlers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import TYPE_CHECKING, Dict, Iterable, Optional, cast
from typing import TYPE_CHECKING, Dict, Iterable, Optional

import attr
from frozendict import frozendict
Expand All @@ -25,7 +25,6 @@

if TYPE_CHECKING:
from synapse.server import HomeServer
from synapse.storage.databases.main import DataStore


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -116,7 +115,7 @@ async def get_relations(
if event is None:
raise SynapseError(404, "Unknown parent event.")

pagination_chunk = await self._main_store.get_relations_for_event(
related_events, next_token = await self._main_store.get_relations_for_event(
event_id=event_id,
event=event,
room_id=room_id,
Expand All @@ -129,9 +128,7 @@ async def get_relations(
to_token=to_token,
)

events = await self._main_store.get_events_as_list(
[c["event_id"] for c in pagination_chunk.chunk]
)
events = await self._main_store.get_events_as_list(related_events)

events = await filter_events_for_client(
self._storage, user_id, events, is_peeking=(member_event_id is None)
Expand All @@ -152,9 +149,16 @@ async def get_relations(
events, now, bundle_aggregations=aggregations
)

return_value = await pagination_chunk.to_dict(self._main_store)
return_value["chunk"] = serialized_events
return_value["original_event"] = original_event
return_value = {
"chunk": serialized_events,
"original_event": original_event,
}

if next_token:
return_value["next_batch"] = await next_token.to_string(self._main_store)

if from_token:
return_value["prev_batch"] = await from_token.to_string(self._main_store)

return return_value

Expand Down Expand Up @@ -196,11 +200,18 @@ async def _get_bundled_aggregation_for_event(
if annotations:
aggregations.annotations = {"chunk": annotations}

references = await self._main_store.get_relations_for_event(
references, next_token = await self._main_store.get_relations_for_event(
event_id, event, room_id, RelationTypes.REFERENCE, direction="f"
)
if references.chunk:
aggregations.references = await references.to_dict(cast("DataStore", self))
if references:
aggregations.references = {
"chunk": [{"event_id": event_id} for event_id in references]
}

if next_token:
aggregations.references["next_batch"] = await next_token.to_string(
self._main_store
)

# Store the bundled aggregations in the event metadata for later use.
return aggregations
Expand Down
17 changes: 8 additions & 9 deletions synapse/storage/databases/main/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
)
from synapse.storage.databases.main.stream import generate_pagination_where_clause
from synapse.storage.engines import PostgresEngine
from synapse.storage.relations import PaginationChunk
from synapse.types import JsonDict, RoomStreamToken, StreamToken
from synapse.util.caches.descriptors import cached, cachedList

Expand Down Expand Up @@ -71,7 +70,7 @@ async def get_relations_for_event(
direction: str = "b",
from_token: Optional[StreamToken] = None,
to_token: Optional[StreamToken] = None,
) -> PaginationChunk:
) -> Tuple[List[str], Optional[StreamToken]]:
"""Get a list of relations for an event, ordered by topological ordering.

Args:
Expand All @@ -88,8 +87,10 @@ async def get_relations_for_event(
to_token: Fetch rows up to the given token, or up to the end if None.

Returns:
List of event IDs that match relations requested. The rows are of
the form `{"event_id": "..."}`.
A tuple of:
A list of related event IDs

The next stream token, if one exists.
"""
# We don't use `event_id`, it's there so that we can cache based on
# it. The `event_id` must match the `event.event_id`.
Expand Down Expand Up @@ -144,7 +145,7 @@ async def get_relations_for_event(

def _get_recent_references_for_event_txn(
txn: LoggingTransaction,
) -> PaginationChunk:
) -> Tuple[List[str], Optional[StreamToken]]:
txn.execute(sql, where_args + [limit + 1])

last_topo_id = None
Expand All @@ -154,7 +155,7 @@ def _get_recent_references_for_event_txn(
# Do not include edits for redacted events as they leak event
# content.
if not is_redacted or row[1] != RelationTypes.REPLACE:
events.append({"event_id": row[0]})
events.append(row[0])
last_topo_id = row[2]
last_stream_id = row[3]

Expand All @@ -177,9 +178,7 @@ def _get_recent_references_for_event_txn(
groups_key=0,
)

return PaginationChunk(
chunk=list(events[:limit]), next_batch=next_token, prev_batch=from_token
)
return events[:limit], next_token

return await self.db_pool.runInteraction(
"get_recent_references_for_event", _get_recent_references_for_event_txn
Expand Down
53 changes: 0 additions & 53 deletions synapse/storage/relations.py

This file was deleted.