(20241129) ??
This commit is contained in:
@@ -10,7 +10,8 @@
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
Here we perform one-time mail syncing activities for our users.
|
||||
To define the interaction between the UI layer and the database connectivity in one place. Here we shall handle
|
||||
all the activities for OAuth2.0 authorization requests for all the users of our service.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -37,8 +38,12 @@ sys.path.append("..")
|
||||
# My async utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.date_time import date_time
|
||||
from utils_v2.database.async_mysql_v2 import AsyncMySQL
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||
|
||||
# Base model:
|
||||
from models.behaviour.base import BaseModel
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
|
||||
@@ -86,105 +91,22 @@ import copy
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class MailSyncModel:
|
||||
class MailReadModel(BaseModel):
|
||||
|
||||
AUTH_COLLECTION = "_authTokens"
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
async def get_id(
|
||||
db_conn: AsyncMongo,
|
||||
async def read_one(
|
||||
self,
|
||||
db_conn: AsyncMySQL,
|
||||
mongo_conn: AsyncMongo,
|
||||
user_info: dict,
|
||||
service_type: Literal["email", "chat"],
|
||||
service_client: Literal["gmail"],
|
||||
auth_type: Literal["oauth"]
|
||||
auth_type: Literal["oauth"],
|
||||
session_token: str = None
|
||||
) -> ObjectId:
|
||||
|
||||
"""
|
||||
Stores params from the session info and gives an identifier to use in the authorization URL. Use this when the
|
||||
user requests an authorization URL to link your service to another service (like GMail).
|
||||
:param db_conn: The database connection to use to perform the action.
|
||||
:param user_info: The dictionary that has the user's session information.
|
||||
:param service_type: The type of service being provided.
|
||||
:param service_client: The name of the company or brand that is providing this service that is being integrated.
|
||||
:param auth_type: To identify the type of authentication being done here. This could indicate simple password
|
||||
authentication, more advance OAuth2.0 authentication, etc.
|
||||
:return: An ObjectId to later store the granted tokens.
|
||||
"""
|
||||
|
||||
# Note down the timestamp at which this event occurred:
|
||||
request_ts = date_time.get_current_utc_date_time(as_string = False)
|
||||
|
||||
# Get the identifier from the database:
|
||||
db_json = await db_conn.find_one_and_update(
|
||||
collection = MailOAuthModel.AUTH_COLLECTION,
|
||||
filter = {
|
||||
"serviceType": service_type,
|
||||
"client": service_client,
|
||||
"authType": auth_type,
|
||||
"user": user_info,
|
||||
},
|
||||
update = {
|
||||
"$set": {
|
||||
"lastRequestTs": request_ts
|
||||
},
|
||||
"$setOnInsert": {
|
||||
"version": "1.0.0",
|
||||
"serviceType": service_type,
|
||||
"client": service_client,
|
||||
"authType": auth_type,
|
||||
"user": user_info,
|
||||
"token": None,
|
||||
"firstRefreshTs": None,
|
||||
"lastRefreshTs": None,
|
||||
"firstRequestTs": request_ts,
|
||||
}
|
||||
},
|
||||
projection = {
|
||||
"_id": True
|
||||
},
|
||||
upsert = True,
|
||||
return_updated = True
|
||||
)
|
||||
|
||||
# Done here:
|
||||
return db_json["_id"] if db_json else None
|
||||
|
||||
@staticmethod
|
||||
async def set_token(
|
||||
db_conn: AsyncMongo,
|
||||
user_identifier: ObjectId | str,
|
||||
token: dict
|
||||
) -> bool:
|
||||
|
||||
"""
|
||||
This method is to be called when the end user authorizes your service to connect to his third-party account. For
|
||||
example, when the end user allows you to access his GMail account.
|
||||
:param db_conn: The database connection to use to perform the action.
|
||||
:param user_identifier: The identifier granted by the 'get_id' method.
|
||||
:param token: The token granted by the third-party service.
|
||||
:return:
|
||||
"""
|
||||
|
||||
# Note down the timestamp at which this event occurred:
|
||||
request_ts = date_time.get_current_utc_date_time(as_string = False)
|
||||
|
||||
# Save the token to the database:
|
||||
token_saved = await db_conn.update_one(
|
||||
collection = MailOAuthModel.AUTH_COLLECTION,
|
||||
filter = {"_id": ObjectId(user_identifier)},
|
||||
update = {
|
||||
"$set": {
|
||||
"token": token,
|
||||
"firstRefreshTs": request_ts,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
# Done here:
|
||||
return token_saved
|
||||
pass
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
|
||||
@@ -905,10 +905,6 @@ if __name__ == "__main__":
|
||||
oauth_json = secrets_dict,
|
||||
http_client = test_client,
|
||||
redirect_url = r"https://api.thecaoffice.com/converse/mail/callback/gmail",
|
||||
scopes = [
|
||||
r"https://www.googleapis.com/auth/gmail.modify",
|
||||
r"https://www.googleapis.com/auth/gmail.labels"
|
||||
],
|
||||
debug = True,
|
||||
debug_prefix = "GMail (M) | ",
|
||||
debug_only_errors = False
|
||||
@@ -961,10 +957,12 @@ if __name__ == "__main__":
|
||||
# print(my_mail.get_raw_message(as_base64 = False))
|
||||
|
||||
# Test some feature:
|
||||
response = await my_gmail.send_message(
|
||||
# response = await my_gmail.list_messages(
|
||||
# tokens = test_tokens,
|
||||
# )
|
||||
response = await my_gmail.get_message(
|
||||
tokens = test_tokens,
|
||||
message = my_mail,
|
||||
thread_id = None
|
||||
message_id = "1936bfbfc912b86f"
|
||||
)
|
||||
print("SUCCESS:", response.success)
|
||||
print("SUMMARY:", response.to_markdown())
|
||||
|
||||
Reference in New Issue
Block a user