(20250117) Major revamping in the mail module. Everything revamped. Sending is a pending task.

This commit is contained in:
2025-01-17 15:36:59 +05:30
parent 32c243f3d3
commit be9bdf797a
12 changed files with 675 additions and 65 deletions
@@ -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,
+51 -21
View File
@@ -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
+2 -2
View File
@@ -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,
+12 -5
View File
@@ -80,6 +80,7 @@ from controllers_v2.message.sms.all_sms import AllSMSController
from controllers_v2.message.sms.nimbus_sms_india import NimbusSMSIndiaController
from controllers_v2.message.sms.savvy_bulk_sms_kenya import SavvyBulkSMSKenyaController
# ---
from controllers_v2.message.mail.all_mail import AllMailController
from controllers_v2.message.mail.gmail import GmailController
# ---
from controllers_v2.message.chat.all_chat import AllChatController
@@ -102,10 +103,10 @@ from icecream import IceCreamDebugger
# Mail Blueprints:
from api.blueprints.message.mail.oauth.request_v2 import mail_oauth_request_bp
from api.blueprints.message.mail.oauth.callback_v2 import mail_oauth_callback_bp
from api.blueprints.message.mail.sync.sync_v2 import mail_sync_bp
from api.blueprints.message.mail.retrieve.list import mail_list_bp
from api.blueprints.message.mail.retrieve.get import mail_get_bp
from api.blueprints.message.mail.tags.update import mail_tags_update_bp
from api.blueprints.message.mail.sync.sync_v3 import mail_sync_bp
from api.blueprints.message.mail.retrieve.list_v2 import mail_list_bp
from api.blueprints.message.mail.retrieve.get_v2 import mail_get_bp
from api.blueprints.message.mail.tags.update_v2 import mail_tags_update_bp
from api.blueprints.message.mail.send.send import mail_send_bp
# SMS Blueprints:
@@ -424,7 +425,7 @@ async def app_startup(**kwargs):
# ┣┫┃┃┃ ┃ ┏┓┏┓╋┏┓┏┓┃┃┏┓┏┓┏
# ┛┗┣┛┻ ┗┛┗┛┛┗┗┛ ┗┛┗┗┗ ┛ ┛
current_app.mail_controller = MailController()
# current_app.mail_controller = MailController()
# current_app.sms_controller = SMSController()
# current_app.payment_controller = PaymentController()
@@ -461,6 +462,12 @@ async def app_startup(**kwargs):
)
# Messages / Mail Controllers:
current_app.mail_controller = AllMailController(
cache = current_app.module_cache,
http_client = current_app.http_client,
alert_url = current_app.script_data["alerts"]["url"],
debug = enable_debugging
)
current_app.gmail_controller = GmailController(
cache = current_app.module_cache,
http_client = current_app.http_client,