diff --git a/scripts/matrix_device_cleanup.py b/scripts/matrix_device_cleanup.py index a21d828..0d1b7f0 100755 --- a/scripts/matrix_device_cleanup.py +++ b/scripts/matrix_device_cleanup.py @@ -14,6 +14,7 @@ import os import subprocess import sys import time +from urllib.parse import quote import httpx @@ -49,8 +50,9 @@ async def list_devices( client: httpx.AsyncClient, homeserver: str, headers: dict, user_id: str, ) -> list[dict]: """List all devices for a user via Synapse Admin API.""" + encoded_user = quote(user_id, safe="") resp = await client.get( - f"{homeserver}/_synapse/admin/v2/users/{user_id}/devices", + f"{homeserver}/_synapse/admin/v2/users/{encoded_user}/devices", headers=headers, ) resp.raise_for_status() @@ -65,8 +67,9 @@ async def delete_devices_batch( device_ids: list[str], ) -> int: """Bulk-delete devices. Returns count deleted.""" + encoded_user = quote(user_id, safe="") resp = await client.post( - f"{homeserver}/_synapse/admin/v2/users/{user_id}/delete_devices", + f"{homeserver}/_synapse/admin/v2/users/{encoded_user}/delete_devices", headers=headers, json={"devices": device_ids}, ) @@ -186,7 +189,7 @@ def main(): parser.add_argument( "--homeserver", default=os.environ.get("MATRIX_HOMESERVER", "https://matrix.agiliton.eu"), - help="Homeserver URL", + help="Homeserver URL (use http://CONTAINER_IP:8008 when running on the matrix VM)", ) parser.add_argument("--keep", type=int, default=1, help="Number of most recent devices to keep") parser.add_argument("--max-age-days", type=int, default=None, help="Only delete devices older than N days")