@@ -652,22 +652,31 @@ def f(txn: LoggingTransaction) -> List[_EventDictReturn]:
652
652
return ret
653
653
654
654
async def get_recent_events_for_room (
655
- self , room_id : str , limit : int , end_token : RoomStreamToken
655
+ self ,
656
+ room_id : str ,
657
+ limit : int ,
658
+ end_token : RoomStreamToken ,
659
+ order_by : str = "topological" ,
656
660
) -> Tuple [List [EventBase ], RoomStreamToken ]:
657
661
"""Get the most recent events in the room in topological ordering.
658
662
659
663
Args:
660
664
room_id
661
665
limit
662
666
end_token: The stream token representing now.
667
+ order_by: Either 'topological' or 'stream' to indicate the order in
668
+ which results should be returned.
663
669
664
670
Returns:
665
671
A list of events and a token pointing to the start of the returned
666
672
events. The events returned are in ascending topological order.
667
673
"""
668
674
669
675
rows , token = await self .get_recent_event_ids_for_room (
670
- room_id , limit , end_token
676
+ room_id ,
677
+ limit ,
678
+ end_token ,
679
+ order_by ,
671
680
)
672
681
673
682
events = await self .get_events_as_list (
@@ -679,14 +688,20 @@ async def get_recent_events_for_room(
679
688
return events , token
680
689
681
690
async def get_recent_event_ids_for_room (
682
- self , room_id : str , limit : int , end_token : RoomStreamToken
691
+ self ,
692
+ room_id : str ,
693
+ limit : int ,
694
+ end_token : RoomStreamToken ,
695
+ order_by : str = "topological" ,
683
696
) -> Tuple [List [_EventDictReturn ], RoomStreamToken ]:
684
697
"""Get the most recent events in the room in topological ordering.
685
698
686
699
Args:
687
700
room_id
688
701
limit
689
702
end_token: The stream token representing now.
703
+ order_by: Either 'topological' or 'stream' to indicate the order in
704
+ which results should be returned.
690
705
691
706
Returns:
692
707
A list of _EventDictReturn and a token pointing to the start of the
@@ -701,6 +716,7 @@ async def get_recent_event_ids_for_room(
701
716
self ._paginate_room_events_txn ,
702
717
room_id ,
703
718
from_token = end_token ,
719
+ order_by = order_by ,
704
720
limit = limit ,
705
721
)
706
722
@@ -1099,6 +1115,7 @@ def _paginate_room_events_txn(
1099
1115
from_token : RoomStreamToken ,
1100
1116
to_token : Optional [RoomStreamToken ] = None ,
1101
1117
direction : str = "b" ,
1118
+ order_by : str = "topological" ,
1102
1119
limit : int = - 1 ,
1103
1120
event_filter : Optional [Filter ] = None ,
1104
1121
) -> Tuple [List [_EventDictReturn ], RoomStreamToken ]:
@@ -1111,6 +1128,8 @@ def _paginate_room_events_txn(
1111
1128
to_token: A token which if given limits the results to only those before
1112
1129
direction: Either 'b' or 'f' to indicate whether we are paginating
1113
1130
forwards or backwards from `from_key`.
1131
+ order_by: Either 'topological' or 'stream' to indicate the order in
1132
+ which results should be returned.
1114
1133
limit: The maximum number of events to return.
1115
1134
event_filter: If provided filters the events to
1116
1135
those that match the filter.
@@ -1123,6 +1142,7 @@ def _paginate_room_events_txn(
1123
1142
"""
1124
1143
1125
1144
assert int (limit ) >= 0
1145
+ assert order_by in ("topological" , "stream" )
1126
1146
1127
1147
# Tokens really represent positions between elements, but we use
1128
1148
# the convention of pointing to the event before the gap. Hence
@@ -1133,6 +1153,12 @@ def _paginate_room_events_txn(
1133
1153
else :
1134
1154
order = "ASC"
1135
1155
1156
+ order_clause = """ORDER BY event.topological_ordering %(order)s, event.stream_ordering %(order)s"""
1157
+ if order_by == "stream" :
1158
+ order_clause = """ORDER BY event.stream_ordering %(order)s, event.topological_ordering %(order)s"""
1159
+
1160
+ order_clause = order_clause % {"order" : order }
1161
+
1136
1162
# The bounds for the stream tokens are complicated by the fact
1137
1163
# that we need to handle the instance_map part of the tokens. We do this
1138
1164
# by fetching all events between the min stream token and the maximum
@@ -1228,13 +1254,13 @@ def _paginate_room_events_txn(
1228
1254
FROM events AS event
1229
1255
%(join_clause)s
1230
1256
WHERE event.outlier = ? AND event.room_id = ? AND %(bounds)s
1231
- ORDER BY event.topological_ordering %(order)s,
1232
- event.stream_ordering %(order)s LIMIT ?
1257
+ %(order_clause)s
1258
+ LIMIT ?
1233
1259
""" % {
1234
1260
"select_keywords" : select_keywords ,
1235
1261
"join_clause" : join_clause ,
1236
1262
"bounds" : bounds ,
1237
- "order " : order ,
1263
+ "order_clause " : order_clause ,
1238
1264
}
1239
1265
1240
1266
txn .execute (sql , args )
@@ -1275,6 +1301,7 @@ async def paginate_room_events(
1275
1301
from_key : RoomStreamToken ,
1276
1302
to_key : Optional [RoomStreamToken ] = None ,
1277
1303
direction : str = "b" ,
1304
+ order_by : str = "topological" ,
1278
1305
limit : int = - 1 ,
1279
1306
event_filter : Optional [Filter ] = None ,
1280
1307
) -> Tuple [List [EventBase ], RoomStreamToken ]:
@@ -1286,6 +1313,8 @@ async def paginate_room_events(
1286
1313
to_key: A token which if given limits the results to only those before
1287
1314
direction: Either 'b' or 'f' to indicate whether we are paginating
1288
1315
forwards or backwards from `from_key`.
1316
+ order_by: Either 'topological' or 'stream' to indicate the order in
1317
+ which results should be returned.
1289
1318
limit: The maximum number of events to return.
1290
1319
event_filter: If provided filters the events to those that match the filter.
1291
1320
@@ -1303,6 +1332,7 @@ async def paginate_room_events(
1303
1332
from_key ,
1304
1333
to_key ,
1305
1334
direction ,
1335
+ order_by ,
1306
1336
limit ,
1307
1337
event_filter ,
1308
1338
)
0 commit comments