Verhinderung von mehrfacheinbindung von Dokumenten #12

Merged
Lars merged 4 commits from develop into main 2026-10-25 10:46:02 +01:00
24 changed files with 1104 additions and 131 deletions

No files matched your search

+40 -12
View File
@@ -1,16 +1,15 @@
name: Test Suite
# Runs once per push, after "Deploy Development" has updated the checkout it tests.
# A push trigger here would start a second run that races the deploy.
# One run per push. The deploy starts at the same time and updates the checkout this suite tests,
# so the jobs wait until that checkout contains the pushed commit. A workflow_run trigger
# (after the deploy) did not start reliably on this Gitea, see git log of this file.
on:
workflow_run:
workflows: ["Deploy Development"]
types: [completed]
push:
branches: [develop]
workflow_dispatch:
jobs:
pytest-backend:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- name: pytest against Dev Postgres (tsumugu_test)
@@ -19,6 +18,19 @@ jobs:
APP_DIR="/home/lars/docker/tsumugu-dev"
COMPOSE_FILE="docker-compose.dev-env.yml"
echo "pytest uses Dev Postgres (${APP_DIR}); event=${{ github.event_name }}"
if [ "${{ github.event_name }}" = "push" ]; then
for i in $(seq 1 180); do
if git -c safe.directory='*' -C "$APP_DIR" merge-base --is-ancestor "${{ github.sha }}" HEAD 2>/dev/null; then
echo "Dev checkout contains ${{ github.sha }} (attempt $i)"
break
fi
if [ "$i" -eq 180 ]; then
echo "the deploy did not reach ${{ github.sha }} within 15 minutes"
exit 1
fi
sleep 5
done
fi
cd "$APP_DIR"
for i in $(seq 1 60); do
if [ -f "$APP_DIR/backend/pytest.ini" ] && docker compose -f "$COMPOSE_FILE" exec -T backend true 2>/dev/null; then
@@ -59,27 +71,43 @@ jobs:
'
smoke-dev:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
# After pytest, so the deploy that started with the push has normally finished. The health check retries.
needs: pytest-backend
runs-on: ubuntu-latest
steps:
- name: Non-destructive smoke against the running Dev app
run: |
set -e
echo "live Dev API (tsumugu_dev), read-only health"
curl -sf http://localhost:8094/api/health
echo
curl -sf http://localhost:3094/api/health
echo
for url in http://localhost:8094/api/health http://localhost:3094/api/health; do
for i in $(seq 1 30); do
if curl -sf "$url"; then echo; break; fi
if [ "$i" -eq 30 ]; then echo "no health answer from $url"; exit 1; fi
sleep 10
done
done
echo "LAN smoke OK. Public URL after TLS: https://dev.tsumugu.jinkendo.de"
frontend-build:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- name: Frontend production build on Dev checkout
run: |
set -e
APP_DIR="/home/lars/docker/tsumugu-dev"
if [ "${{ github.event_name }}" = "push" ]; then
for i in $(seq 1 180); do
if git -c safe.directory='*' -C "$APP_DIR" merge-base --is-ancestor "${{ github.sha }}" HEAD 2>/dev/null; then
echo "Dev checkout contains ${{ github.sha }} (attempt $i)"
break
fi
if [ "$i" -eq 180 ]; then
echo "the deploy did not reach ${{ github.sha }} within 15 minutes"
exit 1
fi
sleep 5
done
fi
cd "$APP_DIR/frontend"
npm ci
npm run build
+52
View File
@@ -0,0 +1,52 @@
"""Placing a source document into a container: a new reference, or one more binding of an existing element."""
from __future__ import annotations
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
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 ExistingIn, ExternalIn
from app.ops import bind_existing_external, create_external, existing_externals
router = APIRouter()
def _ctx_model(value) -> Ctx | None:
return None if value is None else Ctx(value.kind, value.id)
def place_external(conn, principal: Principal, body: ExternalIn) -> dict:
context = Ctx(body.context.kind, body.context.id)
placement = _ctx_model(body.placement)
if body.bind_element_id is not None:
return bind_existing_external(conn, principal.account_id, principal.scope_id, body.bind_element_id, context, placement, body.chapter_id)
return create_external(
conn,
principal.account_id,
principal.scope_id,
body.title,
body.source,
body.external_id,
context,
placement,
body.chapter_id,
body.dates.document_on,
body.dates.sent_on,
body.dates.received_on,
body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None,
duplicate=body.duplicate,
)
@router.post("/api/v1/external-refs/existing")
def existing(body: ExistingIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
"""Read only: which of these documents are already present around the target. Nothing is written."""
items = [
{"source": item.source, "external_id": item.external_id, "connector_id": str(item.connector_id) if item.connector_id else None}
for item in body.items
]
result = existing_externals(conn, principal.account_id, Ctx(body.context.kind, body.context.id), _ctx_model(body.placement), items)
return JSONResponse(result, headers={"Cache-Control": "no-store"})
+41
View File
@@ -0,0 +1,41 @@
"""Account preferences: read the effective value, change the own value, bind the team."""
from __future__ import annotations
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from psycopg import Connection
from app.access.policy import audit
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import PreferenceIn
from app.preferences import preference_state, store_preference
router = APIRouter()
_NO_STORE = {"Cache-Control": "no-store"}
@router.get("/api/v1/auth/preferences/{key}")
def get_preference(key: str, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
return JSONResponse(preference_state(conn, principal.account_id, key), headers=_NO_STORE)
@router.put("/api/v1/auth/preferences/{key}")
def put_preference(
key: str,
body: PreferenceIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
state = store_preference(conn, principal.account_id, key, body.value, body.team_mode)
audit(
conn,
principal.account_id,
None,
"preference_changed",
"account",
principal.account_id,
detail={"key": key, "value": body.value, "team_mode": body.team_mode},
)
return JSONResponse(state, headers=_NO_STORE)
+2 -16
View File
@@ -34,6 +34,7 @@ from app.access.policy import (
)
from app.access.limits import ensure_can_add_member
from app.api.deps import Principal, get_conn, get_principal, get_session
from app.api.externals import place_external
from app.api.schemas import (
AcceptInviteIn,
AnnotationIn,
@@ -113,7 +114,6 @@ from app.ops import (
create_chapter,
create_cross_link,
create_deadline,
create_external,
create_file,
create_matter,
create_note,
@@ -860,21 +860,7 @@ def post_annotation(
@router.post("/api/v1/external-refs")
def post_external(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
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(mode="json", exclude_none=True) if body.detail else None,
)
element = place_external(conn, principal, body)
return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]})
+23
View File
@@ -352,6 +352,22 @@ class ExternalIn(APIModel):
chapter_id: UUID | None = None
dates: DatesIn = DatesIn()
detail: ExternalDetail | None = None
# "ask": a document that is already present at the account's check level answers 409.
# "insert": the user chose to insert it again. `bind_element_id` binds that element instead of creating one.
duplicate: Literal["ask", "insert"] = "ask"
bind_element_id: UUID | None = None
class ExistingItemIn(APIModel):
source: str = Field(min_length=1, max_length=40)
external_id: str = Field(min_length=1, max_length=200)
connector_id: UUID | None = None
class ExistingIn(APIModel):
context: ContextIn
placement: ContextIn | None = None
items: list[ExistingItemIn] = Field(min_length=1, max_length=100)
class ConnectorIn(APIModel):
@@ -521,6 +537,13 @@ class DispatchCorrectIn(APIModel):
clear_counterpart: bool = False
class PreferenceIn(APIModel):
"""`value` replaces the own value; null means: inherit. `team_mode` is for main users and stays as it is when absent."""
value: str | None = Field(default=None, max_length=40)
team_mode: Literal["free", "locked"] | None = None
class DownloadPolicyIn(APIModel):
"""Who may save or open the original outside of Tsumugu. `direct` only applies to the account default."""
+4 -18
View File
@@ -8,15 +8,15 @@ from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse, Response
from psycopg import Connection
from app.access.policy import Ctx, element_rights, get_element, parse_query_context
from app.access.policy import element_rights, get_element, parse_query_context
from app.api.deps import Principal, get_conn, get_principal
from app.api.routes import _created, _ctx_model
from app.api.routes import _created
from app.api.schemas import ConnectorIn, ExternalIn, SourceAssignmentIn
from app.core.errors import DOWNLOAD_REFUSED, forbidden, not_found, unprocessable, unsupported
from app.mailpack import _inline_images, parse_mail, sanitize_html
from app.remoteimages import RemoteImages
from app.media.storage import content_disposition
from app.ops import create_external
from app.api.externals import place_external
from app.retrieval import RetrievalFailure
from app.sourcebind import assign_reference
from app.sourceport import capability_payload, with_capabilities
@@ -370,21 +370,7 @@ def read_mail(
def link_source(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
if get_source(body.source) is None:
raise unprocessable("Verknüpft werden registrierte Quellen.")
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(mode="json", exclude_none=True) if body.detail else None,
)
element = place_external(conn, principal, body)
return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]})
+166
View File
@@ -0,0 +1,166 @@
"""Documents of a source that are inserted into a container again.
The same document in different Vorgänge stays allowed. This module only finds where a document is
already present around the target, so the dialog can warn and the user can decide. Levels (account
preference `duplicate_check`):
- here: bound directly to the target.
- inside: also what the target shows: Teilvorgänge of a Vorgang, the linked Vorgänge of an Akte.
- around: also where the target lies: its Vorgang, the Akten it is linked to, the parent Akte.
Only elements the actor may read are reported. A hit outside the actor's rights does not exist here.
"""
from __future__ import annotations
from uuid import UUID
from app.access.policy import (
Ctx,
case_rights,
element_rights,
get_case,
get_matter,
matter_rights,
)
from app.core.errors import Problem
LEVELS = {"off": (), "here": ("here",), "inside": ("here", "inside"), "around": ("here", "inside", "around")}
_ORDER = {"here": 0, "inside": 1, "around": 2}
def _stored_connector(row: dict) -> str | None:
if row["connector_id"] is not None:
return str(row["connector_id"])
stored = (row["detail"] or {}).get("connector_id")
return str(stored) if stored else None
def _candidates(conn, source: str, external_id: str, connector_id: str | None) -> list[dict]:
"""Elements for the same document. `certain` is False when only one side knows the connection."""
rows = conn.execute(
"""
SELECT e.id, e.title, e.owner_subject_id, er.connector_id, er.detail
FROM external_references er
JOIN elements e ON e.id = er.element_id
WHERE er.source = %s AND er.external_id = %s AND e.deleted_at IS NULL
""",
(source, external_id.strip()[:200]),
).fetchall()
found = []
for row in rows:
stored = _stored_connector(row)
if stored is not None and connector_id is not None and stored != connector_id:
continue
found.append(row | {"certain": stored == connector_id})
return found
def _children(conn, matter_id: UUID) -> list[UUID]:
rows = conn.execute("SELECT id FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL", (matter_id,)).fetchall()
return [row["id"] for row in rows]
def _zones(conn, target: Ctx) -> dict[str, list[Ctx]]:
inside: list[Ctx] = []
around: list[Ctx] = []
if target.kind == "matter":
inside = [Ctx("matter", child) for child in _children(conn, target.id)]
matter = get_matter(conn, target.id)
anchors = [target.id]
if matter is not None and matter["parent_matter_id"] is not None:
around.append(Ctx("matter", matter["parent_matter_id"]))
anchors.append(matter["parent_matter_id"])
links = conn.execute("SELECT DISTINCT case_id FROM matter_case_links WHERE matter_id = ANY(%s)", (anchors,)).fetchall()
around += [Ctx("case", row["case_id"]) for row in links]
else:
links = conn.execute("SELECT matter_id FROM matter_case_links WHERE case_id = %s", (target.id,)).fetchall()
for row in links:
inside.append(Ctx("matter", row["matter_id"]))
inside += [Ctx("matter", child) for child in _children(conn, row["matter_id"])]
case = get_case(conn, target.id)
if case is not None and case["parent_case_id"] is not None:
around.append(Ctx("case", case["parent_case_id"]))
return {"here": [target], "inside": inside, "around": around}
def _blocked(conn, case_id: UUID, element_id: UUID) -> bool:
row = conn.execute(
"""
SELECT 1 FROM case_link_blocks b JOIN matter_case_links l ON l.id = b.link_id
WHERE l.case_id = %s AND b.element_id = %s
""",
(case_id, element_id),
).fetchone()
return row is not None
def _title_if_readable(conn, actor: UUID, container: Ctx) -> str | None:
if container.kind == "case":
case = get_case(conn, container.id)
return case["title"] if case is not None and case_rights(conn, actor, case).read else None
matter = get_matter(conn, container.id)
return matter["title"] if matter is not None and matter_rights(conn, actor, matter, None).read else None
def find_existing(
conn,
actor: UUID,
source: str,
external_id: str,
connector_id: str | None,
target: Ctx,
rights_context: Ctx | None,
level: str,
) -> list[dict]:
"""Matches of one document at the chosen level, readable by the actor, nearest zone first."""
zones = LEVELS.get(level, ())
if not zones:
return []
candidates = _candidates(conn, source, external_id, connector_id)
if not candidates:
return []
layout = _zones(conn, target)
by_element: dict[UUID, dict] = {}
for candidate in candidates:
bindings = conn.execute(
"SELECT b.context_kind, b.context_id, c.title AS chapter FROM bindings b LEFT JOIN chapters c ON c.id = b.chapter_id WHERE b.element_id = %s",
(candidate["id"],),
).fetchall()
for zone in zones:
for container in layout[zone]:
hit = next((b for b in bindings if b["context_kind"] == container.kind and b["context_id"] == container.id), None)
if hit is None:
continue
if target.kind == "case" and zone == "inside" and _blocked(conn, target.id, candidate["id"]):
continue
seen_in = [container] if zone == "around" else [c for c in (target, rights_context) if c is not None]
rights = [element_rights(conn, actor, candidate, ctx) for ctx in seen_in]
if not any(r.read for r in rights):
continue
match = {
"element_id": str(candidate["id"]),
"title": candidate["title"],
"where": zone,
"certain": candidate["certain"],
"chapter": hit["chapter"] if zone == "here" else None,
"container": {
"kind": container.kind,
"title": _title_if_readable(conn, actor, container) if zone != "here" else None,
},
"can_bind": any(r.share for r in rights),
}
known = by_element.get(candidate["id"])
if known is None or _ORDER[zone] < _ORDER[known["where"]]:
by_element[candidate["id"]] = match
return sorted(by_element.values(), key=lambda item: (_ORDER[item["where"]], item["title"]))
def already_present(matches: list[dict]) -> Problem:
where = {"here": "hier", "inside": "in diesem Bestand", "around": "im Umfeld"}[matches[0]["where"]]
return Problem(
409,
"Bereits vorhanden",
f"Dieses Dokument ist {where} schon vorhanden. Füge es nur dann erneut ein, wenn das gewollt ist.",
"already_present",
)
+4
View File
@@ -13,7 +13,9 @@ from app.api.accounts import router as accounts_router
from app.api.correspondence import router as correspondence_router
from app.api.downloads import router as downloads_router
from app.api.export import router as export_router
from app.api.externals import router as externals_router
from app.api.library import router as library_router
from app.api.preferences import router as preferences_router
from app.api.rooms import router as rooms_router
from app.api.roomshare import router as roomshare_router
from app.api.team import router as team_router
@@ -130,6 +132,8 @@ def create_app() -> FastAPI:
app.include_router(sources_router)
app.include_router(export_router)
app.include_router(downloads_router)
app.include_router(preferences_router)
app.include_router(externals_router)
app.include_router(correspondence_router)
return app
+55 -1
View File
@@ -36,6 +36,8 @@ from app.access.policy import (
from app.content.documents import clean_document
from app.core.errors import forbidden, not_found, unprocessable, unsupported
from app.core.timeutil import utcnow
from app.preferences import effective_preference
from app.duplicates import already_present, find_existing
from app.media.port import LEGACY_PROFILE, StoredHandle, adapter_for, instance_for_write, publish, removal_for
from app.media.storage import display_filename, sha256, sniff
@@ -1142,7 +1144,7 @@ def add_annotation(conn, actor: UUID, binding_id: UUID, body: str, context: Ctx
return row
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None, communication_direction: str | None = None) -> dict:
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None, communication_direction: str | None = None, duplicate: str = "insert") -> dict:
ensure_can_create(conn, actor, scope_id)
from app.sourceregistry import accepts_reference
@@ -1150,6 +1152,13 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str,
raise unprocessable("Diese Quelle ist nicht registriert.")
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
if duplicate == "ask":
# The level is the account's choice. Only a certain match warns: an unclear connection never blocks.
connector = (detail or {}).get("connector_id")
level = effective_preference(conn, actor, "duplicate_check")
found = [item for item in find_existing(conn, actor, source, external_id, str(connector) if connector else None, target, rights_context, level) if item["certain"]]
if found:
raise already_present(found)
element = _new_element(conn, actor, scope_id, title, "external_ref")
conn.execute(
"INSERT INTO external_references (element_id, source, external_id, detail) VALUES (%s, %s, %s, %s)",
@@ -1167,6 +1176,51 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str,
return element | {"binding_id": binding["id"]}
def existing_externals(conn, actor: UUID, context: Ctx, placement: Ctx | None, items: list[dict]) -> dict:
"""For each document: where it is already present around the target, at the account's check level."""
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
level = effective_preference(conn, actor, "duplicate_check")
return {
"level": level,
"items": [
item
| {"matches": find_existing(conn, actor, item["source"], item["external_id"], item.get("connector_id"), target, rights_context, level)}
for item in items
],
}
def bind_existing_external(conn, actor: UUID, scope_id: UUID, element_id: UUID, context: Ctx, placement: Ctx | None, chapter_id: UUID | None) -> dict:
"""Bind an element that already holds the document into a further container: one element, one more binding.
The element must be a certain match around the target, readable for the actor, and the actor may share it
there. Nothing is copied and nothing is handed over: rights stay those of each context.
"""
ensure_can_create(conn, actor, scope_id)
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
ref = conn.execute(
"SELECT source, external_id, connector_id, detail FROM external_references WHERE element_id = %s",
(element_id,),
).fetchone()
if ref is None:
raise not_found()
connector = ref["connector_id"] or (ref["detail"] or {}).get("connector_id")
matches = find_existing(conn, actor, ref["source"], ref["external_id"], str(connector) if connector else None, target, rights_context, "around")
match = next((item for item in matches if item["element_id"] == str(element_id) and item["certain"]), None)
if match is None:
raise not_found()
if match["where"] == "here":
raise unprocessable("Das Dokument ist hier bereits eingebunden.")
if not match["can_bind"]:
raise forbidden("Du darfst dieses Element nicht in einen weiteren Zusammenhang einbinden.")
element = get_element(conn, element_id)
binding = insert_binding(conn, element_id, target.kind, target.id, chapter_id, actor)
audit(conn, actor, scope_id, "external_ref_bound", "element", element_id, rights_context or target)
return element | {"binding_id": binding["id"]}
def create_file(conn, actor: UUID, scope_id: UUID, title: str, filename: str, data: bytes, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, media_root: str, max_bytes: int, rollback_files: list[Path], communication_direction: str | None = None) -> dict:
ensure_can_create(conn, actor, scope_id)
if not data or len(data) > max_bytes:
+112
View File
@@ -0,0 +1,112 @@
"""Account preferences with inheritance.
An employee inherits the value of the main user who sponsors the account and may change it,
unless the main user made the preference binding for the team. A binding value overrides an own
value without deleting it: it applies again once the binding is lifted.
Allowed values, default and lockability of each key are fixed here.
"""
from __future__ import annotations
from dataclasses import dataclass
from uuid import UUID
from psycopg.types.json import Json
from app.core.errors import forbidden, not_found, unprocessable
@dataclass(frozen=True)
class Preference:
default: str
allowed: tuple[str, ...]
team_lockable: bool = True
PREFERENCES: dict[str, Preference] = {
# Where an element that is inserted again counts as already present.
"duplicate_check": Preference("here", ("off", "here", "inside", "around")),
}
def spec(key: str) -> Preference:
found = PREFERENCES.get(key)
if found is None:
raise not_found()
return found
def _valid(wanted: Preference, value) -> str | None:
return value if isinstance(value, str) and value in wanted.allowed else None
def preference_state(conn, account_id: UUID, key: str) -> dict:
"""The effective value and where it comes from: locked, own, team or default."""
wanted = spec(key)
row = conn.execute(
"""
SELECT a.account_kind,
own.value AS own_value, own.team_mode AS own_mode,
sp.value AS team_value, sp.team_mode AS team_mode
FROM auth_accounts a
LEFT JOIN account_preferences own ON own.account_id = a.id AND own.key = %s
LEFT JOIN account_preferences sp ON sp.account_id = a.sponsor_id AND sp.key = %s
WHERE a.id = %s
""",
(key, key, account_id),
).fetchone()
if row is None:
raise not_found()
employee = row["account_kind"] == "employee"
own = _valid(wanted, row["own_value"])
team = _valid(wanted, row["team_value"]) if employee else None
locked = employee and wanted.team_lockable and row["team_mode"] == "locked"
inherited = team or wanted.default
if locked:
value, source = inherited, "locked"
elif own is not None:
value, source = own, "own"
elif team is not None:
value, source = team, "team"
else:
value, source = wanted.default, "default"
return {
"key": key,
"value": value,
"source": source,
"own": own,
"inherited": inherited,
"locked": locked,
"allowed": list(wanted.allowed),
"default": wanted.default,
"can_lock": row["account_kind"] == "main" and wanted.team_lockable,
"team_mode": row["own_mode"] if row["own_mode"] and not employee else "free",
}
def effective_preference(conn, account_id: UUID, key: str) -> str:
return preference_state(conn, account_id, key)["value"]
def store_preference(conn, account_id: UUID, key: str, value: str | None, team_mode: str | None) -> dict:
"""Own value (None: inherit) and, for a main user, the binding for the team."""
wanted = spec(key)
state = preference_state(conn, account_id, key)
if value is not None and value not in wanted.allowed:
raise unprocessable("Dieser Wert ist für die Einstellung nicht vorgesehen.")
if team_mode is not None and not state["can_lock"]:
raise forbidden("Nur der Hauptnutzer kann eine Einstellung für das Team verbindlich setzen.")
if state["locked"]:
raise forbidden("Der Hauptnutzer hat diese Einstellung für das Team vorgegeben.")
conn.execute(
"""
INSERT INTO account_preferences (account_id, key, value, team_mode)
VALUES (%s, %s, %s, COALESCE(%s, 'free'))
ON CONFLICT (account_id, key) DO UPDATE
SET value = EXCLUDED.value,
team_mode = COALESCE(%s, account_preferences.team_mode),
updated_at = now()
""",
(account_id, key, Json(value) if value is not None else None, team_mode, team_mode),
)
return preference_state(conn, account_id, key)
+2
View File
@@ -1082,4 +1082,6 @@ LABELS = {
"room_default_changed": "Voreinstellung des Datenraums geändert",
"room_download_policy_changed": "Download-Regel des Datenraums geändert",
"download_defaults_changed": "Standard für Downloads geändert",
"preference_changed": "Einstellung des Kontos geändert",
"external_ref_bound": "Vorhandene Referenz zusätzlich eingebunden",
}
@@ -0,0 +1,18 @@
-- Einstellungen je Konto. Ein Mitarbeiter erbt den Wert seines Hauptnutzers und kann ihn ändern,
-- solange der Hauptnutzer die Einstellung nicht für das Team verbindlich gesetzt hat (team_mode).
-- Erlaubte Werte, Standard und Verbindlichkeit je Schlüssel stehen im Code (app/preferences.py).
-- value NULL: kein eigener Wert, es gilt die Erbung bzw. der Standard.
CREATE TABLE account_preferences (
account_id uuid NOT NULL REFERENCES auth_accounts (id) ON DELETE CASCADE,
key text NOT NULL,
value jsonb,
team_mode text NOT NULL DEFAULT 'free',
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (account_id, key),
CONSTRAINT account_preferences_key_len CHECK (char_length(key) BETWEEN 1 AND 60),
CONSTRAINT account_preferences_team_mode CHECK (team_mode IN ('free', 'locked'))
);
-- Dieselbe Quelle und Kennung: Suche nach bereits eingefügten Dokumenten (app/duplicates.py).
CREATE INDEX external_references_identity ON external_references (source, external_id);
+126
View File
@@ -0,0 +1,126 @@
"""Bereits eingefügte Quelldokumente: Vorabfrage, Warnung (409), erneutes Einfügen und zusätzliches Einbinden."""
from __future__ import annotations
from uuid import uuid4
import pytest
from tests.support import matter_of
from tests.test_team import _main
PREF = "/api/v1/auth/preferences/duplicate_check"
EXISTING = "/api/v1/external-refs/existing"
CONNECTOR = str(uuid4())
@pytest.fixture
def ada(client, db):
return _main(client, db, "ada", main=True)
def _ref(actor, kind, container, external_id="41", connector=CONNECTOR, status=201, **extra):
body = {
"title": f"Dokument {external_id}",
"source": "paperless",
"external_id": external_id,
"context": {"kind": kind, "id": container},
"detail": {"connector_id": connector},
**extra,
}
return actor.request("POST", "/api/v1/external-refs", json=body, status=status)
def _ask(actor, kind, container, external_id="41", connector=CONNECTOR):
body = {
"context": {"kind": kind, "id": container},
"items": [{"source": "paperless", "external_id": external_id, "connector_id": connector}],
}
return actor.request("POST", EXISTING, json=body)["items"][0]["matches"]
def _level(actor, value):
actor.request("PUT", PREF, json={"value": value})
def test_a_document_in_the_same_container_warns_and_can_be_inserted_anyway(ada):
matter = matter_of(ada, "Mietminderung")["id"]
first = _ref(ada, "matter", matter)
found = _ask(ada, "matter", matter)
assert [(m["element_id"], m["where"], m["certain"]) for m in found] == [(first["id"], "here", True)]
refused = _ref(ada, "matter", matter, status=409)
assert refused["code"] == "already_present"
# A deliberate second insert goes through. Another document or another connection is no duplicate.
_ref(ada, "matter", matter, duplicate="insert")
_ref(ada, "matter", matter, external_id="42")
_ref(ada, "matter", matter, connector=str(uuid4()))
assert _ask(ada, "matter", matter, external_id="42")[0]["where"] == "here"
def test_the_same_document_in_another_vorgang_is_no_duplicate(ada):
one = matter_of(ada, "Eins")["id"]
two = matter_of(ada, "Zwei")["id"]
_ref(ada, "matter", one)
assert _ask(ada, "matter", two) == []
_ref(ada, "matter", two)
def test_the_check_can_be_switched_off(ada):
matter = matter_of(ada, "Mietminderung")["id"]
_ref(ada, "matter", matter)
_level(ada, "off")
assert _ask(ada, "matter", matter) == []
_ref(ada, "matter", matter)
def test_levels_reach_into_teilvorgang_vorgang_and_akte(ada):
parent = matter_of(ada, "Hauptvorgang")["id"]
child = ada.request("POST", "/api/v1/matters", json={"title": "Teil", "parent_matter_id": parent}, status=201)["id"]
case = ada.request("POST", "/api/v1/cases", json={"title": "Akte"}, status=201)["id"]
ada.request("POST", f"/api/v1/matters/{parent}/case-links", json={"case_id": case}, status=201)
_ref(ada, "matter", child, external_id="1")
_ref(ada, "matter", parent, external_id="2")
_ref(ada, "case", case, external_id="3")
def where(kind, container, external_id):
return [m["where"] for m in _ask(ada, kind, container, external_id)]
# Standard: only what sits directly in the target.
assert where("matter", parent, "1") == []
_level(ada, "inside")
assert where("matter", parent, "1") == ["inside"]
assert where("matter", child, "2") == []
assert where("case", case, "2") == ["inside"]
assert where("case", case, "3") == ["here"]
_level(ada, "around")
assert where("matter", child, "2") == ["around"]
assert where("matter", parent, "3") == ["around"]
assert where("matter", child, "3") == ["around"]
# The server warns at the same level.
_ref(ada, "matter", child, external_id="3", status=409)
def test_an_existing_element_can_be_bound_instead_of_a_new_one(ada):
parent = matter_of(ada, "Hauptvorgang")["id"]
child = ada.request("POST", "/api/v1/matters", json={"title": "Teil", "parent_matter_id": parent}, status=201)["id"]
original = _ref(ada, "matter", child)
_level(ada, "inside")
assert _ask(ada, "matter", parent)[0]["can_bind"] is True
bound = _ref(ada, "matter", parent, bind_element_id=original["id"])
assert bound["id"] == original["id"]
assert [m["where"] for m in _ask(ada, "matter", parent)] == ["here"]
refused = _ref(ada, "matter", parent, bind_element_id=original["id"], status=422)
assert "bereits" in refused["detail"]
def test_a_stranger_neither_sees_nor_binds_my_element(client, db, ada):
mine = matter_of(ada, "Mein Vorgang")["id"]
element = _ref(ada, "matter", mine)
ben = _main(client, db, "ben")
theirs = matter_of(ben, "Ihr Vorgang")["id"]
_level(ben, "around")
assert _ask(ben, "matter", theirs) == []
_ref(ben, "matter", theirs)
_ref(ben, "matter", theirs, bind_element_id=element["id"], status=404)
+79
View File
@@ -0,0 +1,79 @@
"""Einstellungen je Konto: Erbung vom Hauptnutzer, eigener Wert, Verbindlichkeit für das Team."""
from __future__ import annotations
import pytest
from tests.test_team import _invite, _main
PATH = "/api/v1/auth/preferences/duplicate_check"
@pytest.fixture
def world(client, db):
ada = _main(client, db, "ada", main=True)
room = ada.request("POST", "/api/v1/rooms", json={"name": "Kanzlei"}, status=201)["id"]
ada.use_room(room)
emp = _invite(ada, room, "emp-pref@example.test", name="Eva")
return {"ada": ada, "emp": emp}
def test_the_default_applies_without_any_choice(world):
for person in (world["ada"], world["emp"]):
state = person.request("GET", PATH)
assert (state["value"], state["source"]) == ("here", "default")
assert world["ada"].request("GET", PATH)["can_lock"] is True
assert world["emp"].request("GET", PATH)["can_lock"] is False
def test_an_employee_inherits_the_main_user_and_may_override(world):
ada, emp = world["ada"], world["emp"]
ada.request("PUT", PATH, json={"value": "inside"})
inherited = emp.request("GET", PATH)
assert (inherited["value"], inherited["source"]) == ("inside", "team")
own = emp.request("PUT", PATH, json={"value": "off"})
assert (own["value"], own["source"]) == ("off", "own")
# The main user's change does not touch an own choice.
ada.request("PUT", PATH, json={"value": "around"})
assert emp.request("GET", PATH)["value"] == "off"
assert ada.request("GET", PATH)["value"] == "around"
# Inheriting again.
back = emp.request("PUT", PATH, json={"value": None})
assert (back["value"], back["source"]) == ("around", "team")
def test_a_binding_overrides_an_own_value_without_deleting_it(world):
ada, emp = world["ada"], world["emp"]
emp.request("PUT", PATH, json={"value": "off"})
ada.request("PUT", PATH, json={"value": "inside", "team_mode": "locked"})
locked = emp.request("GET", PATH)
assert (locked["value"], locked["source"], locked["locked"]) == ("inside", "locked", True)
refused = emp.request("PUT", PATH, json={"value": "here"}, status=403)
assert "vorgegeben" in refused["detail"]
# The main user is not bound by the own binding.
assert ada.request("PUT", PATH, json={"value": "here"})["value"] == "here"
assert ada.request("GET", PATH)["team_mode"] == "locked"
assert emp.request("GET", PATH)["value"] == "here"
ada.request("PUT", PATH, json={"value": "here", "team_mode": "free"})
again = emp.request("GET", PATH)
assert (again["value"], again["source"]) == ("off", "own")
def test_only_the_main_user_binds_and_values_are_checked(world):
ada, emp = world["ada"], world["emp"]
emp.request("PUT", PATH, json={"value": "off", "team_mode": "locked"}, status=403)
assert emp.request("GET", PATH)["source"] == "default"
ada.request("PUT", PATH, json={"value": "everywhere"}, status=422)
ada.request("GET", "/api/v1/auth/preferences/unknown", status=404)
ada.request("PUT", "/api/v1/auth/preferences/unknown", json={"value": "off"}, status=404)
def test_a_binding_of_a_stranger_does_not_reach_my_employee(client, db, world):
ben = _main(client, db, "ben")
ben.request("PUT", PATH, json={"value": "off", "team_mode": "locked"})
assert world["emp"].request("GET", PATH)["value"] == "here"
+3 -3
View File
@@ -30,7 +30,7 @@ Unter `/home/lars/docker/` legt der Lauf nur diese zwei Verzeichnisse an:
|----------|---------|--------|
| `deploy-dev.yml` | Push `develop` | Deploy nach `/home/lars/docker/tsumugu-dev`, Health `localhost:8094` und `localhost:3094` |
| `deploy-prod.yml` | Push `main` | Deploy nach `/home/lars/docker/tsumugu`, Health `localhost:8007` und `localhost:3007` |
| `test.yml` | Nach erfolgreichem Dev-Deploy (`workflow_run`), manuell | Pytest gegen `tsumugu_test` auf der Dev-Postgres, Smoke der laufenden Dev-App, Frontend-Build |
| `test.yml` | Push auf `develop` (wartet, bis der Dev-Checkout den Commit enthält), manuell | Pytest gegen `tsumugu_test` auf der Dev-Postgres, Smoke der laufenden Dev-App, Frontend-Build |
Gearbeitet wird auf `develop`. Production entsteht nur durch einen Merge-Commit `develop` → `main` (`git merge --no-ff develop` auf `main`) und den anschließenden Push von `main`. Kein Fast-Forward, kein direkter Commit auf `main`, kein manuelles `git pull` auf dem Prod-Checkout. Gitea (`deploy-prod.yml`) ist der einzige Prod-Schreibpfad für Code.
@@ -50,11 +50,11 @@ Diese Punkte sind im [Code-Review](review/06_BEFUNDE.md) beschrieben. Sie stehen
|---|---|---|---|
| Betreiberkonto | Auf einer leeren Datenbank legt die öffentliche Anmeldeseite das Betreiberkonto an. Keine MFA | Nach jedem Neuaufsetzen einer Umgebung sofort selbst anmelden, bevor die Adresse öffentlich erreichbar ist. Alternativ vorher `docker compose exec backend python -m app.cli create-account --email … --name … --password …` ausführen | R-04 |
| Sicherung | Es gibt kein Backup von Datenbank und Volume `tsumugu-media`/`dev-tsumugu-media` und keinen geprobten Restore | Laut technischer Spezifikation 8.2 nur synthetische Daten in Produktion, bis Paket P7 umgesetzt ist | R-21 |
| Release | `deploy-prod.yml` baut bei jedem Push auf `main` neu. Tests laufen nur auf `develop` nach dem Dev-Deploy | Vor dem Merge nach `main` den grünen `test.yml`-Lauf zum selben Stand prüfen | R-21, R-27 |
| Release | `deploy-prod.yml` baut bei jedem Push auf `main` neu. Tests laufen nur auf `develop`, sobald der Dev-Deploy den Commit ausgecheckt hat | Vor dem Merge nach `main` den grünen `test.yml`-Lauf zum selben Stand prüfen | R-21, R-27 |
| Datenbankzugang | Das Backend migriert beim Start mit dem Owner-Zugang. Die Runtime-Rolle `tsumugu_runtime` erhält dasselbe Passwort wie `DB_USER` | Ein Wechsel von `DB_PASSWORD` ändert beim nächsten Start beide Rollen | R-09 |
| Quellen-Geheimnisse | Paperless-Token und IMAP-Passwörter stehen unverschlüsselt in der Tabelle `connectors` | Datenbank-Dumps wie Zugangsdaten schützen | R-05 |
| Sessions | 14 Tage gültig, keine Leerlaufabmeldung, kein Passwortwechsel in der Anwendung | Passwort nur über die Datenbank oder ein neues Konto änderbar | R-08 |
| Testläufe | `test.yml` startet je Push einmal, nach dem Dev-Deploy; alle Läufe teilen die Datenbank `tsumugu_test`, und das Image bleibt das Laufzeitimage | Zwei manuelle Läufe gleichzeitig vermeiden; sie würden sich die Datenbank wegnehmen | R-27 |
| Testläufe | `test.yml` startet je Push einmal, parallel zum Deploy, und wartet auf dessen Checkout (`workflow_run` startete auf dieser Gitea nicht verlässlich); alle Läufe teilen die Datenbank `tsumugu_test`, und das Image bleibt das Laufzeitimage | Zwei manuelle Läufe gleichzeitig vermeiden; sie würden sich die Datenbank wegnehmen | R-27 |
---
+3 -1
View File
@@ -34,6 +34,7 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R**
| POST | `/api/v1/auth/login` | – | E-Mail, Passwort → Session-Sicht und Cookie. 8 Fehlversuche je Bucket (Hash aus IP und E-Mail) in 10 min, gezählt in `auth_login_attempts` |
| POST | `/api/v1/auth/logout` | S | Session widerrufen, Cookie löschen, 204 |
| GET / PUT | `/api/v1/auth/download-defaults` | S+R, Hauptnutzer | Voreinstellung je Rolle und `direct` (direkt freigegebene Personen); neue Räume übernehmen die drei Rollenwerte. Audit `download_defaults_changed` |
| GET / PUT | `/api/v1/auth/preferences/{key}` | S+R | Einstellung des Kontos mit Erbung. GET liefert `value` (wirksam), `source` (`locked`/`own`/`team`/`default`), `own`, `inherited`, `locked`, `allowed`, `can_lock`, `team_mode`. PUT setzt den eigenen Wert (`value`, `null` = erben) und beim Hauptnutzer die Verbindlichkeit für das Team (`team_mode` `free`/`locked`). Ein Mitarbeiter erhält bei verbindlicher Vorgabe 403. Schlüssel: `duplicate_check` (`off`/`here`/`inside`/`around`, Standard `here`). Audit `preference_changed` |
| GET | `/api/v1/auth/session` | S | Konto, bestätigter Datenraum, Räume, CSRF-Token, Epoche |
| POST | `/api/v1/auth/session/scope` | S | Datenraum wechseln (`kind`, `id`); erhöht die Epoche |
| GET | `/api/v1/auth/bootstrap` | – | `{available: true}`, solange kein Betreiber existiert, sonst 404 |
@@ -145,7 +146,8 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R**
| PATCH | `/api/v1/notes/{id}` | gemeinsamen Text ändern; Antwort mit `visible_bindings` und `notice` |
| POST | `/api/v1/files` | Datei hochladen (Multipart: `title`, `context_kind`, `context_id`, `upload`, optional Platzierung, Kapitel, Daten, Richtung); PDF, PNG, JPEG, WebP bis 20 MiB |
| GET | `/api/v1/files/{id}/content` | Datei als `attachment` mit Dateiname (UTF-8, `filename*`), `no-store`. Mit `save=true` zusätzlich das Download-Recht, sonst 403 |
| POST | `/api/v1/external-refs` | externe Referenz (`paperless`, `imap`, `url`) mit `detail` |
| POST | `/api/v1/external-refs` | externe Referenz (`paperless`, `imap`, `url`) mit `detail`. `duplicate` (`ask` Standard, `insert`): bei `ask` antwortet ein bereits vorhandenes Dokument mit 409 `already_present`, wenn nur sichere Treffer auf der Prüfstufe des Kontos zählen (Einstellung `duplicate_check`). `bind_element_id` bindet stattdessen das vorhandene Element zusätzlich in den Zielcontainer (eine Referenz, eine weitere Einbindung; Audit `external_ref_bound`). Nur ein sicherer, lesbarer Treffer im Umfeld des Ziels, mit Weitergaberecht (sonst 403 bzw. 404) |
| POST | `/api/v1/external-refs/existing` | nur lesend: `context`, optional `placement`, `items` (`source`, `external_id`, `connector_id`) → je Dokument die Treffer (`element_id`, `title`, `where` `here`/`inside`/`around`, `certain`, `chapter`, `container`, `can_bind`) auf der Prüfstufe des Kontos. Nur Lesbares, ein Treffer außerhalb der Rechte gilt als nicht vorhanden |
| POST | `/api/v1/sources/link` | wie oben, nur `paperless`/`imap` (doppelter Weg, siehe Connectoren-Befund) |
| GET | `/api/v1/elements` | eigene, nicht eingebundene Elemente (max. 100) |
| POST | `/api/v1/elements/{id}/delete` | überall löschen; nur Element-Owner |
+2 -1
View File
@@ -1,6 +1,6 @@
# Tsumugu – Datenmodell (Ist-Stand)
**Stand:** 19.10.2026, nach den Migrationen in `backend/migrations/` (letzte: `20261022T090000Z_account_kinds.sql`). Entstanden im Code-Review vom 18.10.2026 und für die Connector-Grundlage nachgezogen. Die Arten `connectors.kind` und `external_references.source` prüft die Registrierung. Die früheren Prüflisten in der Datenbank sind entfernt. `url` bleibt eine Referenz ohne Connector.
**Stand:** 19.10.2026, nach den Migrationen in `backend/migrations/` (letzte: `20261025T090000Z_account_preferences.sql`). Entstanden im Code-Review vom 18.10.2026 und für die Connector-Grundlage nachgezogen. Die Arten `connectors.kind` und `external_references.source` prüft die Registrierung. Die früheren Prüflisten in der Datenbank sind entfernt. `url` bleibt eine Referenz ohne Connector.
Die Sollfassung steht in der [technischen Spezifikation, Abschnitt 4](TSUMUGU_TECHNICAL_SPEC.md). Diese Datei beschreibt, was tatsächlich angelegt ist. Bei jeder neuen Migration nachziehen.
@@ -22,6 +22,7 @@ Die Sollfassung steht in der [technischen Spezifikation, Abschnitt 4](TSUMUGU_TE
| `data_rooms` | `id`, `kind` (`personal`/`organization`), `name`, `status` (`active`/`archived`), `created_by` | persönlicher Raum: `id = account_id` |
| `room_memberships` | PK (`room_id`, `subject_id`), `role` (`member`/`manager`/`reader`), `status` (`active`/`removed`) | – |
| `auth_accounts` (Spalte seit 23.10.2026) | `download_defaults` (jsonb: `manager`, `member`, `reader`, `direct`; Voreinstellung für das Herunterladen von Originalen, Standard überall erlaubt) | Neue Räume übernehmen die drei Rollenwerte |
| `account_preferences` (seit 25.10.2026) | PK (`account_id`, `key`), `value` (jsonb, `NULL` = erben), `team_mode` (`free`/`locked`, nur beim Hauptnutzer ausgewertet), `updated_at` | Ein Mitarbeiter erbt den Wert seines Hauptnutzers (`sponsor_id`) und kann ihn ändern. Ist die Einstellung für das Team verbindlich, gilt der Wert des Hauptnutzers; ein eigener Wert bleibt gespeichert und gilt wieder, sobald die Bindung endet. Erlaubte Werte und Standard je Schlüssel stehen in `app/preferences.py` |
| `data_rooms` (Spalte seit 23.10.2026) | `download_roles` (jsonb: `manager`, `member`, `reader`) | Regel je Raum, nur der Eigentümer ändert sie |
| `data_rooms` (Spalten seit 21.10.2026) | `owner_id` (Eigentümer; für persönliche Räume das Konto), `default_visibility` (`private`/`room`, Voreinstellung für neue Akten und Vorgänge) | Bestehende Räume `private`, Eigentümer aus `created_by` |
| `room_shares` | `resource_kind` (`case`/`matter`), `resource_id`, `room_id`, `access` (`read`/`edit`), `created_by` | Freigabe einer Akte oder eines Vorgangs an einen Datenraum (Team); eindeutig je Eintrag und Raum. Endet, wenn der Freigebende den Raum verlässt oder Leser wird |
+2
View File
@@ -4,6 +4,7 @@ import { SessionContext } from "../session.js";
import { ErrorNote } from "../pages/widgets.jsx";
import { formatWhen } from "./admin-core.js";
import { DownloadPolicy } from "./DownloadPolicy.jsx";
import { DuplicatePreference } from "./DuplicatePreference.jsx";
import { deviceClass, readImageChoice, writeImageChoice } from "../mail-images.js";
import { isEmployee } from "./admin-core.js";
import { PasswordForm } from "./widgets.jsx";
@@ -82,6 +83,7 @@ export function Account() {
<ErrorNote error={error} />
{notice ? <p className="notice" role="status">{notice}</p> : null}
<MailImages />
<DuplicatePreference />
{isEmployee(session) ? null : (
<section className="card stack" aria-label="Herunterladen">
<h2>Herunterladen von Originalen</h2>
@@ -0,0 +1,77 @@
import { useEffect, useState } from "react";
import { api } from "../api.js";
import { ErrorNote } from "../pages/widgets.jsx";
const PATH = "/api/v1/auth/preferences/duplicate_check";
export const DUPLICATE_LEVELS = {
off: "Keine Prüfung",
here: "Nur hier (direkt in diesem Vorgang oder dieser Akte)",
inside: "Hier und was darin angezeigt wird (Teilvorgänge, zugeordnete Vorgänge)",
around: "Zusätzlich dort, wo es liegt (übergeordneter Vorgang, zugeordnete Akten)",
};
// Where a document that is inserted again counts as already present. Only a warning, never a block.
// An employee inherits the value of the main user and may change it unless the main user made it binding.
export function DuplicatePreference() {
const [state, setState] = useState(null);
const [error, setError] = useState("");
const [notice, setNotice] = useState("");
useEffect(() => {
let alive = true;
api(PATH).then((data) => alive && setState(data)).catch((err) => alive && setError(err.message));
return () => {
alive = false;
};
}, []);
async function save(body) {
setError("");
setNotice("");
try {
setState(await api(PATH, { method: "PUT", body }));
setNotice("Die Einstellung ist gespeichert.");
} catch (err) {
setError(err.message);
}
}
if (!state) return error ? <ErrorNote error={error} /> : null;
const ownChoice = state.can_lock ? state.own || state.default : state.own || "";
return (
<section className="card stack" aria-label="Doppelte Dokumente">
<h2>Doppelte Dokumente</h2>
<p className="muted">
Beim Einfügen eines Dokuments aus einer Quelle warnt Tsumugu, wenn es an der gewählten Stelle schon vorhanden ist. Du kannst es trotzdem
einfügen oder das vorhandene Element zusätzlich einbinden. Dasselbe Dokument in verschiedenen Vorgängen bleibt immer erlaubt.
</p>
<label>
Prüfen in
<select
value={ownChoice}
disabled={state.locked}
onChange={(event) => save({ value: event.target.value || null })}
>
{state.can_lock ? null : <option value="">Wie beim Hauptnutzer ({DUPLICATE_LEVELS[state.inherited]})</option>}
{state.allowed.map((level) => (
<option key={level} value={level}>{DUPLICATE_LEVELS[level]}</option>
))}
</select>
</label>
{state.locked ? <p className="muted">Der Hauptnutzer hat diese Einstellung für das Team vorgegeben: {DUPLICATE_LEVELS[state.value]}.</p> : null}
{state.can_lock ? (
<label className="check">
<input
type="checkbox"
checked={state.team_mode === "locked"}
onChange={(event) => save({ value: state.own, team_mode: event.target.checked ? "locked" : "free" })}
/>
Für Mitarbeiter verbindlich
</label>
) : null}
<ErrorNote error={error} />
{notice ? <p className="notice" role="status">{notice}</p> : null}
</section>
);
}
+22 -17
View File
@@ -15,6 +15,7 @@ import { Workspace, rememberScroll, restoreScroll, useWorkspaceLayout } from "./
import { useLibraryFocus } from "../library/LibraryLayout.jsx";
import { outlineSignature } from "../library/library-core.js";
import { NavIcon } from "./icons.jsx";
import { useExternalPlacement } from "../sources/duplicates.jsx";
function matterOrigin(group) {
const kind = group.kind_label === "Teilvorgang" ? "Teilvorgang" : "Vorgang";
@@ -905,6 +906,15 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "",
const [sentOn, setSentOn] = useState("");
const [receivedOn, setReceivedOn] = useState("");
const [error, setError] = useState("");
const { place, panel } = useExternalPlacement({
context,
placement,
onPlaced: () => {
setTitle("");
setExternalId("");
onChange();
},
});
useEffect(() => { setChapterId(initialChapter || ""); }, [initialChapter]);
@@ -912,25 +922,19 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "",
event.preventDefault();
setError("");
try {
await api("/api/v1/external-refs", {
method: "POST",
body: {
title,
source,
external_id: externalId,
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
dates: {
document_on: documentOn || null,
sent_on: sentOn || null,
received_on: receivedOn || null,
},
await place({
title,
source,
external_id: externalId,
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
dates: {
document_on: documentOn || null,
sent_on: sentOn || null,
received_on: receivedOn || null,
},
});
setTitle("");
setExternalId("");
onChange();
} catch (err) {
setError(err.message);
}
@@ -951,6 +955,7 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "",
{chapterSelect(chapters, chapterId, setChapterId)}
<button type="submit">Referenz erfassen</button>
<ErrorNote error={error} />
{panel}
</form>
);
}
+132 -49
View File
@@ -5,6 +5,7 @@ import { api, fetchBlob } from "../api.js";
import { MailBody } from "../mail-view.jsx";
import { NewConnector, useSourceModules } from "../sources/connector-config.jsx";
import { SourceFrame, registerSourceModule } from "../sources/registry.jsx";
import { PresentNote, bindable, describeMatch, lookupPresent, presentKey, useExternalPlacement } from "../sources/duplicates.jsx";
const SEARCH_LIMITS = {
paperless: "Die Suche läuft in Paperless selbst, über Titel und Inhalt, den Titel allein, den Inhalt allein oder den Volltextindex. Filter nach Korrespondent, Dokumenttyp, Speicherpfad, Tags, Dokumentdatum, Hinzugefügt-Datum und Archivnummer grenzen sie ein.",
@@ -303,6 +304,9 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
const [total, setTotal] = useState(null);
const [searched, setSearched] = useState(false);
const [picked, setPicked] = useState(() => new Map());
// Documents that are already present around the target, by presentKey. `review` holds the choices while the user decides.
const [present, setPresent] = useState(() => new Map());
const [review, setReview] = useState(null);
const [previewKey, setPreviewKey] = useState("");
const [previewUrl, setPreviewUrl] = useState("");
const [pending, setPending] = useState(false);
@@ -417,12 +421,33 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
setTotal(typeof data.total === "number" ? data.total : null);
setSearched(true);
if (!asked && data.connector_id) setConnectorId(data.connector_id);
refreshPresent(data.items || [], asked || data.connector_id || "");
} catch (err) {
if (token !== searchRef.current) return;
setError(err.message);
}
}
function presentEntry(item, bound = connectorId) {
return { source: "paperless", external_id: String(item.id), connector_id: item.connector_id || bound };
}
function matchesOf(item) {
const entry = presentEntry(item);
return present.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || [];
}
async function refreshPresent(list, bound) {
const entries = list.map((item) => presentEntry(item, bound));
const found = await lookupPresent(context, placement, entries);
setPresent((current) => {
const next = new Map(current);
entries.forEach((entry) => next.delete(presentKey(entry.source, entry.connector_id, entry.external_id)));
found.forEach((value, key) => next.set(key, value));
return next;
});
}
function resetFilters() {
invalidate();
closePreview();
@@ -469,52 +494,82 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
}
}
async function linkMany() {
async function linkMany(choices = null) {
const chosen = [...picked.values()];
if (!chosen.length) return;
setError("");
const keyOf = (item) => `${item.connector_id || connectorId}:${item.id}`;
if (!choices) {
const known = chosen.filter((item) => matchesOf(item).length);
if (known.length) {
setReview(new Map(known.map((item) => [keyOf(item), "skip"])));
return;
}
}
setReview(null);
setPending(true);
const inserted = [];
const skipped = [];
const conflicts = [];
try {
for (const item of chosen) {
await api("/api/v1/external-refs", {
method: "POST",
body: {
title: item.title,
source: "paperless",
external_id: String(item.id),
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
...(item.document_on || item.correspondent || item.open_url || connectorId
? {
dates: { document_on: item.document_on || null, sent_on: null, received_on: null },
detail: {
...(item.connector_id || connectorId ? { connector_id: item.connector_id || connectorId } : {}),
...(item.correspondent ? { correspondent: item.correspondent } : {}),
...(item.open_url ? { open_url: item.open_url } : {}),
},
}
: {}),
},
});
inserted.push(`${item.connector_id || connectorId}:${item.id}`);
const key = keyOf(item);
const choice = choices?.get(key);
if (choice === "skip") {
skipped.push(key);
continue;
}
const existing = choice === "bind" ? bindable(matchesOf(item)) : null;
try {
await api("/api/v1/external-refs", {
method: "POST",
body: {
title: item.title,
source: "paperless",
external_id: String(item.id),
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
...(choice === "insert" ? { duplicate: "insert" } : {}),
...(existing ? { bind_element_id: existing.element_id } : {}),
...(item.document_on || item.correspondent || item.open_url || connectorId
? {
dates: { document_on: item.document_on || null, sent_on: null, received_on: null },
detail: {
...(item.connector_id || connectorId ? { connector_id: item.connector_id || connectorId } : {}),
...(item.correspondent ? { correspondent: item.correspondent } : {}),
...(item.open_url ? { open_url: item.open_url } : {}),
},
}
: {}),
},
});
inserted.push(key);
} catch (err) {
// Inserted by someone else in the meantime: ask again instead of failing.
if (err.code !== "already_present") throw err;
conflicts.push(item);
}
}
} catch (err) {
setError(err.message);
} finally {
setPending(false);
}
if (conflicts.length) {
await refreshPresent(conflicts, connectorId);
setReview(new Map(conflicts.map((item) => [keyOf(item), "skip"])));
}
if (inserted.length) {
setItems((current) => current.filter((item) => !inserted.includes(`${connectorId}:${item.id}`)));
setPicked((current) => {
const next = new Map(current);
inserted.forEach((id) => next.delete(id));
return next;
});
onInserted?.();
}
if (inserted.length === chosen.length) onDone();
setPicked((current) => {
const next = new Map(current);
[...inserted, ...skipped].forEach((id) => next.delete(id));
return next;
});
if (inserted.length && inserted.length + skipped.length === chosen.length) onDone();
}
function setFilter(name, value) {
@@ -706,8 +761,37 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
<ErrorNote error={error} />
<p className="muted">{SEARCH_LIMITS.paperless}</p>
<p className="muted">Die Verbindung richtest du unter <Link to="/sources">Quellen</Link> ein. Ausgewählte Belege bleiben erhalten, wenn du weiterblätterst oder neu suchst.</p>
{review ? (
<div className="card stack duplicate-panel" role="alert">
<strong>Diese Belege sind schon vorhanden. Was soll geschehen?</strong>
<ul className="duplicate-list">
{[...picked.values()].filter((item) => review.has(`${item.connector_id || connectorId}:${item.id}`)).map((item) => {
const key = `${item.connector_id || connectorId}:${item.id}`;
const matches = matchesOf(item);
return (
<li key={key}>
<span>{item.title}: {matches.length ? describeMatch(matches[0]) : "schon vorhanden"}</span>
<select
value={review.get(key)}
aria-label={`Entscheidung für ${item.title}`}
onChange={(event) => setReview((current) => new Map(current).set(key, event.target.value))}
>
<option value="skip">Nicht einfügen</option>
<option value="insert">Trotzdem erneut einfügen</option>
{bindable(matches) ? <option value="bind">Vorhandenes zusätzlich einbinden</option> : null}
</select>
</li>
);
})}
</ul>
<div className="row">
<button type="button" className="quiet" disabled={pending} onClick={() => setReview(null)}>Zurück zur Auswahl</button>
<button type="button" disabled={pending} onClick={() => linkMany(review)}>Entscheidung ausführen</button>
</div>
</div>
) : null}
{items.length || picked.size ? (
<button type="button" disabled={pending || picked.size === 0} onClick={linkMany}>
<button type="button" disabled={pending || picked.size === 0 || Boolean(review)} onClick={() => linkMany()}>
{picked.size ? `${picked.size} Belege einfügen` : "Belege auswählen"}
</button>
) : null}
@@ -745,6 +829,7 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
</button>
<div className="paperless-card__body">
<strong>{item.title}</strong>
<PresentNote matches={matchesOf(item)} />
{facts.length ? <span className="paperless-card__facts">{facts.join(" · ")}</span> : null}
{item.tags?.length ? (
<ul className="paperless-card__tags" aria-label="Tags">
@@ -901,6 +986,7 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle =
const [preview, setPreview] = useState(null);
const [previewError, setPreviewError] = useState("");
const [adopting, setAdopting] = useState(false);
const { place, panel } = useExternalPlacement({ context, placement, onPlaced: () => onDone() });
const [loading, setLoading] = useState("");
const [error, setError] = useState("");
const requestRef = useRef(0);
@@ -1083,26 +1169,22 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle =
if (cc) detail.message_cc = cc;
if (attachments.length) detail.attachments = attachments;
try {
await api("/api/v1/external-refs", {
method: "POST",
body: {
title: item.title,
source: "imap",
external_id: item.id,
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
...(item.document_on ? {
dates: {
document_on: item.document_on,
sent_on: direction === "out" ? item.document_on : null,
received_on: direction === "in" ? item.document_on : null,
},
} : {}),
detail,
},
await place({
title: item.title,
source: "imap",
external_id: item.id,
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
...(item.document_on ? {
dates: {
document_on: item.document_on,
sent_on: direction === "out" ? item.document_on : null,
received_on: direction === "in" ? item.document_on : null,
},
} : {}),
detail,
});
onDone();
} catch (err) {
setError(err.message);
} finally {
@@ -1183,6 +1265,7 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle =
{selected && !preview && !previewError ? <p className="muted">Vorschau wird geladen …</p> : null}
{preview ? <MailBody html={preview.html} text={preview.text} truncated={preview.text_truncated} notices={preview.notices} /> : null}
<ErrorNote error={error} />
{panel}
<button type="button" onClick={() => link(selected)} disabled={!selected.uid || adopting}>{adopting ? "Wird übernommen …" : "Übernehmen"}</button>
</>
)}
+121
View File
@@ -0,0 +1,121 @@
import { useState } from "react";
import { api } from "../api.js";
// A document that is already present around the target. The server decides; this only asks and explains.
export function presentKey(source, connectorId, externalId) {
return `${source}:${connectorId || ""}:${externalId}`;
}
export function describeMatch(match) {
const title = match.container?.title;
const maybe = match.certain ? "" : "möglicherweise ";
if (match.where === "here") return `${maybe}schon hier${match.chapter ? ` (Kapitel „${match.chapter}“)` : ""}`;
if (match.where === "inside") return `${maybe}schon ${title ? `in „${title}“` : "in einem zugehörigen Teil"}`;
const kind = match.container?.kind === "case" ? "in der Akte" : "im Vorgang";
return `${maybe}schon ${kind}${title ? ` „${title}“` : ""}`;
}
export function bindable(matches) {
return (matches || []).find((item) => item.certain && item.can_bind && item.where !== "here") || null;
}
// entries: [{ source, external_id, connector_id }]. Answers a Map of presentKey -> matches, only for documents that are present.
// A failed lookup answers nothing: the warning is a help, the server still protects on insert.
export async function lookupPresent(context, placement, entries) {
const found = new Map();
if (!entries.length) return found;
try {
const data = await api("/api/v1/external-refs/existing", {
method: "POST",
body: {
context,
...(placement ? { placement } : {}),
items: entries.map((item) => ({ source: item.source, external_id: item.external_id, ...(item.connector_id ? { connector_id: item.connector_id } : {}) })),
},
});
for (const entry of data.items || []) {
if (entry.matches?.length) found.set(presentKey(entry.source, entry.connector_id, entry.external_id), entry.matches);
}
} catch {
return found;
}
return found;
}
export function PresentNote({ matches }) {
if (!matches?.length) return null;
return <p className="duplicate-note" role="status">Schon vorhanden: {matches.map(describeMatch).join(", ")}.</p>;
}
// Choices when a document is already present: do not insert, insert again, or bind the existing element as well.
export function DuplicatePanel({ matches, busy, onCancel, onInsert, onBind }) {
const existing = bindable(matches);
return (
<div className="card stack duplicate-panel" role="alert">
<strong>Dieses Dokument ist schon vorhanden.</strong>
<ul>
{matches.map((item) => <li key={item.element_id}>{item.title}: {describeMatch(item)}</li>)}
</ul>
<div className="row">
<button type="button" className="quiet" disabled={busy} onClick={onCancel}>Nicht einfügen</button>
<button type="button" className="quiet" disabled={busy} onClick={onInsert}>Trotzdem erneut einfügen</button>
{existing ? <button type="button" disabled={busy} onClick={() => onBind(existing.element_id)}>Vorhandenes zusätzlich einbinden</button> : null}
</div>
</div>
);
}
// One document at a time. `place(body)` posts it. If the server finds it already present, `panel` offers the choices
// and `onPlaced` runs once the document is in, whichever way.
export function useExternalPlacement({ context, placement, onPlaced }) {
const [conflict, setConflict] = useState(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
async function send(body) {
await api("/api/v1/external-refs", { method: "POST", body });
setConflict(null);
await onPlaced?.();
}
async function place(body) {
setError("");
try {
await send(body);
} catch (err) {
if (err.code !== "already_present") throw err;
const entry = { source: body.source, external_id: body.external_id, connector_id: body.detail?.connector_id };
const found = await lookupPresent(context, placement, [entry]);
const matches = found.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || [];
if (!matches.length) throw err;
setConflict({ body, matches });
}
}
async function resolve(extra) {
if (!conflict) return;
setBusy(true);
setError("");
try {
await send({ ...conflict.body, ...extra });
} catch (err) {
setError(err.message);
} finally {
setBusy(false);
}
}
const panel = conflict ? (
<>
<DuplicatePanel
matches={conflict.matches}
busy={busy}
onCancel={() => setConflict(null)}
onInsert={() => resolve({ duplicate: "insert" })}
onBind={(elementId) => resolve({ bind_element_id: elementId })}
/>
{error ? <p className="error" role="alert">{error}</p> : null}
</>
) : null;
return { place, panel };
}
+11 -13
View File
@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
import { api } from "../api.js";
import { TargetLine } from "../pages/content.jsx";
import { ErrorNote } from "../pages/widgets.jsx";
import { useExternalPlacement } from "./duplicates.jsx";
import { defineSourceModules, moduleFor, registerSourceModule, sourceModules, unregisterSourceModule } from "./registry-core.js";
export { defineSourceModules, moduleFor, registerSourceModule, sourceModules, unregisterSourceModule };
@@ -71,6 +72,7 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId
const [error, setError] = useState("");
const [pending, setPending] = useState(false);
const searchRef = useRef(0);
const { place, panel } = useExternalPlacement({ context, placement, onPlaced: () => { onInserted?.(); onDone?.(); } });
const hint = module?.searchHint || "Suche im heute unterstützten Umfang dieser Quelle.";
useEffect(() => {
@@ -111,20 +113,15 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId
setPending(true);
setError("");
try {
await api("/api/v1/external-refs", {
method: "POST",
body: {
title: item.title,
source: sourceKind,
external_id: String(item.id),
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
...(connectorId ? { detail: { connector_id: connectorId } } : {}),
},
await place({
title: item.title,
source: sourceKind,
external_id: String(item.id),
context,
...(placement ? { placement } : {}),
...(chapterId ? { chapter_id: chapterId } : {}),
...(connectorId ? { detail: { connector_id: connectorId } } : {}),
});
onInserted?.();
onDone?.();
} catch (err) {
setError(err.message);
} finally {
@@ -149,6 +146,7 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId
<button type="submit">Suchen</button>
</form>
<ErrorNote error={error} />
{panel}
<p className="muted">{hint}</p>
<div className="stack">
{items.map((item) => (
+7
View File
@@ -1837,3 +1837,10 @@ button.pick-tree__twist {
.mini-icon--link { color: var(--kind-room); }
.structure__links { margin-top: 12px; padding-top: 8px; border-top: 1px solid var(--border); }
.structure__links h3 { font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text2); margin: 0 8px 4px; }
.duplicate-note { margin: 0; color: var(--text); font-size: 13px; font-weight: 600; }
.duplicate-panel { border: 1px solid var(--border); background: var(--surface2); }
.duplicate-panel ul { margin: 0; padding-left: 18px; }
.duplicate-list { list-style: none; padding-left: 0 !important; display: grid; gap: 8px; }
.duplicate-list li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; }
.duplicate-list select { max-width: 100%; }