Mache große IMAP-Fächer, die Paperless-Suche und die interne Markdown-Notiz über die gemeinsamen Schnittstellen benutzbar.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c3a07cee64
commit
7e39fbb2bd
29 files changed
+6053
-320
No files matched your search
+10
-2
@@ -4,6 +4,14 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def discard_stored(item) -> None:
|
||||
discard = getattr(item, "discard", None)
|
||||
if callable(discard):
|
||||
discard()
|
||||
return
|
||||
Path(item).unlink(missing_ok=True)
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import Depends, Request
|
||||
@@ -35,11 +43,11 @@ def get_conn(request: Request):
|
||||
yield conn
|
||||
conn.commit()
|
||||
for path in getattr(request.state, "delete_after_commit", []):
|
||||
Path(path).unlink(missing_ok=True)
|
||||
discard_stored(path)
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
for path in getattr(request.state, "cleanup_files", []):
|
||||
Path(path).unlink(missing_ok=True)
|
||||
discard_stored(path)
|
||||
raise
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ from app.core.errors import not_found, unprocessable, unsupported
|
||||
from app.mailpack import parse_mail
|
||||
from app.media.storage import content_disposition
|
||||
from app.ops import create_external
|
||||
from app.retrieval import RetrievalFailure
|
||||
from app.sourcebind import assign_reference
|
||||
from app.sourceport import capability_payload, with_capabilities
|
||||
from app.sourceregistry import get_source, require_source
|
||||
@@ -23,8 +24,8 @@ from app.sources import (
|
||||
delete_connector,
|
||||
imap_preview,
|
||||
list_connectors,
|
||||
paperless_content,
|
||||
probe,
|
||||
read_linked_mail,
|
||||
save_connector,
|
||||
update_connector,
|
||||
)
|
||||
@@ -158,7 +159,13 @@ def _opened_ref(conn, principal: Principal, element_id: UUID, context_kind: str
|
||||
|
||||
|
||||
def _complete_mail(conn, principal: Principal, element, context, ref):
|
||||
raw = read_linked_mail(conn, principal.account_id, element, context, ref)
|
||||
module = get_source(ref["source"]) if ref is not None else None
|
||||
if module is None or module.representation != "email":
|
||||
raise unprocessable("Das ist keine E-Mail.")
|
||||
opened = module.open_original(conn, principal.account_id, element, context, ref)
|
||||
if isinstance(opened, RetrievalFailure):
|
||||
raise unprocessable(opened.message)
|
||||
raw = opened[0] if isinstance(opened, tuple) else opened
|
||||
parsed = parse_mail(raw)
|
||||
if parsed.missing:
|
||||
names = ", ".join(parsed.missing[:5])
|
||||
@@ -246,6 +253,34 @@ def search_paperless(
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/{kind}/facets")
|
||||
def source_facets(
|
||||
kind: str,
|
||||
connector_id: UUID,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
module = require_source(kind)
|
||||
if module.facets is None:
|
||||
raise unsupported("Diese Quelle hat keine Auswahllisten.")
|
||||
return module.facets(conn, principal.account_id, connector_id)
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/paperless/documents/{document_id}/preview")
|
||||
def preview_paperless_document(
|
||||
document_id: str,
|
||||
connector_id: UUID,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
data, media, name = paperless_content(conn, principal.account_id, document_id, connector_id)
|
||||
return Response(
|
||||
content=data,
|
||||
media_type=media,
|
||||
headers={**_PRIVATE, "Content-Disposition": content_disposition(name).replace("attachment;", "inline;", 1)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/imap/mailboxes")
|
||||
def list_mailboxes(
|
||||
connector_id: UUID,
|
||||
@@ -269,6 +304,7 @@ def search_mail(
|
||||
document_to: str = "",
|
||||
sort: str = "",
|
||||
cursor: str = "",
|
||||
uidvalidity: int | None = None,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
@@ -292,6 +328,7 @@ def search_mail(
|
||||
"sort": sort,
|
||||
"cursor": cursor,
|
||||
},
|
||||
uidvalidity,
|
||||
)
|
||||
|
||||
|
||||
@@ -357,7 +394,7 @@ _SEARCH_FLAGS = {
|
||||
}
|
||||
|
||||
|
||||
def _run_search(kind: str, conn, owner, query: str, connector_id: UUID | None, mailbox: str, before: int | None, filters: dict) -> dict:
|
||||
def _run_search(kind: str, conn, owner, query: str, connector_id: UUID | None, mailbox: str, before: int | None, filters: dict, uidvalidity: int | None = None) -> dict:
|
||||
module = require_source(kind)
|
||||
search = module.capabilities["search"]
|
||||
refused = [name for name, value in filters.items() if str(value or "").strip() and not search.get(_SEARCH_FLAGS.get(name, name))]
|
||||
@@ -365,7 +402,11 @@ def _run_search(kind: str, conn, owner, query: str, connector_id: UUID | None, m
|
||||
refused.append("before")
|
||||
if refused:
|
||||
raise unsupported("Diese Suche unterstützt den Filter nicht: " + ", ".join(refused) + ".")
|
||||
found = module.find(conn, owner, connector_id, query, {"mailbox": mailbox, "before": before})
|
||||
options = {"mailbox": mailbox, "before": before}
|
||||
if uidvalidity is not None:
|
||||
options["uidvalidity"] = uidvalidity
|
||||
options.update({key: value for key, value in filters.items() if str(value or "").strip()})
|
||||
found = module.find(conn, owner, connector_id, query, options)
|
||||
bound = str(found.get("connector_id") or connector_id or "")
|
||||
return with_capabilities(kind, bound, found)
|
||||
|
||||
|
||||
Reference in new issue
Block a user