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:
1 parent
cfa2a1d0ab
commit
d3a52c25a7
18 files changed
+1043
-133
No files matched your search
@@ -16,7 +16,12 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import get_settings
|
||||
from app.models import Clip, ClipKind, StoredFile, utcnow
|
||||
from app.services.projects import require_project
|
||||
from app.services.access import (
|
||||
clip_audience,
|
||||
require_clip_write,
|
||||
require_project_write,
|
||||
visible_clip,
|
||||
)
|
||||
from app.services.users import UserError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -158,7 +163,7 @@ async def store_upload(
|
||||
if title is not None and len(title) > 200:
|
||||
raise UserError("Der Titel ist zu lang (höchstens 200 Zeichen).")
|
||||
if project_id is not None:
|
||||
require_project(db, user_id, project_id)
|
||||
require_project_write(db, user_id, project_id)
|
||||
_check_limits(db, user_id, size)
|
||||
|
||||
settings = get_settings()
|
||||
@@ -215,9 +220,11 @@ async def store_upload(
|
||||
|
||||
|
||||
def get_file(db: Session, user_id: int, clip_id: int) -> StoredFile | None:
|
||||
"""The file of a clip of this user, unless it has expired."""
|
||||
"""The file of a clip the user may see, unless it has expired."""
|
||||
stored = db.scalar(
|
||||
select(StoredFile).where(StoredFile.clip_id == clip_id, StoredFile.user_id == user_id)
|
||||
select(StoredFile)
|
||||
.join(Clip, Clip.id == StoredFile.clip_id)
|
||||
.where(StoredFile.clip_id == clip_id, visible_clip(user_id))
|
||||
)
|
||||
if stored is None or (stored.expires_at is not None and stored.expires_at <= utcnow()):
|
||||
return None
|
||||
@@ -228,10 +235,11 @@ def set_expiry(db: Session, user_id: int, clip: Clip, expires_days: int) -> Clip
|
||||
"""Change how long a file is kept; counts as a change of the clip, not as an edit."""
|
||||
if clip.file is None:
|
||||
raise UserError("Dieser Clip ist keine Datei.")
|
||||
require_clip_write(db, user_id, clip)
|
||||
clip.file.expires_at = _expires_at(expires_days)
|
||||
db.execute(
|
||||
update(Clip)
|
||||
.where(Clip.id == clip.id, Clip.user_id == user_id)
|
||||
.where(Clip.id == clip.id)
|
||||
.values(version=Clip.version + 1)
|
||||
.execution_options(synchronize_session=False)
|
||||
)
|
||||
@@ -240,20 +248,27 @@ def set_expiry(db: Session, user_id: int, clip: Clip, expires_days: int) -> Clip
|
||||
return clip
|
||||
|
||||
|
||||
def delete_expired(db: Session) -> list[tuple[int, int]]:
|
||||
"""Delete clips whose file has expired; returns (user_id, clip_id) for notifications."""
|
||||
def delete_expired(db: Session) -> list[tuple[int, set[int]]]:
|
||||
"""Delete clips whose file has expired; returns (clip_id, audience) for notifications."""
|
||||
rows = db.execute(
|
||||
select(StoredFile.user_id, StoredFile.clip_id, StoredFile.storage_name).where(
|
||||
StoredFile.expires_at.is_not(None), StoredFile.expires_at <= utcnow()
|
||||
select(
|
||||
StoredFile.user_id.label("uploader_id"),
|
||||
StoredFile.storage_name,
|
||||
Clip.id,
|
||||
Clip.user_id.label("owner_id"),
|
||||
Clip.project_id,
|
||||
)
|
||||
.join(Clip, Clip.id == StoredFile.clip_id)
|
||||
.where(StoredFile.expires_at.is_not(None), StoredFile.expires_at <= utcnow())
|
||||
).all()
|
||||
if not rows:
|
||||
return []
|
||||
db.execute(delete(Clip).where(Clip.id.in_([row.clip_id for row in rows])))
|
||||
audiences = [(row.id, clip_audience(db, row.owner_id, row.project_id)) for row in rows]
|
||||
db.execute(delete(Clip).where(Clip.id.in_([row.id for row in rows])))
|
||||
db.commit()
|
||||
for row in rows:
|
||||
remove_from_disk(row.user_id, row.storage_name)
|
||||
return [(row.user_id, row.clip_id) for row in rows]
|
||||
remove_from_disk(row.uploader_id, row.storage_name)
|
||||
return audiences
|
||||
|
||||
|
||||
def remove_orphans(db: Session) -> int:
|
||||
|
||||
Reference in new issue
Block a user