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

Do not return unspecced original_event field when using the stable /relations endpoint #14025

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/14025.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not return an unspecified `original_event` field when using the stable `/relations` endpoint. Introduced in Synapse v1.57.0.
25 changes: 13 additions & 12 deletions synapse/handlers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def get_relations(
direction: str = "b",
from_token: Optional[StreamToken] = None,
to_token: Optional[StreamToken] = None,
include_original_event: bool = False,
) -> JsonDict:
"""Get related events of a event, ordered by topological ordering.

Expand All @@ -94,6 +95,7 @@ async def get_relations(
oldest first (`"f"`).
from_token: Fetch rows from the given token, or from the start if None.
to_token: Fetch rows up to the given token, or up to the end if None.
include_original_event: Whether to include the parent event.

Returns:
The pagination chunk.
Expand Down Expand Up @@ -138,25 +140,24 @@ async def get_relations(
is_peeking=(member_event_id is None),
)

now = self._clock.time_msec()
# Do not bundle aggregations when retrieving the original event because
# we want the content before relations are applied to it.
original_event = self._event_serializer.serialize_event(
event, now, bundle_aggregations=None
)
# The relations returned for the requested event do include their
# bundled aggregations.
aggregations = await self.get_bundled_aggregations(
events, requester.user.to_string()
)
serialized_events = self._event_serializer.serialize_events(
events, now, bundle_aggregations=aggregations
)

return_value = {
"chunk": serialized_events,
"original_event": original_event,
now = self._clock.time_msec()
return_value: JsonDict = {
"chunk": self._event_serializer.serialize_events(
events, now, bundle_aggregations=aggregations
),
}
if include_original_event:
# Do not bundle aggregations when retrieving the original event because
# we want the content before relations are applied to it.
return_value["original_event"] = self._event_serializer.serialize_event(
event, now, bundle_aggregations=None
)

if next_token:
return_value["next_batch"] = await next_token.to_string(self._main_store)
Expand Down
8 changes: 8 additions & 0 deletions synapse/rest/client/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ async def on_GET(
if to_token_str:
to_token = await StreamToken.from_string(self.store, to_token_str)

# The unstable version of this API returns an extra field for client
# compatibility, see
clokep marked this conversation as resolved.
Show resolved Hide resolved
assert request.path is not None
include_original_event = request.path.startswith(b"/_matrix/client/unstable/")

result = await self._relations_handler.get_relations(
requester=requester,
event_id=parent_id,
Expand All @@ -92,6 +97,9 @@ async def on_GET(
direction=direction,
from_token=from_token,
to_token=to_token,
# The unstable version of this API returns an extra field for client
# compatibility, see
clokep marked this conversation as resolved.
Show resolved Hide resolved
include_original_event=include_original_event,
)

return 200, result
Expand Down
13 changes: 8 additions & 5 deletions tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,14 @@ def test_unknown_relations(self) -> None:
)

# We also expect to get the original event (the id of which is self.parent_id)
# when requesting the unstable endpoint.
self.assertNotIn("original_event", channel.json_body)
channel = self.make_request(
"GET",
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1",
access_token=self.user_token,
)
self.assertEqual(200, channel.code, channel.json_body)
self.assertEqual(
channel.json_body["original_event"]["event_id"], self.parent_id
)
Expand Down Expand Up @@ -755,11 +763,6 @@ def test_basic_paginate_relations(self) -> None:
channel.json_body["chunk"][0],
)

# We also expect to get the original event (the id of which is self.parent_id)
self.assertEqual(
channel.json_body["original_event"]["event_id"], self.parent_id
)

# Make sure next_batch has something in it that looks like it could be a
# valid token.
self.assertIsInstance(
Expand Down