Korrigiere IMAP-Ordner, MIME-Ansicht und die gemeinsamen Leser, damit große Postfächer, Notizen und verspätete Quellenantworten zuverlässig bleiben.
Deploy Development / deploy (push) Successful in 39s
Test Suite / pytest-backend (push) Successful in 2m29s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 20s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-20 08:47:24 +02:00
1 parent 34c4084a7c
commit c29bd29f34
29 files changed
+2562 -410

No files matched your search

+222 -49
View File
@@ -211,11 +211,36 @@ def record_dispatch(
raise unprocessable("Dieselbe Anlage ist mehrfach im Versand genannt.")
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
host = _bound_element(conn, actor, host_id, page, scope)
bump_version(conn, "elements", host_id, expected)
known = {row["attachment_element_id"] for row in _uses(conn, scope, host_id)}
if any(item not in known for item in attachment_ids):
raise unprocessable("Mitversendet werden nur Anlagen dieses Schreibens.")
counterpart_text = _counterpart(counterpart)
edition_plans = []
if capture_editions:
edition_plans.append(_plan_edition(conn, actor, host, "host", 0, page, historical_gap, media_root, cleanup))
ordered = [row for row in _uses(conn, scope, host_id) if row["attachment_element_id"] in set(attachment_ids)]
position_of = {item: index for index, item in enumerate(attachment_ids)}
ordered.sort(key=lambda row: position_of[row["attachment_element_id"]])
for index, row in enumerate(ordered, start=1):
element = _readable(conn, actor, row["attachment_element_id"], page)
if element is None:
raise not_found()
edition_plans.append(
_plan_edition(conn, actor, element, "attachment", index, page, historical_gap, media_root, cleanup)
)
evidence_plans = []
for item in evidence:
element = _readable(conn, actor, item["element_id"], page)
if element is None:
raise not_found()
evidence_plans.append(
_plan_evidence(
conn, actor, element, item["role"], item.get("fold") or "keep", page, media_root, cleanup, container_row["home_scope_id"]
)
)
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
host = _bound_element(conn, actor, host_id, page, scope)
bump_version(conn, "elements", host_id, expected)
dispatch = conn.execute(
"""
INSERT INTO dispatches (
@@ -237,37 +262,11 @@ def record_dispatch(
actor,
),
).fetchone()
states = []
if capture_editions:
states.append(_capture_item(conn, actor, dispatch, host, "host", 0, page, historical_gap, media_root, cleanup))
ordered = [row for row in _uses(conn, scope, host_id) if row["attachment_element_id"] in set(attachment_ids)]
position_of = {item: index for index, item in enumerate(attachment_ids)}
ordered.sort(key=lambda row: position_of[row["attachment_element_id"]])
for index, row in enumerate(ordered, start=1):
element = _readable(conn, actor, row["attachment_element_id"], page)
if element is None:
raise not_found()
states.append(
_capture_item(conn, actor, dispatch, element, "attachment", index, page, historical_gap, media_root, cleanup)
)
evidence_rows = []
for item in evidence:
evidence_rows.append(
_add_evidence(
conn,
actor,
container_row,
page,
scope,
dispatch,
item["element_id"],
item["role"],
item.get("fold") or "keep",
media_root,
cleanup,
log=False,
)
)
states = [_write_edition(conn, dispatch, plan) for plan in edition_plans]
evidence_rows = [
_write_evidence(conn, actor, container_row, page, scope, dispatch, plan, log=False)
for plan in evidence_plans
]
completeness = _completeness(states) if capture_editions else "secured"
conn.execute(
"UPDATE dispatches SET completeness = %s, updated_at = now() WHERE id = %s",
@@ -292,11 +291,16 @@ def add_dispatch_evidence(
) -> dict:
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
dispatch = _dispatch_in(conn, dispatch_id, scope)
_bump_dispatch(conn, dispatch_id, expected)
row = _add_evidence(
conn, actor, container_row, page, scope, dispatch, element_id, role, fold, media_root, cleanup, log=True
element = _readable(conn, actor, element_id, page)
if element is None:
raise not_found()
plan = _plan_evidence(
conn, actor, element, role, fold, page, media_root, cleanup, container_row["home_scope_id"], dispatch_id
)
return row
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
dispatch = _dispatch_in(conn, dispatch_id, scope)
_bump_dispatch(conn, dispatch_id, expected)
return _write_evidence(conn, actor, container_row, page, scope, dispatch, plan, log=True)
def detach_dispatch_evidence(
@@ -760,7 +764,7 @@ def _identify_evidence(conn, actor, dispatch, element, page, media_root, cleanup
if isinstance(loaded, RetrievalFailure):
return False, loaded.message, None, None, [], None
if isinstance(loaded, MailPayload):
comparisons = _compare_mail(conn, actor, dispatch["id"], page, loaded)
comparisons = _compare_mail(conn, actor, dispatch["id"], page, loaded) if dispatch is not None else []
payload = FilePayload("nachweis.eml", "message/rfc822", loaded.raw, sha256(loaded.raw))
return True, None, loaded.parsed.message_id or None, payload.sha256, comparisons, payload
payload = FilePayload(loaded.filename, loaded.media_type, loaded.data, sha256(loaded.data))
@@ -931,15 +935,179 @@ def _working_bytes(conn, actor, element, media_root: str):
return None, "Für diesen Inhalt ist keine sicherbare Fassung vorgesehen.", None, None, None
def _store_snapshot(conn, scope_id, actor, payload: FilePayload, media_root: str, cleanup: list[Path], dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> UUID:
snapshot_id = uuid4()
def _stage_snapshot(conn, media_root: str, data: bytes, cleanup: list) -> dict:
"""Publish bytes only while the domain transaction is idle. The row comes later."""
from app.sourceaccess import release_for_external
instance = instance_for_write(conn, media_root)
snapshot_id = uuid4()
object_key = f"dispatch/{snapshot_id}"
content_revision = str(uuid4())
adapter = adapter_for(instance)
publish(adapter, object_key, payload.data)
release_for_external(conn)
publish(adapter, object_key, data)
cleanup.append(StoredHandle(adapter, object_key))
digest = sha256(payload.data)
return {
"id": snapshot_id,
"instance": instance,
"object_key": object_key,
"content_revision": str(uuid4()),
}
def _plan_edition(conn, actor, element, role, position, page, historical_gap, media_root, cleanup) -> dict:
dates = _dates(conn, element["id"])
payload = None
gap_reason = None
note_document = None
external_source = None
external_id = None
if historical_gap:
state = "gap"
gap_reason = _HISTORICAL_GAP
else:
payload, gap_reason, note_document, external_source, external_id = _working_bytes(
conn, actor, element, media_root
)
state = "snapshot" if payload is not None else "gap"
staged = _stage_snapshot(conn, media_root, payload.data, cleanup) if payload is not None else None
return {
"element": element,
"role": role,
"position": position,
"dates": dates,
"state": state,
"payload": payload,
"gap_reason": gap_reason,
"note_document": note_document,
"external_source": external_source,
"external_id": external_id,
"staged": staged,
}
def _write_edition(conn, dispatch, plan: dict) -> str:
payload = plan["payload"]
filename = payload.filename if payload is not None else None
content_type = payload.media_type if payload is not None else None
digest = sha256(payload.data) if payload is not None else None
byte_size = len(payload.data) if payload is not None else None
item = conn.execute(
"""
INSERT INTO dispatch_items (
dispatch_id, role, position, element_id, title, document_on, filename,
content_type, byte_size, content_sha256, source_state, gap_reason,
note_document, external_source, external_id
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id
""",
(
dispatch["id"],
plan["role"],
plan["position"],
plan["element"]["id"],
plan["element"]["title"],
plan["dates"]["document_on"],
filename,
content_type,
byte_size,
digest,
plan["state"],
plan["gap_reason"],
Json(plan["note_document"]) if plan["note_document"] is not None else None,
plan["external_source"],
plan["external_id"],
),
).fetchone()
if plan["staged"] is not None and payload is not None:
_record_snapshot(conn, dispatch["home_scope_id"], dispatch["recorded_by"], payload, plan["staged"], dispatch_item_id=item["id"])
return plan["state"]
def _plan_evidence(conn, actor, element, role, fold, page, media_root, cleanup, scope_id, dispatch_id=None) -> dict:
if role not in EVIDENCE_ROLES:
raise unprocessable("Die Rolle des Nachweises ist nicht vorgesehen.")
if fold not in {"keep", "evidence"}:
raise unprocessable("Die Darstellung des Nachweises ist nicht festgelegt.")
if dispatch_id is not None and conn.execute(
"SELECT id FROM dispatch_evidence WHERE dispatch_id = %s AND element_id = %s",
(dispatch_id, element["id"]),
).fetchone():
raise unprocessable("Dieser Nachweis ist dem Versand bereits zugeordnet.")
dispatch = {"id": dispatch_id} if dispatch_id is not None else None
identified, gap_reason, message_id, digest, comparisons, snapshot = _identify_evidence(
conn, actor, dispatch, element, page, media_root, cleanup, scope_id
)
staged = _stage_snapshot(conn, media_root, snapshot.data, cleanup) if snapshot is not None else None
return {
"element": element,
"role": role,
"fold": fold,
"identified": identified,
"gap_reason": gap_reason,
"message_id": message_id,
"digest": digest,
"comparisons": comparisons,
"snapshot": snapshot,
"staged": staged,
}
def _write_evidence(conn, actor, container_row, page, scope, dispatch, plan: dict, log: bool) -> dict:
snapshot = plan["snapshot"]
comparisons = plan["comparisons"]
if snapshot is not None and snapshot.media_type == "message/rfc822":
from app.mailpack import parse_mail
comparisons = _compare_mail(conn, actor, dispatch["id"], page, MailPayload(snapshot.data, parse_mail(snapshot.data)))
proof = conn.execute(
"""
INSERT INTO dispatch_evidence (
dispatch_id, element_id, role, source_state, gap_reason, message_id,
identity_sha256, comparisons, manual, created_by
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, true, %s)
RETURNING *
""",
(
dispatch["id"],
plan["element"]["id"],
plan["role"],
"identified" if plan["identified"] else "gap",
plan["gap_reason"],
plan["message_id"],
plan["digest"],
Json(comparisons),
actor,
),
).fetchone()
if plan["staged"] is not None and snapshot is not None:
_record_snapshot(
conn,
container_row["home_scope_id"],
actor,
snapshot,
plan["staged"],
evidence_id=proof["id"],
)
if plan["fold"] == "evidence":
_set_binding_fold(conn, scope, plan["element"]["id"], "evidence")
if log:
_log(conn, dispatch["id"], actor, f"Nachweis ergänzt: {EVIDENCE_ROLES[plan['role']]}")
audit(conn, actor, container_row["home_scope_id"], "dispatch_evidence", "element", plan["element"]["id"], page)
return {
"id": str(proof["id"]),
"element_id": str(plan["element"]["id"]),
"role": plan["role"],
"role_label": EVIDENCE_ROLES[plan["role"]],
"source_state": proof["source_state"],
"gap_reason": plan["gap_reason"],
"manual": True,
"comparisons": comparisons,
}
def _record_snapshot(conn, scope_id, actor, payload: FilePayload, staged: dict, dispatch_item_id=None, evidence_id=None) -> UUID:
conn.execute(
"""
INSERT INTO dispatch_snapshots (
@@ -951,7 +1119,7 @@ def _store_snapshot(conn, scope_id, actor, payload: FilePayload, media_root: str
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'none')
""",
(
snapshot_id,
staged["id"],
dispatch_item_id,
evidence_id,
scope_id,
@@ -959,15 +1127,20 @@ def _store_snapshot(conn, scope_id, actor, payload: FilePayload, media_root: str
display_filename(payload.filename),
payload.media_type or "application/octet-stream",
len(payload.data),
digest,
instance["id"],
instance["generation"],
object_key,
content_revision,
sha256(payload.data),
staged["instance"]["id"],
staged["instance"]["generation"],
staged["object_key"],
staged["content_revision"],
LEGACY_PROFILE,
),
)
return snapshot_id
return staged["id"]
def _store_snapshot(conn, scope_id, actor, payload: FilePayload, media_root: str, cleanup: list[Path], dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> UUID:
staged = _stage_snapshot(conn, media_root, payload.data, cleanup)
return _record_snapshot(conn, scope_id, actor, payload, staged, dispatch_item_id, evidence_id)
def _snapshot_file(conn, media_root: str, dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> FilePayload: