(20250117) Major revamping in the mail module. Everything revamped. Sending is a pending task.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Tuesday, 3rd Dec., 2024
|
||||
Friday, 17th Jan., 2025.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
@@ -178,7 +178,7 @@ async def get_one_mail(
|
||||
|
||||
# Get the mail:
|
||||
message = await current_app.mail_controller.get_one_mail(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
message_id = inbound_data.messageId
|
||||
)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Tuesday, 3rd Dec., 2024
|
||||
Friday, 17th Jan., 2025.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
@@ -181,7 +181,7 @@ async def list_mails(
|
||||
|
||||
# Get the token ids from the token keys:
|
||||
auth_tokens = await current_app.mail_controller.get_tokens_from_keys(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
token_keys = inbound_data.tokenKeys
|
||||
)
|
||||
token_ids = [t.authTokenId for t in auth_tokens]
|
||||
@@ -193,7 +193,7 @@ async def list_mails(
|
||||
|
||||
# Get the mails:
|
||||
mails_list = await current_app.mail_controller.list_mails(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
token_ids = token_ids,
|
||||
limit = inbound_data.count,
|
||||
skip = inbound_data.fromCount,
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Monday, 2nd Dec., 2024
|
||||
Friday, 17th Jan., 2025.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To receive requests for synchronising mails from various mail clients to the database. Sync'ing means we pull
|
||||
the mail from the mail client (like GMail) and store it to our database. The mail is then ready for showing on
|
||||
the mail from the mail client (like Gmail) and store it to our database. The mail is then ready for showing on
|
||||
the UI at any time.
|
||||
|
||||
REFERENCES:
|
||||
@@ -129,7 +129,8 @@ def init(blueprint_setup_state):
|
||||
async def sync_mails(
|
||||
user_info: CoreUserInfoModel,
|
||||
inbound_headers: dict,
|
||||
inbound_data: MailSyncRequestData
|
||||
inbound_data: MailSyncRequestData,
|
||||
is_background: bool = True
|
||||
) -> MailSyncManyResults:
|
||||
|
||||
"""
|
||||
@@ -138,23 +139,54 @@ async def sync_mails(
|
||||
:param user_info: The information of the user as extracted from the session token.
|
||||
:param inbound_headers: The headers that came in with the request.
|
||||
:param inbound_data: The data that came in with the request.
|
||||
:param is_background: To know whether, or not, this request is being services in the background. If it is happening
|
||||
in the foreground, user feedback is not a concern. But, if it is happening in the background, user feedback will
|
||||
be a concern. This flag will help alter the treatment of sending alerts.
|
||||
:return: The results of the mail-sync'ing attempt.
|
||||
"""
|
||||
|
||||
# Try to sync the mails:
|
||||
return await current_app.mail_controller.sync(
|
||||
db_conn = current_app.sql_writer,
|
||||
mongo_conn = current_app.data_mongo,
|
||||
user_info = user_info,
|
||||
# Start by assuming failure:
|
||||
client_controller = None
|
||||
client_connector = None
|
||||
sync_results = MailSyncManyResults()
|
||||
|
||||
# Get the auth-token from the key:
|
||||
auth_token = await current_app.mail_controller.get_token_from_key(
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
token_key = inbound_data.tokenKey,
|
||||
llm = current_app.llm,
|
||||
force_sync = inbound_data.forceSync,
|
||||
start_date = inbound_data.startDate,
|
||||
end_date = inbound_data.endDate,
|
||||
max_count = inbound_data.maxCount,
|
||||
session_token = inbound_headers["X-Session-Token"],
|
||||
)
|
||||
|
||||
# Figure out the client connector:
|
||||
match auth_token.client:
|
||||
case "gmail": client_controller, client_connector = current_app.gmail_controller, current_app.gmail_client
|
||||
case _: client_controller, client_connector = None, None
|
||||
|
||||
# Invoke the client:
|
||||
if client_controller is not None and client_connector is not None:
|
||||
sync_results = await client_controller.sync_mails(
|
||||
sql_conn = current_app.sql_writer,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
mail_client = client_connector,
|
||||
auth_token = auth_token,
|
||||
user_info = user_info,
|
||||
llm = current_app.llm,
|
||||
force_sync = inbound_data.forceSync,
|
||||
start_date = inbound_data.startDate,
|
||||
end_date = inbound_data.endDate,
|
||||
max_count = inbound_data.maxCount,
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
|
||||
# If the client was not matched:
|
||||
else: sync_results.message = f"Invalid/unimplemented client '{auth_token.client}'"
|
||||
|
||||
# Send an alert if this is a background process:
|
||||
if is_background:
|
||||
pass
|
||||
|
||||
# Done here:
|
||||
return sync_results
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -206,29 +238,27 @@ async def sync_mail(
|
||||
http_code = HttpCodes.UNAUTHORIZED
|
||||
)
|
||||
|
||||
# Make the variables available in the scope of the current request:
|
||||
g.inbound_headers = inbound_headers
|
||||
g.inbound_data = inbound_data
|
||||
|
||||
# If we've been asked to sync the mails in the background:
|
||||
if mode in ["background", "bg"]:
|
||||
current_app.add_background_task(
|
||||
sync_mails,
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
inbound_headers = inbound_headers,
|
||||
inbound_data = inbound_data
|
||||
inbound_data = inbound_data,
|
||||
is_background = True
|
||||
)
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK,
|
||||
http_code = HttpCodes.ACCEPTED,
|
||||
message = "your mails are being sync'd in the background"
|
||||
message = "Your mails are being sync'd in the background."
|
||||
)
|
||||
|
||||
# Otherwise we process it right here:
|
||||
sync_results = await sync_mails(
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
inbound_headers = inbound_headers,
|
||||
inbound_data = inbound_data
|
||||
inbound_data = inbound_data,
|
||||
is_background = False
|
||||
)
|
||||
|
||||
# Response:
|
||||
|
||||
@@ -177,7 +177,7 @@ async def update_mail_tags(
|
||||
|
||||
# Get the mail:
|
||||
message = await current_app.mail_controller.get_one_mail(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
message_id = inbound_data.messageId
|
||||
)
|
||||
|
||||
@@ -204,7 +204,7 @@ async def update_mail_tags(
|
||||
|
||||
# Update the mail:
|
||||
success = await current_app.mail_controller.update_mail_tags(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
message_id = inbound_data.messageId,
|
||||
unset_tags = inbound_data.unsetTags,
|
||||
set_tags = inbound_data.setTags
|
||||
|
||||
@@ -180,7 +180,7 @@ async def update_sms_tags(
|
||||
|
||||
# Check if the token(s) belong to the user claiming ownership:
|
||||
if not await token_check.is_authorized(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
token_ids = [message.tokenId]
|
||||
): return ResponseModel(
|
||||
@@ -195,7 +195,7 @@ async def update_sms_tags(
|
||||
# ┛ ┛
|
||||
|
||||
# Update the message:
|
||||
success = await current_app.sms_controller.update_tags(
|
||||
success = await current_app.sms_controller.update_sms_tags(
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
message_id = inbound_data.messageId,
|
||||
unset_tags = inbound_data.unsetTags,
|
||||
|
||||
Reference in New Issue
Block a user