Skip to content

Commit

Permalink
fixed time delay of cached messages being sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Dec 2, 2024
1 parent f5326f6 commit a1e3f51
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 0 additions & 62 deletions src/astroidapi/endpoint_update_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."})

Expand Down Expand Up @@ -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,
Expand Down
71 changes: 70 additions & 1 deletion src/astroidapi/queue_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions src/astroidapi/sending_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
3 changes: 2 additions & 1 deletion src/astroidapi/surrealdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}`")
Expand Down

0 comments on commit a1e3f51

Please sign in to comment.