4747 DeviceListUpdates ,
4848 JsonDict ,
4949 JsonMapping ,
50+ MultiWriterStreamToken ,
5051 RoomAlias ,
5152 RoomStreamToken ,
5253 StreamKeyType ,
@@ -217,7 +218,7 @@ async def handle_room_events(events: Iterable[EventBase]) -> None:
217218 def notify_interested_services_ephemeral (
218219 self ,
219220 stream_key : StreamKeyType ,
220- new_token : Union [int , RoomStreamToken ],
221+ new_token : Union [int , RoomStreamToken , MultiWriterStreamToken ],
221222 users : Collection [Union [str , UserID ]],
222223 ) -> None :
223224 """
@@ -259,19 +260,6 @@ def notify_interested_services_ephemeral(
259260 ):
260261 return
261262
262- # Assert that new_token is an integer (and not a RoomStreamToken).
263- # All of the supported streams that this function handles use an
264- # integer to track progress (rather than a RoomStreamToken - a
265- # vector clock implementation) as they don't support multiple
266- # stream writers.
267- #
268- # As a result, we simply assert that new_token is an integer.
269- # If we do end up needing to pass a RoomStreamToken down here
270- # in the future, using RoomStreamToken.stream (the minimum stream
271- # position) to convert to an ascending integer value should work.
272- # Additional context: https://github.com/matrix-org/synapse/pull/11137
273- assert isinstance (new_token , int )
274-
275263 # Ignore to-device messages if the feature flag is not enabled
276264 if (
277265 stream_key == StreamKeyType .TO_DEVICE
@@ -286,6 +274,9 @@ def notify_interested_services_ephemeral(
286274 ):
287275 return
288276
277+ # We know we're not a `RoomStreamToken` at this point.
278+ assert not isinstance (new_token , RoomStreamToken )
279+
289280 # Check whether there are any appservices which have registered to receive
290281 # ephemeral events.
291282 #
@@ -327,7 +318,7 @@ async def _notify_interested_services_ephemeral(
327318 self ,
328319 services : List [ApplicationService ],
329320 stream_key : StreamKeyType ,
330- new_token : int ,
321+ new_token : Union [ int , MultiWriterStreamToken ] ,
331322 users : Collection [Union [str , UserID ]],
332323 ) -> None :
333324 logger .debug ("Checking interested services for %s" , stream_key )
@@ -340,6 +331,7 @@ async def _notify_interested_services_ephemeral(
340331 #
341332 # Instead we simply grab the latest typing updates in _handle_typing
342333 # and, if they apply to this application service, send it off.
334+ assert isinstance (new_token , int )
343335 events = await self ._handle_typing (service , new_token )
344336 if events :
345337 self .scheduler .enqueue_for_appservice (service , ephemeral = events )
@@ -350,15 +342,23 @@ async def _notify_interested_services_ephemeral(
350342 (service .id , stream_key )
351343 ):
352344 if stream_key == StreamKeyType .RECEIPT :
345+ assert isinstance (new_token , MultiWriterStreamToken )
346+
347+ # We store appservice tokens as integers, so we ignore
348+ # the `instance_map` components and instead simply
349+ # follow the base stream position.
350+ new_token = MultiWriterStreamToken (stream = new_token .stream )
351+
353352 events = await self ._handle_receipts (service , new_token )
354353 self .scheduler .enqueue_for_appservice (service , ephemeral = events )
355354
356355 # Persist the latest handled stream token for this appservice
357356 await self .store .set_appservice_stream_type_pos (
358- service , "read_receipt" , new_token
357+ service , "read_receipt" , new_token . stream
359358 )
360359
361360 elif stream_key == StreamKeyType .PRESENCE :
361+ assert isinstance (new_token , int )
362362 events = await self ._handle_presence (service , users , new_token )
363363 self .scheduler .enqueue_for_appservice (service , ephemeral = events )
364364
@@ -368,6 +368,7 @@ async def _notify_interested_services_ephemeral(
368368 )
369369
370370 elif stream_key == StreamKeyType .TO_DEVICE :
371+ assert isinstance (new_token , int )
371372 # Retrieve a list of to-device message events, as well as the
372373 # maximum stream token of the messages we were able to retrieve.
373374 to_device_messages = await self ._get_to_device_messages (
@@ -383,6 +384,7 @@ async def _notify_interested_services_ephemeral(
383384 )
384385
385386 elif stream_key == StreamKeyType .DEVICE_LIST :
387+ assert isinstance (new_token , int )
386388 device_list_summary = await self ._get_device_list_summary (
387389 service , new_token
388390 )
@@ -432,7 +434,7 @@ async def _handle_typing(
432434 return typing
433435
434436 async def _handle_receipts (
435- self , service : ApplicationService , new_token : int
437+ self , service : ApplicationService , new_token : MultiWriterStreamToken
436438 ) -> List [JsonMapping ]:
437439 """
438440 Return the latest read receipts that the given application service should receive.
@@ -455,15 +457,17 @@ async def _handle_receipts(
455457 from_key = await self .store .get_type_stream_id_for_appservice (
456458 service , "read_receipt"
457459 )
458- if new_token is not None and new_token <= from_key :
460+ if new_token is not None and new_token . stream <= from_key :
459461 logger .debug (
460462 "Rejecting token lower than or equal to stored: %s" % (new_token ,)
461463 )
462464 return []
463465
466+ from_token = MultiWriterStreamToken (stream = from_key )
467+
464468 receipts_source = self .event_sources .sources .receipt
465469 receipts , _ = await receipts_source .get_new_events_as (
466- service = service , from_key = from_key , to_key = new_token
470+ service = service , from_key = from_token , to_key = new_token
467471 )
468472 return receipts
469473
0 commit comments