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

Commit 8dcdb4e

Browse files
committed
Allow limiting threads by participation.
1 parent e9a649e commit 8dcdb4e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

synapse/handlers/relations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ async def get_threads(
485485
self,
486486
requester: Requester,
487487
room_id: str,
488+
include: str,
488489
limit: int = 5,
489490
from_token: Optional[StreamToken] = None,
490491
to_token: Optional[StreamToken] = None,
@@ -494,6 +495,8 @@ async def get_threads(
494495
Args:
495496
requester: The user requesting the relations.
496497
room_id: The room the event belongs to.
498+
include: One of "all" or "participated" to indicate which threads should
499+
be returned.
497500
limit: Only fetch the most recent `limit` events.
498501
from_token: Fetch rows from the given token, or from the start if None.
499502
to_token: Fetch rows up to the given token, or up to the end if None.
@@ -518,6 +521,21 @@ async def get_threads(
518521

519522
events = await self._main_store.get_events_as_list(thread_roots)
520523

524+
if include == "participated":
525+
# Pre-seed thread participation with whether the requester sent the event.
526+
participated = {event.event_id: event.sender == user_id for event in events}
527+
# For events the requester did not send, check the database for whether
528+
# the requester sent a threaded reply.
529+
participated.update(
530+
await self._main_store.get_threads_participated(
531+
[eid for eid, p in participated.items() if not p],
532+
user_id,
533+
)
534+
)
535+
536+
# Limit the returned threads to those the user has participated in.
537+
events = [event for event in events if participated[event.event_id]]
538+
521539
events = await filter_events_for_client(
522540
self._storage_controllers,
523541
user_id,

synapse/rest/client/relations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ async def on_GET(
113113
limit = parse_integer(request, "limit", default=5)
114114
from_token_str = parse_string(request, "from")
115115
to_token_str = parse_string(request, "to")
116+
include = parse_string(
117+
request, "include", default="all", allowed_values=["all", "participated"]
118+
)
116119

117120
# Return the relations
118121
from_token = None
@@ -125,6 +128,7 @@ async def on_GET(
125128
result = await self._relations_handler.get_threads(
126129
requester=requester,
127130
room_id=room_id,
131+
include=include,
128132
limit=limit,
129133
from_token=from_token,
130134
to_token=to_token,

tests/rest/client/test_relations.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,4 +1750,50 @@ def test_pagination(self) -> None:
17501750

17511751
self.assertNotIn("next_batch", channel.json_body, channel.json_body)
17521752

1753+
@unittest.override_config({"experimental_features": {"msc3856_enabled": True}})
1754+
def test_include(self) -> None:
1755+
"""Filtering threads to all or participated in should work."""
1756+
# Thread 1 has the user as the root event.
1757+
thread_1 = self.parent_id
1758+
self._send_relation(
1759+
RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
1760+
)
1761+
1762+
# Thread 2 has the user replying.
1763+
res = self.helper.send(self.room, body="Thread Root!", tok=self.user2_token)
1764+
thread_2 = res["event_id"]
1765+
self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
1766+
1767+
# Thread 3 has the user not participating in.
1768+
res = self.helper.send(self.room, body="Another thread!", tok=self.user2_token)
1769+
thread_3 = res["event_id"]
1770+
self._send_relation(
1771+
RelationTypes.THREAD,
1772+
"m.room.test",
1773+
access_token=self.user2_token,
1774+
parent_id=thread_3,
1775+
)
1776+
1777+
# All threads in the room.
1778+
channel = self.make_request(
1779+
"GET",
1780+
f"/_matrix/client/unstable/org.matrix.msc3856/rooms/{self.room}/threads",
1781+
access_token=self.user_token,
1782+
)
1783+
self.assertEquals(200, channel.code, channel.json_body)
1784+
thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
1785+
self.assertEqual(
1786+
thread_roots, [thread_3, thread_2, thread_1], channel.json_body
1787+
)
1788+
1789+
# Only participated threads.
1790+
channel = self.make_request(
1791+
"GET",
1792+
f"/_matrix/client/unstable/org.matrix.msc3856/rooms/{self.room}/threads?include=participated",
1793+
access_token=self.user_token,
1794+
)
1795+
self.assertEquals(200, channel.code, channel.json_body)
1796+
thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
1797+
self.assertEqual(thread_roots, [thread_2, thread_1], channel.json_body)
1798+
17531799
# XXX Test ignoring users.

0 commit comments

Comments
 (0)