Liefere Zähler, Rauminhalt und Teilen im Stapel für die neue Ablage.

Akten und Vorgänge melden Unterakten, Vorgänge und die Räume, mit denen sie geteilt sind, jeweils nur aus Lesbarem. Neue Endpunkte zeigen die Räume als Kacheln, den Inhalt eines Raums und teilen mehrere Einträge auf einmal oder gar nicht. Beim Anlegen lässt sich ein Eintrag gleich in einen Raum legen.

Die lokale Suite läuft mit 148 Tests grün.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-10-22 11:38:02 +02:00
1 parent 30b6544514
commit d39c7d7999
9 files changed
+411 -24

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
"""Library endpoints: rooms as tiles, what lies in a room, and sharing several entries at once."""
from __future__ import annotations
from uuid import UUID
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from psycopg import Connection
from app.access.roles import room_role
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import BatchShareIn
from app.core.errors import forbidden
from app.library import case_items, matter_items, room_contents, room_effect
from app.ops import share_with_room
router = APIRouter()
_NO_STORE = {"Cache-Control": "no-store"}
@router.get("/api/v1/rooms/overview")
def rooms_overview(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
"""The team rooms of the person as tiles. Names of members are only shown to managers."""
actor = principal.account_id
rooms = conn.execute(
"""
SELECT r.id, r.name, r.owner_id, r.default_visibility, m.role,
(SELECT count(*) FROM room_memberships x WHERE x.room_id = r.id AND x.status = 'active') AS members
FROM room_memberships m JOIN data_rooms r ON r.id = m.room_id
WHERE m.subject_id = %s AND m.status = 'active' AND r.kind = 'organization' AND r.status = 'active'
ORDER BY r.name, r.id
""",
(actor,),
).fetchall()
items = []
for room in rooms:
cases, matters = room_contents(conn, actor, room["id"])
people = []
if room["role"] == "manager":
people = [
row["display_name"]
for row in conn.execute(
"""
SELECT a.display_name FROM room_memberships x JOIN auth_accounts a ON a.id = x.subject_id
WHERE x.room_id = %s AND x.status = 'active'
ORDER BY a.display_name, a.id LIMIT 6
""",
(room["id"],),
).fetchall()
]
items.append(
{
"id": str(room["id"]),
"name": room["name"],
"role": room["role"],
"is_owner": room["owner_id"] == actor,
"private": room["members"] <= 1,
"default_visibility": room["default_visibility"],
"member_count": room["members"],
"members": people,
"case_count": sum(1 for case, _ in cases if not case["parent_case_id"]),
"matter_count": sum(1 for matter, _ in matters if not matter["parent_matter_id"]),
}
)
return JSONResponse({"items": items}, headers=_NO_STORE)
@router.get("/api/v1/rooms/{room_id}/contents")
def contents(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
"""Cases and matters shared with the room that the person may read."""
if room_role(conn, principal.account_id, room_id) is None:
raise forbidden()
cases, matters = room_contents(conn, principal.account_id, room_id)
return JSONResponse(
{
"cases": case_items(conn, principal.account_id, cases),
"matters": matter_items(conn, principal.account_id, matters),
},
headers=_NO_STORE,
)
@router.post("/api/v1/rooms/{room_id}/shares")
def share_many(
room_id: UUID,
body: BatchShareIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
"""Share several own entries with the room. All or none: one failure rolls the request back."""
for item in body.items:
share_with_room(conn, principal.account_id, item.kind, item.id, room_id, body.access)
return {
"shared": len(body.items),
"access": body.access,
"effect": room_effect(conn, room_id, principal.account_id, body.access),
}
+4 -1
View File
@@ -12,6 +12,7 @@ from psycopg import Connection
from app.access.policy import ROOM_RIGHTS, Rights
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import ExclusionIn, RoomShareIn
from app.library import room_effect
from app.ops import _shareable, set_person_exclusion, share_with_room, unshare_from_room
router = APIRouter()
@@ -108,7 +109,9 @@ def _register(kind: str, plural: str) -> None:
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
return share_with_room(conn, principal.account_id, kind, resource_id, room_id, body.access)
shared = share_with_room(conn, principal.account_id, kind, resource_id, room_id, body.access)
shared["effect"] = room_effect(conn, room_id, principal.account_id, body.access)
return shared
@router.delete(base + "/rooms/{room_id}", status_code=204, name=f"delete_{kind}_room_share")
def delete_share(
+8 -17
View File
@@ -165,9 +165,10 @@ from app.present import (
matter_detail,
matter_summary,
note_update_payload,
shared_room_names,
task_payload,
)
from app.library import case_items, matter_items
from app.ops import share_with_room as _share_with_room
from app.seed import demo_public
router = APIRouter()
@@ -382,20 +383,7 @@ def find_principal(email: str, principal: Principal = Depends(get_principal), co
@router.get("/api/v1/cases")
def cases(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
items = []
for case, rights in list_cases(conn, principal.account_id):
summary = case_summary(case, rights)
summary["home_scope_name"] = room_name(conn, case["home_scope_id"])
summary["kind_label"] = "Unterakte" if case["parent_case_id"] else "Akte"
summary["parent_title"] = None
summary["is_owner"] = case["owner_subject_id"] == principal.account_id
summary["shared_with"] = shared_room_names(conn, "case", case["id"]) if summary["is_owner"] else []
if case["parent_case_id"]:
parent = get_case(conn, case["parent_case_id"])
if parent is not None and case_rights(conn, principal.account_id, parent).read:
summary["parent_title"] = parent["title"]
items.append(summary)
return {"items": items, "next_cursor": None}
return {"items": case_items(conn, principal.account_id, list_cases(conn, principal.account_id)), "next_cursor": None}
@router.post("/api/v1/cases")
@@ -408,6 +396,8 @@ def post_case(body: CaseIn, principal: Principal = Depends(get_principal), conn:
body.parent_case_id,
body.rights_mode,
)
if body.share_with_room_id is not None:
_share_with_room(conn, principal.account_id, "case", case["id"], body.share_with_room_id, "edit")
rights = case_rights(conn, principal.account_id, case)
return _created(f"/api/v1/cases/{case['id']}", case_summary(case, rights))
@@ -492,8 +482,7 @@ def post_case_grant(
@router.get("/api/v1/matters")
def matters(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
items = [matter_summary(conn, principal.account_id, matter) for matter, _contexts in list_matters(conn, principal.account_id)]
return {"items": items, "next_cursor": None}
return {"items": matter_items(conn, principal.account_id, list_matters(conn, principal.account_id)), "next_cursor": None}
@router.post("/api/v1/matters")
@@ -506,6 +495,8 @@ def post_matter(body: MatterIn, principal: Principal = Depends(get_principal), c
body.parent_matter_id,
_ctx_model(body.context),
)
if body.share_with_room_id is not None:
_share_with_room(conn, principal.account_id, "matter", matter["id"], body.share_with_room_id, "edit")
return _created(f"/api/v1/matters/{matter['id']}", matter_summary(conn, principal.account_id, matter))
+12
View File
@@ -77,6 +77,16 @@ class VisibilityIn(APIModel):
visibility: Literal["private", "room"]
class ShareItemIn(APIModel):
kind: Literal["case", "matter"]
id: UUID
class BatchShareIn(APIModel):
items: list[ShareItemIn] = Field(min_length=1, max_length=200)
access: Literal["read", "edit"] = "edit"
class RoomShareIn(APIModel):
access: Literal["read", "edit"] = "edit"
@@ -122,6 +132,7 @@ class CaseIn(APIModel):
parent_case_id: UUID | None = None
rights_mode: Literal["inherit_parent", "explicit"] = "inherit_parent"
home_scope_id: UUID | None = None
share_with_room_id: UUID | None = None
class CasePatch(APIModel):
@@ -197,6 +208,7 @@ class MatterIn(APIModel):
parent_matter_id: UUID | None = None
context: ContextIn | None = None
home_scope_id: UUID | None = None
share_with_room_id: UUID | None = None
class MatterPatch(APIModel):
+126
View File
@@ -0,0 +1,126 @@
"""Overviews for the library: cases and matters as tiles, and what lies in a room.
A room is a virtual folder. Cases and matters stay where they are; a share puts them into a room.
Counts only include what the person may read. Hidden entries are neither named nor counted.
"""
from __future__ import annotations
from collections import Counter
from uuid import UUID
from app.access.policy import (
ROOM_RIGHTS,
Ctx,
Rights,
case_rights,
get_case,
list_cases,
list_matters,
matter_rights,
room_name,
)
from app.present import case_summary, matter_summary
def shared_rooms(conn, kind: str, resource_id: UUID) -> list[dict]:
rows = conn.execute(
"""
SELECT r.id, r.name, s.access FROM room_shares s JOIN data_rooms r ON r.id = s.room_id
WHERE s.resource_kind = %s AND s.resource_id = %s
ORDER BY r.name, r.id
""",
(kind, resource_id),
).fetchall()
return [{"id": str(row["id"]), "name": row["name"], "access": row["access"]} for row in rows]
def readable_matter_count(conn, actor: UUID, case: dict) -> int:
rows = conn.execute(
"""
SELECT m.* FROM matter_case_links l JOIN matters m ON m.id = l.matter_id
WHERE l.case_id = %s AND m.deleted_at IS NULL AND m.parent_matter_id IS NULL
""",
(case["id"],),
).fetchall()
context = Ctx("case", case["id"])
return sum(1 for matter in rows if matter_rights(conn, actor, matter, context).read)
def case_items(conn, actor: UUID, visible: list[tuple[dict, Rights]]) -> list[dict]:
children = Counter(case["parent_case_id"] for case, _ in visible if case["parent_case_id"])
items = []
for case, rights in visible:
summary = case_summary(case, rights)
owner = case["owner_subject_id"] == actor
summary["home_scope_name"] = room_name(conn, case["home_scope_id"])
summary["kind_label"] = "Unterakte" if case["parent_case_id"] else "Akte"
summary["parent_title"] = None
summary["is_owner"] = owner
summary["shared_with"] = shared_rooms(conn, "case", case["id"]) if owner else []
summary["child_cases"] = children.get(case["id"], 0)
summary["matter_count"] = readable_matter_count(conn, actor, case)
if case["parent_case_id"]:
parent = get_case(conn, case["parent_case_id"])
if parent is not None and case_rights(conn, actor, parent).read:
summary["parent_title"] = parent["title"]
items.append(summary)
return items
def matter_items(conn, actor: UUID, visible: list[tuple[dict, list[dict]]]) -> list[dict]:
children = Counter(matter["parent_matter_id"] for matter, _ in visible if matter["parent_matter_id"])
items = []
for matter, _contexts in visible:
summary = matter_summary(conn, actor, matter)
summary["child_matters"] = children.get(matter["id"], 0)
items.append(summary)
return items
def room_contents(conn, actor: UUID, room_id: UUID) -> tuple[list[tuple[dict, Rights]], list[tuple[dict, list[dict]]]]:
"""Cases and matters shared with the room that the person may read, with their readable sub-entries."""
rows = conn.execute(
"SELECT resource_kind, resource_id FROM room_shares WHERE room_id = %s",
(room_id,),
).fetchall()
case_ids = {row["resource_id"] for row in rows if row["resource_kind"] == "case"}
matter_ids = {row["resource_id"] for row in rows if row["resource_kind"] == "matter"}
cases = [
(case, rights)
for case, rights in list_cases(conn, actor)
if case["id"] in case_ids
or (case["rights_mode"] == "inherit_parent" and case["parent_case_id"] in case_ids)
]
matters = [
(matter, contexts)
for matter, contexts in list_matters(conn, actor)
if matter["id"] in matter_ids or matter["parent_matter_id"] in matter_ids
]
return cases, matters
def room_effect(conn, room_id: UUID, actor: UUID, access: str) -> list[dict]:
"""Who gets which rights through a share with this room."""
rows = conn.execute(
"""
SELECT m.subject_id, m.role, a.display_name
FROM room_memberships m JOIN auth_accounts a ON a.id = m.subject_id
WHERE m.room_id = %s AND m.status = 'active' AND a.status = 'active' AND m.subject_id <> %s
ORDER BY a.display_name, a.id
""",
(room_id, actor),
).fetchall()
effect = []
for row in rows:
rights = ROOM_RIGHTS.get(row["role"], Rights())
if access == "read":
rights = Rights(read=rights.read)
effect.append(
{
"display_name": row["display_name"],
"role": row["role"],
"rights": {"read": rights.read, "edit": rights.edit},
}
)
return effect
+2
View File
@@ -12,6 +12,7 @@ from fastapi.responses import JSONResponse
from app.api.accounts import router as accounts_router
from app.api.correspondence import router as correspondence_router
from app.api.export import router as export_router
from app.api.library import router as library_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
@@ -121,6 +122,7 @@ def create_app() -> FastAPI:
app.include_router(router)
app.include_router(accounts_router)
app.include_router(library_router)
app.include_router(rooms_router)
app.include_router(roomshare_router)
app.include_router(team_router)
+4 -4
View File
@@ -747,23 +747,23 @@ def matter_contexts(conn, actor: UUID, matter: dict) -> list[dict]:
return contexts
def shared_room_names(conn, kind: str, resource_id: UUID) -> list[str]:
def _shared_rooms(conn, kind: str, resource_id: UUID) -> list[dict]:
rows = conn.execute(
"""
SELECT r.name FROM room_shares s JOIN data_rooms r ON r.id = s.room_id
SELECT r.id, r.name, s.access FROM room_shares s JOIN data_rooms r ON r.id = s.room_id
WHERE s.resource_kind = %s AND s.resource_id = %s
ORDER BY r.name, r.id
""",
(kind, resource_id),
).fetchall()
return [row["name"] for row in rows]
return [{"id": str(row["id"]), "name": row["name"], "access": row["access"]} for row in rows]
def matter_summary(conn, actor: UUID, matter: dict) -> dict:
owner = matter["owner_subject_id"] == actor
return {
"is_owner": owner,
"shared_with": shared_room_names(conn, "matter", matter["id"]) if owner else [],
"shared_with": _shared_rooms(conn, "matter", matter["id"]) if owner else [],
"id": str(matter["id"]),
"title": matter["title"],
"status": matter["record_status"],