"""HTTP for attachment roles, documented dispatches and secured editions.""" from __future__ import annotations from uuid import UUID from fastapi import APIRouter, Depends, Request, Response from psycopg import Connection from app.access.policy import Ctx from app.api.deps import Principal, get_conn, get_principal from app.api.schemas import ( AttachmentAssignIn, AttachmentDetachIn, AttachmentOrderIn, DispatchCorrectIn, DispatchEvidenceDetachIn, DispatchEvidenceIn, DispatchIn, FoldIn, ) from app.correspondence import ( GapNote, add_dispatch_evidence, assign_attachments, detach_dispatch_evidence, correct_dispatch, detach_attachment, dispatch_view, open_container, order_attachments, read_snapshot, record_dispatch, set_fold, ) from app.core.errors import not_found from app.media.storage import content_disposition router = APIRouter() def _ctx(value) -> Ctx: return Ctx(value.kind, value.id) def _placement(value) -> Ctx | None: if value is None: return None return Ctx(value.kind, value.id) @router.post("/api/v1/elements/{element_id}/attachments", status_code=201) def post_attachments( element_id: UUID, body: AttachmentAssignIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): return assign_attachments( conn, principal.account_id, element_id, body.expected_version, _ctx(body.context), _placement(body.placement), body.attachment_element_ids, body.fold, [], ) @router.post("/api/v1/elements/{element_id}/attachments/order") def post_attachment_order( element_id: UUID, body: AttachmentOrderIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): return order_attachments( conn, principal.account_id, element_id, body.expected_version, _ctx(body.context), _placement(body.placement), body.attachment_element_ids, ) @router.post("/api/v1/elements/{element_id}/attachments/{attachment_id}/detach", status_code=204) def post_detach( element_id: UUID, attachment_id: UUID, body: AttachmentDetachIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): detach_attachment( conn, principal.account_id, element_id, attachment_id, body.expected_version, _ctx(body.context), _placement(body.placement), ) @router.patch("/api/v1/bindings/{binding_id}/fold") def patch_fold( binding_id: UUID, body: FoldIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): return set_fold( conn, principal.account_id, binding_id, body.expected_version, _ctx(body.context), _placement(body.placement), body.fold, ) @router.post("/api/v1/elements/{element_id}/dispatches", status_code=201) def post_dispatch( element_id: UUID, body: DispatchIn, request: Request, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): request.state.cleanup_files = [] return record_dispatch( conn, principal.account_id, element_id, body.expected_version, _ctx(body.context), _placement(body.placement), body.channel, body.sent_on, body.counterpart, body.attachment_element_ids, [item.model_dump() for item in body.evidence], body.historical_gap, body.capture_editions, request.app.state.settings.media_root, request.state.cleanup_files, ) @router.post("/api/v1/dispatches/{dispatch_id}/evidence", status_code=201) def post_evidence( dispatch_id: UUID, body: DispatchEvidenceIn, request: Request, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): request.state.cleanup_files = [] return add_dispatch_evidence( conn, principal.account_id, dispatch_id, body.expected_version, _ctx(body.context), _placement(body.placement), body.element_id, body.role, body.fold, request.app.state.settings.media_root, request.state.cleanup_files, ) @router.post("/api/v1/dispatches/{dispatch_id}/evidence/{element_id}/detach", status_code=204) def post_detach_evidence( dispatch_id: UUID, element_id: UUID, body: DispatchEvidenceDetachIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): detach_dispatch_evidence( conn, principal.account_id, dispatch_id, element_id, body.expected_version, _ctx(body.context), _placement(body.placement), ) @router.post("/api/v1/dispatches/{dispatch_id}/correct") def post_correct( dispatch_id: UUID, body: DispatchCorrectIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): return correct_dispatch( conn, principal.account_id, dispatch_id, body.expected_version, _ctx(body.context), _placement(body.placement), body.channel, body.sent_on, body.clear_sent_on, body.counterpart, body.clear_counterpart, ) @router.get("/api/v1/dispatches/{dispatch_id}") def get_dispatch( dispatch_id: UUID, context_kind: str, context_id: UUID, placement_kind: str | None = None, placement_id: UUID | None = None, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): context = Ctx(context_kind, context_id) placement = Ctx(placement_kind, placement_id) if placement_kind and placement_id else None _row, page, scope = open_container(conn, principal.account_id, context, placement, "read") return dispatch_view(conn, principal.account_id, dispatch_id, page, scope) @router.get("/api/v1/dispatches/{dispatch_id}/items/{item_id}/content") def get_dispatch_item( dispatch_id: UUID, item_id: UUID, context_kind: str, context_id: UUID, request: Request, placement_kind: str | None = None, placement_id: UUID | None = None, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): context = Ctx(context_kind, context_id) placement = Ctx(placement_kind, placement_id) if placement_kind and placement_id else None result = read_snapshot( conn, principal.account_id, dispatch_id, item_id, context, placement, request.app.state.settings.media_root, ) if isinstance(result, GapNote): raise not_found() return Response( content=result.data, media_type=result.media_type or "application/octet-stream", headers={ "Content-Disposition": content_disposition(result.filename), "X-Content-Type-Options": "nosniff", "Cache-Control": "private, no-store", }, )