Add a background job to automatically delete stale devices (#12855)

Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>
This commit is contained in:
Brendan Abolivier 2022-05-27 17:47:32 +02:00 committed by GitHub
parent 888eb736a1
commit 28989cb301
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 135 additions and 1 deletions

View file

@ -1154,6 +1154,45 @@ class DeviceWorkerStore(SQLBaseStore):
_prune_txn,
)
async def get_local_devices_not_accessed_since(
self, since_ms: int
) -> Dict[str, List[str]]:
"""Retrieves local devices that haven't been accessed since a given date.
Args:
since_ms: the timestamp to select on, every device with a last access date
from before that time is returned.
Returns:
A dictionary with an entry for each user with at least one device matching
the request, which value is a list of the device ID(s) for the corresponding
device(s).
"""
def get_devices_not_accessed_since_txn(
txn: LoggingTransaction,
) -> List[Dict[str, str]]:
sql = """
SELECT user_id, device_id
FROM devices WHERE last_seen < ? AND hidden = FALSE
"""
txn.execute(sql, (since_ms,))
return self.db_pool.cursor_to_dict(txn)
rows = await self.db_pool.runInteraction(
"get_devices_not_accessed_since",
get_devices_not_accessed_since_txn,
)
devices: Dict[str, List[str]] = {}
for row in rows:
# Remote devices are never stale from our point of view.
if self.hs.is_mine_id(row["user_id"]):
user_devices = devices.setdefault(row["user_id"], [])
user_devices.append(row["device_id"])
return devices
class DeviceBackgroundUpdateStore(SQLBaseStore):
def __init__(