From a550f5f189a8d99619475fb9d5847e136cc085bf Mon Sep 17 00:00:00 2001 From: Jason <81298350+Deutscher775@users.noreply.github.com> Date: Sat, 31 Aug 2024 20:08:06 +0200 Subject: [PATCH] fixed bug, added eligible_enpoints list for attachmenr support --- src/api.py | 2 +- src/astroidapi/attachment_processor.py | 5 +++-- src/astroidapi/errors.py | 5 +++++ src/astroidapi/sending_handler.py | 6 ++++-- src/astroidapi/surrealdb_handler.py | 15 +++++++++++++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/api.py b/src/api.py index bcde7bb..9e19e25 100644 --- a/src/api.py +++ b/src/api.py @@ -315,7 +315,7 @@ async def clear_temporary_attachments(master_token: Annotated[str, fastapi.Query if master_token == Bot.config.MASTER_TOKEN: try: total_files = len(os.listdir(f"{pathlib.Path(__file__).parent.resolve()}/astroidapi/TMP_attachments")) - 1 - astroidapi.attachment_processor.force_clear_temporary_attachments() + await astroidapi.attachment_processor.force_clear_temporary_attachments() requests.post("https://discord.com/api/webhooks/1279497897016299553/3GrZI75dDYwIkwYBac4o2ApJgzlVVCPIZnon_iE5RtaRIyiYUwcdaXxA327oNZyWZXs4", json={"content": f"[Astroid API - TMP Attachments] Deleted {total_files} temporary attachments."}) return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."}) except Exception as e: diff --git a/src/astroidapi/attachment_processor.py b/src/astroidapi/attachment_processor.py index 88e8bc7..9af32c8 100644 --- a/src/astroidapi/attachment_processor.py +++ b/src/astroidapi/attachment_processor.py @@ -7,7 +7,7 @@ import string async def download_attachment(attachment_url, registeredPlatforms): - try: + try: response = requests.get(attachment_url) if response.status_code == 200: if int(response.headers["content-length"]) > 50 * 1024 * 1024: # value in B -> KB -> MB @@ -68,12 +68,13 @@ def clear_temporary_attachment(attachment_path): except Exception as e: raise errors.AttachmentProcessError.AttachmentClearError.DeletionError(f"Error deleting temporary attachment. Error: {e}") -def force_clear_temporary_attachments(): +async def force_clear_temporary_attachments(): try: path = f"{pathlib.Path(__file__).parent.resolve()}/TMP_attachments" for file in pathlib.Path(path).iterdir(): if file.name == ".placeholder": continue + await surrealdb_handler.AttachmentProcessor.delete_attachment(file.name.split('.')[0]) file.unlink() except Exception as e: raise errors.AttachmentProcessError.AttachmentClearError.DeletionError(f"Error deleting temporary attachments. Error: {e}") \ No newline at end of file diff --git a/src/astroidapi/errors.py b/src/astroidapi/errors.py index fd9ce49..3d7d9a0 100644 --- a/src/astroidapi/errors.py +++ b/src/astroidapi/errors.py @@ -167,6 +167,11 @@ class GetAttachmentError(Exception): def __init__(self, message): self.message = message super().__init__(self.message) + + class CheckEligibilityError(Exception): + def __init__(self, message): + self.message = message + super().__init__(self.message) class ReadHandlerError: diff --git a/src/astroidapi/sending_handler.py b/src/astroidapi/sending_handler.py index 5944aba..059722d 100644 --- a/src/astroidapi/sending_handler.py +++ b/src/astroidapi/sending_handler.py @@ -23,8 +23,10 @@ async def distribute(cls, endpoint, updated_json): registered_platforms = [platform for platform in updated_json["config"]["channels"] if len(updated_json["config"]["channels"][platform]) > 0] - print("Author ID: " + updated_json["meta"]["message"]["author"]["id"]) - if updated_json["meta"]["message"]["author"]["id"] == "-11111": + is_eligible = await surrealdb_handler.AttachmentProcessor.check_eligibility(endpoint) + + print(f"Is eligible: {is_eligible}") + if is_eligible is True: if len(updated_json["meta"]["message"]["attachments"]) > 0: attachments = [] for attachment in updated_json["meta"]["message"]["attachments"]: diff --git a/src/astroidapi/surrealdb_handler.py b/src/astroidapi/surrealdb_handler.py index ff4e203..4946861 100644 --- a/src/astroidapi/surrealdb_handler.py +++ b/src/astroidapi/surrealdb_handler.py @@ -169,6 +169,8 @@ async def update_attachment(cls, attachment_id: str, status: str = None, sentby: @classmethod async def delete_attachment(cls, attachment_id: str): + if attachment_id == "eligible_endpoints": + raise errors.SurrealDBHandler.DeleteAttachmentError("Cannot delete eligible_endpoints") try: async with Surreal(config.SDB_URL) as db: await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS}) @@ -187,6 +189,19 @@ async def get_attachment(cls, attachment_id: str): return await db.select(f"attachments:`{attachment_id}`") except Exception as e: raise errors.SurrealDBHandler.GetAttachmentError(e) + + @classmethod + async def check_eligibility(cls, endpoint: int): + try: + async with Surreal(config.SDB_URL) as db: + await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS}) + await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE) + elegible_endpoints = await db.select("attachments:eligible_endpoints") + if endpoint in elegible_endpoints["endpoints"]: + return True + + except Exception as e: + raise errors.SurrealDBHandler.CheckEligibilityError(e) class GetEndpoint: