@@ -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
0 commit comments