(20250118) Ready to test Mail Sync (Cron) on the server.

This commit is contained in:
2025-01-18 13:14:42 +05:30
parent 766b196a80
commit 258b2fdbaf
8 changed files with 80 additions and 44 deletions
+2 -2
View File
@@ -128,7 +128,7 @@ class CoreLLMController(BaseModel):
async def invoke(
self,
mongo_conn: AsyncMongo,
mongo_data_conn: AsyncMongo,
user_info: CoreUserInfoModel,
llm_input: LLMInput
) -> LLMOutput:
@@ -158,7 +158,7 @@ class CoreLLMController(BaseModel):
# Store this into MongoDB:
mongo_document = {"user": user_info.model_dump()}
for k, v in llm_response.model_dump().items(): mongo_document[k] = v
inserted_id = await mongo_conn.insert_one(
inserted_id = await mongo_data_conn.insert_one(
collection = self.AI_USAGE_COLLECTION,
document = mongo_document
)
+32 -25
View File
@@ -474,31 +474,32 @@ class GmailController(MailController):
# Start by assuming failure:
sync_result = MailSyncOneResult()
# 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_records = await self.get_message_previews(
mongo_data_conn = mongo_data_conn,
token_ids = [ObjectId(auth_token.authTokenId)],
limit = 1,
skip = 0,
additional_filter = {
"tokenId": ObjectId(auth_token.authTokenId),
"serviceType": auth_token.serviceType,
"client": auth_token.client,
"clientMessageId": message_id
}
# Check if the mail already exists in your database:
mail_records = await self.get_message_previews(
mongo_data_conn = mongo_data_conn,
token_ids = [ObjectId(auth_token.authTokenId)],
limit = 1,
skip = 0,
additional_filter = {
"tokenId": ObjectId(auth_token.authTokenId),
"serviceType": auth_token.serviceType,
"client": auth_token.client,
"clientMessageId": message_id
}
)
if mail_records:
sync_result.success = True
sync_result.isNew = False
sync_result.message = (
f"Gmail message '{message_id}' already "
f"sync'd on {mail_records[0].syncTs} (UTC)."
)
if mail_records:
sync_result.success = True
sync_result.message = (
f"Gmail message '{message_id}' already "
f"sync'd on {mail_records[0].syncTs} (UTC)."
)
print(sync_result)
return sync_result
# If the mail already exists and we have not been asked to force-sync:
if not sync_result.isNew and not force_sync: return sync_result
# Now that we know that we have to fetch the mail from GMail:
sync_result.attempted = True
client_response = await mail_client.get_message(
tokens = google_tokens,
message_id = message_id,
@@ -660,8 +661,14 @@ class GmailController(MailController):
sync_results.totalCount = len(individual_sync_results)
mongo_operations = []
for result in individual_sync_results:
if result.success: sync_results.successCount += 1
else: sync_results.failureCount += 1
# Maintain the result counters:
if result.attempted:
if result.success: sync_results.successCount += 1
else: sync_results.failureCount += 1
if result.isNew: sync_results.newCount += 1
# Create the database requests:
if result.mailMessage:
replacement_json = result.mailMessage.model_dump()
replacement_json.pop("_id", None)
@@ -678,7 +685,7 @@ class GmailController(MailController):
)
)
# Make the bulk insert operation:
# Make the bulk operation:
if mongo_operations:
sync_count = await self.bulk_operate_messages(
mongo_data_conn = mongo_data_conn,