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

+26 -20
View File
@@ -6,8 +6,8 @@ from sqlalchemy.orm import Session
from app.config import get_settings
from app.languages import LANGUAGES
from app.models import Clip, ClipKind, StoredFile, utcnow
from app.services.access import require_clip_write, require_project_write, visible_clip
from app.services.files import remove_from_disk
from app.services.projects import require_project
from app.services.users import UserError
# Changing only these fields does not count as an edit (no new updated_at)
@@ -50,9 +50,10 @@ def _normalize(kind: ClipKind, title: str | None, content: str, language: str |
def _filters(user_id: int, project: int | str | None, query: str | None) -> list:
conditions = [Clip.user_id == user_id]
conditions = [visible_clip(user_id)]
if project == NO_PROJECT:
conditions.append(Clip.project_id.is_(None))
# Clips without project are always private
conditions += [Clip.user_id == user_id, Clip.project_id.is_(None)]
elif project is not None:
conditions.append(Clip.project_id == project)
needle = (query or "").strip().casefold()
@@ -76,7 +77,7 @@ def list_clips(
project: int | str | None = None,
query: str | None = None,
) -> tuple[list[Clip], int]:
"""Clips of the user, optionally only those of a project (or NO_PROJECT) matching a query."""
"""Visible clips, optionally only those of a project (or NO_PROJECT) matching a query."""
conditions = _filters(user_id, project, query)
items = db.scalars(
select(Clip)
@@ -90,7 +91,8 @@ def list_clips(
def get_clip(db: Session, user_id: int, clip_id: int) -> Clip | None:
return db.scalar(select(Clip).where(Clip.id == clip_id, Clip.user_id == user_id))
"""A clip the user may see (own or in a shared project), else None."""
return db.scalar(select(Clip).where(Clip.id == clip_id, visible_clip(user_id)))
def create_clip(
@@ -108,7 +110,7 @@ def create_clip(
raise UserError("Dateien werden über den Upload angelegt.")
title, language = _normalize(kind, title, content, language)
if project_id is not None:
require_project(db, user_id, project_id)
require_project_write(db, user_id, project_id)
clip = Clip(
user_id=user_id,
project_id=project_id,
@@ -130,7 +132,11 @@ def update_clip(
changes: dict[str, Any],
expected_version: int | None = None,
) -> Clip:
"""Apply a partial update. With expected_version, a concurrent change raises ClipConflict."""
"""Apply a partial update. With expected_version, a concurrent change raises ClipConflict.
The clip must be visible to the user (get_clip); write access is checked here.
"""
require_clip_write(db, user_id, clip)
# Early check so a stale client gets the conflict, not a follow-up validation error
if expected_version is not None and clip.version != expected_version:
raise ClipConflict(clip)
@@ -144,8 +150,8 @@ def update_clip(
language = changes.get("language", clip.language)
title, language = _normalize(kind, title, content, language)
project_id = changes.get("project_id", clip.project_id)
if "project_id" in changes and project_id is not None:
require_project(db, user_id, project_id)
if "project_id" in changes and project_id not in (None, clip.project_id):
require_project_write(db, user_id, project_id)
values: dict[str, Any] = {
"project_id": project_id,
@@ -160,7 +166,7 @@ def update_clip(
values["updated_at"] = utcnow()
# Conditional UPDATE keeps the version check atomic across concurrent requests
stmt = update(Clip).where(Clip.id == clip.id, Clip.user_id == user_id)
stmt = update(Clip).where(Clip.id == clip.id)
if expected_version is not None:
stmt = stmt.where(Clip.version == expected_version)
result = db.execute(stmt.values(**values).execution_options(synchronize_session=False))
@@ -173,14 +179,14 @@ def update_clip(
return clip
def delete_clip(db: Session, user_id: int, clip_id: int) -> bool:
storage_name = db.scalar(
select(StoredFile.storage_name).where(
StoredFile.clip_id == clip_id, StoredFile.user_id == user_id
)
)
result = db.execute(delete(Clip).where(Clip.id == clip_id, Clip.user_id == user_id))
def delete_clip(db: Session, user_id: int, clip: Clip) -> None:
"""Delete a visible clip; write access is checked here. Its file goes with it."""
require_clip_write(db, user_id, clip)
stored = db.execute(
select(StoredFile.user_id, StoredFile.storage_name).where(StoredFile.clip_id == clip.id)
).first()
db.execute(delete(Clip).where(Clip.id == clip.id))
db.commit()
if storage_name is not None:
remove_from_disk(user_id, storage_name)
return result.rowcount > 0
if stored is not None:
# Stored under the uploader, who may be another member of a shared project
remove_from_disk(stored.user_id, stored.storage_name)