Files
lan-clipboard/backend/app/api/clips.py
T
dbildhauerandClaude Opus 5.5 d3a52c25a7 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>
2026-10-25 14:36:56 +01:00

107 lines
3.9 KiB
Python

from typing import Annotated
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.services.access import clip_audience
router = APIRouter(prefix="/clips", tags=["clips"])
# Fields that may be null on purpose (clearing them); all others are ignored when null
_NULLABLE_FIELDS = {"title", "language", "project_id"}
# "none" or a project id
_PROJECT_FILTER = r"^(none|[1-9][0-9]{0,17})$"
def _clip_data(clip: Clip) -> dict:
return ClipOut.model_validate(clip).model_dump(mode="json")
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.")
return clip
@router.get("", response_model=ClipList)
def list_clips(
user: CurrentUser,
db: DbSession,
limit: Annotated[int, Query(ge=1, le=100)] = 50,
offset: Annotated[int, Query(ge=0)] = 0,
project: Annotated[str | None, Query(pattern=_PROJECT_FILTER)] = None,
q: Annotated[str | None, Query(max_length=clip_service.SEARCH_MAX_LENGTH)] = None,
) -> ClipList:
project_filter = project if project in (None, clip_service.NO_PROJECT) else int(project)
items, total = clip_service.list_clips(
db, user.id, limit, offset, project=project_filter, query=q
)
return ClipList(items=[ClipOut.model_validate(c) for c in items], total=total)
@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,
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_visible_clip(db, user, clip_id)
@router.patch("/{clip_id}", response_model=ClipOut)
def update_clip(
clip_id: int,
body: ClipUpdate,
user: CurrentUser,
db: DbSession,
background: BackgroundTasks,
):
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:
return JSONResponse(
status_code=status.HTTP_409_CONFLICT,
content={
"detail": "Der Clip wurde inzwischen auf einem anderen Gerät geändert.",
"current": _clip_data(exc.current),
},
)
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
@router.delete("/{clip_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_clip(
clip_id: int, user: CurrentUser, db: DbSession, background: BackgroundTasks
) -> None:
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})