From 2c053fa6f2867a09b8a28993194bb41bd965805e Mon Sep 17 00:00:00 2001 From: khushal Date: Thu, 5 Dec 2024 15:07:07 +0530 Subject: [PATCH] (20241205) Accepting many token ids to list mails. --- api/blueprints/mail/list.py | 2 +- api/blueprints/mail/oauth_callback.py | 2 +- api/blueprints/sms/auth.py | 2 +- models/behaviour/mail/retrieve.py | 12 ++++++++---- models/behaviour/mail/sync_v2.py | 2 +- models/data/mail/list.py | 6 +++--- utils_v2/goog/models/data/auth_tokens.py | 4 ++++ 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/api/blueprints/mail/list.py b/api/blueprints/mail/list.py index c23ce82..5000baf 100644 --- a/api/blueprints/mail/list.py +++ b/api/blueprints/mail/list.py @@ -121,7 +121,7 @@ def init(blueprint_setup_state): # --------------------------------------------------------------------------------------------------------------------- -@mail_list_bp.route("/list/id/token", methods = ["GET"]) +@mail_list_bp.route("/list/id/token", methods = ["GET", "POST"]) @set_api_version(api_version = "1.0.0") @read_input(sanitize_headers = False, sanitize_data = False) @get_session_info(key = "X-Session-Token", session_coro = "get_session") diff --git a/api/blueprints/mail/oauth_callback.py b/api/blueprints/mail/oauth_callback.py index 9cc1fbc..b773c96 100644 --- a/api/blueprints/mail/oauth_callback.py +++ b/api/blueprints/mail/oauth_callback.py @@ -211,7 +211,7 @@ async def handle_gmail_callback() -> render_template: mongo_conn = current_app.data_mongo, session_token = g.inbound_headers.get("X-Session-Token"), token_id = g.inbound_data["state"], - client_user_id = {"email": tokens.email}, + client_user_id = tokens.client_user_id, token = tokens.model_dump() ) diff --git a/api/blueprints/sms/auth.py b/api/blueprints/sms/auth.py index 6b7d498..7693d40 100644 --- a/api/blueprints/sms/auth.py +++ b/api/blueprints/sms/auth.py @@ -176,7 +176,7 @@ async def request_oauth_authorization_url( client_user_id = { "userId": inbound_data.auth.userId, "senderId": inbound_data.auth.senderId, - "entityId": inbound_data.auth.enttiyId + "entityId": inbound_data.auth.entityId }, auth = inbound_data.auth.model_dump(), token = None, diff --git a/models/behaviour/mail/retrieve.py b/models/behaviour/mail/retrieve.py index 3cf1604..a0f01c9 100644 --- a/models/behaviour/mail/retrieve.py +++ b/models/behaviour/mail/retrieve.py @@ -46,7 +46,7 @@ from models.behaviour.base import BaseModel from bson import ObjectId # To work with datatypes: -from typing import Literal +from typing import Literal, List # For asynchronous activities: import asyncio @@ -142,7 +142,7 @@ class MailRetrieveModel(BaseModel): async def list_for_token_id( self, mongo_conn: AsyncMongo, - token_id: str | ObjectId, + token_id: str | ObjectId | List[str | ObjectId], limit: int = 25, skip: int = 0 ): @@ -150,16 +150,20 @@ class MailRetrieveModel(BaseModel): """ To enlist mails for one account. :param mongo_conn: The instance of the database connector to use to get the mail's data. - :param token_id: The id of the document in the database that holds the tokens to access the account. + :param token_id: The id(s) of the document in the database that holds the tokens to access the account. :param limit: How many records to fetch. :param skip: How many initial records to skip. useful for pagination. :return: Either the JSON that describes the mails or None if something failed. """ + # Ensure that the token ids are in expected format: + if not isinstance(token_id, list): token_id = [token_id] + token_id = [ObjectId(t) for t in token_id] + # Get the data from the database: mails_list = await mongo_conn.find_many( collection = self.MAIL_COLLECTION, - filter = {"tokenId": ObjectId(token_id)}, + filter = {"tokenId": {"$in": token_id}}, projection = { "_id": True, "serviceType": True, diff --git a/models/behaviour/mail/sync_v2.py b/models/behaviour/mail/sync_v2.py index 4f271dc..fd93f4b 100644 --- a/models/behaviour/mail/sync_v2.py +++ b/models/behaviour/mail/sync_v2.py @@ -389,7 +389,7 @@ class MailSyncModel(BaseModel): db_conn = current_app.sql_writer, mongo_conn = mongo_conn, token_id = token_id, - email_id = tokens.email, + client_user_id = tokens.client_user_id, token = tokens, session_token = session_token ) diff --git a/models/data/mail/list.py b/models/data/mail/list.py index 79cb935..03081ea 100644 --- a/models/data/mail/list.py +++ b/models/data/mail/list.py @@ -37,7 +37,7 @@ sys.path.append("..") # For making data behaviour_models: from pydantic import BaseModel, Field, field_validator, PastDatetime -from typing import Optional, Literal +from typing import Optional, Literal, List # My utils: from utils_v2.string import regex @@ -101,8 +101,8 @@ class MailListRequestHeaders(BaseModel): class MailListByAccountIdRequestData(BaseModel): - tokenId: str = Field( - description = "the account identifier (Mongo ObjectId) granted by 'MailOAuthModel.get_account_identifier'", + tokenId: str | List[str] = Field( + description = "the account identifier(s) (Mongo ObjectId) granted by 'MailOAuthModel.get_account_identifier'", frozen = True ) diff --git a/utils_v2/goog/models/data/auth_tokens.py b/utils_v2/goog/models/data/auth_tokens.py index ad4cd41..6dc568b 100644 --- a/utils_v2/goog/models/data/auth_tokens.py +++ b/utils_v2/goog/models/data/auth_tokens.py @@ -118,6 +118,10 @@ class GoogleAuthTokens(BaseModel): # ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛ # ┛ + @property + def client_user_id(self): + return {"email": self.email} + @property def expired(self): return True if date_time.get_current_utc_date_time() >= self.expiresAt else False