(20250117) Major revamping in the mail module. Everything revamped. Sending is a pending task.
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user