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

Commit 659860a

Browse files
committed
Re-use get_threads_participated instead of custom SQL.
1 parent 478f1ec commit 659860a

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

synapse/handlers/relations.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,28 @@ async def get_threads(
522522
# not passing them in here we should get a better cache hit rate).
523523
thread_roots, next_token = await self._main_store.get_threads(
524524
room_id=room_id,
525-
participating_user_id=None if filter == "all" else user_id,
526525
limit=limit,
527526
from_token=from_token,
528527
to_token=to_token,
529528
)
530529

531530
events = await self._main_store.get_events_as_list(thread_roots)
532531

532+
if filter == "participated":
533+
# Pre-seed thread participation with whether the requester sent the event.
534+
participated = {event.event_id: event.sender == user_id for event in events}
535+
# For events the requester did not send, check the database for whether
536+
# the requester sent a threaded reply.
537+
participated.update(
538+
await self._main_store.get_threads_participated(
539+
[eid for eid, p in participated.items() if not p],
540+
user_id,
541+
)
542+
)
543+
544+
# Limit the returned threads to those the user has participated in.
545+
events = [event for event in events if participated[event.event_id]]
546+
533547
events = await filter_events_for_client(
534548
self._storage_controllers,
535549
user_id,

synapse/storage/databases/main/relations.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,6 @@ def _get_event_relations(
818818
async def get_threads(
819819
self,
820820
room_id: str,
821-
participating_user_id: Optional[str] = None,
822821
limit: int = 5,
823822
from_token: Optional[StreamToken] = None,
824823
to_token: Optional[StreamToken] = None,
@@ -828,8 +827,6 @@ async def get_threads(
828827
829828
Args:
830829
room_id: The room the event belongs to.
831-
participating_user_id: A user who must have participated in the thread.
832-
Provide None to return all threads.
833830
limit: Only fetch the most recent `limit` events.
834831
from_token: Fetch rows from the given token, or from the start if None.
835832
to_token: Fetch rows up to the given token, or up to the end if None.
@@ -840,21 +837,9 @@ async def get_threads(
840837
841838
The next stream token, if one exists.
842839
"""
843-
where_clause = [
844-
"child.room_id = ?",
845-
f"relation_type = '{RelationTypes.THREAD}'",
846-
]
847-
where_args: List[Union[str, int]] = [room_id]
848-
849-
# The user has participated if they sent the thread root or have any reply
850-
# to the thread.
851-
if participating_user_id:
852-
where_clause.append("parent.sender = ?")
853-
where_args.append(participating_user_id)
854-
855840
pagination_clause = generate_pagination_where_clause(
856841
direction="b",
857-
column_names=("child.topological_ordering", "child.stream_ordering"),
842+
column_names=("topological_ordering", "stream_ordering"),
858843
from_token=from_token.room_key.as_historical_tuple()
859844
if from_token
860845
else None,
@@ -863,25 +848,25 @@ async def get_threads(
863848
)
864849

865850
if pagination_clause:
866-
where_clause.append(pagination_clause)
851+
pagination_clause = "AND " + pagination_clause
867852

868853
sql = f"""
869-
SELECT relates_to_id, MAX(child.topological_ordering), MAX(child.stream_ordering)
870-
FROM events AS child
871-
INNER JOIN event_relations USING (event_id)
872-
INNER JOIN events AS parent ON
873-
parent.event_id = relates_to_id
874-
AND parent.room_id = child.room_id
875-
WHERE {" AND ".join(where_clause)}
854+
SELECT relates_to_id, MAX(topological_ordering), MAX(stream_ordering)
855+
FROM event_relations
856+
INNER JOIN events USING (event_id)
857+
WHERE
858+
room_id = ? AND
859+
relation_type = '{RelationTypes.THREAD}'
860+
{pagination_clause}
876861
GROUP BY relates_to_id
877-
ORDER BY child.topological_ordering DESC, child.stream_ordering DESC
862+
ORDER BY topological_ordering DESC, stream_ordering DESC
878863
LIMIT ?
879864
"""
880865

881866
def _get_threads_txn(
882867
txn: LoggingTransaction,
883868
) -> Tuple[List[str], Optional[StreamToken]]:
884-
txn.execute(sql, where_args + [limit + 1])
869+
txn.execute(sql, [room_id, limit + 1])
885870

886871
last_topo_id = None
887872
last_stream_id = None

tests/rest/client/test_relations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ def test_pagination(self) -> None:
17521752

17531753
@unittest.override_config({"experimental_features": {"msc3856_enabled": True}})
17541754
def test_filter(self) -> None:
1755-
"""Create threads and ensure the ordering is due to their latest event."""
1755+
"""Filtering threads to all or participated in should work."""
17561756
# Thread 1 has the user as the root event.
17571757
thread_1 = self.parent_id
17581758
self._send_relation(

0 commit comments

Comments
 (0)