(20241213) Mail Module Reworked!
This commit is contained in:
@@ -110,7 +110,7 @@ def init(blueprint_setup_state):
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@llm_invoke_bp.route("/llm/invoke", methods = ["POST"])
|
||||
@llm_invoke_bp.route("/llm/invoke", methods = ["GET"])
|
||||
@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")
|
||||
|
||||
@@ -81,7 +81,7 @@ import asyncio
|
||||
|
||||
|
||||
# Related to Quart:
|
||||
mail_callback_bp = Blueprint("mail_cb", __name__)
|
||||
mail_oauth_callback_bp = Blueprint("mail_cb", __name__)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -101,7 +101,7 @@ mail_callback_bp = Blueprint("mail_cb", __name__)
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
@mail_callback_bp.record_once
|
||||
@mail_oauth_callback_bp.record_once
|
||||
def init(blueprint_setup_state):
|
||||
|
||||
# This gets called when the blueprint is registered.
|
||||
@@ -239,7 +239,7 @@ async def handle_gmail_callback() -> render_template:
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@mail_callback_bp.route("/callback/<mail_client>", methods = ["POST", "GET"])
|
||||
@mail_oauth_callback_bp.route("/callback/<mail_client>", methods = ["POST", "GET"])
|
||||
@set_api_version(api_version = "1.0.0")
|
||||
@read_input(sanitize_headers = False, sanitize_data = False)
|
||||
@log_request_to_mongo(
|
||||
|
||||
@@ -85,7 +85,7 @@ import asyncio
|
||||
|
||||
|
||||
# Related to Quart:
|
||||
mail_oauth_bp = Blueprint("mail_oauth", __name__)
|
||||
mail_oauth_request_bp = Blueprint("mail_oauth", __name__)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -105,7 +105,7 @@ mail_oauth_bp = Blueprint("mail_oauth", __name__)
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
@mail_oauth_bp.record_once
|
||||
@mail_oauth_request_bp.record_once
|
||||
def init(blueprint_setup_state):
|
||||
|
||||
# This gets called when the blueprint is registered.
|
||||
@@ -116,7 +116,7 @@ def init(blueprint_setup_state):
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@mail_oauth_bp.route("/oauth", methods = ["GET"])
|
||||
@mail_oauth_request_bp.route("/oauth", methods = ["GET"])
|
||||
@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")
|
||||
|
||||
@@ -36,8 +36,8 @@
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
sys.path.append("../..")
|
||||
|
||||
# For using Quart:
|
||||
from quart import Blueprint, current_app, g, request
|
||||
@@ -136,13 +136,13 @@ def init(blueprint_setup_state):
|
||||
@log_chain_to_mongo(attr_name = "logs_mongo")
|
||||
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
||||
@validate_input(
|
||||
header_validator = lambda x: MailGetRequestHeaders(**x).model_dump(),
|
||||
data_validator = lambda x: MailGetRequestData(**x)
|
||||
header_validator = lambda x: MailUpdateTagsRequestHeaders(**x).model_dump(),
|
||||
data_validator = lambda x: MailUpdateTagsRequestData(**x)
|
||||
)
|
||||
@handle_cancelled_request()
|
||||
async def update_mail_tags(
|
||||
inbound_headers: dict | MailGetRequestHeaders = None,
|
||||
inbound_data: dict | MailGetRequestData = None,
|
||||
inbound_headers: dict | MailUpdateTagsRequestHeaders = None,
|
||||
inbound_data: dict | MailUpdateTagsRequestData = None,
|
||||
inbound_files: dict = None,
|
||||
**kwargs
|
||||
):
|
||||
@@ -164,17 +164,18 @@ async def update_mail_tags(
|
||||
)
|
||||
|
||||
# Get the mail:
|
||||
message = await current_app.mail_controller.get_one_mail(
|
||||
success = await current_app.mail_controller.update_tags(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
token_id = inbound_data.tokenId,
|
||||
message_id = inbound_data.messageId
|
||||
message_id = inbound_data.messageId,
|
||||
unset_tags = inbound_data.unsetTags,
|
||||
set_tags = inbound_data.setTags
|
||||
)
|
||||
|
||||
# Done here:
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK if message else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if message else HttpCodes.NOT_FOUND,
|
||||
data = message.full
|
||||
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ def init(blueprint_setup_state):
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@test_callback_bp.route("/callback", methods = ["POST", "GET"])
|
||||
@test_callback_bp.route("/callback", methods = ["GET", "POST", "PUT", "PATCH", "DELETE"])
|
||||
@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")
|
||||
@@ -131,21 +131,7 @@ async def callback_test(
|
||||
**kwargs
|
||||
):
|
||||
|
||||
print("SESSION INFO:", kwargs.get("session_info"))
|
||||
|
||||
# Return a random page:
|
||||
return await render_template(
|
||||
random.choice([
|
||||
r"/mail/oauth/oauth_success.html",
|
||||
r"/mail/oauth/oauth_failure.html"
|
||||
]),
|
||||
mail_client = random.choice([
|
||||
"GMail",
|
||||
"Outlook",
|
||||
"WhatsApp",
|
||||
"Telegram"
|
||||
])
|
||||
)
|
||||
return f"ok ({request.method})"
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
|
||||
+16
-22
@@ -42,29 +42,21 @@ import os
|
||||
import psutil
|
||||
|
||||
# For using Quart:
|
||||
from quart import Quart, request, current_app
|
||||
from quart import Quart, current_app
|
||||
from quart_cors import cors
|
||||
|
||||
# Common:
|
||||
from shared import constants
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.api import async_quart
|
||||
from utils_v2.date_time import date_time
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo, AsyncMongoStorage
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||
from utils_v2.database.async_mysql_v2 import AsyncMySQL
|
||||
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
|
||||
from utils_v2.serialization.json_serializer import JSONSerializer
|
||||
from utils_v2.api.async_quart import (
|
||||
set_api_version,
|
||||
read_input,
|
||||
log_request_to_mongo,
|
||||
should_not_be_under_maintenance,
|
||||
only_whitelisted_ips,
|
||||
limit_rate,
|
||||
validate_input,
|
||||
handle_cancelled_request
|
||||
log_request_to_mongo
|
||||
)
|
||||
|
||||
# GMail-related utils:
|
||||
@@ -92,11 +84,12 @@ import httpx
|
||||
from icecream import IceCreamDebugger
|
||||
|
||||
# All the blueprints:
|
||||
from api.blueprints.mail.oauth_request import mail_oauth_bp
|
||||
from api.blueprints.mail.oauth_callback import mail_callback_bp
|
||||
from api.blueprints.mail.sync_v2 import mail_sync_bp
|
||||
# from api.blueprints.mail.list import mail_list_bp
|
||||
# from api.blueprints.mail.retrieve import mail_retrieve_bp
|
||||
from api.blueprints.mail.oauth.request import mail_oauth_request_bp
|
||||
from api.blueprints.mail.oauth.callback import mail_oauth_callback_bp
|
||||
from api.blueprints.mail.sync.sync_v2 import mail_sync_bp
|
||||
from api.blueprints.mail.retrieve.list import mail_list_bp
|
||||
from api.blueprints.mail.retrieve.get import mail_get_bp
|
||||
from api.blueprints.mail.tags.update import mail_tags_update_bp
|
||||
# from api.blueprints.sms.auth import sms_auth_bp
|
||||
# from api.blueprints.sms.send import sms_send_bp
|
||||
# from api.blueprints.chat.auth import chat_auth_bp
|
||||
@@ -131,11 +124,12 @@ APP_VERSION = constants.APP_VERSION
|
||||
# The Quart app:
|
||||
app = Quart(__name__, template_folder = r"../views")
|
||||
app = cors(app)
|
||||
app.register_blueprint(mail_oauth_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_callback_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_oauth_request_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_oauth_callback_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_sync_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
# app.register_blueprint(mail_list_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
# app.register_blueprint(mail_retrieve_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_list_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_get_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
app.register_blueprint(mail_tags_update_bp, url_prefix = f"/{MODULE_BASE}/mail")
|
||||
# app.register_blueprint(sms_auth_bp, url_prefix = f"/{MODULE_BASE}/sms")
|
||||
# app.register_blueprint(sms_send_bp, url_prefix = f"/{MODULE_BASE}/sms")
|
||||
# app.register_blueprint(chat_auth_bp, url_prefix = f"/{MODULE_BASE}/chat")
|
||||
@@ -329,7 +323,7 @@ async def app_startup(**kwargs):
|
||||
# ┃ ┏┓┏┓┏┓ ┃┃┃┏┓┏┫┏┓┃┏
|
||||
# ┗┛┗┛┛ ┗ ┛ ┗┗┛┗┻┗ ┗┛
|
||||
|
||||
current_app.core_message_controller = MessageController(
|
||||
current_app.core_auth_token_controller = AuthTokenController(
|
||||
cache = current_app.module_cache,
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
http_client = current_app.http_client,
|
||||
@@ -337,7 +331,7 @@ async def app_startup(**kwargs):
|
||||
debug_prefix = "Message (CM) | ",
|
||||
debug_only_errors = True
|
||||
)
|
||||
current_app.core_auth_token_controller = AuthTokenController(
|
||||
current_app.core_message_controller = MessageController(
|
||||
cache = current_app.module_cache,
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
http_client = current_app.http_client,
|
||||
|
||||
Reference in New Issue
Block a user