diff --git a/matrixctl/addons/delroom/addon.py b/matrixctl/addons/delroom/addon.py index 7426cffa..8da8b6a0 100644 --- a/matrixctl/addons/delroom/addon.py +++ b/matrixctl/addons/delroom/addon.py @@ -23,6 +23,7 @@ import logging from argparse import Namespace +from time import sleep from matrixctl.errors import InternalResponseError from matrixctl.handlers.api import RequestBuilder @@ -62,7 +63,7 @@ def addon(arg: Namespace, yaml: YAML) -> int: domain=yaml.get("server", "api", "domain"), path=f"rooms/{arg.room}", method="DELETE", - api_version="v1", + api_version="v2", json=body, timeout=1200, ) @@ -79,11 +80,117 @@ def addon(arg: Namespace, yaml: YAML) -> int: logger.fatal("The JSON response could not be loaded by MatrixCtl.") raise InternalResponseError(f"The response was: {response = }") from e + try: + json_response = handle_status(yaml, json_response["delete_id"]) + except InternalResponseError as e: + if e.message: + logger.fatal(e.message) + logger.fatal( + "MatrixCtl was not able to verify the status of the request." + ) + return 1 + print(json.dumps(json_response, indent=4)) return 0 +def handle_status(yaml: YAML, delete_id: str) -> JsonDict: # noqa: C901 + """Handle the status of a delete room request. + + Parameters + ---------- + yaml : matrixctl.handlers.yaml.YAML + The configuration file handler. + delete_id: str + The delete id of a delete room request. + + Returns + ------- + response: matrixctl.typehints.JsonDict, optional + The response as dict, containing the status. + + """ + req: RequestBuilder = RequestBuilder( + token=yaml.get("server", "api", "token"), + domain=yaml.get("server", "api", "domain"), + path=f"rooms/delete_status/{delete_id}", + method="GET", + api_version="v2", + timeout=1200.0, + ) + + # Lock messages to only print them once + msglock_shutting_down: bool = False + msglock_purging: bool = False + + while True: + + sleep(1) + try: + response: Response = request(req) + except InternalResponseError as e: + raise InternalResponseError( + "The delete room request was probably successful but the" + " status request failed. You just have to wait a bit." + ) from e + + try: + json_response: JsonDict = response.json() + except json.decoder.JSONDecodeError as e: + logger.fatal( + "The JSON status response could not be loaded by MatrixCtl." + ) + raise InternalResponseError( + f"The response was: {response = }" + ) from e + + if response is not None: + logger.debug(f"{response=}") + # complete + if json_response["status"] == "complete": + print( + "Status: Complete (the room has been deleted successfully" + ) + break + # shutting_down + if json_response["status"] == "shutting_down": + if not msglock_shutting_down: + print( + "Status: Shutting Down (removing users from the room)" + ) + msglock_shutting_down = True + logger.info( + "The server is still shutting_down the room. " + "Please wait..." + ) + sleep(5) + continue + # purging + if json_response["status"] == "purging": + if not msglock_purging: + print( + "Status: Purging (purging the room and event data from" + " database)" + ) + msglock_purging = True + logger.info( + "The server is still purging the room. Please wait..." + ) + sleep(5) + continue + # failed + if json_response["status"] == "failed": + logger.critical( + "The server returned, that the approach failed with the" + f" following message: {json_response['status']}." + ) + break + break + + return json_response + + def handle_arguments(arg: Namespace) -> JsonDict: """Build the parameters used for the delroom request. @@ -113,6 +220,7 @@ def handle_arguments(arg: Namespace) -> JsonDict: else: body["message"] = arg.message + logger.debug("Body: %s", body) return body diff --git a/matrixctl/addons/purge_history/handler.py b/matrixctl/addons/purge_history/handler.py index 05cad273..78573a68 100644 --- a/matrixctl/addons/purge_history/handler.py +++ b/matrixctl/addons/purge_history/handler.py @@ -41,7 +41,7 @@ def handle_purge_status(yaml: YAML, purge_id: str) -> int: - """Check the status pf the purge history request. + """Check the status of the purge history request. Parameters ---------- diff --git a/matrixctl/errors.py b/matrixctl/errors.py index bf5d0a81..5a153732 100644 --- a/matrixctl/errors.py +++ b/matrixctl/errors.py @@ -46,7 +46,7 @@ def __init__( ) -> None: # pylint: disable=keyword-arg-before-vararg """Use this error like a normal error in your day-to-day programming. - This is a commandline application. Therefor no user should ever see an + This is a commandline application. Therefore no user should ever see an exception (except in debug-mode). This error informs the user that, getting a traceback is a bug in this application. It gives the person instructions, how to hand in a bug report, to contain them asap. @@ -63,6 +63,7 @@ def __init__( None """ + self.message = message self.payload: Any = payload msg: str = self.__class__.BUGMSG diff --git a/news/305.feature b/news/305.feature new file mode 100644 index 00000000..689a713c --- /dev/null +++ b/news/305.feature @@ -0,0 +1 @@ +Update to *Delete Room API v2*.