@@ -671,12 +671,12 @@ async def delete_all_devices_for_user(
671671 except_device_id: optional device id which should not be deleted
672672 """
673673 device_map = await self .store .get_devices_by_user (user_id )
674- device_ids = list (device_map )
675674 if except_device_id is not None :
676- device_ids = [d for d in device_ids if d != except_device_id ]
677- await self .delete_devices (user_id , device_ids )
675+ device_map .pop (except_device_id , None )
676+ user_device_ids = device_map .keys ()
677+ await self .delete_devices (user_id , user_device_ids )
678678
679- async def delete_devices (self , user_id : str , device_ids : List [ str ] ) -> None :
679+ async def delete_devices (self , user_id : str , device_ids : StrCollection ) -> None :
680680 """Delete several devices
681681
682682 Args:
@@ -695,24 +695,24 @@ async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
695695 else :
696696 raise
697697
698- # Delete data specific to each device. Not optimised as it is not
699- # considered as part of a critical path.
700- for device_id in device_ids :
701- await self ._auth_handler .delete_access_tokens_for_user (
702- user_id , device_id = device_id
703- )
704- await self .store .delete_e2e_keys_by_device (
705- user_id = user_id , device_id = device_id
706- )
707-
708- if self .hs .config .experimental .msc3890_enabled :
698+ # Delete data specific to each device. Not optimised as its an
699+ # experimental MSC.
700+ if self .hs .config .experimental .msc3890_enabled :
701+ for device_id in device_ids :
709702 # Remove any local notification settings for this device in accordance
710703 # with MSC3890.
711704 await self ._account_data_handler .remove_account_data_for_user (
712705 user_id ,
713706 f"org.matrix.msc3890.local_notification_settings.{ device_id } " ,
714707 )
715708
709+ # If we're deleting a lot of devices, a bunch of them may not have any
710+ # to-device messages queued up. We filter those out to avoid scheduling
711+ # unnecessary tasks.
712+ devices_with_messages = await self .store .get_devices_with_messages (
713+ user_id , device_ids
714+ )
715+ for device_id in devices_with_messages :
716716 # Delete device messages asynchronously and in batches using the task scheduler
717717 # We specify an upper stream id to avoid deleting non delivered messages
718718 # if an user re-uses a device ID.
@@ -726,6 +726,10 @@ async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
726726 },
727727 )
728728
729+ await self ._auth_handler .delete_access_tokens_for_devices (
730+ user_id , device_ids = device_ids
731+ )
732+
729733 # Pushers are deleted after `delete_access_tokens_for_user` is called so that
730734 # modules using `on_logged_out` hook can use them if needed.
731735 await self .hs .get_pusherpool ().remove_pushers_by_devices (user_id , device_ids )
@@ -819,10 +823,11 @@ async def notify_device_update(
819823 # This should only happen if there are no updates, so we bail.
820824 return
821825
822- for device_id in device_ids :
823- logger .debug (
824- "Notifying about update %r/%r, ID: %r" , user_id , device_id , position
825- )
826+ if logger .isEnabledFor (logging .DEBUG ):
827+ for device_id in device_ids :
828+ logger .debug (
829+ "Notifying about update %r/%r, ID: %r" , user_id , device_id , position
830+ )
826831
827832 # specify the user ID too since the user should always get their own device list
828833 # updates, even if they aren't in any rooms.
@@ -922,9 +927,6 @@ async def rehydrate_device(
922927 # can't call self.delete_device because that will clobber the
923928 # access token so call the storage layer directly
924929 await self .store .delete_devices (user_id , [old_device_id ])
925- await self .store .delete_e2e_keys_by_device (
926- user_id = user_id , device_id = old_device_id
927- )
928930
929931 # tell everyone that the old device is gone and that the dehydrated
930932 # device has a new display name
@@ -946,7 +948,6 @@ async def delete_dehydrated_device(self, user_id: str, device_id: str) -> None:
946948 raise errors .NotFoundError ()
947949
948950 await self .delete_devices (user_id , [device_id ])
949- await self .store .delete_e2e_keys_by_device (user_id = user_id , device_id = device_id )
950951
951952 @wrap_as_background_process ("_handle_new_device_update_async" )
952953 async def _handle_new_device_update_async (self ) -> None :
0 commit comments