simplifications and refactorings
This commit is contained in:
@@ -46,7 +46,7 @@ class Settings(BaseSettings):
|
||||
smtp_username: str = ""
|
||||
smtp_password: str = ""
|
||||
smtp_from_address: str = ""
|
||||
smtp_from_name: str = "Media Library Viewer"
|
||||
smtp_from_name: str = "Manage"
|
||||
smtp_use_tls: bool = True
|
||||
smtp_use_ssl: bool = False
|
||||
smtp_timeout: int = 30
|
||||
|
||||
@@ -34,9 +34,9 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Media Library Viewer API",
|
||||
title="Manage API",
|
||||
version="0.1.0",
|
||||
description="Backend API for Jellyfin media browsing, SSH file inspection, server monitoring, and JWT-protected access.",
|
||||
description="Manage API for Jellyfin media browsing, SSH file inspection, server monitoring, and JWT-protected access.",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ from media_library_viewer_api.dependencies import (
|
||||
get_jellyseerr_client,
|
||||
get_mail_queue,
|
||||
)
|
||||
from media_library_viewer_api.services.mailer import EmailAttachment, test_smtp_connection, validate_smtp_settings
|
||||
from media_library_viewer_api.services.mailer import EmailAttachment, validate_smtp_settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -287,13 +287,6 @@ def get_user_message_status(mail_queue=Depends(get_mail_queue)) -> dict[str, Any
|
||||
return mail_queue.status()
|
||||
|
||||
|
||||
@router.post("/message/test-smtp")
|
||||
def test_user_message_smtp() -> dict[str, Any]:
|
||||
"""Test the configured SMTP connection without sending an email."""
|
||||
settings = get_settings()
|
||||
return test_smtp_connection(settings)
|
||||
|
||||
|
||||
@router.post("/message", status_code=status.HTTP_202_ACCEPTED)
|
||||
async def post_user_message(
|
||||
recipient_ids: str = Form(...),
|
||||
|
||||
@@ -99,11 +99,6 @@ def _smtp_settings(settings: object) -> dict[str, object]:
|
||||
}
|
||||
|
||||
|
||||
def _smtp_mode_label(mode: dict[str, object]) -> str:
|
||||
transport = "SSL" if mode["use_ssl"] else "STARTTLS" if mode["use_tls"] else "plain SMTP"
|
||||
return f"{mode['smtp_host']}:{mode['smtp_port']} via {transport}"
|
||||
|
||||
|
||||
def _smtp_mode_candidates(settings: object) -> list[dict[str, Any]]:
|
||||
base = _smtp_settings(settings)
|
||||
candidates = [dict(base, mode_label="configured")]
|
||||
@@ -175,7 +170,7 @@ def _smtp_sender_not_authorized(error: Exception) -> bool:
|
||||
def _smtp_attempt_metadata(mode: dict[str, Any]) -> dict[str, Any]:
|
||||
transport = "SSL" if mode["use_ssl"] else "STARTTLS" if mode["use_tls"] else "plain SMTP"
|
||||
return {
|
||||
"label": str(mode.get("mode_label") or _smtp_mode_label(mode)),
|
||||
"label": str(mode.get("mode_label") or f"{mode["smtp_host"]}:{mode["smtp_port"]}"),
|
||||
"smtp_host": str(mode["smtp_host"]),
|
||||
"smtp_port": int(mode["smtp_port"]),
|
||||
"use_tls": bool(mode["use_tls"]),
|
||||
@@ -224,127 +219,6 @@ def describe_smtp_error(error: Exception) -> str:
|
||||
return f"SMTP delivery failed: {error}"
|
||||
|
||||
|
||||
def test_smtp_connection(settings: object) -> dict[str, object]:
|
||||
"""Validate SMTP connectivity and authentication without sending a message."""
|
||||
try:
|
||||
base = _smtp_settings(settings)
|
||||
except ValueError as exc:
|
||||
return {
|
||||
"status": "error",
|
||||
"message": str(exc),
|
||||
"attempts": [],
|
||||
"selected_mode": None,
|
||||
"from_address": "",
|
||||
"from_name": str(getattr(settings, "smtp_from_name", "") or "").strip() or "Media Library Viewer",
|
||||
"smtp_host": str(getattr(settings, "smtp_host", "") or "").strip(),
|
||||
"smtp_port": int(getattr(settings, "smtp_port", 587) or 587),
|
||||
"use_tls": bool(getattr(settings, "smtp_use_tls", True)),
|
||||
"use_ssl": bool(getattr(settings, "smtp_use_ssl", False)),
|
||||
"authenticated": False,
|
||||
}
|
||||
|
||||
from_address = _from_address(settings)
|
||||
from_name = str(getattr(settings, "smtp_from_name", "") or "").strip() or "Media Library Viewer"
|
||||
attempts: list[dict[str, Any]] = []
|
||||
last_error = ""
|
||||
logger.info(
|
||||
"SMTP test requested from_address=%s auth_user=%s",
|
||||
from_address,
|
||||
base["smtp_username"] or "<none>",
|
||||
)
|
||||
|
||||
for mode in _smtp_mode_candidates(settings):
|
||||
meta = _smtp_attempt_metadata(mode)
|
||||
logger.info(
|
||||
"SMTP test attempting label=%s host=%s port=%s transport=%s auth_user=%s",
|
||||
meta["label"],
|
||||
meta["smtp_host"],
|
||||
meta["smtp_port"],
|
||||
meta["transport"],
|
||||
meta["auth_user"],
|
||||
)
|
||||
try:
|
||||
_probe_smtp_connection(mode)
|
||||
attempts.append(
|
||||
{
|
||||
"label": meta["label"],
|
||||
"smtp_host": meta["smtp_host"],
|
||||
"smtp_port": meta["smtp_port"],
|
||||
"use_tls": meta["use_tls"],
|
||||
"use_ssl": meta["use_ssl"],
|
||||
"status": "ok",
|
||||
}
|
||||
)
|
||||
logger.info(
|
||||
"SMTP test succeeded label=%s host=%s port=%s transport=%s auth_user=%s",
|
||||
meta["label"],
|
||||
meta["smtp_host"],
|
||||
meta["smtp_port"],
|
||||
meta["transport"],
|
||||
meta["auth_user"],
|
||||
)
|
||||
return {
|
||||
"status": "ok",
|
||||
"message": f"SMTP connection successful using {meta['label']}",
|
||||
"from_address": from_address,
|
||||
"from_name": from_name,
|
||||
"smtp_host": meta["smtp_host"],
|
||||
"smtp_port": meta["smtp_port"],
|
||||
"use_tls": meta["use_tls"],
|
||||
"use_ssl": meta["use_ssl"],
|
||||
"authenticated": bool(str(mode["smtp_username"])),
|
||||
"selected_mode": {
|
||||
"label": meta["label"],
|
||||
"smtp_host": meta["smtp_host"],
|
||||
"smtp_port": meta["smtp_port"],
|
||||
"use_tls": meta["use_tls"],
|
||||
"use_ssl": meta["use_ssl"],
|
||||
},
|
||||
"attempts": attempts,
|
||||
}
|
||||
except Exception as exc:
|
||||
last_error = describe_smtp_error(exc)
|
||||
attempts.append(
|
||||
{
|
||||
"label": meta["label"],
|
||||
"smtp_host": meta["smtp_host"],
|
||||
"smtp_port": meta["smtp_port"],
|
||||
"use_tls": meta["use_tls"],
|
||||
"use_ssl": meta["use_ssl"],
|
||||
"status": "failed",
|
||||
"error": last_error,
|
||||
}
|
||||
)
|
||||
logger.warning(
|
||||
"SMTP test failed label=%s host=%s port=%s transport=%s auth_user=%s error=%s",
|
||||
meta["label"],
|
||||
meta["smtp_host"],
|
||||
meta["smtp_port"],
|
||||
meta["transport"],
|
||||
meta["auth_user"],
|
||||
last_error,
|
||||
)
|
||||
|
||||
logger.error(
|
||||
"SMTP test exhausted attempts=%s error=%s",
|
||||
[attempt["label"] for attempt in attempts],
|
||||
last_error or "SMTP test failed.",
|
||||
)
|
||||
return {
|
||||
"status": "error",
|
||||
"message": last_error or "SMTP test failed.",
|
||||
"from_address": from_address,
|
||||
"from_name": from_name,
|
||||
"smtp_host": base["smtp_host"],
|
||||
"smtp_port": base["smtp_port"],
|
||||
"use_tls": base["use_tls"],
|
||||
"use_ssl": base["use_ssl"],
|
||||
"authenticated": bool(base["smtp_username"]),
|
||||
"selected_mode": None,
|
||||
"attempts": attempts,
|
||||
}
|
||||
|
||||
|
||||
def build_email_message(
|
||||
settings: object,
|
||||
recipients: list[str],
|
||||
@@ -358,7 +232,7 @@ def build_email_message(
|
||||
) -> tuple[EmailMessage, str]:
|
||||
"""Build a MIME email message with HTML and attachments."""
|
||||
from_address = sender_address or _from_address(settings)
|
||||
from_name = str(getattr(settings, "smtp_from_name", "") or "").strip() or "Media Library Viewer"
|
||||
from_name = str(getattr(settings, "smtp_from_name", "") or "").strip() or "Manage"
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = formataddr((from_name, from_address))
|
||||
|
||||
Reference in New Issue
Block a user