(20241212) Day-end push.
This commit is contained in:
+52
-22
@@ -213,6 +213,26 @@ class MailController:
|
||||
)
|
||||
)
|
||||
|
||||
def drop_attachments(
|
||||
self,
|
||||
payload: dict
|
||||
):
|
||||
|
||||
# If the part is some sort of file:
|
||||
if payload["contentMainType"] not in ["multipart", "text"]:
|
||||
payload["payload"] = None
|
||||
payload["payloadId"] = None
|
||||
payload["payloadUrl"] = None
|
||||
|
||||
# If the payload is of multipart type,
|
||||
# we use recursion to look inside it:
|
||||
elif payload["contentMainType"] == "multipart":
|
||||
for part in payload["payload"]:
|
||||
self.drop_attachments(part)
|
||||
|
||||
# Done here:
|
||||
return payload
|
||||
|
||||
# ┏┓┏┓ ┓ ┏┓ ┏┓
|
||||
# ┃┃┣┫┓┏╋┣┓┏┛ ┃┫
|
||||
# ┗┛┛┗┗┻┗┛┗┗━•┗┛
|
||||
@@ -300,7 +320,7 @@ class MailController:
|
||||
# If we've not been forced to re-sync the mail message,
|
||||
# we first check if the mail already exists in our database:
|
||||
if not force_sync:
|
||||
mail_record = await current_app.core_message_controller.get_previews(
|
||||
mail_records = await current_app.core_message_controller.get_previews(
|
||||
mongo_conn = mongo_conn,
|
||||
token_ids = [ObjectId(token_id)],
|
||||
limit = 1,
|
||||
@@ -312,9 +332,12 @@ class MailController:
|
||||
"clientMessageId": message_id
|
||||
}
|
||||
)
|
||||
if mail_record:
|
||||
if mail_records:
|
||||
sync_result.success = True
|
||||
sync_result.message = f"gmail message '{message_id}' already sync'd on '{mail_record['readTs']} (UTC)'"
|
||||
sync_result.message = (
|
||||
f"gmail message '{message_id}' already "
|
||||
f"sync'd on '{mail_records[0].syncTs} (UTC)'"
|
||||
)
|
||||
return sync_result
|
||||
|
||||
# Now that we know that we have to fetch the mail from GMail:
|
||||
@@ -330,7 +353,7 @@ class MailController:
|
||||
return sync_result
|
||||
|
||||
# HANDLE ATTACHMENTS HERE:
|
||||
pass
|
||||
client_response.data["payload"] = self.drop_attachments(client_response.data["payload"])
|
||||
|
||||
# Now we structure the message into the model:
|
||||
mail_message = CoreMessageModel(
|
||||
@@ -341,7 +364,8 @@ class MailController:
|
||||
client = auth_token.client,
|
||||
clientMessageId = message_id,
|
||||
clientThreadId = client_response.data["threadId"],
|
||||
payload = client_response.data
|
||||
preview = client_response.data["subject"],
|
||||
message = client_response.data
|
||||
)
|
||||
|
||||
# Give a quick indicator of whether this mail is an inbox mail or sent mail:
|
||||
@@ -351,16 +375,17 @@ class MailController:
|
||||
else: mail_message.isSent = True
|
||||
|
||||
# Invoke the LLM:
|
||||
mail_message.aiSnippet = await self.summarize_mail_with_ai(
|
||||
ai_snippet = await self.summarize_mail_with_ai(
|
||||
mongo_conn = mongo_conn,
|
||||
user_info = user_info,
|
||||
llm = llm,
|
||||
message = mail_message
|
||||
)
|
||||
mail_message.aiSnippet = ai_snippet.summary
|
||||
|
||||
# Done here:
|
||||
print("ONE MAIL:", json.to_string(mail_message.model_dump(), default = str))
|
||||
sync_result.success = True
|
||||
sync_result.mailMessage = mail_message
|
||||
return sync_result
|
||||
|
||||
async def __sync_many_gmail(
|
||||
@@ -415,7 +440,7 @@ class MailController:
|
||||
query = query_string
|
||||
)
|
||||
if not client_response.success:
|
||||
sync_results["message"] = f"gmail: {client_response.message}"
|
||||
sync_results.message = f"gmail: {client_response.message}"
|
||||
return sync_results
|
||||
messages_list = client_response.data["messages"]
|
||||
|
||||
@@ -442,22 +467,27 @@ class MailController:
|
||||
for result in individual_sync_results:
|
||||
if result.success: sync_results.successCount += 1
|
||||
else: sync_results.failureCount += 1
|
||||
if result.mailMessage: mongo_operations.append(ReplaceOne(
|
||||
filter = {
|
||||
"tokenId": token_id,
|
||||
"serviceType": auth_token.serviceType,
|
||||
"client": auth_token.client,
|
||||
"clientMessageId": result.mailMessage.clientMessageId
|
||||
},
|
||||
replacement = result.mailMessage.model_dump(),
|
||||
upsert = True
|
||||
))
|
||||
if result.mailMessage:
|
||||
replacement_json = result.mailMessage.model_dump()
|
||||
replacement_json.pop("_id", None)
|
||||
mongo_operations.append(
|
||||
ReplaceOne(
|
||||
filter = {
|
||||
"tokenId": ObjectId(token_id),
|
||||
"serviceType": auth_token.serviceType,
|
||||
"client": auth_token.client,
|
||||
"clientMessageId": result.mailMessage.clientMessageId
|
||||
},
|
||||
replacement = replacement_json,
|
||||
upsert = True
|
||||
)
|
||||
)
|
||||
|
||||
# Make the bulk insert operation:
|
||||
if mongo_operations:
|
||||
sync_count = await current_app.core_message_controller.bulk_write(
|
||||
mongo_conn = mongo_conn,
|
||||
requests = mongo_operations
|
||||
mongo_operations = mongo_operations
|
||||
)
|
||||
|
||||
# Apply the labels to the read messages:
|
||||
@@ -496,7 +526,7 @@ class MailController:
|
||||
# ┻ ┗ ┗┗┛┗ ┻ ┗┛┛┗┗ ┛┗┛
|
||||
|
||||
# We first load the authorization tokens:
|
||||
auth_token = await current_app.mail_oauth_model.get_token(
|
||||
auth_token = await self.get_token(
|
||||
mongo_conn = mongo_conn,
|
||||
token_id = token_id,
|
||||
)
|
||||
@@ -556,5 +586,5 @@ if __name__ == "__main__":
|
||||
# raw_mail_json = json.from_file(file_options[1])
|
||||
# print("FROM FILE:", json.to_string(raw_mail_json["payload"]))
|
||||
# print("\n\n---------\n\n")
|
||||
# mail_model = MailAPIModel()
|
||||
# print(mail_model.extract_plaintext_parts(raw_mail_json["payload"]))
|
||||
# mail_controller = MailController()
|
||||
# print(json.to_string(mail_controller.drop_attachments(raw_mail_json["payload"])))
|
||||
|
||||
Reference in New Issue
Block a user