From e749a27a924db5dbb7c64412ef480824bf9223ce Mon Sep 17 00:00:00 2001 From: khushal Date: Tue, 3 Dec 2024 16:59:00 +0530 Subject: [PATCH] (20241203) Working with labels. --- api/blueprints/mail/oauth_callback.py | 34 +++++++++++++++++++++++++++ models/behaviour/mail/sync_v2.py | 11 ++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/api/blueprints/mail/oauth_callback.py b/api/blueprints/mail/oauth_callback.py index 730cfd2..553875a 100644 --- a/api/blueprints/mail/oauth_callback.py +++ b/api/blueprints/mail/oauth_callback.py @@ -153,6 +153,40 @@ async def handle_gmail_callback() -> render_template: failure_hint = f"We were expecting authorization from '{placeholder_token['clientUserId']['email']}' but got authorization from '{tokens.email}' instead." ) + # We create standard labels that we will use: + labels = [ + { + "name": "TCAOFF", + "textColor": "#434343", + "backgroundColor": "#cccccc" + }, + { + "name": "CA-Doc", + "textColor": "#434343", + "backgroundColor": "#cccccc" + }, + { + "name": "CA-AI", + "textColor": "#434343", + "backgroundColor": "#cccccc" + } + ] + tasks = [ + current_app.gmail_client.create_label( + tokens = tokens, + label_name = label["name"], + label_visibility = "labelShow", + message_visibility = "show", + label_text_color = label["textColor"], + label_background_color = label["backgroundColor"] + ) for label in labels + ] + client_responses = await asyncio.gather(*tasks) + + # Add the labels to the tokens data: + client_response = await current_app.gmail_client.list_labels(tokens = tokens) + tokens.labels = client_response.data if client_response.success else None + # Now that we have passed the check, # we save the tokens to the database: tokens_saved = await current_app.mail_oauth_model.set_token( diff --git a/models/behaviour/mail/sync_v2.py b/models/behaviour/mail/sync_v2.py index 591b458..91873bb 100644 --- a/models/behaviour/mail/sync_v2.py +++ b/models/behaviour/mail/sync_v2.py @@ -31,6 +31,8 @@ # To make sibling directories accessible for imports: import sys +from logging import exception + sys.path.append(".") sys.path.append("..") @@ -134,7 +136,6 @@ class MailSyncModel(BaseModel): @staticmethod async def __save_one_attachment( - self, session_token: str, attachment: Dict[str, Any], attachment_tags: List[str], @@ -453,6 +454,14 @@ class MailSyncModel(BaseModel): requests = mongo_operations ) + # Apply the labels to the read messages: + try: client_response = await mail_client.modify_messages( + tokens = tokens, + message_ids = [v["id"] for v in messages_list], + add_label_ids = [tokens.labels.get("TCAOFF")] + ) + except Exception as exception: self._printer(exception) + # Done here: sync_results.message = f"{sync_results.successCount}/{sync_results.totalCount} mail(s) sync'd from gmail" return sync_results