Zeige den Bezug zwischen Schreiben und lies eine E-Mail vor dem Uebernehmen.
Deploy Development / deploy (push) Successful in 27s
Test Suite / pytest-backend (push) Successful in 1m10s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 8s

Die Liste behaelt die Schreiben dazwischen. Richtung, Absender, Empfaenger und Anhaenge bleiben an der Nachricht erkennbar.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-17 17:27:17 +02:00
1 parent 97bffc6175
commit f27df3ff35
19 files changed
+1049 -49

No files matched your search

+41
View File
@@ -67,6 +67,7 @@ from app.api.schemas import (
RoomIn,
ScopeIn,
TaskDeadlineIn,
ElementReferenceIn,
TaskDependencyIn,
TaskElementsIn,
SummaryPatch,
@@ -106,8 +107,10 @@ from app.ops import (
grant_access,
link_matter_to_case,
link_task_deadline,
link_element_reference,
link_task_dependency,
set_task_elements,
unlink_element_reference,
unlink_task_dependency,
patch_case,
patch_deadline,
@@ -794,6 +797,8 @@ def post_note(body: NoteIn, principal: Principal = Depends(get_principal), conn:
_ctx_model(body.placement),
body.chapter_id,
body.dates.document_on if body.dates else None,
body.dates.sent_on if body.dates else None,
body.dates.received_on if body.dates else None,
)
element = created["element"]
return _created(
@@ -1100,6 +1105,42 @@ def delete_task_dependency(
return Response(status_code=204)
@router.post("/api/v1/elements/{element_id}/references", status_code=204)
def post_element_reference(
element_id: UUID,
body: ElementReferenceIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
link_element_reference(
conn,
principal.account_id,
element_id,
body.refers_to_element_id,
_ctx_model(body.context),
)
return Response(status_code=204)
@router.delete("/api/v1/elements/{element_id}/references/{refers_to_element_id}", status_code=204)
def delete_element_reference(
element_id: UUID,
refers_to_element_id: UUID,
context_kind: str | None = None,
context_id: str | None = None,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
unlink_element_reference(
conn,
principal.account_id,
element_id,
refers_to_element_id,
parse_query_context(context_kind, context_id),
)
return Response(status_code=204)
@router.post("/api/v1/deadlines")
def post_deadline(body: DeadlineIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
deadline = create_deadline(
+17
View File
@@ -205,9 +205,21 @@ class DatesIn(APIModel):
received_on: date | None = None
class MailAttachment(APIModel):
filename: str = Field(min_length=1, max_length=200)
content_type: str | None = Field(default=None, max_length=120)
class ExternalDetail(APIModel):
correspondent: str | None = Field(default=None, max_length=200)
message_from: str | None = Field(default=None, max_length=200)
message_to: str | None = Field(default=None, max_length=500)
message_cc: str | None = Field(default=None, max_length=500)
direction: Literal["in", "out"] | None = None
mailbox: str | None = Field(default=None, max_length=200)
connector_id: UUID | None = None
uid: int | None = Field(default=None, ge=1)
attachments: list[MailAttachment] = Field(default_factory=list, max_length=40)
open_url: str | None = Field(default=None, max_length=500, pattern=r"^https?://")
@@ -292,3 +304,8 @@ class TaskElementsIn(APIModel):
class TaskDependencyIn(APIModel):
depends_on_task_id: UUID
context: ContextIn | None = None
class ElementReferenceIn(APIModel):
refers_to_element_id: UUID
context: ContextIn | None = None
+44 -1
View File
@@ -19,6 +19,7 @@ from app.sources import (
delete_connector,
imap_mailboxes,
imap_messages,
imap_preview,
list_connectors,
paperless_content,
paperless_documents,
@@ -70,7 +71,7 @@ def view_paperless(
(element_id,),
).fetchone()
if ref is None or ref["source"] != "paperless":
raise unprocessable("Nur ein Paperless-Dokument lässt sich hier ansehen.")
raise unprocessable("Nur ein Paperless-Dokument lässt sich hier als Datei ansehen.")
data, media_type, filename = paperless_content(conn, principal.account_id, ref["external_id"])
safe_name = (filename or f"paperless-{ref['external_id']}.pdf").replace('"', "")
return Response(
@@ -84,6 +85,35 @@ def view_paperless(
)
@router.get("/api/v1/external-refs/{element_id}/mail")
def view_mail(
element_id: UUID,
context_kind: str | None = None,
context_id: str | None = None,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
element = get_element(conn, element_id)
if element is None or element["kind"] != "external_ref":
raise not_found()
rights = element_rights(conn, principal.account_id, element, parse_query_context(context_kind, context_id))
if not rights.read:
raise not_found()
ref = conn.execute(
"SELECT source, detail FROM external_references WHERE element_id = %s",
(element_id,),
).fetchone()
detail = (ref or {}).get("detail") or {}
if ref is None or ref["source"] != "imap":
raise unprocessable("Das ist keine E-Mail.")
connector_id = detail.get("connector_id")
mailbox = detail.get("mailbox") or ""
uid = detail.get("uid")
if not connector_id or not mailbox or not uid:
raise unprocessable("Für diese E-Mail ist keine Postfach-Vorschau hinterlegt.")
return imap_preview(conn, principal.account_id, UUID(str(connector_id)), str(mailbox), int(uid))
@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())}
@@ -112,6 +142,19 @@ def search_mail(
return imap_messages(conn, principal.account_id, connector_id, q.strip(), mailbox.strip(), before)
@router.get("/api/v1/sources/imap/message")
def read_mail(
connector_id: UUID,
mailbox: str,
uid: int,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
if uid < 1:
raise unprocessable("Die Nachricht ist ungültig.")
return imap_preview(conn, principal.account_id, connector_id, mailbox.strip(), uid)
@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"}:
+5
View File
@@ -58,6 +58,11 @@ _PURGE = (
OR created_by IN (SELECT id FROM fixture_accounts)
""",
"""
DELETE FROM element_references
WHERE element_id IN (SELECT id FROM elements WHERE owner_subject_id IN (SELECT id FROM fixture_accounts))
OR refers_to_element_id IN (SELECT id FROM elements WHERE owner_subject_id IN (SELECT id FROM fixture_accounts))
""",
"""
DELETE FROM task_dependencies
WHERE task_id IN (SELECT id FROM tasks WHERE owner_subject_id IN (SELECT id FROM fixture_accounts))
OR depends_on_task_id IN (SELECT id FROM tasks WHERE owner_subject_id IN (SELECT id FROM fixture_accounts))
+60 -2
View File
@@ -529,6 +529,8 @@ def create_note(
placement: Ctx | None,
chapter_id: UUID | None,
document_on: date | None = None,
sent_on: date | None = None,
received_on: date | None = None,
) -> dict:
document = clean_document(document)
target, rights_context = _placement(context, placement)
@@ -540,8 +542,8 @@ def create_note(
)
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
audit(conn, actor, scope_id, "note_created", "element", element["id"], rights_context or target)
if document_on is not None:
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, None, None)
if document_on is not None or sent_on is not None or received_on is not None:
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, sent_on, received_on)
return {"element": element, "binding": binding, "document": document}
@@ -962,6 +964,62 @@ def unlink_task_dependency(conn, actor: UUID, task_id: UUID, depends_on_task_id:
)
def _reference_chain(conn, start: UUID) -> set[UUID]:
seen: set[UUID] = set()
stack = [start]
while stack:
current = stack.pop()
if current in seen:
continue
seen.add(current)
rows = conn.execute(
"SELECT refers_to_element_id FROM element_references WHERE element_id = %s",
(current,),
).fetchall()
stack.extend(row["refers_to_element_id"] for row in rows)
return seen
def link_element_reference(conn, actor: UUID, element_id: UUID, refers_to_element_id: UUID, context: Ctx | None) -> None:
if element_id == refers_to_element_id:
raise unprocessable("Eine Unterlage kann nicht auf sich selbst Bezug nehmen.")
element = get_element(conn, element_id)
target = get_element(conn, refers_to_element_id)
if element is None or target is None:
raise not_found()
rights = element_rights(conn, actor, element, context)
target_rights = element_rights(conn, actor, target, context)
if not rights.read or not target_rights.read:
raise not_found()
if not rights.edit:
raise forbidden()
if element_id in _reference_chain(conn, refers_to_element_id):
raise unprocessable("Dieser Bezug würde einen Kreis bilden.")
conn.execute(
"""
INSERT INTO element_references (element_id, refers_to_element_id)
VALUES (%s, %s)
ON CONFLICT DO NOTHING
""",
(element_id, refers_to_element_id),
)
def unlink_element_reference(conn, actor: UUID, element_id: UUID, refers_to_element_id: UUID, context: Ctx | None) -> None:
element = get_element(conn, element_id)
if element is None:
raise not_found()
rights = element_rights(conn, actor, element, context)
if not rights.read:
raise not_found()
if not rights.edit:
raise forbidden()
conn.execute(
"DELETE FROM element_references WHERE element_id = %s AND refers_to_element_id = %s",
(element_id, refers_to_element_id),
)
def set_content_order(
conn,
actor: UUID,
+65 -1
View File
@@ -118,6 +118,8 @@ def _preview_meta(kind: str, content_type: str | None, source: str | None) -> di
return {"available": True, "media": "pdf", "source": "file"}
if kind == "external_ref" and source == "paperless":
return {"available": True, "media": "paperless", "source": "paperless"}
if kind == "external_ref" and source == "imap":
return {"available": True, "media": "email", "source": "imap"}
if kind == "file":
return {"available": False, "media": "file", "source": "file"}
return {"available": False, "media": "none", "source": source}
@@ -193,6 +195,66 @@ def _exception(conn, kind: str, resource_id: UUID, context: Ctx | None) -> dict
return {"remove_read": row["can_read"], "remove_edit": row["can_edit"], "remove_share": row["can_share"]}
def _reference_view(conn, actor: UUID, element_id: UUID, context: Ctx) -> dict | None:
element = get_element(conn, element_id)
if element is None or not element_rights(conn, actor, element, context).read:
return None
dates = _dates(conn, element)
direction = None
if element["kind"] == "external_ref":
ref = conn.execute(
"SELECT source, detail FROM external_references WHERE element_id = %s",
(element["id"],),
).fetchone()
detail = (ref["detail"] or {}) if ref is not None and ref["source"] == "imap" else {}
if detail.get("direction") in ("in", "out"):
direction = detail["direction"]
if direction is None:
if dates["sent_on"] and not dates["received_on"]:
direction = "out"
elif dates["received_on"] and not dates["sent_on"]:
direction = "in"
return {
"id": str(element["id"]),
"title": element["title"],
"document_on": dates["document_on"],
"direction": direction,
}
def _attach_references(conn, actor: UUID, element: dict, context: Ctx, body: dict) -> None:
refers = []
for row in conn.execute(
"""
SELECT refers_to_element_id
FROM element_references
WHERE element_id = %s
ORDER BY created_at, refers_to_element_id
""",
(element["id"],),
).fetchall():
view = _reference_view(conn, actor, row["refers_to_element_id"], context)
if view is not None:
refers.append(view)
replies = []
for row in conn.execute(
"""
SELECT element_id
FROM element_references
WHERE refers_to_element_id = %s
ORDER BY created_at, element_id
""",
(element["id"],),
).fetchall():
view = _reference_view(conn, actor, row["element_id"], context)
if view is not None:
replies.append(view)
if refers:
body["refers_to"] = refers
if replies:
body["referred_by"] = replies
def binding_payload(conn, actor: UUID, binding: dict, context: Ctx) -> dict | None:
element = get_element(conn, binding["element_id"])
if element is None:
@@ -200,13 +262,15 @@ def binding_payload(conn, actor: UUID, binding: dict, context: Ctx) -> dict | No
rights = element_rights(conn, actor, element, context)
if not rights.read:
return None
body = _element_body(conn, element)
_attach_references(conn, actor, element, context, body)
payload = {
"id": str(binding["id"]),
"position": binding["position"],
"chapter_id": str(binding["chapter_id"]) if binding["chapter_id"] else None,
"rights": rights.as_dict(),
"annotations": _annotations(conn, binding["id"]),
"element": _element_body(conn, element),
"element": body,
}
if element["owner_subject_id"] == actor:
payload["exception"] = _exception(conn, "element", element["id"], context)
+167 -1
View File
@@ -143,6 +143,21 @@ def imap_messages(
_imap_logout(client)
def imap_preview(conn, owner_id: UUID, connector_id: UUID, mailbox: str, uid: int) -> dict:
row = _owned(conn, owner_id, connector_id)
if row["kind"] != "imap":
raise unprocessable("Das ist kein E-Mail-Konto.")
_check_mailbox(mailbox)
if uid < 1:
raise unprocessable("Die Nachricht ist ungültig.")
client = _imap_login(row["settings"], row["secret"])
try:
_select_mailbox(client, mailbox)
return _fetch_message(client, uid, mailbox)
finally:
_imap_logout(client)
def _owned(conn, owner_id: UUID, connector_id: UUID) -> dict:
row = conn.execute(
"SELECT id, kind, name, settings, secret FROM connectors WHERE id = %s AND owner_id = %s",
@@ -432,13 +447,42 @@ def _uid_search(client: imaplib.IMAP4, query: str):
raise unprocessable("Das Postfach kann diese Suche nicht ausführen. Blättere im Fach oder kürze den Suchtext.") from exc
def _check_mailbox(mailbox: str) -> None:
if any(char in mailbox for char in "\r\n\x00"):
raise unprocessable("Der Fachname ist ungültig.")
def _select_mailbox(client: imaplib.IMAP4, mailbox: str) -> None:
folders = _imap_mailboxes(client)
match = next((item for item in folders if item["name"] == mailbox), None)
if match is None or not match["selectable"]:
raise unprocessable("Dieses Fach gibt es in dem Konto nicht.")
try:
status, _ = client.select(mailbox, readonly=True)
except imaplib.IMAP4.error as exc:
raise unprocessable("Dieses Fach lässt sich nicht öffnen.") from exc
if status != "OK":
raise unprocessable("Dieses Fach lässt sich nicht öffnen.")
def _fetch_header(client: imaplib.IMAP4, uid: int, mailbox: str) -> dict | None:
try:
status, fetched = client.uid("FETCH", str(uid), "(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE MESSAGE-ID)])")
status, fetched = client.uid(
"FETCH",
str(uid),
"(BODY.PEEK[HEADER.FIELDS (FROM TO CC SUBJECT DATE MESSAGE-ID)])",
)
except imaplib.IMAP4.error:
return None
if status != "OK" or not fetched or not isinstance(fetched[0], tuple):
return None
attachments = []
try:
struct_status, structure = client.uid("FETCH", str(uid), "(BODYSTRUCTURE)")
if struct_status == "OK":
attachments = _structure_attachments(structure)
except imaplib.IMAP4.error:
attachments = []
header = BytesParser().parsebytes(fetched[0][1])
subject = _decode(header.get("Subject")) or "(ohne Betreff)"
sender = _decode(header.get("From"))
@@ -448,10 +492,132 @@ def _fetch_header(client: imaplib.IMAP4, uid: int, mailbox: str) -> dict | None:
"uid": uid,
"title": subject[:200],
"message_from": sender[:200],
"message_to": _decode(header.get("To"))[:500],
"message_cc": _decode(header.get("Cc"))[:500],
"direction": _direction(mailbox),
"attachments": attachments,
"document_on": _mail_date(header.get("Date")),
}
def _fetch_message(client: imaplib.IMAP4, uid: int, mailbox: str) -> dict:
try:
status, fetched = client.uid("FETCH", str(uid), "(BODY.PEEK[])")
except imaplib.IMAP4.error as exc:
raise unprocessable("Die Nachricht lässt sich nicht lesen.") from exc
raw = _literal(fetched)
if status != "OK" or raw is None:
raise unprocessable("Die Nachricht lässt sich nicht lesen.")
if len(raw) > 1_500_000:
raise unprocessable("Die Nachricht ist zu groß für die Vorschau.")
message = BytesParser().parsebytes(raw)
subject = _decode(message.get("Subject")) or "(ohne Betreff)"
return {
"uid": uid,
"mailbox": mailbox,
"title": subject[:200],
"message_from": _decode(message.get("From"))[:200],
"message_to": _decode(message.get("To"))[:500],
"message_cc": _decode(message.get("Cc"))[:500],
"direction": _direction(mailbox),
"document_on": _mail_date(message.get("Date")),
"attachments": _message_attachments(message),
"text": _message_text(message),
}
_SENT_FOLDER = re.compile(
r"(^|/)(sent|sent items|sent messages|gesendet|gesendete|gesendete elemente|outbox|postausgang)($|/)",
re.IGNORECASE,
)
def _direction(mailbox: str) -> str:
folded = mailbox.casefold().replace("\\", "/")
return "out" if _SENT_FOLDER.search(folded) else "in"
def _literal(fetched) -> bytes | None:
for part in fetched or []:
if isinstance(part, tuple) and len(part) > 1 and isinstance(part[1], (bytes, bytearray)):
return bytes(part[1])
return None
def _structure_attachments(fetched) -> list[dict]:
chunks: list[bytes] = []
for part in fetched or []:
if isinstance(part, tuple):
chunks.extend(bytes(item) for item in part if isinstance(item, (bytes, bytearray)))
elif isinstance(part, (bytes, bytearray)):
chunks.append(bytes(part))
blob = b" ".join(chunks)
names = re.findall(br'FILENAME"\s+"([^"\r\n]+)"', blob, flags=re.IGNORECASE)
items = []
seen: set[str] = set()
for raw in names:
name = raw.decode("utf-8", errors="replace").strip()[:200]
if not name or name in seen:
continue
seen.add(name)
items.append({"filename": name})
if len(items) == 40:
break
return items
def _message_attachments(message) -> list[dict]:
items = []
seen: set[str] = set()
for part in message.walk():
filename = part.get_filename()
if not filename:
continue
name = _decode(filename)[:200]
if not name or name in seen:
continue
seen.add(name)
items.append({"filename": name, "content_type": part.get_content_type()[:120]})
if len(items) == 40:
break
return items
def _message_text(message) -> str:
plain: list[str] = []
html: list[str] = []
parts = message.walk() if message.is_multipart() else [message]
for part in parts:
if part.is_multipart() or part.get_content_disposition() == "attachment" or part.get_filename():
continue
kind = part.get_content_type()
if kind == "text/plain":
plain.append(_part_text(part))
elif kind == "text/html":
html.append(_html_text(_part_text(part)))
text = "\n".join(chunk for chunk in plain if chunk).strip()
if not text:
text = "\n".join(chunk for chunk in html if chunk).strip()
return text[:8000]
def _part_text(part) -> str:
payload = part.get_payload(decode=True)
if payload is None:
raw = part.get_payload()
return raw.strip() if isinstance(raw, str) else ""
charset = part.get_content_charset() or "utf-8"
return payload.decode(charset, errors="replace").strip()
def _html_text(value: str) -> str:
text = re.sub(r"(?is)<(script|style)\b.*?>.*?</\1>", " ", value)
text = re.sub(r"(?i)<br\s*/?>", "\n", text)
text = re.sub(r"(?i)</p>", "\n", text)
text = re.sub(r"<[^>]+>", " ", text)
return re.sub(r"[ \t]{2,}", " ", text).strip()
def _parse_list_line(raw) -> dict | None:
if not isinstance(raw, (bytes, str)) or not raw:
return None