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

Support the dir parameter for /relations. #11941

Merged
merged 4 commits into from
Feb 11, 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/11941.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support the `dir` parameter on the `/relations` endpoint, per [MSC3715](https://github.com/matrix-org/matrix-doc/pull/3715).
2 changes: 2 additions & 0 deletions synapse/rest/client/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async def on_GET(
raise SynapseError(404, "Unknown parent event.")

limit = parse_integer(request, "limit", default=5)
direction = parse_string(request, "dir", default="b", allowed_values=["f", "b"])
from_token_str = parse_string(request, "from")
to_token_str = parse_string(request, "to")

Expand All @@ -102,6 +103,7 @@ async def on_GET(
relation_type=relation_type,
event_type=event_type,
limit=limit,
direction=direction,
from_token=from_token,
to_token=to_token,
)
Expand Down
36 changes: 30 additions & 6 deletions tests/rest/client/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,28 @@ def test_basic_paginate_relations(self):
"""Tests that calling pagination API correctly the latest relations."""
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
self.assertEquals(200, channel.code, channel.json_body)
first_annotation_id = channel.json_body["event_id"]

channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
self.assertEquals(200, channel.code, channel.json_body)
annotation_id = channel.json_body["event_id"]
second_annotation_id = channel.json_body["event_id"]

channel = self.make_request(
"GET",
"/_matrix/client/unstable/rooms/%s/relations/%s?limit=1"
% (self.room, self.parent_id),
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1",
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)

# We expect to get back a single pagination result, which is the full
# relation event we sent above.
# We expect to get back a single pagination result, which is the latest
# full relation event we sent above.
self.assertEquals(len(channel.json_body["chunk"]), 1, channel.json_body)
self.assert_dict(
{"event_id": annotation_id, "sender": self.user_id, "type": "m.reaction"},
{
"event_id": second_annotation_id,
"sender": self.user_id,
"type": "m.reaction",
},
channel.json_body["chunk"][0],
)

Expand All @@ -200,6 +204,26 @@ def test_basic_paginate_relations(self):
channel.json_body.get("next_batch"), str, channel.json_body
)

# Request the relations again, but with a different direction.
channel = self.make_request(
"GET",
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1&dir=f",
access_token=self.user_token,
)
self.assertEquals(200, channel.code, channel.json_body)

# We expect to get back a single pagination result, which is the earliest
# full relation event we sent above.
self.assertEquals(len(channel.json_body["chunk"]), 1, channel.json_body)
self.assert_dict(
{
"event_id": first_annotation_id,
"sender": self.user_id,
"type": "m.reaction",
},
channel.json_body["chunk"][0],
)

def test_repeated_paginate_relations(self):
"""Test that if we paginate using a limit and tokens then we get the
expected events.
Expand Down