Skip to content

Commit

Permalink
fixed bug, added eligible_enpoints list for attachmenr support
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Aug 31, 2024
1 parent 593cab4 commit a550f5f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/astroidapi/attachment_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
5 changes: 5 additions & 0 deletions src/astroidapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/astroidapi/sending_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
15 changes: 15 additions & 0 deletions src/astroidapi/surrealdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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:

Expand Down

0 comments on commit a550f5f

Please sign in to comment.