From a1e3f51416849d99aa43e7a3612961aeccba7f02 Mon Sep 17 00:00:00 2001 From: Jason <81298350+Deutscher775@users.noreply.github.com> Date: Mon, 2 Dec 2024 22:09:06 +0100 Subject: [PATCH] fixed time delay of cached messages being sent --- src/api.py | 1 + src/astroidapi/endpoint_update_handler.py | 62 -------------------- src/astroidapi/queue_processor.py | 71 ++++++++++++++++++++++- src/astroidapi/sending_handler.py | 1 + src/astroidapi/surrealdb_handler.py | 3 +- 5 files changed, 74 insertions(+), 64 deletions(-) diff --git a/src/api.py b/src/api.py index 7105b07..aaf969e 100644 --- a/src/api.py +++ b/src/api.py @@ -380,6 +380,7 @@ async def clear_temporary_attachments(master_token: Annotated[str, fastapi.Query else: return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."}) + @api.post("/update/{endpoint}", description="Modify an endpoint.", response_description="Endpoint with updated data.") async def post_endpoint( endpoint: int, diff --git a/src/astroidapi/endpoint_update_handler.py b/src/astroidapi/endpoint_update_handler.py index 761bc6a..a4cc4aa 100644 --- a/src/astroidapi/endpoint_update_handler.py +++ b/src/astroidapi/endpoint_update_handler.py @@ -165,7 +165,6 @@ async def update_endpoint( if sender: if sender in ["discord", "guilded", "revolt", "nerimity"]: endpoint_data["meta"]["sender"] = sender - endpoint_data["meta"]["read"][sender] = True else: return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid sender."}) @@ -256,67 +255,6 @@ async def update_endpoint( if not updated_json["config"]["self-user"] is True: if updated_json["meta"]["trigger"]: asyncio.create_task(queue_processor.QueueProcessor.handleUpdatedEndpointData(endpoint, updated_json)) - waiting_secs = 0 - max_secs = 10 - while True: - check_json = await surrealdb_handler.get_endpoint(endpoint, __file__) - if (check_json["meta"]["read"]["discord"] == True - and check_json["meta"]["read"]["guilded"] == True - and check_json["meta"]["read"]["revolt"] == True - and check_json["meta"]["read"]["nerimity"] == True): - check_json["meta"]["message"]["content"] = None - check_json["meta"]["message"]["attachments"].clear() - check_json["meta"]["message"]["author"]["avatar"] = None - check_json["meta"]["message"]["author"]["name"] = None - check_json["meta"]["message"]["author"]["id"] = None - check_json["meta"]["trigger"] = False - check_json["meta"]["sender"] = None - check_json["meta"]["sender-channel"] = None - try: - check_json["meta"]["message"]["isReply"] = False - check_json["meta"]["message"]["reply"]["message"] = None - check_json["meta"]["message"]["reply"]["author"] = None - except: - await health_check.HealthCheck.EndpointCheck.repair_structure(endpoint) - check_json["meta"]["message"]["isReply"] = False - check_json["meta"]["message"]["reply"]["message"] = None - check_json["meta"]["message"]["reply"]["author"] = None - check_json["meta"]["read"]["discord"] = False - check_json["meta"]["read"]["guilded"] = False - check_json["meta"]["read"]["revolt"] = False - check_json["meta"]["read"]["nerimity"] = False - await surrealdb_handler.update(endpoint, check_json) - print("Everything is read. Cleared.") - break - - await asyncio.sleep(1) - waiting_secs += 1 - if waiting_secs >= max_secs: - check_json["meta"]["message"]["content"] = None - check_json["meta"]["message"]["attachments"].clear() - check_json["meta"]["message"]["author"]["avatar"] = None - check_json["meta"]["message"]["author"]["name"] = None - check_json["meta"]["message"]["author"]["id"] = None - check_json["meta"]["trigger"] = False - check_json["meta"]["sender"] = None - check_json["meta"]["sender-channel"] = None - try: - check_json["meta"]["message"]["isReply"] = False - check_json["meta"]["message"]["reply"]["message"] = None - check_json["meta"]["message"]["reply"]["author"] = None - except: - await health_check.HealthCheck.EndpointCheck.repair_structure(endpoint) - check_json["meta"]["message"]["isReply"] = False - check_json["meta"]["message"]["reply"]["message"] = None - check_json["meta"]["message"]["reply"]["author"] = None - check_json["meta"]["read"]["discord"] = False - check_json["meta"]["read"]["guilded"] = False - check_json["meta"]["read"]["revolt"] = False - check_json["meta"]["read"]["nerimity"] = False - await surrealdb_handler.update(endpoint, check_json) - print("Not everything is read. Cleared anyways.") - break - else: return fastapi.responses.JSONResponse( status_code=200, diff --git a/src/astroidapi/queue_processor.py b/src/astroidapi/queue_processor.py index 11ee414..7dec2a6 100644 --- a/src/astroidapi/queue_processor.py +++ b/src/astroidapi/queue_processor.py @@ -7,7 +7,73 @@ import os -class QueueProcessor: +class QueueProcessor: + + @classmethod + async def clearLoadedMessage(cls, endpoint, message): + print(f"Clearing message for endpoint {endpoint}") + waiting_secs = 0 + max_secs = 10 + while True: + check_json = await surrealdb_handler.get_endpoint(endpoint, __file__) + if (check_json["meta"]["read"]["discord"] == True + and check_json["meta"]["read"]["guilded"] == True + and check_json["meta"]["read"]["revolt"] == True + and check_json["meta"]["read"]["nerimity"] == True): + check_json["meta"]["message"]["content"] = None + check_json["meta"]["message"]["attachments"].clear() + check_json["meta"]["message"]["author"]["avatar"] = None + check_json["meta"]["message"]["author"]["name"] = None + check_json["meta"]["message"]["author"]["id"] = None + check_json["meta"]["trigger"] = False + check_json["meta"]["sender"] = None + check_json["meta"]["sender-channel"] = None + try: + check_json["meta"]["message"]["isReply"] = False + check_json["meta"]["message"]["reply"]["message"] = None + check_json["meta"]["message"]["reply"]["author"] = None + except: + await health_check.HealthCheck.EndpointCheck.repair_structure(endpoint) + check_json["meta"]["message"]["isReply"] = False + check_json["meta"]["message"]["reply"]["message"] = None + check_json["meta"]["message"]["reply"]["author"] = None + check_json["meta"]["read"]["discord"] = False + check_json["meta"]["read"]["guilded"] = False + check_json["meta"]["read"]["revolt"] = False + check_json["meta"]["read"]["nerimity"] = False + await surrealdb_handler.update(endpoint, check_json) + print("Everything is read. Cleared.") + break + + await asyncio.sleep(1) + waiting_secs += 1 + if waiting_secs >= max_secs: + check_json["meta"]["message"]["content"] = None + check_json["meta"]["message"]["attachments"].clear() + check_json["meta"]["message"]["author"]["avatar"] = None + check_json["meta"]["message"]["author"]["name"] = None + check_json["meta"]["message"]["author"]["id"] = None + check_json["meta"]["trigger"] = False + check_json["meta"]["sender"] = None + check_json["meta"]["sender-channel"] = None + try: + check_json["meta"]["message"]["isReply"] = False + check_json["meta"]["message"]["reply"]["message"] = None + check_json["meta"]["message"]["reply"]["author"] = None + except: + await health_check.HealthCheck.EndpointCheck.repair_structure(endpoint) + check_json["meta"]["message"]["isReply"] = False + check_json["meta"]["message"]["reply"]["message"] = None + check_json["meta"]["message"]["reply"]["author"] = None + check_json["meta"]["read"]["discord"] = False + check_json["meta"]["read"]["guilded"] = False + check_json["meta"]["read"]["revolt"] = False + check_json["meta"]["read"]["nerimity"] = False + await surrealdb_handler.update(endpoint, check_json) + print("Not everything is read. Cleared anyways.") + break + + @classmethod async def appendMessage(cls, endpoint, updated_json): print(f"Appending message for endpoint {endpoint}") @@ -33,14 +99,17 @@ async def sendMessage(cls, endpoint): print(f"Loading message {message}") updated = await surrealdb_handler.QueueHandler.loadMessage(endpoint, message) await sending_handler.SendingHandler.distribute(endpoint, updated) + await cls.clearLoadedMessage(endpoint, message) return True elif len(queue) > 1: for message in queue: updated = await surrealdb_handler.QueueHandler.loadMessage(endpoint, message) await sending_handler.SendingHandler.distribute(endpoint, updated) + await cls.clearLoadedMessage(endpoint, message) return True else: raise Exception("Unknown error in sendMessage while processing queue. Queue length is unsupported type or negative.") + @classmethod async def handleUpdatedEndpointData(cls, endpoint, updated_json): diff --git a/src/astroidapi/sending_handler.py b/src/astroidapi/sending_handler.py index 962e275..0050be4 100644 --- a/src/astroidapi/sending_handler.py +++ b/src/astroidapi/sending_handler.py @@ -22,6 +22,7 @@ async def distribute(cls, endpoint, updated_json): try: sender = updated_json["meta"]["sender"] registered_platforms = [platform for platform in updated_json["config"]["channels"] if len(updated_json["config"]["channels"][platform]) > 0] + updated_json["meta"]["read"][sender] = True if len(updated_json["meta"]["message"]["attachments"]) > 0: attachments = [] diff --git a/src/astroidapi/surrealdb_handler.py b/src/astroidapi/surrealdb_handler.py index 483642a..fc56bc7 100644 --- a/src/astroidapi/surrealdb_handler.py +++ b/src/astroidapi/surrealdb_handler.py @@ -224,7 +224,8 @@ async def loadMessage(cls, endpoint: int, message: dict): print(message["sender-channel"]) first = await db.query(f"UPDATE endpoints:`{endpoint}` SET meta.sender = '{message['sender']}'") print(f"First: {first}") - second = await db.query(f"UPDATE endpoints:`{endpoint}` SET meta.`sender-channel` = '{message['sender-channel']}'") + second = await db.query(f"UPDATE endpoints:`{endpoint}` SET meta.read.{message['sender']} = true") + third = await db.query(f"UPDATE endpoints:`{endpoint}` SET meta.`sender-channel` = '{message['sender-channel']}'") last = await db.query(f"UPDATE endpoints:`{endpoint}` SET meta.message = {message}") await cls.remove_from_queue(endpoint, message) return await db.select(f"endpoints:`{endpoint}`")