"""Connector administration and live Paperless / IMAP lookup.""" from __future__ import annotations from uuid import UUID from fastapi import APIRouter, Depends from psycopg import Connection from app.access.policy import Ctx from app.api.deps import Principal, get_conn, get_principal from app.api.routes import _created, _ctx_model from app.api.schemas import ConnectorIn, ExternalIn from app.core.errors import unprocessable from app.ops import create_external from app.sources import ( clean_base_url, delete_connector, imap_messages, list_connectors, paperless_documents, probe, save_connector, ) router = APIRouter() @router.get("/api/v1/connectors") def get_connectors(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return {"items": list_connectors(conn, principal.account_id)} @router.post("/api/v1/connectors", status_code=201) def post_connector(body: ConnectorIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): name, settings, secret = _connector_parts(body) row = save_connector(conn, principal.account_id, body.kind, name, settings, secret) return row @router.delete("/api/v1/connectors/{connector_id}", status_code=204) def remove_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): delete_connector(conn, principal.account_id, connector_id) @router.post("/api/v1/connectors/{connector_id}/probe") def probe_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return probe(conn, principal.account_id, connector_id) @router.get("/api/v1/sources/paperless/documents") def search_paperless(q: str = "", principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return {"items": paperless_documents(conn, principal.account_id, q.strip())} @router.get("/api/v1/sources/imap/messages") def search_mail( connector_id: UUID, q: str = "", principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): return {"items": imap_messages(conn, principal.account_id, connector_id, q.strip())} @router.post("/api/v1/sources/link", status_code=201) def link_source(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): if body.source not in {"paperless", "imap"}: raise unprocessable("Verknüpft werden Paperless-Dokumente und E-Mails.") element = create_external( conn, principal.account_id, principal.scope_id, body.title, body.source, body.external_id, Ctx(body.context.kind, body.context.id), _ctx_model(body.placement), body.chapter_id, body.dates.document_on, body.dates.sent_on, body.dates.received_on, body.detail.model_dump(exclude_none=True) if body.detail else None, ) return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]}) def _connector_parts(body: ConnectorIn) -> tuple[str, dict, str]: if body.kind == "paperless": if not body.base_url or not body.token or len(body.token.strip()) < 8: raise unprocessable("Paperless braucht eine Adresse und einen API-Schlüssel.") return body.name.strip(), {"base_url": clean_base_url(body.base_url)}, body.token.strip() if not body.host or not body.username or not body.password: raise unprocessable("Ein E-Mail-Konto braucht Server, Benutzername und Passwort.") host = body.host.strip() if " " in host or len(host) > 200: raise unprocessable("Der Mailserver ist ungültig.") return body.name.strip(), { "host": host, "port": body.port or 993, "username": body.username.strip(), "mailbox": (body.mailbox or "INBOX").strip() or "INBOX", "security": body.security or "ssl", }, body.password