Fix registering a device on an account with lots of devices (#15348)

Fixes up #15183
This commit is contained in:
Erik Johnston 2023-03-29 14:37:06 +01:00 committed by GitHub
parent 5350b5d04d
commit f0d8f66eaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 3 deletions

View file

@ -1638,19 +1638,22 @@ class DeviceBackgroundUpdateStore(SQLBaseStore):
"""
rows = await self.db_pool.execute(
"check_too_many_devices_for_user_last_seen", None, sql, (user_id,)
"check_too_many_devices_for_user_last_seen",
None,
sql,
user_id,
)
if rows:
max_last_seen = max(rows[0][0], max_last_seen)
# Fetch the devices to delete.
sql = """
SELECT DISTINCT device_id FROM devices
SELECT device_id FROM devices
LEFT JOIN e2e_device_keys_json USING (user_id, device_id)
WHERE
user_id = ?
AND NOT hidden
AND last_seen < ?
AND last_seen <= ?
AND key_json IS NULL
ORDER BY last_seen
"""