feat: projekte mit anderen benutzern teilen (backend)

Mitglieder mit Rolle "bearbeiten" oder "lesen"; nur der Besitzer
verwaltet das Projekt. Sichtbar sind eigene Clips und alle Clips in
eigenen oder geteilten Projekten, zentral in services/access.py. Beim
Entfernen oder Verlassen landen die eigenen Clips wieder bei "ohne
Projekt". Live-Ereignisse gehen an alle, die einen Clip sehen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dbildhauerandClaude Opus 5.5 committed 2026-10-25 14:36:56 +01:00
1 parent cfa2a1d0ab
commit d3a52c25a7
18 files changed
+1043 -133

No files matched your search

+21 -15
View File
@@ -4,10 +4,11 @@ from fastapi import APIRouter, BackgroundTasks, HTTPException, Query, status
from fastapi.responses import JSONResponse
from app.api.deps import CurrentUser, DbSession
from app.api.notify import notify
from app.models import Clip
from app.schemas.clips import ClipCreate, ClipList, ClipOut, ClipUpdate
from app.services import clips as clip_service
from app.ws.hub import hub
from app.services.access import clip_audience
router = APIRouter(prefix="/clips", tags=["clips"])
@@ -18,17 +19,12 @@ _NULLABLE_FIELDS = {"title", "language", "project_id"}
_PROJECT_FILTER = r"^(none|[1-9][0-9]{0,17})$"
def _notify(background: BackgroundTasks, user_id: int, event: str, data: dict) -> None:
"""Tell all devices of the user about a change, after the response is sent."""
background.add_task(hub.broadcast, user_id, {"type": event, "data": data})
def _clip_data(clip: Clip) -> dict:
return ClipOut.model_validate(clip).model_dump(mode="json")
def _get_own_clip(db: DbSession, user: CurrentUser, clip_id: int) -> Clip:
# Foreign and missing clips look the same, so ids of other users are not revealed
def _get_visible_clip(db: DbSession, user: CurrentUser, clip_id: int) -> Clip:
# Invisible and missing clips look the same, so ids of other users are not revealed
clip = clip_service.get_clip(db, user.id, clip_id)
if clip is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Clip nicht gefunden.")
@@ -54,13 +50,18 @@ def list_clips(
@router.post("", response_model=ClipOut, status_code=status.HTTP_201_CREATED)
def create_clip(body: ClipCreate, user: CurrentUser, db: DbSession, background: BackgroundTasks):
clip = clip_service.create_clip(db, user.id, **body.model_dump())
_notify(background, user.id, "clip.created", _clip_data(clip))
notify(
background,
clip_audience(db, clip.user_id, clip.project_id),
"clip.created",
_clip_data(clip),
)
return clip
@router.get("/{clip_id}", response_model=ClipOut)
def get_clip(clip_id: int, user: CurrentUser, db: DbSession):
return _get_own_clip(db, user, clip_id)
return _get_visible_clip(db, user, clip_id)
@router.patch("/{clip_id}", response_model=ClipOut)
@@ -71,12 +72,13 @@ def update_clip(
db: DbSession,
background: BackgroundTasks,
):
clip = _get_own_clip(db, user, clip_id)
clip = _get_visible_clip(db, user, clip_id)
changes = {
field: getattr(body, field)
for field in body.model_fields_set - {"version"}
if getattr(body, field) is not None or field in _NULLABLE_FIELDS
}
before = clip_audience(db, clip.user_id, clip.project_id)
try:
clip = clip_service.update_clip(db, user.id, clip, changes, body.version)
except clip_service.ClipConflict as exc:
@@ -87,7 +89,10 @@ def update_clip(
"current": _clip_data(exc.current),
},
)
_notify(background, user.id, "clip.updated", _clip_data(clip))
after = clip_audience(db, clip.user_id, clip.project_id)
notify(background, after, "clip.updated", _clip_data(clip))
# Moved out of a shared project: the others no longer see it
notify(background, before - after, "clip.deleted", {"id": clip.id})
return clip
@@ -95,6 +100,7 @@ def update_clip(
def delete_clip(
clip_id: int, user: CurrentUser, db: DbSession, background: BackgroundTasks
) -> None:
if not clip_service.delete_clip(db, user.id, clip_id):
raise HTTPException(status.HTTP_404_NOT_FOUND, "Clip nicht gefunden.")
_notify(background, user.id, "clip.deleted", {"id": clip_id})
clip = _get_visible_clip(db, user, clip_id)
audience = clip_audience(db, clip.user_id, clip.project_id)
clip_service.delete_clip(db, user.id, clip)
notify(background, audience, "clip.deleted", {"id": clip_id})