From b80dded55d5332a9d894543ba2f4024f7617f4af Mon Sep 17 00:00:00 2001 From: Florian Heinz Date: Sun, 25 Oct 2026 09:45:31 +0100 Subject: [PATCH] Release 0.6.0 --- CloudronManifest.json | 2 +- docs/ACCEPTANCE.md | 34 + docs/ARCHITECTURE.md | 87 +- docs/DECISIONS.md | 28 +- docs/RELEASE_NOTES.md | 53 + docs/STATUS.md | 41 +- .../0015_completion_archive_and_shares.sql | 115 ++ migrations/0016_shares_for_active_shows.sql | 43 + package-lock.json | 4 +- package.json | 2 +- src/app.css | 323 ++++ src/hooks.server.ts | 31 +- src/lib/components/ShareManagement.svelte | 249 +++ src/lib/server/auth.ts | 4 +- src/lib/server/completion.spec.ts | 204 +++ src/lib/server/completion.ts | 210 +++ src/lib/server/content.ts | 2 +- src/lib/server/database.spec.ts | 97 +- src/lib/server/live.ts | 34 +- src/lib/server/shares.spec.ts | 266 +++ src/lib/server/shares.ts | 430 +++++ src/lib/server/shows.spec.ts | 24 +- src/lib/server/shows.ts | 173 +- src/lib/server/time.spec.ts | 12 + src/lib/server/time.ts | 44 + src/routes/+layout.server.ts | 6 +- src/routes/+layout.svelte | 11 + src/routes/archiv/+page.server.ts | 20 +- src/routes/archiv/+page.svelte | 23 +- src/routes/freigabe/[token]/+page.server.ts | 31 + src/routes/freigabe/[token]/+page.svelte | 141 ++ .../freigabe/[token]/media/[id]/+server.ts | 18 + src/routes/live/+page.server.ts | 23 +- src/routes/live/+page.svelte | 112 +- src/routes/sendungen/+page.server.ts | 34 +- src/routes/sendungen/[id]/+page.server.ts | 124 +- src/routes/sendungen/[id]/+page.svelte | 1517 +++++++++-------- src/routes/verwaltung/+page.server.ts | 53 + src/routes/verwaltung/+page.svelte | 16 +- tests/ui.e2e.ts | 295 +++- 40 files changed, 4080 insertions(+), 856 deletions(-) create mode 100644 migrations/0015_completion_archive_and_shares.sql create mode 100644 migrations/0016_shares_for_active_shows.sql create mode 100644 src/lib/components/ShareManagement.svelte create mode 100644 src/lib/server/completion.spec.ts create mode 100644 src/lib/server/completion.ts create mode 100644 src/lib/server/shares.spec.ts create mode 100644 src/lib/server/shares.ts create mode 100644 src/lib/server/time.spec.ts create mode 100644 src/lib/server/time.ts create mode 100644 src/routes/freigabe/[token]/+page.server.ts create mode 100644 src/routes/freigabe/[token]/+page.svelte create mode 100644 src/routes/freigabe/[token]/media/[id]/+server.ts diff --git a/CloudronManifest.json b/CloudronManifest.json index d4cb3a9..75a11cc 100644 --- a/CloudronManifest.json +++ b/CloudronManifest.json @@ -3,7 +3,7 @@ "title": "Sendewerk", "description": "Redaktionelle Planung und Durchführung von heinzlive-Sendungen.", "tagline": "Vom Fundstück bis zur Livesendung", - "version": "0.5.12", + "version": "0.6.0", "healthCheckPath": "/healthz", "httpPort": 3000, "addons": { diff --git a/docs/ACCEPTANCE.md b/docs/ACCEPTANCE.md index 5aa7a47..e09882f 100644 --- a/docs/ACCEPTANCE.md +++ b/docs/ACCEPTANCE.md @@ -506,3 +506,37 @@ vollständige erneute Matrix. - Zusätzliche visuelle Chromium-Prüfung bei 656 × 400 und 390 × 700 Pixeln: Sendungskarte unter 130 Pixel hoch, Menü-/Ausgabe-/Timericons sichtbar und Pausebutton wechselt bedienbar zu Fortsetzen. + +## Etappe 6 + +| Kriterium | Nachweis | Stand | +| -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | --------------------- | +| Spätere Originaländerung verändert Historie nicht | Verhaltenstest ändert Piece und Abschnittskategorie nach Abschluss; historische Snapshots bleiben gleich | erfüllt am 25.10.2026 | +| Abschluss nicht doppelt; übersprungen ist nicht gesendet | idempotente Befehlswiederholung, fremde Befehls-ID und materialisierte Usage-Historie im Verhaltenstest | erfüllt am 25.10.2026 | +| Bewusste Auswahl archiviert nur gewählte Originale | Abschlusskandidaten und explizite Archivauswahl im Service; Dialog trennt gesendet, übersprungen und offen | erfüllt am 25.10.2026 | +| Archivierte Sendung stabil und duplizierbar | Service- und Browsertest prüfen Nur-Lese-Ansicht sowie neuen Entwurf ohne Live-, Historien- oder Freigabedaten | erfüllt am 25.10.2026 | +| Gast sieht genau eine Sendung und nur erlaubte Felder | anonymer Chromium-Kontext und Scope-Verhaltenstests für Ablauf, Moderation und Recherche | erfüllt am 25.10.2026 | +| Gast-Schreibzugriff und fremde IDs abgelehnt | anonymer direkter POST erhält 403; fremde Medien-ID erhält 404 | erfüllt am 25.10.2026 | +| Passwort, Ablauf und Widerruf schützen direkte Aufrufe | Service- und Browsertests prüfen Seite, Gast-Sitzung und direkten Medienrequest vor und nach Widerruf | erfüllt am 25.10.2026 | +| Barkeeper verwaltet Freigaben | Anlage, Bearbeitung und Löschung direkt in der Sendung sowie gruppiert in der Verwaltung im Browser geprüft | erfüllt am 25.10.2026 | +| Offene Sendungen sind sicher freigebbar | Service und anonymer Browser prüfen aktuellen Plan vor Abschluss und stabilen Stand danach | erfüllt am 25.10.2026 | +| Bestehende Abschlüsse werden einmalig übernommen | Upgrade-Test wendet `0015` auf einen Stand bis `0014` zweimal an und findet genau eine Historie | erfüllt am 25.10.2026 | +| Bestehende Freigaben bleiben bei Erweiterung erhalten | Migration `0016` ersetzt nur die Fremdschlüsselgrenze und kopiert Freigaben sowie Gast-Sitzungen | erfüllt am 25.10.2026 | + +## Ausgeführte Prüfungen Etappe 6 + +- `npm run check`: 0 Fehler und 0 Warnungen +- `npm test`: 52 Tests in 11 Dateien erfolgreich +- gezielter Chromium-Ablauf für Abschluss, Archiv, historische Ansicht, + offene und abgeschlossene Freigabe, Bearbeitung und Löschung in beiden + Barkeeper-Oberflächen, Passwort, privaten Scope, Reserve-Medienschutz, fremde + ID und Gast-POST: 1 von 1 erfolgreich +- `npm run test:e2e`: nach Korrektur eines bestehenden, auf den 24. Oktober + festgelegten „Heute“-Fixtures 18 von 18 Chromium-Browsertests erfolgreich +- Browserprüfung von Sendungsfreigabe und Gastansicht bei 390 × 844 Pixeln sowie + der Verwaltung bei 1280 × 900 Pixeln ohne horizontalen Überlauf; Screenshots + visuell kontrolliert +- `npm run build`: erfolgreich mit `@sveltejs/adapter-node` +- `npm run lint`: Prettier und ESLint erfolgreich +- Versionsgleichheit `0.6.0` in Paket, Lockdatei und Cloudron-Manifest geprüft +- `bash -n start.sh` und `git diff --check`: erfolgreich diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f9a63bd..8c4518a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -33,22 +33,22 @@ spätere API dieselben Regeln verwenden. ## Gesamtdatenmodell -Die ersten fünf Etappen materialisieren Identität, Inhaltsverwaltung, -Sendungsplanung und Live-Durchführung. Folgende Grenzen gelten für umgesetzte -und spätere Bereiche. +Die ersten sechs Etappen materialisieren Identität, Inhaltsverwaltung, +Sendungsplanung, Live-Durchführung, Historie und Lesefreigaben. Folgende Grenzen +gelten für umgesetzte und spätere Bereiche. -| Bereich | Geplante Entitäten und Beziehungen | -| ----------- | ----------------------------------------------------------------------------------------------------------------------------------- | -| Identität | `users`, Rollen, Sessions, Einladungen, Reset-Tokens und Sperren sind umgesetzt; technische Zugänge folgen später | -| Taxonomie | getrennte `section_categories`, `formats`, `topics` samt optionalem Themenicon; Pieces und Vorschläge mehrfach verknüpft | -| Inhalt | `pieces` mit Status, Soft Delete, Favorit, Revision, Texten, Quellen, Werkzeugen und privaten Medien; Vorlagen speichern Snapshots | -| Sendungsbau | `shows`, geordnete `show_sections`, `piece_usages` samt Quellen-/Werkzeug-/Themen-/Mediensnapshots, Reservebank und Vorlagen | +| Bereich | Geplante Entitäten und Beziehungen | +| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------- | +| Identität | `users`, Rollen, Sessions, Einladungen, Reset-Tokens und Sperren sind umgesetzt; technische Zugänge folgen später | +| Taxonomie | getrennte `section_categories`, `formats`, `topics` samt optionalem Themenicon; Pieces und Vorschläge mehrfach verknüpft | +| Inhalt | `pieces` mit Status, Soft Delete, Favorit, Revision, Texten, Quellen, Werkzeugen und privaten Medien; Vorlagen speichern Snapshots | +| Sendungsbau | `shows`, geordnete `show_sections`, `piece_usages` samt Quellen-/Werkzeug-/Themen-/Mediensnapshots, Reservebank und Vorlagen | | Live | expliziter Live-Stand, aktuelles Element, tatsächliche Piece-Zeiten, persistente Abschnitts-/Gesamttimer und getrennte Präsentationsausgabe | -| Historie | unveränderliche Abschluss-Snapshots und Verwendungshistorie; Übersprungenes zählt nicht als gesendet | -| Freigaben | widerrufbare, gehashte Lesetokens mit Sendung, Umfang, Passwortoption und Ablaufzeit | -| RSS | Feedquellen, Abrufläufe und externe Einträge mit stabiler Herkunfts-ID sowie Status neu/übernommen/verworfen | -| API/Audit | technische Berechtigungen, Idempotenzschlüssel und Audit-Ereignisse mit Akteur, Aktion und Ziel | -| Betrieb | Anwendungseinstellungen und spätere Job-Zustände, keine Geheimnisse im Klartext ohne begründete Schutzstrategie | +| Historie | unveränderliche Abschluss-Snapshots und Verwendungshistorie; Übersprungenes zählt nicht als gesendet | +| Freigaben | widerrufbare, gehashte Lesetokens mit Sendung, Umfang, Passwortoption und Ablaufzeit | +| RSS | Feedquellen, Abrufläufe und externe Einträge mit stabiler Herkunfts-ID sowie Status neu/übernommen/verworfen | +| API/Audit | technische Berechtigungen, Idempotenzschlüssel und Audit-Ereignisse mit Akteur, Aktion und Ziel | +| Betrieb | Anwendungseinstellungen und spätere Job-Zustände, keine Geheimnisse im Klartext ohne begründete Schutzstrategie | ### Historien- und Kopierregel @@ -238,13 +238,12 @@ Zurückgehen, Wiederaufnahme und bereits abgeschlossene Laufzeit getrennt von de geplanten Dauer nachvollziehbar. `live_show_timer_runs` hält die unabhängige Gesamtzeit. Pause und Fortsetzen -behandeln Gesamt-, Abschnitts- und Piece-Intervalle gemeinsam. Beenden setzt den -Sendungsstatus auf `completed`, schließt laufende Intervalle, markiert offene -Pieces aber nicht als erledigt. Eine ausdrückliche Wiederaufnahme bewahrt Zeiten -und Fortschritt; nur „Neuer Durchlauf“ verwirft Laufintervalle und setzt die -Piece-Status zurück. Die Restzeitschätzung verwendet je Abschnitt entweder die -positive Abschnittsdauer oder die verbleibenden Piece-Dauern und zählt beide nie -zusammen. +behandeln Gesamt-, Abschnitts- und Piece-Intervalle gemeinsam. Beenden schließt +laufende Intervalle und setzt `ended_at_utc`, finalisiert die Sendung aber noch +nicht. Eine ausdrückliche Wiederaufnahme bewahrt Zeiten und Fortschritt; nur +„Neuer Durchlauf“ verwirft Laufintervalle und setzt die Piece-Status zurück. Die +Restzeitschätzung verwendet je Abschnitt entweder die positive Abschnittsdauer +oder die verbleibenden Piece-Dauern und zählt beide nie zusammen. Die interne Route `/live` benötigt weiterhin eine aktive Sitzung und darf Recherche, Moderation und eigene Arbeitsdaten lesen. Die separate Ausgabe nutzt @@ -295,6 +294,52 @@ sichtbar; die Oberfläche kennzeichnet den Abbruch und versucht die Verbindung mit verlangsamtem Intervall erneut. Private Live-Daten werden nicht Teil dieses Zustandsobjekts. +## Abschluss, Historie und Lesefreigaben + +`src/lib/server/completion.ts` ist die Transaktionsgrenze des endgültigen +Sendungsabschlusses. Er verlangt einen beendeten Live-Lauf sowie passende Show- +und Live-Revisionen. Eine Befehls-ID in `show_finalizations` macht Wiederholungen +idempotent. In derselben Transaktion werden offene Intervalle geschlossen, +Abschnittskategorien eingefroren, ausschließlich erledigte Verwendungen außerhalb +der Reservebank in `piece_usage_history` materialisiert, bewusst gewählte +Fundus-Originale archiviert und die Sendung auf `completed` gesetzt. Jede normale +Planungs- und Live-Mutation sperrt diesen Zustand. + +Die eigentlichen Sendungstexte, Quellen, Werkzeuge, Themen und Medien liegen +weiterhin in den beim Einplanen erzeugten Usage-Snapshots. Finalisierung friert +die zuvor noch katalogabhängige Abschnittskategorie ein. Abgeschlossene +Sendungen werden ausschließlich gelesen oder vollständig in einen neuen Entwurf +dupliziert. Das Duplikat erhält neue Show-, Abschnitts-, Usage- und Relations-IDs, +offene Usage-Status und weder Live-, Historien- noch Freigabedaten. + +`src/lib/server/shares.ts` erzeugt für Barkeeper zufällige 256-Bit-Lesetokens; +SQLite speichert nur SHA-256-Hashes. Eine Freigabe verweist direkt auf eine +Sendung und darf deshalb bereits im Entwurf, im bereiten oder im laufenden +Zustand entstehen. Der Umfang ist eine Positivliste aus `rundown`, `moderation` +oder `research`. Optionale Passwörter liegen als Argon2id-Hash vor. Nach +erfolgreicher Prüfung entsteht eine höchstens zwölf Stunden gültige, ebenfalls +nur gehashte Gast-Sitzung; Fehlversuche werden pro Link und Clientadresse +begrenzt. Änderungen an Umfang, Passwort oder Ablauf invalidieren bestehende +Gast-Sitzungen. Ablauf und Löschung werden bei jedem HTML-, Seitendaten- und +Medienrequest erneut geprüft. + +Die öffentliche Route `/freigabe/[token]` erhält keine interne Sendungs-ID und +keinen ungefilterten `Show`-Datensatz. Sie projiziert genau die am Token gebundene +Sendung und nur die Felder ihres Umfangs. Vor dem Abschluss werden aktuelle +Sendungs- und Usage-Snapshots bei jedem Aufruf neu gelesen; danach liefert +dieselbe Projektion die durch die Finalisierung stabilisierten Snapshots. Die +separate Medienroute akzeptiert ausschließlich das ausdrücklich gewählte +Präsentationsmedium einer eingeplanten Nicht-Reserve-Verwendung derselben +Sendung. Private `/media`-Dateien bleiben weiterhin an eine Redaktionssitzung +gebunden. + +Die wiederverwendbare Barkeeper-Komponente `ShareManagement.svelte` liegt direkt +in der Sendungsbearbeitung und in der Verwaltung. Beide Routen verwenden +dieselben Servicefunktionen für Anlage, Bearbeitung und Löschung; die +Sendungsroute bindet Mutationen zusätzlich an ihre eigene Sendungs-ID. Die +Verwaltung gruppiert bestehende Freigaben nach Sendung und zeigt deren aktuellen +Status und Termin. + ## Oberflächenstruktur Die Hauptnavigation umfasst Eingang, Fundus, Sendungen, Archiv und Verwaltung. diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md index 66b4223..f51ca82 100644 --- a/docs/DECISIONS.md +++ b/docs/DECISIONS.md @@ -419,9 +419,35 @@ verbleibenden Piece-Dauern. Bereits abgeschlossene Abschnitte werden ausgelassen ungeplante Abschnitte werden sichtbar gezählt und eine Überziehung reduziert nicht die geplante Zeit kommender Inhalte. -`completed` beendet einen Durchlauf, nicht automatisch dessen offene Pieces. +`ended_at_utc` beendet einen Durchlauf, nicht automatisch dessen offene Pieces. Wiederaufnahme und neuer Durchlauf sind deshalb getrennte Befehle: Erstere bewahrt Intervalle und Fortschritt, letzterer setzt beides ausdrücklich zurück. Das private Markdownfeld „Interaktion“ folgt derselben Original-/Snapshotgrenze wie Moderation und Überleitung, ist aber nicht Teil der öffentlichen Präsentations-Positivliste. + +## D-027: Abschlusszustand und Freigaben als eigene Sicherheitsgrenzen + +Ein beendeter Live-Lauf ist wiederaufnehmbar und deshalb nicht identisch mit +einer historischen Sendung. Erst eine eigene transaktionale Finalisierung setzt +`shows.status` auf `completed`. Eine dauerhaft gespeicherte Befehls-ID verhindert +Doppelverbuchung; Usage-Historie enthält nur `done`, nie `skipped` oder Reserve. +Bestehende Usage-Snapshots bleiben Inhaltsquelle, während Abschnittskategorien +beim Abschluss zusätzlich eingefroren werden. Historische Sendungen werden nicht +erneut bearbeitet, sondern mit neuen IDs und zurückgesetztem Laufzustand kopiert. + +Lesefreigaben sind keine abgeschwächten Redaktionssitzungen. Sie verwenden eine +eigene Positivprojektion mit drei kumulativen Umfängen. Roh-Tokens und +Gast-Sitzungstokens stehen nie in SQLite, optionale Passwörter verwenden Argon2id. +Passwort, Ablauf und Widerruf werden zentral vor Seite, Seitendaten und Medium +geprüft. Präsentationsmedien sind an Freigabe, Sendung und Usage-Snapshot +gebunden; beliebige Fundus-Dateien bleiben ausgeschlossen. + +Freigaben verweisen auf `shows` statt ausschließlich auf Finalisierungen. Damit +kann ein Barkeeper bereits einen Entwurf oder eine laufende Planung teilen. Die +Gastprojektion liest bei jedem Aufruf die aktuellen Sendungssnapshots; der +spätere Abschluss stabilisiert genau diese bestehende Datenquelle, ohne Token +oder URL zu wechseln. Bearbeitungen behalten den Link, invalidieren aber +vorhandene Passwortsitzungen. Vollständiges Löschen ist der unmittelbare +Widerruf. Die Sendungsbearbeitung und die zentrale Verwaltung verwenden dieselbe +Geschäftslogik und bleiben Barkeepern vorbehalten. diff --git a/docs/RELEASE_NOTES.md b/docs/RELEASE_NOTES.md index 1ea864a..12a8aa9 100644 --- a/docs/RELEASE_NOTES.md +++ b/docs/RELEASE_NOTES.md @@ -1,5 +1,58 @@ # Release Notes +## 0.6.0 - Abschluss, Archiv und Freigaben + +Veröffentlicht am 25. Oktober 2026 + +Sendewerk 0.6.0 trennt das wiederaufnehmbare Ende eines Live-Laufs vom +unveränderlichen redaktionellen Abschluss. Abgeschlossene Sendungen erhalten +eine stabile Historie, liegen im Archiv und können als neuer Entwurf dupliziert +werden. Barkeeper geben genau eine geplante, laufende oder historische Sendung +über widerrufbare, optional passwortgeschützte Leselinks frei. + +### Abschluss und Historie + +- Ein Abschlussdialog zeigt gesendete, übersprungene und offene Pieces getrennt + und bietet ausschließlich gesendete Originale zur bewussten Archivierung an. +- Finalisierung ist transaktional und idempotent. Übersprungene Pieces und die + Reservebank werden nicht als gesendet verbucht. +- Usage-Texte, Relationen, Kategorien und tatsächliche Zeiten bleiben nach dem + Abschluss stabil, auch wenn Fundus-Originale oder Kataloge später wechseln. +- Historische Sendungen sind intern nur lesbar. Die Duplikation erzeugt einen + unabhängigen Entwurf mit offenen Pieces und ohne früheren Lauf- oder + Freigabezustand. + +### Archiv und Freigaben + +- Das Archiv zeigt abgeschlossene Sendungen neben archivierten und gelöschten + Pieces und bietet die Duplikation direkt an. +- Barkeeper erstellen Leselinks direkt in der Sendungsbearbeitung oder zentral + in der Verwaltung für Ablauf, Moderation oder vollständige Recherche, optional + mit Passwort und Ablaufzeit. +- Noch nicht abgeschlossene Sendungen zeigen Gästen beim nächsten Aufruf den + aktuellen Plan. Nach dem Abschluss bleibt derselbe Link auf dem stabilen + historischen Stand. +- Freigaben lassen sich in nach Sendung gruppierten Verwaltungskarten bearbeiten + und endgültig löschen; Passwortänderungen beenden vorhandene Gast-Sitzungen. +- Tokens und Gast-Sitzungen liegen nur gehasht vor; Passwörter verwenden + Argon2id und Fehlversuche sind begrenzt. +- Gastseiten projizieren genau eine Sendung. Private Originaldaten, Nutzer, + interne IDs und Felder außerhalb des gewählten Umfangs werden nicht geliefert. +- Direkte Medienaufrufe prüfen dieselbe Freigabe, Passwortsitzung, Ablaufzeit und + den Widerruf erneut und erlauben nur das freigegebene Präsentationsmedium. + +### Technik und Prüfung + +- Migration `0015_completion_archive_and_shares.sql` übernimmt bestehende + abgeschlossene Sendungen und ergänzt Finalisierung, Historie, + Duplikationsbefehle, Freigaben, Gast-Sitzungen und + Passwortversuchsbegrenzung. +- Migration `0016_shares_for_active_shows.sql` erhält vorhandene Freigaben und + erweitert ihre Referenz auf alle Sendungszustände. +- Paket, Lockdatei und Cloudron-Manifest tragen gemeinsam Version `0.6.0`. +- 52 Verhaltenstests und 18 Chromium-Abläufe decken den bisherigen Umfang sowie + Abschluss, Archiv und anonyme Lesefreigaben ab. + ## 0.5.12 - Live-Regie und Sendungskalender Veröffentlicht am 24. Oktober 2026 diff --git a/docs/STATUS.md b/docs/STATUS.md index ded0c8a..72803f9 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -2,10 +2,45 @@ ## Aktueller Umfang -Etappe 1 bis Etappe 5 sind abgeschlossen. Architektur, Gesamtdatenmodell, +Etappe 1 bis Etappe 6 sind abgeschlossen. Architektur, Gesamtdatenmodell, Anmeldung, Rollen, persistente Inhaltsbearbeitung, Sendungsplanung, -Live-Steuerung, Präsentationsausgabe und lokaler Betriebsweg sind dokumentiert. -Etappe 6 wurde nicht begonnen. +Live-Steuerung, Präsentationsausgabe, unveränderliche Sendungshistorie, +Archiv und sichere Lesefreigaben sind dokumentiert. + +## Abschluss, Archiv und Freigaben 0.6.0 + +- das Ende eines Live-Laufs bleibt wiederaufnehmbar; erst der getrennte, + idempotente Abschluss friert Sendung, Kategorien, tatsächliche Verwendungen + und Zeiten unveränderlich ein +- der Abschlussdialog zählt gesendete, übersprungene und offene Pieces getrennt + und archiviert Fundus-Originale ausschließlich nach bewusster Auswahl +- die Verwendungshistorie wird beim Abschluss materialisiert; übersprungene + Verwendungen und die Reservebank zählen nicht als gesendet +- abgeschlossene Sendungen liegen getrennt im Archiv, sind intern nur lesbar und + lassen sich mit neuen IDs, offenen Verwendungen und ohne Live- oder + Freigabezustand in einen neuen Entwurf duplizieren +- Barkeeper verwalten gehashte Leselinks direkt in der Sendungsbearbeitung und + gesammelt in der Verwaltung; Umfang, Passwort und Ablauf lassen sich ändern, + Freigaben vollständig löschen +- Entwürfe, bereite und laufende Sendungen lassen sich bereits freigeben; + Planänderungen werden beim nächsten Gastaufruf sichtbar, während der Abschluss + denselben Link auf den unveränderlichen historischen Stand festlegt +- passwortgeschützte Freigaben verwenden kurzlebige, gehashte Gast-Sitzungen und + begrenzen Fehlversuche pro Link und Clientadresse +- Gastseiten liefern genau eine positiv projizierte Sendung ohne interne IDs, + Originaldaten oder nicht freigegebene Felder; geschützte Präsentationsmedien + prüfen Umfang, Passwortsitzung, Ablauf und Widerruf bei jedem Direktaufruf +- bestehende abgeschlossene Sendungen werden durch Migration `0015` mit + eingefrorener Historie übernommen; Migration `0016` erweitert bestehende + Freigaben verlustfrei auf noch nicht abgeschlossene Sendungen + +Die lokale Prüfmatrix umfasst 52 Verhaltenstests und 18 Chromium-Abläufe. +Abschluss, Archiv, historische Nur-Lese-Ansicht, Passwortfreigabe, +Scope-Begrenzung, direkter Medienzugriff, fremde IDs, Gast-Schreibversuch und +Widerruf wurden zusätzlich in einem anonymen Browserkontext geprüft. Der +erweiterte Ablauf prüft außerdem offene Sendungen, aktuelle Planänderungen, +Bearbeiten und Löschen in beiden Barkeeper-Oberflächen sowie Desktop- und +Smartphone-Layout. ## Live-Regie und Sendungskalender 0.5.12 diff --git a/migrations/0015_completion_archive_and_shares.sql b/migrations/0015_completion_archive_and_shares.sql new file mode 100644 index 0000000..7f03a58 --- /dev/null +++ b/migrations/0015_completion_archive_and_shares.sql @@ -0,0 +1,115 @@ +ALTER TABLE shows ADD COLUMN completed_at_utc TEXT; + +ALTER TABLE show_sections ADD COLUMN category_name_snapshot TEXT; +ALTER TABLE show_sections ADD COLUMN category_color_snapshot TEXT; + +CREATE TABLE show_finalizations ( + show_id TEXT PRIMARY KEY REFERENCES shows(id) ON DELETE CASCADE, + command_id TEXT NOT NULL, + source_revision INTEGER NOT NULL CHECK (source_revision >= 1), + finalized_by_user_id TEXT REFERENCES users(id) ON DELETE SET NULL, + finalized_at_utc TEXT NOT NULL, + UNIQUE (show_id, command_id) +); + +CREATE TABLE show_finalization_archived_pieces ( + show_id TEXT NOT NULL REFERENCES show_finalizations(show_id) ON DELETE CASCADE, + piece_id TEXT NOT NULL REFERENCES pieces(id) ON DELETE RESTRICT, + PRIMARY KEY (show_id, piece_id) +); + +CREATE TABLE piece_usage_history ( + usage_id TEXT PRIMARY KEY REFERENCES piece_usages(id) ON DELETE RESTRICT, + show_id TEXT NOT NULL REFERENCES shows(id) ON DELETE RESTRICT, + original_piece_id TEXT NOT NULL REFERENCES pieces(id) ON DELETE RESTRICT, + show_title_snapshot TEXT NOT NULL, + usage_title_snapshot TEXT NOT NULL, + used_at_utc TEXT NOT NULL, + actual_duration_seconds INTEGER NOT NULL CHECK (actual_duration_seconds >= 0) +); + +CREATE INDEX idx_piece_usage_history_original + ON piece_usage_history(original_piece_id, used_at_utc DESC); + +CREATE TABLE show_duplication_commands ( + source_show_id TEXT NOT NULL REFERENCES show_finalizations(show_id) ON DELETE CASCADE, + command_id TEXT NOT NULL, + created_show_id TEXT NOT NULL REFERENCES shows(id) ON DELETE CASCADE, + created_at_utc TEXT NOT NULL, + PRIMARY KEY (source_show_id, command_id) +); + +CREATE TABLE show_shares ( + id TEXT PRIMARY KEY, + show_id TEXT NOT NULL REFERENCES show_finalizations(show_id) ON DELETE CASCADE, + token_hash TEXT NOT NULL UNIQUE, + scope TEXT NOT NULL CHECK (scope IN ('rundown', 'moderation', 'research')), + password_hash TEXT, + expires_at_utc TEXT, + revoked_at_utc TEXT, + created_by_user_id TEXT NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + created_at_utc TEXT NOT NULL +); + +CREATE INDEX idx_show_shares_show ON show_shares(show_id, created_at_utc DESC); + +CREATE TABLE show_share_sessions ( + token_hash TEXT PRIMARY KEY, + share_id TEXT NOT NULL REFERENCES show_shares(id) ON DELETE CASCADE, + created_at_utc TEXT NOT NULL, + expires_at_utc TEXT NOT NULL +); + +CREATE INDEX idx_show_share_sessions_share ON show_share_sessions(share_id, expires_at_utc); + +CREATE TABLE share_password_attempts ( + id TEXT PRIMARY KEY, + share_key TEXT NOT NULL, + address_key TEXT NOT NULL, + attempted_at_utc TEXT NOT NULL +); + +CREATE INDEX idx_share_password_attempts_share + ON share_password_attempts(share_key, attempted_at_utc); +CREATE INDEX idx_share_password_attempts_address + ON share_password_attempts(address_key, attempted_at_utc); + +UPDATE show_sections +SET category_name_snapshot = ( + SELECT name FROM section_categories WHERE id = show_sections.category_id + ), + category_color_snapshot = ( + SELECT color FROM section_categories WHERE id = show_sections.category_id + ) +WHERE show_id IN (SELECT id FROM shows WHERE status = 'completed'); + +UPDATE shows +SET completed_at_utc = COALESCE( + (SELECT ended_at_utc FROM show_live_states WHERE show_id = shows.id), + updated_at_utc +) +WHERE status = 'completed'; + +INSERT INTO show_finalizations ( + show_id, command_id, source_revision, finalized_by_user_id, finalized_at_utc +) +SELECT id, 'migration-' || id, revision, NULL, completed_at_utc +FROM shows +WHERE status = 'completed'; + +INSERT INTO piece_usage_history ( + usage_id, show_id, original_piece_id, show_title_snapshot, usage_title_snapshot, + used_at_utc, actual_duration_seconds +) +SELECT u.id, sh.id, u.original_piece_id, sh.title, u.title_snapshot, + COALESCE(sh.starts_at_utc, sh.completed_at_utc), + COALESCE(( + SELECT sum(max(0, CAST((julianday(COALESCE(r.ended_at_utc, sh.completed_at_utc)) - + julianday(r.started_at_utc)) * 86400 AS INTEGER))) + FROM live_usage_runs r WHERE r.usage_id = u.id + ), 0) +FROM piece_usages u +JOIN show_sections s ON s.id = u.show_section_id +JOIN shows sh ON sh.id = s.show_id +WHERE sh.status = 'completed' AND s.is_reserve_bank = 0 + AND u.status = 'done' AND u.original_piece_id IS NOT NULL; diff --git a/migrations/0016_shares_for_active_shows.sql b/migrations/0016_shares_for_active_shows.sql new file mode 100644 index 0000000..b77ef63 --- /dev/null +++ b/migrations/0016_shares_for_active_shows.sql @@ -0,0 +1,43 @@ +DROP INDEX idx_show_share_sessions_share; +DROP INDEX idx_show_shares_show; + +ALTER TABLE show_share_sessions RENAME TO show_share_sessions_v15; +ALTER TABLE show_shares RENAME TO show_shares_v15; + +CREATE TABLE show_shares ( + id TEXT PRIMARY KEY, + show_id TEXT NOT NULL REFERENCES shows(id) ON DELETE CASCADE, + token_hash TEXT NOT NULL UNIQUE, + scope TEXT NOT NULL CHECK (scope IN ('rundown', 'moderation', 'research')), + password_hash TEXT, + expires_at_utc TEXT, + revoked_at_utc TEXT, + created_by_user_id TEXT NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + created_at_utc TEXT NOT NULL +); + +CREATE INDEX idx_show_shares_show ON show_shares(show_id, created_at_utc DESC); + +CREATE TABLE show_share_sessions ( + token_hash TEXT PRIMARY KEY, + share_id TEXT NOT NULL REFERENCES show_shares(id) ON DELETE CASCADE, + created_at_utc TEXT NOT NULL, + expires_at_utc TEXT NOT NULL +); + +CREATE INDEX idx_show_share_sessions_share ON show_share_sessions(share_id, expires_at_utc); + +INSERT INTO show_shares ( + id, show_id, token_hash, scope, password_hash, expires_at_utc, revoked_at_utc, + created_by_user_id, created_at_utc +) +SELECT id, show_id, token_hash, scope, password_hash, expires_at_utc, revoked_at_utc, + created_by_user_id, created_at_utc +FROM show_shares_v15; + +INSERT INTO show_share_sessions (token_hash, share_id, created_at_utc, expires_at_utc) +SELECT token_hash, share_id, created_at_utc, expires_at_utc +FROM show_share_sessions_v15; + +DROP TABLE show_share_sessions_v15; +DROP TABLE show_shares_v15; diff --git a/package-lock.json b/package-lock.json index 125d290..b6d1d29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sendewerk", - "version": "0.5.12", + "version": "0.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sendewerk", - "version": "0.5.12", + "version": "0.6.0", "dependencies": { "@node-rs/argon2": "^2.2.2", "better-sqlite3": "^13.0.3", diff --git a/package.json b/package.json index 75cf458..9b518f3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sendewerk", "private": true, - "version": "0.5.12", + "version": "0.6.0", "type": "module", "engines": { "node": ">=22.17 <25" diff --git a/src/app.css b/src/app.css index 5589ed8..33f06ba 100644 --- a/src/app.css +++ b/src/app.css @@ -47,6 +47,329 @@ --radius-small: 0.65rem; } +.completion-dialog { + width: min(38rem, calc(100% - 2rem)); + max-height: calc(100dvh - 2rem); + padding: 0; + border: 1px solid color-mix(in srgb, var(--gold) 55%, var(--line)); + border-radius: var(--radius-large); + background: var(--panel-strong); + color: var(--text); +} + +.completion-dialog::backdrop { + background: rgb(3 8 16 / 68%); +} + +.completion-dialog form { + display: grid; + gap: 1rem; + padding: 1.25rem; +} + +.completion-dialog h2, +.completion-dialog p { + margin: 0; +} + +.completion-dialog fieldset { + display: grid; + gap: 0.5rem; + max-height: 15rem; + overflow-y: auto; + padding: 0.8rem; + border: 1px solid var(--line); + border-radius: var(--radius-medium); +} + +.completion-dialog footer, +.completion-counts, +.check-row { + display: flex; + align-items: center; + gap: 0.75rem; +} + +.completion-dialog footer { + justify-content: flex-end; +} + +.completion-counts { + flex-wrap: wrap; +} + +.completion-counts span { + padding: 0.45rem 0.65rem; + border-radius: 999px; + background: var(--panel-soft); +} + +.check-row input { + width: auto; +} + +.check-row span { + display: grid; +} + +.share-management { + margin-bottom: 1.5rem; + padding: 1rem; +} + +.share-create-form { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); + gap: 0.8rem; + margin-top: 1rem; +} + +.share-create-form label, +.share-edit-form label { + display: grid; + gap: 0.35rem; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; +} + +.share-create-form button { + align-self: end; +} + +.share-intro { + max-width: 58rem; + margin: 0; + color: var(--text-muted); + font-size: 0.76rem; +} + +.share-show-list { + display: grid; + gap: 0.75rem; + margin-top: 1rem; +} + +.share-show-card { + padding: 0.85rem; + border: 1px solid var(--line); + border-radius: var(--radius-medium); + background: var(--panel-soft); +} + +.share-show-card > header, +.share-row summary { + display: grid; + align-items: center; + gap: 0.75rem; +} + +.share-show-card > header { + grid-template-columns: minmax(0, 1fr) auto; +} + +.share-show-card > header > div, +.share-row summary > span:nth-child(2) { + display: grid; + min-width: 0; +} + +.share-show-card > header a { + color: var(--text); + font-weight: 760; +} + +.share-show-card small, +.share-row small { + color: var(--text-muted); + font-size: 0.68rem; +} + +.share-list { + display: grid; + gap: 0.4rem; + margin-top: 0.75rem; +} + +.share-row { + border-top: 1px solid var(--line); +} + +.share-row summary { + grid-template-columns: auto minmax(0, 1fr) auto auto; + min-height: 3.25rem; + cursor: pointer; + list-style: none; +} + +.share-row summary::-webkit-details-marker { + display: none; +} + +.share-row-icon { + display: grid; + width: 2rem; + height: 2rem; + place-items: center; + border-radius: 50%; + background: var(--accent-soft); + color: var(--accent-text); +} + +.share-row summary > span:nth-child(3) { + padding: 0.2rem 0.45rem; + border-radius: 999px; + background: var(--accent-soft); + color: var(--accent-text); + font-size: 0.65rem; + font-weight: 700; +} + +.share-row summary > span.locked { + background: color-mix(in srgb, var(--danger) 12%, var(--panel)); + color: var(--danger); +} + +.share-edit-area { + display: grid; + gap: 0.75rem; + padding: 0.75rem 0 0.25rem 2.75rem; +} + +.share-edit-area > p { + margin: 0; + color: var(--text-muted); + font-size: 0.74rem; +} + +.share-edit-form { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 0.65rem; +} + +.share-edit-form input, +.share-edit-form select { + width: 100%; +} + +.share-result { + display: grid; + gap: 0.25rem; +} + +.share-result a { + overflow-wrap: anywhere; +} + +.guest-shell { + min-height: 100dvh; +} + +.share-page { + width: min(58rem, calc(100% - 2rem)); + margin: 0 auto; + padding: clamp(2rem, 6vw, 5rem) 0; +} + +.share-gate { + width: min(30rem, 100%); + margin: 8vh auto 0; +} + +.share-heading { + padding-bottom: 1.5rem; + border-bottom: 1px solid var(--line); +} + +.share-heading h1 { + margin: 0.25rem 0; + font-size: clamp(2rem, 6vw, 4rem); +} + +.shared-rundown, +.historical-rundown { + display: grid; + gap: 1rem; + margin-top: 1.25rem; +} + +.shared-section > header, +.historical-section > header, +.shared-usage-heading, +.historical-usage > header { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 1rem; +} + +.shared-section h2, +.shared-usage h3, +.historical-section h2, +.historical-usage h3 { + margin: 0; +} + +.shared-usage, +.historical-usage { + margin-top: 0.75rem; + padding: 1rem; + border: 1px solid var(--line); + border-radius: var(--radius-medium); + background: var(--panel-soft); +} + +.shared-usage img { + max-width: min(100%, 32rem); + max-height: 22rem; + margin-top: 0.8rem; + border-radius: var(--radius-medium); + object-fit: contain; +} + +.shared-copy, +.historical-usage section, +.historical-usage details { + margin-top: 0.8rem; +} + +.historical-show-page { + width: min(72rem, 100%); +} + +.historical-usage p { + white-space: pre-wrap; +} + +@media (max-width: 42rem) { + .share-create-form, + .share-edit-form { + grid-template-columns: 1fr; + } + + .share-row summary { + grid-template-columns: auto minmax(0, 1fr) auto; + } + + .share-row summary > .app-icon:last-child { + display: none; + } + + .share-edit-area { + padding-left: 0; + } + + .completion-dialog footer, + .shared-section > header, + .historical-section > header, + .shared-usage-heading, + .historical-usage > header { + align-items: stretch; + flex-direction: column; + } +} + :root[data-theme='dark'] { color-scheme: dark; --bg: #0b1220; diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 68ff24a..942d2f4 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,3 +1,4 @@ +import { randomBytes } from 'node:crypto'; import { redirect } from '@sveltejs/kit'; import type { Handle } from '@sveltejs/kit/hooks'; import { deleteSession, getSessionUser, hasUsers, SESSION_COOKIE } from '#lib/server/auth.js'; @@ -14,16 +15,32 @@ const publicPaths = new Set([ export const handle: Handle = async ({ event, resolve }) => { const presentationPath = event.url.pathname.startsWith('/heinzlive/'); + const sharePath = event.url.pathname.startsWith('/freigabe/'); + const shareMediaPath = /^\/freigabe\/[^/]+\/media\/[^/]+$/.test(event.url.pathname); const presentationTestPath = /^\/fundus\/[^/]+\/praesentation(?:\/|$)/.test(event.url.pathname); const token = event.cookies.get(SESSION_COOKIE); event.locals.user = getSessionUser(token); if (token && !event.locals.user) deleteSession(event.cookies); - if (!presentationPath && !publicPaths.has(event.url.pathname) && !event.locals.user) { + if ( + !presentationPath && + !sharePath && + !publicPaths.has(event.url.pathname) && + !event.locals.user + ) { redirect(303, hasUsers() ? '/login' : '/setup'); } - const response = await resolve(event); + const sharePageNonce = sharePath && !shareMediaPath ? randomBytes(16).toString('base64') : null; + const response = await resolve( + event, + sharePageNonce + ? { + transformPageChunk: ({ html }) => + html.replaceAll(' { "frame-src https://www.youtube-nocookie.com https://www.youtube.com https://www.tiktok.com; frame-ancestors 'none'" ); } + if (sharePath) { + response.headers.set('Cache-Control', 'private, no-store'); + response.headers.set('Referrer-Policy', 'same-origin'); + if (!shareMediaPath) { + response.headers.set( + 'Content-Security-Policy', + `default-src 'self'; script-src 'self' 'nonce-${sharePageNonce}'; style-src 'self' 'unsafe-inline'; img-src 'self'; frame-ancestors 'none'` + ); + } + } if (event.url.pathname === '/einladung' || event.url.pathname === '/passwort-zuruecksetzen') { response.headers.set('Cache-Control', 'no-store'); } diff --git a/src/lib/components/ShareManagement.svelte b/src/lib/components/ShareManagement.svelte new file mode 100644 index 0000000..2d92b44 --- /dev/null +++ b/src/lib/components/ShareManagement.svelte @@ -0,0 +1,249 @@ + + + diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index fd142ae..d02e531 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -110,11 +110,11 @@ export async function verifyPassword(passwordHash: string, password: string): Pr const dummyPasswordHash = hashPassword(randomBytes(32).toString('hex')); -function createToken(): string { +export function createToken(): string { return randomBytes(32).toString('base64url'); } -function hashToken(token: string): string { +export function hashToken(token: string): string { return createHash('sha256').update(token).digest('hex'); } diff --git a/src/lib/server/completion.spec.ts b/src/lib/server/completion.spec.ts new file mode 100644 index 0000000..c220e35 --- /dev/null +++ b/src/lib/server/completion.spec.ts @@ -0,0 +1,204 @@ +import { execFileSync } from 'node:child_process'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import type { AuthUser } from './auth.js'; +import { finalizeShow, getCompletionSummary } from './completion.js'; +import { closeDatabase, getDatabase } from './database.js'; +import { + addPieceUsage, + addShowSection, + createShow, + duplicateCompletedShow, + getPieceUsageContexts, + getShow +} from './shows.js'; + +let directory: string; +const actor: AuthUser = { + id: 'completion-actor', + email: 'completion@example.test', + displayName: 'Abschluss Test', + role: 'barkeeper', + lockedAtUtc: null +}; + +beforeEach(() => { + directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-completion-test-')); + process.env.SENDEWERK_DB_PATH = path.join(directory, 'test.sqlite'); + process.env.SENDEWERK_UPLOAD_DIR = path.join(directory, 'uploads'); + execFileSync(process.execPath, ['scripts/migrate.mjs'], { cwd: process.cwd(), env: process.env }); + execFileSync(process.execPath, ['scripts/seed.mjs'], { cwd: process.cwd(), env: process.env }); + getDatabase() + .prepare( + `INSERT INTO users (id, email, email_normalized, display_name, password_hash, role, + password_changed_at_utc, created_at_utc, updated_at_utc) + VALUES (?, ?, ?, ?, 'unused', 'barkeeper', ?, ?, ?)` + ) + .run( + actor.id, + actor.email, + actor.email, + actor.displayName, + ...Array(3).fill(new Date().toISOString()) + ); +}); + +afterEach(() => { + closeDatabase(); + delete process.env.SENDEWERK_DB_PATH; + delete process.env.SENDEWERK_UPLOAD_DIR; + rmSync(directory, { recursive: true, force: true }); +}); + +function endedShow() { + const created = createShow({ title: 'Finale Sendung', startsAtUtc: null, templateId: null }); + const section = addShowSection(created.id, created.revision, { + name: 'Gesendeter Block', + categoryId: 'cat-jc', + explicitDurationSeconds: null + }); + const done = addPieceUsage(created.id, section.revision, { + pieceId: 'piece-console', + sectionId: section.id + }); + const skipped = addPieceUsage(created.id, done.revision, { + pieceId: 'piece-console', + sectionId: section.id + }); + const database = getDatabase(); + database.prepare("UPDATE piece_usages SET status = 'done' WHERE id = ?").run(done.id); + database.prepare("UPDATE piece_usages SET status = 'skipped' WHERE id = ?").run(skipped.id); + database + .prepare( + `INSERT INTO show_live_states + (show_id, revision, started_at_utc, ended_at_utc, presentation_revision, updated_at_utc) + VALUES (?, 0, ?, ?, 0, ?)` + ) + .run( + created.id, + '2026-10-25T18:00:00.000Z', + '2026-10-25T19:00:00.000Z', + '2026-10-25T19:00:00.000Z' + ); + return { showId: created.id, doneUsageId: done.id, revision: skipped.revision }; +} + +describe('show completion', () => { + it('finalizes once, records only sent usages and archives only the explicit selection', () => { + const prepared = endedShow(); + expect(getCompletionSummary(prepared.showId)).toMatchObject({ + doneCount: 1, + skippedCount: 1, + candidates: [{ pieceId: 'piece-console', usageCount: 1 }] + }); + const first = finalizeShow({ + showId: prepared.showId, + commandId: 'complete-once', + expectedShowRevision: prepared.revision, + expectedLiveRevision: 0, + archivePieceIds: ['piece-console'], + actor, + now: new Date('2026-10-25T19:05:00.000Z') + }); + expect(first.duplicate).toBe(false); + expect( + finalizeShow({ + showId: prepared.showId, + commandId: 'complete-once', + expectedShowRevision: prepared.revision, + expectedLiveRevision: 0, + archivePieceIds: ['piece-console'], + actor + }).duplicate + ).toBe(true); + expect(() => + finalizeShow({ + showId: prepared.showId, + commandId: 'different-command', + expectedShowRevision: prepared.revision, + expectedLiveRevision: 0, + archivePieceIds: [], + actor + }) + ).toThrow('SHOW_COMPLETED'); + const database = getDatabase(); + expect( + ( + database + .prepare('SELECT count(*) AS count FROM piece_usage_history WHERE show_id = ?') + .get(prepared.showId) as { count: number } + ).count + ).toBe(1); + expect(database.prepare('SELECT status FROM pieces WHERE id = ?').get('piece-console')).toEqual( + { status: 'archived' } + ); + expect(getPieceUsageContexts(['piece-console'])['piece-console'].sentHistory).toHaveLength(1); + }); + + it('keeps historical snapshots stable and duplicates them into an editable clean run', () => { + const prepared = endedShow(); + finalizeShow({ + showId: prepared.showId, + commandId: 'freeze', + expectedShowRevision: prepared.revision, + expectedLiveRevision: 0, + archivePieceIds: [], + actor + }); + const historical = getShow(prepared.showId)!; + getDatabase() + .prepare("UPDATE section_categories SET name = 'Später geändert' WHERE id = 'cat-jc'") + .run(); + getDatabase() + .prepare("UPDATE pieces SET title = 'Später geändert' WHERE id = 'piece-console'") + .run(); + const unchanged = getShow(prepared.showId)!; + expect(unchanged.sections[0].category).toBe(historical.sections[0].category); + expect(unchanged.sections[0].usages[0].title).toBe(historical.sections[0].usages[0].title); + + const copy = duplicateCompletedShow(prepared.showId, 'copy-once'); + expect(duplicateCompletedShow(prepared.showId, 'copy-once').id).toBe(copy.id); + expect(getShow(copy.id)).toMatchObject({ + status: 'draft', + startsAtUtc: null, + sections: [ + { + category: historical.sections[0].category, + usages: [{ status: 'open' }, { status: 'open' }] + } + ] + }); + expect( + getDatabase().prepare('SELECT 1 FROM show_finalizations WHERE show_id = ?').get(copy.id) + ).toBeUndefined(); + getDatabase() + .prepare( + `UPDATE piece_usages SET status = 'done' + WHERE show_section_id IN (SELECT id FROM show_sections WHERE show_id = ?)` + ) + .run(copy.id); + getDatabase() + .prepare( + `INSERT INTO show_live_states + (show_id, revision, started_at_utc, ended_at_utc, presentation_revision, updated_at_utc) + VALUES (?, 0, ?, ?, 0, ?)` + ) + .run( + copy.id, + '2026-10-26T18:00:00.000Z', + '2026-10-26T19:00:00.000Z', + '2026-10-26T19:00:00.000Z' + ); + finalizeShow({ + showId: copy.id, + commandId: 'finalize-copy', + expectedShowRevision: 1, + expectedLiveRevision: 0, + archivePieceIds: [], + actor + }); + expect(getShow(copy.id)?.sections[0].category).toBe(historical.sections[0].category); + }); +}); diff --git a/src/lib/server/completion.ts b/src/lib/server/completion.ts new file mode 100644 index 0000000..59b923f --- /dev/null +++ b/src/lib/server/completion.ts @@ -0,0 +1,210 @@ +import type { AuthUser } from './auth.js'; +import { getDatabase } from './database.js'; + +export type CompletionCandidate = { + pieceId: string; + title: string; + usageCount: number; +}; + +export type CompletionSummary = { + doneCount: number; + skippedCount: number; + openCount: number; + candidates: CompletionCandidate[]; +}; + +export function isShowFinalized(showId: string): boolean { + return Boolean( + getDatabase().prepare('SELECT 1 FROM show_finalizations WHERE show_id = ?').get(showId) + ); +} + +export function getCompletionSummary(showId: string): CompletionSummary { + const database = getDatabase(); + if (!database.prepare('SELECT 1 FROM shows WHERE id = ?').get(showId)) + throw new Error('NOT_FOUND'); + const counts = database + .prepare( + `SELECT + sum(CASE WHEN u.status = 'done' THEN 1 ELSE 0 END) AS done_count, + sum(CASE WHEN u.status = 'skipped' THEN 1 ELSE 0 END) AS skipped_count, + sum(CASE WHEN u.status IN ('open', 'current') THEN 1 ELSE 0 END) AS open_count + FROM piece_usages u JOIN show_sections s ON s.id = u.show_section_id + WHERE s.show_id = ? AND s.is_reserve_bank = 0` + ) + .get(showId) as { + done_count: number | null; + skipped_count: number | null; + open_count: number | null; + }; + const candidates = database + .prepare( + `SELECT u.original_piece_id AS piece_id, p.title, count(*) AS usage_count + FROM piece_usages u + JOIN show_sections s ON s.id = u.show_section_id + JOIN pieces p ON p.id = u.original_piece_id + WHERE s.show_id = ? AND s.is_reserve_bank = 0 AND u.status = 'done' + AND p.status != 'archived' AND p.deleted_at_utc IS NULL + GROUP BY u.original_piece_id, p.title ORDER BY p.title COLLATE NOCASE` + ) + .all(showId) as { piece_id: string; title: string; usage_count: number }[]; + return { + doneCount: counts.done_count ?? 0, + skippedCount: counts.skipped_count ?? 0, + openCount: counts.open_count ?? 0, + candidates: candidates.map((candidate) => ({ + pieceId: candidate.piece_id, + title: candidate.title, + usageCount: candidate.usage_count + })) + }; +} + +export function getHistoricalActualDurations(showId: string): Record { + return Object.fromEntries( + ( + getDatabase() + .prepare( + `SELECT usage_id, actual_duration_seconds FROM piece_usage_history WHERE show_id = ?` + ) + .all(showId) as { usage_id: string; actual_duration_seconds: number }[] + ).map((row) => [row.usage_id, row.actual_duration_seconds]) + ); +} + +export function finalizeShow(input: { + showId: string; + commandId: string; + expectedShowRevision: number; + expectedLiveRevision: number; + archivePieceIds: string[]; + actor: AuthUser; + now?: Date; +}) { + if (!input.commandId || input.commandId.length > 200) throw new Error('INVALID_COMMAND'); + if (!Number.isInteger(input.expectedShowRevision) || input.expectedShowRevision < 1) { + throw new Error('INVALID_REVISION'); + } + if (!Number.isInteger(input.expectedLiveRevision) || input.expectedLiveRevision < 0) { + throw new Error('INVALID_REVISION'); + } + const archivePieceIds = [...new Set(input.archivePieceIds.filter(Boolean))]; + const database = getDatabase(); + return database.transaction(() => { + const existing = database + .prepare('SELECT command_id FROM show_finalizations WHERE show_id = ?') + .get(input.showId) as { command_id: string } | undefined; + if (existing) { + if (existing.command_id !== input.commandId) throw new Error('SHOW_COMPLETED'); + return { duplicate: true }; + } + const state = database + .prepare( + `SELECT sh.revision AS show_revision, ls.revision AS live_revision, ls.ended_at_utc + FROM shows sh JOIN show_live_states ls ON ls.show_id = sh.id WHERE sh.id = ?` + ) + .get(input.showId) as + { show_revision: number; live_revision: number; ended_at_utc: string | null } | undefined; + if (!state) throw new Error('NOT_FOUND'); + if (!state.ended_at_utc) throw new Error('NOT_ENDED'); + if ( + state.show_revision !== input.expectedShowRevision || + state.live_revision !== input.expectedLiveRevision + ) { + throw new Error('CONFLICT'); + } + + const candidates = getCompletionSummary(input.showId).candidates.map( + (candidate) => candidate.pieceId + ); + if (archivePieceIds.some((pieceId) => !candidates.includes(pieceId))) { + throw new Error('INVALID_ARCHIVE_SELECTION'); + } + const now = (input.now ?? new Date()).toISOString(); + database + .prepare( + `UPDATE live_usage_runs SET ended_at_utc = ? WHERE show_id = ? AND ended_at_utc IS NULL` + ) + .run(now, input.showId); + database + .prepare( + `UPDATE live_show_timer_runs SET ended_at_utc = ? WHERE show_id = ? AND ended_at_utc IS NULL` + ) + .run(now, input.showId); + database + .prepare( + `UPDATE live_section_timer_runs SET ended_at_utc = ? + WHERE ended_at_utc IS NULL AND timer_id IN ( + SELECT id FROM live_section_timers WHERE show_id = ? + )` + ) + .run(now, input.showId); + database + .prepare( + `UPDATE live_section_timers SET ended_at_utc = ? WHERE show_id = ? AND ended_at_utc IS NULL` + ) + .run(now, input.showId); + database + .prepare( + `UPDATE show_sections SET + category_name_snapshot = COALESCE(category_name_snapshot, + (SELECT name FROM section_categories WHERE id = show_sections.category_id)), + category_color_snapshot = COALESCE(category_color_snapshot, + (SELECT color FROM section_categories WHERE id = show_sections.category_id)) + WHERE show_id = ?` + ) + .run(input.showId); + + database + .prepare( + `INSERT INTO piece_usage_history ( + usage_id, show_id, original_piece_id, show_title_snapshot, usage_title_snapshot, + used_at_utc, actual_duration_seconds + ) + SELECT u.id, sh.id, u.original_piece_id, sh.title, u.title_snapshot, + COALESCE(sh.starts_at_utc, ?), + COALESCE(( + SELECT sum(max(0, CAST((julianday(COALESCE(r.ended_at_utc, ?)) - + julianday(r.started_at_utc)) * 86400 AS INTEGER))) + FROM live_usage_runs r WHERE r.usage_id = u.id + ), 0) + FROM piece_usages u + JOIN show_sections s ON s.id = u.show_section_id + JOIN shows sh ON sh.id = s.show_id + WHERE sh.id = ? AND s.is_reserve_bank = 0 AND u.status = 'done' + AND u.original_piece_id IS NOT NULL` + ) + .run(now, now, input.showId); + + const archivePiece = database.prepare( + `UPDATE pieces SET archived_from_status = status, status = 'archived', revision = revision + 1, + updated_at_utc = ? WHERE id = ? AND status != 'archived' AND deleted_at_utc IS NULL` + ); + for (const pieceId of archivePieceIds) { + archivePiece.run(now, pieceId); + } + + const updated = database + .prepare( + `UPDATE shows SET status = 'completed', completed_at_utc = ?, revision = revision + 1, + updated_at_utc = ? WHERE id = ? AND revision = ? AND status != 'completed'` + ) + .run(now, now, input.showId, input.expectedShowRevision); + if (updated.changes !== 1) throw new Error('CONFLICT'); + database + .prepare( + `INSERT INTO show_finalizations + (show_id, command_id, source_revision, finalized_by_user_id, finalized_at_utc) + VALUES (?, ?, ?, ?, ?)` + ) + .run(input.showId, input.commandId, input.expectedShowRevision, input.actor.id, now); + const recordArchivedPiece = database.prepare( + 'INSERT INTO show_finalization_archived_pieces (show_id, piece_id) VALUES (?, ?)' + ); + for (const pieceId of archivePieceIds) { + recordArchivedPiece.run(input.showId, pieceId); + } + return { duplicate: false }; + })(); +} diff --git a/src/lib/server/content.ts b/src/lib/server/content.ts index 7475cd4..cca99d9 100644 --- a/src/lib/server/content.ts +++ b/src/lib/server/content.ts @@ -926,7 +926,7 @@ export async function duplicatePiece(sourceId: string): Promise { presentation_metadata_fetched_at_utc, created_at_utc, updated_at_utc, revision) VALUES ( ?, ?, ?, ?, ?, 'preparing', ?, ?, ?, ?, ?, ?, ?, - ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1 + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1 )` ) .run( diff --git a/src/lib/server/database.spec.ts b/src/lib/server/database.spec.ts index 3b3a176..de4eed0 100644 --- a/src/lib/server/database.spec.ts +++ b/src/lib/server/database.spec.ts @@ -1,6 +1,6 @@ import Database from 'better-sqlite3'; import { execFileSync } from 'node:child_process'; -import { mkdtempSync, rmSync } from 'node:fs'; +import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, describe, expect, it, vi } from 'vitest'; @@ -62,7 +62,7 @@ describe('database setup', () => { database.close(); expect(counts).toEqual({ - migrations: 12, + migrations: 16, pieces: 6, shows: 2, usages: 5, @@ -90,4 +90,97 @@ describe('database setup', () => { expect(show.totalDurationSeconds).toBe(5100); expect(show.sections[1].usages.find((usage) => usage.isReserve)?.durationSeconds).toBe(540); }); + + it('upgrades existing completed shows into stable finalizations exactly once', () => { + const directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-upgrade-test-')); + temporaryDirectories.push(directory); + const databasePath = path.join(directory, 'test.sqlite'); + const environment = { + ...process.env, + SENDEWERK_DB_PATH: databasePath, + SENDEWERK_UPLOAD_DIR: path.join(directory, 'uploads') + }; + const database = new Database(databasePath); + database.pragma('foreign_keys = ON'); + database.exec( + 'CREATE TABLE schema_migrations (name TEXT PRIMARY KEY, applied_at_utc TEXT NOT NULL)' + ); + const migrationDirectory = path.resolve('migrations'); + const oldMigrations = readdirSync(migrationDirectory) + .filter( + (name) => + /^\d+.+\.sql$/.test(name) && !name.startsWith('0015_') && !name.startsWith('0016_') + ) + .sort(); + for (const name of oldMigrations) { + database.exec(readFileSync(path.join(migrationDirectory, name), 'utf8')); + database + .prepare('INSERT INTO schema_migrations (name, applied_at_utc) VALUES (?, ?)') + .run(name, new Date().toISOString()); + } + database.close(); + execFileSync(process.execPath, ['scripts/seed.mjs'], { cwd: process.cwd(), env: environment }); + + const legacy = new Database(databasePath); + legacy.pragma('foreign_keys = ON'); + const usage = legacy + .prepare( + `SELECT u.id FROM piece_usages u JOIN show_sections s ON s.id = u.show_section_id + WHERE s.show_id = 'show-october' AND s.is_reserve_bank = 0 LIMIT 1` + ) + .get() as { id: string }; + legacy.prepare("UPDATE piece_usages SET status = 'done' WHERE id = ?").run(usage.id); + legacy.prepare("UPDATE shows SET status = 'completed' WHERE id = 'show-october'").run(); + const completionMigration = '0015_completion_archive_and_shares.sql'; + legacy.exec(readFileSync(path.join(migrationDirectory, completionMigration), 'utf8')); + legacy + .prepare('INSERT INTO schema_migrations (name, applied_at_utc) VALUES (?, ?)') + .run(completionMigration, new Date().toISOString()); + const now = new Date().toISOString(); + legacy + .prepare( + `INSERT INTO users (id, email, email_normalized, display_name, password_hash, role, + password_changed_at_utc, created_at_utc, updated_at_utc) + VALUES ('upgrade-barkeeper', 'upgrade@example.test', 'upgrade@example.test', + 'Upgrade', 'unused', 'barkeeper', ?, ?, ?)` + ) + .run(now, now, now); + legacy + .prepare( + `INSERT INTO show_shares + (id, show_id, token_hash, scope, password_hash, expires_at_utc, revoked_at_utc, + created_by_user_id, created_at_utc) + VALUES ('upgrade-share', 'show-october', 'upgrade-token-hash', 'rundown', NULL, + NULL, NULL, 'upgrade-barkeeper', ?)` + ) + .run(now); + legacy + .prepare( + `INSERT INTO show_share_sessions (token_hash, share_id, created_at_utc, expires_at_utc) + VALUES ('upgrade-session-hash', 'upgrade-share', ?, ?)` + ) + .run(now, new Date(Date.now() + 60_000).toISOString()); + legacy.close(); + + for (let run = 0; run < 2; run += 1) { + execFileSync(process.execPath, ['scripts/migrate.mjs'], { + cwd: process.cwd(), + env: environment + }); + } + const upgraded = new Database(databasePath, { readonly: true }); + expect( + upgraded + .prepare( + `SELECT + (SELECT count(*) FROM show_finalizations WHERE show_id = 'show-october') AS finalizations, + (SELECT count(*) FROM piece_usage_history WHERE show_id = 'show-october') AS history, + (SELECT count(*) FROM show_shares WHERE id = 'upgrade-share') AS shares, + (SELECT count(*) FROM show_share_sessions WHERE token_hash = 'upgrade-session-hash') AS sessions` + ) + .get() + ).toEqual({ finalizations: 1, history: 1, shares: 1, sessions: 1 }); + expect(upgraded.pragma('foreign_key_check')).toEqual([]); + upgraded.close(); + }); }); diff --git a/src/lib/server/live.ts b/src/lib/server/live.ts index 9ce7d41..34e3d0c 100644 --- a/src/lib/server/live.ts +++ b/src/lib/server/live.ts @@ -197,10 +197,7 @@ function showTimerRunning(database: Database.Database, showId: string) { ); } -function elapsedRuns( - runs: { started_at_utc: string; ended_at_utc: string | null }[], - now: string -) { +function elapsedRuns(runs: { started_at_utc: string; ended_at_utc: string | null }[], now: string) { return runs.reduce( (total, run) => total + @@ -236,9 +233,7 @@ function resumeSectionTimerRun(database: Database.Database, showId: string, now: .get(showId) as { id: string } | undefined; if (!timer) return; const running = database - .prepare( - 'SELECT 1 FROM live_section_timer_runs WHERE timer_id = ? AND ended_at_utc IS NULL' - ) + .prepare('SELECT 1 FROM live_section_timer_runs WHERE timer_id = ? AND ended_at_utc IS NULL') .get(timer.id); if (!running) { database @@ -335,6 +330,9 @@ function executeCommand( const show = database.prepare('SELECT status FROM shows WHERE id = ?').get(showId) as { status: string; }; + if (database.prepare('SELECT 1 FROM show_finalizations WHERE show_id = ?').get(showId)) { + throw new Error('SHOW_COMPLETED'); + } if (show.status === 'completed' && !allowCompleted) throw new Error('SHOW_COMPLETED'); if (state.revision !== expectedRevision) throw new Error('CONFLICT'); operation(database, state, now); @@ -643,9 +641,7 @@ export function resetSectionTimer( `SELECT section_id, planned_duration_seconds FROM live_section_timers WHERE show_id = ? AND ended_at_utc IS NULL` ) - .get(showId) as - | { section_id: string; planned_duration_seconds: number } - | undefined; + .get(showId) as { section_id: string; planned_duration_seconds: number } | undefined; if (!timer) throw new Error('NO_SECTION_TIMER'); const running = showTimerRunning(database, showId); closeSectionTimer(database, showId, now); @@ -741,12 +737,10 @@ export function endLive( ) .run(now, showId); database - .prepare( - `UPDATE show_live_states SET ended_at_utc = ? WHERE show_id = ?` - ) + .prepare(`UPDATE show_live_states SET ended_at_utc = ? WHERE show_id = ?`) .run(now, showId); database - .prepare(`UPDATE shows SET status = 'completed', revision = revision + 1, updated_at_utc = ? WHERE id = ?`) + .prepare(`UPDATE shows SET revision = revision + 1, updated_at_utc = ? WHERE id = ?`) .run(now, showId); }, nowValue @@ -766,7 +760,11 @@ export function resumeEndedLive( 'resume-ended', (database, state, now) => { if (!state.ended_at_utc) throw new Error('NOT_ENDED'); - database.prepare(`UPDATE shows SET status = 'live', revision = revision + 1, updated_at_utc = ? WHERE id = ?`).run(now, showId); + database + .prepare( + `UPDATE shows SET status = 'live', revision = revision + 1, updated_at_utc = ? WHERE id = ?` + ) + .run(now, showId); database .prepare('UPDATE show_live_states SET ended_at_utc = NULL WHERE show_id = ?') .run(showId); @@ -815,7 +813,11 @@ export function startNewLiveRun( WHERE show_id = ?` ) .run(now, showId); - database.prepare(`UPDATE shows SET status = 'live', revision = revision + 1, updated_at_utc = ? WHERE id = ?`).run(now, showId); + database + .prepare( + `UPDATE shows SET status = 'live', revision = revision + 1, updated_at_utc = ? WHERE id = ?` + ) + .run(now, showId); openShowTimerRun(database, showId, now); const target = orderedUsages(database, showId).find((usage) => usage.status === 'open'); if (target) activateUsage(database, showId, target.id, now); diff --git a/src/lib/server/shares.spec.ts b/src/lib/server/shares.spec.ts new file mode 100644 index 0000000..3e99d9a --- /dev/null +++ b/src/lib/server/shares.spec.ts @@ -0,0 +1,266 @@ +import { execFileSync } from 'node:child_process'; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import type { AuthUser } from './auth.js'; +import { finalizeShow } from './completion.js'; +import { closeDatabase, getDatabase } from './database.js'; +import { + authenticateShare, + createShare, + deleteShare, + getSharedMedia, + getSharedShow, + revokeShare, + updateShare +} from './shares.js'; +import { addPieceUsage, addShowSection, createShow } from './shows.js'; + +let directory: string; +const actor: AuthUser = { + id: 'share-actor', + email: 'share@example.test', + displayName: 'Freigabe Test', + role: 'barkeeper', + lockedAtUtc: null +}; + +beforeEach(() => { + directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-shares-test-')); + process.env.SENDEWERK_DB_PATH = path.join(directory, 'test.sqlite'); + process.env.SENDEWERK_UPLOAD_DIR = path.join(directory, 'uploads'); + execFileSync(process.execPath, ['scripts/migrate.mjs'], { cwd: process.cwd(), env: process.env }); + execFileSync(process.execPath, ['scripts/seed.mjs'], { cwd: process.cwd(), env: process.env }); + const now = new Date().toISOString(); + getDatabase() + .prepare( + `INSERT INTO users (id, email, email_normalized, display_name, password_hash, role, + password_changed_at_utc, created_at_utc, updated_at_utc) + VALUES (?, ?, ?, ?, 'unused', 'barkeeper', ?, ?, ?)` + ) + .run(actor.id, actor.email, actor.email, actor.displayName, now, now, now); +}); + +afterEach(() => { + closeDatabase(); + delete process.env.SENDEWERK_DB_PATH; + delete process.env.SENDEWERK_UPLOAD_DIR; + rmSync(directory, { recursive: true, force: true }); +}); + +function finalizedShow() { + const created = createShow({ + title: 'Freigegebene Sendung', + startsAtUtc: null, + templateId: null + }); + const section = addShowSection(created.id, created.revision, { + name: 'Freigabeblock', + categoryId: null, + explicitDurationSeconds: null + }); + const usage = addPieceUsage(created.id, section.revision, { + pieceId: 'piece-console', + sectionId: section.id + }); + const reserveUsage = addPieceUsage(created.id, usage.revision, { + pieceId: 'piece-console', + sectionId: null + }); + const database = getDatabase(); + database.prepare("UPDATE piece_usages SET status = 'done' WHERE id = ?").run(usage.id); + database + .prepare( + `INSERT INTO show_live_states + (show_id, revision, started_at_utc, ended_at_utc, presentation_revision, updated_at_utc) + VALUES (?, 0, ?, ?, 0, ?)` + ) + .run( + created.id, + '2026-10-25T18:00:00.000Z', + '2026-10-25T19:00:00.000Z', + '2026-10-25T19:00:00.000Z' + ); + finalizeShow({ + showId: created.id, + commandId: `finalize-${created.id}`, + expectedShowRevision: reserveUsage.revision, + expectedLiveRevision: 0, + archivePieceIds: [], + actor + }); + return { showId: created.id, usageId: usage.id, reserveUsageId: reserveUsage.id }; +} + +describe('show shares', () => { + it('shares an unfinished show and keeps its current plan visible', async () => { + const created = createShow({ + title: 'Offene Freigabe', + startsAtUtc: null, + templateId: null + }); + const shared = await createShare(actor, { + showId: created.id, + scope: 'rundown', + password: '', + expiresAtUtc: null + }); + const initiallyEmpty = getSharedShow(shared.token); + expect(initiallyEmpty.status).toBe('authorized'); + if (initiallyEmpty.status !== 'authorized') throw new Error('share unavailable'); + expect(initiallyEmpty.share.isCompleted).toBe(false); + expect(initiallyEmpty.share.sections).toEqual([]); + + const section = addShowSection(created.id, created.revision, { + name: 'Nachträglich geplant', + categoryId: null, + explicitDurationSeconds: null + }); + addPieceUsage(created.id, section.revision, { + pieceId: 'piece-console', + sectionId: section.id + }); + const updatedPlan = getSharedShow(shared.token); + expect(updatedPlan.status).toBe('authorized'); + if (updatedPlan.status !== 'authorized') throw new Error('share unavailable'); + expect(updatedPlan.share.sections[0]).toMatchObject({ + name: 'Nachträglich geplant', + usages: [{ title: 'Die kleine Konsole mit dem großen Dreh' }] + }); + + await updateShare(actor, shared.id, { + scope: 'moderation', + passwordMode: 'replace', + password: 'neues-freigabe-passwort', + expiresAtUtc: null + }); + expect(getSharedShow(shared.token).status).toBe('password'); + const session = await authenticateShare(shared.token, 'neues-freigabe-passwort', '192.0.2.20'); + expect(getSharedShow(shared.token, session.token).status).toBe('authorized'); + deleteShare(actor, shared.id, created.id); + expect(getSharedShow(shared.token, session.token).status).toBe('unavailable'); + }); + + it('projects exactly the selected information scope', async () => { + const prepared = finalizedShow(); + const rundown = await createShare(actor, { + showId: prepared.showId, + scope: 'rundown', + password: '', + expiresAtUtc: null + }); + const basic = getSharedShow(rundown.token); + expect(basic.status).toBe('authorized'); + if (basic.status !== 'authorized') throw new Error('share unavailable'); + const basicUsage = basic.share.sections[0].usages[0]; + expect(basicUsage).not.toHaveProperty('moderationHtml'); + expect(basicUsage).not.toHaveProperty('researchHtml'); + expect(JSON.stringify(basic)).not.toContain('originalPieceId'); + expect(JSON.stringify(basic)).not.toContain(prepared.showId); + getDatabase() + .prepare( + `INSERT INTO usage_tool_snapshots + (id, usage_id, original_tool_id, name, start_url, work_url, note, position) + VALUES ('private-share-tool', ?, NULL, 'Schnittwerkzeug', + 'https://private.example/start', 'https://private.example/work?secret=1', + 'Freigegebene Werkzeugnotiz', 0)` + ) + .run(prepared.usageId); + + const research = await createShare(actor, { + showId: prepared.showId, + scope: 'research', + password: '', + expiresAtUtc: null + }); + const full = getSharedShow(research.token); + expect(full.status).toBe('authorized'); + if (full.status !== 'authorized') throw new Error('share unavailable'); + expect(full.share.sections[0].usages[0]).toHaveProperty('moderationHtml'); + expect(full.share.sections[0].usages[0]).toHaveProperty('researchHtml'); + expect(full.share.sections[0].usages[0]).not.toHaveProperty('ownNotesHtml'); + expect(JSON.stringify(full)).not.toContain('private.example'); + expect(JSON.stringify(full)).toContain('Freigegebene Werkzeugnotiz'); + }); + + it('enforces password, expiry and revocation for page and direct media access', async () => { + const prepared = finalizedShow(); + const database = getDatabase(); + mkdirSync(process.env.SENDEWERK_UPLOAD_DIR!, { recursive: true }); + writeFileSync(path.join(process.env.SENDEWERK_UPLOAD_DIR!, 'shared.png'), Buffer.from('image')); + database + .prepare( + `INSERT INTO piece_media + (id, piece_id, storage_name, original_name, mime_type, byte_size, alt_text, position, created_at_utc) + VALUES ('share-media', 'piece-console', 'shared.png', 'shared.png', 'image/png', 5, 'Freigegeben', 99, ?)` + ) + .run(new Date().toISOString()); + database + .prepare( + `INSERT INTO usage_media_snapshots + (id, usage_id, original_media_id, alt_text, position, is_presentation) + VALUES ('share-snapshot', ?, 'share-media', 'Freigegeben', 99, 1)` + ) + .run(prepared.usageId); + database + .prepare( + `INSERT INTO usage_media_snapshots + (id, usage_id, original_media_id, alt_text, position, is_presentation) + VALUES ('reserve-share-snapshot', ?, 'share-media', 'Private Reserve', 99, 1)` + ) + .run(prepared.reserveUsageId); + const created = await createShare(actor, { + showId: prepared.showId, + scope: 'research', + password: 'ein-sicheres-passwort', + expiresAtUtc: new Date(Date.now() + 60_000).toISOString() + }); + expect(getSharedShow(created.token).status).toBe('password'); + expect(getSharedShow(created.token)).not.toHaveProperty('share'); + expect(await getSharedMedia(created.token, undefined, 'share-snapshot')).toBeNull(); + await expect(authenticateShare(created.token, 'falsch', '127.0.0.1')).rejects.toThrow( + 'INVALID_PASSWORD' + ); + const session = await authenticateShare(created.token, 'ein-sicheres-passwort', '127.0.0.1'); + expect(getSharedShow(created.token, session.token).status).toBe('authorized'); + expect((await getSharedMedia(created.token, session.token, 'share-snapshot'))?.byteSize).toBe( + 5 + ); + expect(await getSharedMedia(created.token, session.token, 'foreign-media')).toBeNull(); + expect(await getSharedMedia(created.token, session.token, 'reserve-share-snapshot')).toBeNull(); + revokeShare(actor, created.id); + expect(getSharedShow(created.token, session.token).status).toBe('unavailable'); + expect(await getSharedMedia(created.token, session.token, 'share-snapshot')).toBeNull(); + + const expired = await createShare(actor, { + showId: prepared.showId, + scope: 'rundown', + password: '', + expiresAtUtc: new Date(Date.now() + 60_000).toISOString() + }); + database + .prepare('UPDATE show_shares SET expires_at_utc = ? WHERE id = ?') + .run('2000-01-01T00:00:00.000Z', expired.id); + expect(getSharedShow(expired.token).status).toBe('unavailable'); + expect(await getSharedMedia(expired.token, undefined, 'share-snapshot')).toBeNull(); + }); + + it('limits repeated password attempts per share', async () => { + const prepared = finalizedShow(); + const created = await createShare(actor, { + showId: prepared.showId, + scope: 'rundown', + password: 'noch-ein-sicheres-passwort', + expiresAtUtc: null + }); + for (let attempt = 0; attempt < 5; attempt += 1) { + await expect(authenticateShare(created.token, 'falsch', '192.0.2.10')).rejects.toThrow( + 'INVALID_PASSWORD' + ); + } + await expect( + authenticateShare(created.token, 'noch-ein-sicheres-passwort', '192.0.2.10') + ).rejects.toThrow('RATE_LIMITED'); + }); +}); diff --git a/src/lib/server/shares.ts b/src/lib/server/shares.ts new file mode 100644 index 0000000..45b8bff --- /dev/null +++ b/src/lib/server/shares.ts @@ -0,0 +1,430 @@ +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; +import { randomUUID } from 'node:crypto'; +import { + createToken, + hashPassword, + hashToken, + validPassword, + verifyPassword, + type AuthUser +} from './auth.js'; +import { getServerPaths } from './config.js'; +import { getDatabase } from './database.js'; +import { renderMarkdown } from './markdown.js'; +import type { ShowStatus } from './shows.js'; + +export const SHARE_SESSION_COOKIE = 'sendewerk_share_session'; +export const SHARE_SCOPES = ['rundown', 'moderation', 'research'] as const; +export type ShareScope = (typeof SHARE_SCOPES)[number]; +const PASSWORD_ATTEMPT_WINDOW_MS = 15 * 60 * 1000; +const SHARE_PASSWORD_ATTEMPT_LIMIT = 5; +const ADDRESS_PASSWORD_ATTEMPT_LIMIT = 20; + +type ShareRow = { + id: string; + show_id: string; + token_hash: string; + scope: ShareScope; + password_hash: string | null; + expires_at_utc: string | null; + revoked_at_utc: string | null; + created_at_utc: string; +}; + +export type ShareListItem = { + id: string; + showId: string; + showTitle: string; + showStatus: ShowStatus; + showStartsAtUtc: string | null; + scope: ShareScope; + isPasswordProtected: boolean; + expiresAtUtc: string | null; + revokedAtUtc: string | null; + createdAtUtc: string; +}; + +function requireBarkeeperActor(actor: AuthUser): void { + if (actor.role !== 'barkeeper') throw new Error('FORBIDDEN'); +} + +function validExpiry(value: string | null): string | null { + if (!value) return null; + const expiry = new Date(value); + if (Number.isNaN(expiry.valueOf()) || expiry.valueOf() <= Date.now()) { + throw new Error('INVALID_EXPIRY'); + } + return expiry.toISOString(); +} + +function activeShare(rawToken: string): ShareRow | null { + if (!rawToken) return null; + const now = new Date().toISOString(); + return ( + (getDatabase() + .prepare( + `SELECT id, show_id, token_hash, scope, password_hash, expires_at_utc, + revoked_at_utc, created_at_utc FROM show_shares + WHERE token_hash = ? AND revoked_at_utc IS NULL + AND (expires_at_utc IS NULL OR expires_at_utc > ?)` + ) + .get(hashToken(rawToken), now) as ShareRow | undefined) ?? null + ); +} + +export function listShares(actor: AuthUser, showId?: string): ShareListItem[] { + requireBarkeeperActor(actor); + const where = showId ? 'WHERE ss.show_id = ?' : ''; + return ( + getDatabase() + .prepare( + `SELECT ss.id, ss.show_id, sh.title AS show_title, sh.status AS show_status, + sh.starts_at_utc AS show_starts_at_utc, ss.scope, ss.password_hash, + ss.expires_at_utc, ss.revoked_at_utc, ss.created_at_utc + FROM show_shares ss JOIN shows sh ON sh.id = ss.show_id + ${where} + ORDER BY ss.created_at_utc DESC` + ) + .all(...(showId ? [showId] : [])) as { + id: string; + show_id: string; + show_title: string; + show_status: ShowStatus; + show_starts_at_utc: string | null; + scope: ShareScope; + password_hash: string | null; + expires_at_utc: string | null; + revoked_at_utc: string | null; + created_at_utc: string; + }[] + ).map((share) => ({ + id: share.id, + showId: share.show_id, + showTitle: share.show_title, + showStatus: share.show_status, + showStartsAtUtc: share.show_starts_at_utc, + scope: share.scope, + isPasswordProtected: Boolean(share.password_hash), + expiresAtUtc: share.expires_at_utc, + revokedAtUtc: share.revoked_at_utc, + createdAtUtc: share.created_at_utc + })); +} + +export async function createShare( + actor: AuthUser, + input: { showId: string; scope: string; password: string; expiresAtUtc: string | null } +) { + requireBarkeeperActor(actor); + if (!SHARE_SCOPES.includes(input.scope as ShareScope)) throw new Error('INVALID_SCOPE'); + if (input.password && !validPassword(input.password)) throw new Error('INVALID_PASSWORD'); + const expiresAtUtc = validExpiry(input.expiresAtUtc); + const database = getDatabase(); + if (!database.prepare('SELECT 1 FROM shows WHERE id = ?').get(input.showId)) { + throw new Error('NOT_FOUND'); + } + const token = createToken(); + const id = randomUUID(); + const now = new Date().toISOString(); + const passwordHash = input.password ? await hashPassword(input.password) : null; + database + .prepare( + `INSERT INTO show_shares + (id, show_id, token_hash, scope, password_hash, expires_at_utc, + created_by_user_id, created_at_utc) VALUES (?, ?, ?, ?, ?, ?, ?, ?)` + ) + .run( + id, + input.showId, + hashToken(token), + input.scope, + passwordHash, + expiresAtUtc, + actor.id, + now + ); + return { id, token }; +} + +export async function updateShare( + actor: AuthUser, + shareId: string, + input: { + scope: string; + passwordMode: string; + password: string; + expiresAtUtc: string | null; + }, + expectedShowId?: string +): Promise { + requireBarkeeperActor(actor); + if (!SHARE_SCOPES.includes(input.scope as ShareScope)) throw new Error('INVALID_SCOPE'); + if (!['keep', 'remove', 'replace'].includes(input.passwordMode)) { + throw new Error('INVALID_PASSWORD_MODE'); + } + if (input.passwordMode === 'replace' && !validPassword(input.password)) { + throw new Error('INVALID_PASSWORD'); + } + const expiresAtUtc = validExpiry(input.expiresAtUtc); + const existing = getDatabase() + .prepare( + 'SELECT show_id, password_hash FROM show_shares WHERE id = ? AND revoked_at_utc IS NULL' + ) + .get(shareId) as { show_id: string; password_hash: string | null } | undefined; + if (!existing || (expectedShowId && existing.show_id !== expectedShowId)) + throw new Error('NOT_FOUND'); + const passwordHash = + input.passwordMode === 'keep' + ? existing.password_hash + : input.passwordMode === 'replace' + ? await hashPassword(input.password) + : null; + const database = getDatabase(); + database.transaction(() => { + const result = database + .prepare( + `UPDATE show_shares SET scope = ?, password_hash = ?, expires_at_utc = ? + WHERE id = ? AND revoked_at_utc IS NULL` + ) + .run(input.scope, passwordHash, expiresAtUtc, shareId); + if (result.changes !== 1) throw new Error('NOT_FOUND'); + database.prepare('DELETE FROM show_share_sessions WHERE share_id = ?').run(shareId); + })(); +} + +export function deleteShare(actor: AuthUser, shareId: string, expectedShowId?: string): void { + requireBarkeeperActor(actor); + const result = getDatabase() + .prepare(`DELETE FROM show_shares WHERE id = ?${expectedShowId ? ' AND show_id = ?' : ''}`) + .run(...(expectedShowId ? [shareId, expectedShowId] : [shareId])); + if (result.changes !== 1) throw new Error('NOT_FOUND'); +} + +export function revokeShare(actor: AuthUser, shareId: string): void { + requireBarkeeperActor(actor); + const database = getDatabase(); + database.transaction(() => { + const result = database + .prepare('UPDATE show_shares SET revoked_at_utc = ? WHERE id = ? AND revoked_at_utc IS NULL') + .run(new Date().toISOString(), shareId); + if (result.changes !== 1) throw new Error('NOT_FOUND'); + database.prepare('DELETE FROM show_share_sessions WHERE share_id = ?').run(shareId); + })(); +} + +export function shareAccessState(rawToken: string, sessionToken?: string) { + const share = activeShare(rawToken); + if (!share) return { status: 'unavailable' as const }; + if (!share.password_hash) return { status: 'authorized' as const, share }; + if (!sessionToken) return { status: 'password' as const }; + const session = getDatabase() + .prepare( + `SELECT 1 FROM show_share_sessions WHERE token_hash = ? AND share_id = ? + AND expires_at_utc > ?` + ) + .get(hashToken(sessionToken), share.id, new Date().toISOString()); + return session ? { status: 'authorized' as const, share } : { status: 'password' as const }; +} + +export async function authenticateShare(rawToken: string, password: string, address: string) { + const share = activeShare(rawToken); + if (!share?.password_hash) throw new Error('SHARE_UNAVAILABLE'); + const database = getDatabase(); + const now = new Date(); + const since = new Date(now.getTime() - PASSWORD_ATTEMPT_WINDOW_MS).toISOString(); + const shareKey = hashToken(rawToken); + const addressKey = hashToken(address || 'unknown'); + database.prepare('DELETE FROM share_password_attempts WHERE attempted_at_utc < ?').run(since); + const shareAttempts = database + .prepare( + `SELECT count(*) AS count FROM share_password_attempts + WHERE share_key = ? AND attempted_at_utc >= ?` + ) + .get(shareKey, since) as { count: number }; + const addressAttempts = database + .prepare( + `SELECT count(*) AS count FROM share_password_attempts + WHERE address_key = ? AND attempted_at_utc >= ?` + ) + .get(addressKey, since) as { count: number }; + if ( + shareAttempts.count >= SHARE_PASSWORD_ATTEMPT_LIMIT || + addressAttempts.count >= ADDRESS_PASSWORD_ATTEMPT_LIMIT + ) { + throw new Error('RATE_LIMITED'); + } + database + .prepare( + `INSERT INTO share_password_attempts + (id, share_key, address_key, attempted_at_utc) VALUES (?, ?, ?, ?)` + ) + .run(randomUUID(), shareKey, addressKey, now.toISOString()); + if (!(await verifyPassword(share.password_hash, password))) throw new Error('INVALID_PASSWORD'); + const token = createToken(); + const defaultExpiry = now.getTime() + 12 * 60 * 60 * 1000; + const shareExpiry = share.expires_at_utc + ? new Date(share.expires_at_utc).getTime() + : defaultExpiry; + const expires = new Date(Math.min(defaultExpiry, shareExpiry)); + database.prepare('DELETE FROM share_password_attempts WHERE share_key = ?').run(shareKey); + database + .prepare('DELETE FROM show_share_sessions WHERE expires_at_utc <= ?') + .run(now.toISOString()); + database + .prepare( + `INSERT INTO show_share_sessions (token_hash, share_id, created_at_utc, expires_at_utc) + VALUES (?, ?, ?, ?)` + ) + .run(hashToken(token), share.id, now.toISOString(), expires.toISOString()); + return { token, expires }; +} + +export function getSharedShow(rawToken: string, sessionToken?: string) { + const access = shareAccessState(rawToken, sessionToken); + if (access.status !== 'authorized' || !access.share) return access; + const share = access.share; + const database = getDatabase(); + const show = database + .prepare('SELECT title, starts_at_utc, status, completed_at_utc FROM shows WHERE id = ?') + .get(share.show_id) as { + title: string; + starts_at_utc: string | null; + status: ShowStatus; + completed_at_utc: string | null; + }; + const sections = database + .prepare( + `SELECT s.id, s.name, s.category_name_snapshot, s.category_color_snapshot, + c.name AS category_name, c.color AS category_color, + s.explicit_duration_seconds, s.position FROM show_sections s + LEFT JOIN section_categories c ON c.id = s.category_id + WHERE s.show_id = ? AND s.is_reserve_bank = 0 ORDER BY s.position` + ) + .all(share.show_id) as { + id: string; + name: string; + category_name_snapshot: string | null; + category_color_snapshot: string | null; + category_name: string | null; + category_color: string | null; + explicit_duration_seconds: number | null; + position: number; + }[]; + const usageStatement = database.prepare( + `SELECT u.id, u.title_snapshot, u.output_title_snapshot, u.status, u.duration_seconds, + u.intro_snapshot, u.research_snapshot, u.moderation_snapshot, u.interaction_snapshot, + u.transition_snapshot, u.own_notes, + COALESCE((SELECT actual_duration_seconds FROM piece_usage_history WHERE usage_id = u.id), 0) + AS actual_duration_seconds + FROM piece_usages u WHERE u.show_section_id = ? ORDER BY u.position` + ); + const sourceStatement = database.prepare( + 'SELECT label, url, timecode_seconds FROM usage_source_snapshots WHERE usage_id = ? ORDER BY position' + ); + const toolStatement = database.prepare( + `SELECT name, start_url, work_url, note FROM usage_tool_snapshots + WHERE usage_id = ? ORDER BY position` + ); + const mediaStatement = database.prepare( + `SELECT id, alt_text FROM usage_media_snapshots + WHERE usage_id = ? AND is_presentation = 1` + ); + const includesModeration = share.scope === 'moderation' || share.scope === 'research'; + const includesResearch = share.scope === 'research'; + return { + status: 'authorized' as const, + share: { + scope: share.scope, + title: show.title, + startsAtUtc: show.starts_at_utc, + completedAtUtc: show.completed_at_utc, + isCompleted: show.status === 'completed', + sections: sections.map((section) => { + const usages = usageStatement.all(section.id) as { + id: string; + title_snapshot: string; + output_title_snapshot: string; + status: string; + duration_seconds: number; + actual_duration_seconds: number; + intro_snapshot: string; + research_snapshot: string; + moderation_snapshot: string; + interaction_snapshot: string; + transition_snapshot: string; + own_notes: string; + }[]; + return { + name: section.name, + category: + show.status === 'completed' ? section.category_name_snapshot : section.category_name, + categoryColor: + show.status === 'completed' ? section.category_color_snapshot : section.category_color, + explicitDurationSeconds: section.explicit_duration_seconds, + usages: usages.map((usage) => ({ + title: usage.title_snapshot, + outputTitle: usage.output_title_snapshot, + status: usage.status, + durationSeconds: usage.duration_seconds, + actualDurationSeconds: usage.actual_duration_seconds, + media: mediaStatement.get(usage.id) as { id: string; alt_text: string } | undefined, + ...(includesModeration + ? { + introHtml: renderMarkdown(usage.intro_snapshot), + moderationHtml: renderMarkdown(usage.moderation_snapshot), + interactionHtml: renderMarkdown(usage.interaction_snapshot), + transitionHtml: renderMarkdown(usage.transition_snapshot) + } + : {}), + ...(includesResearch + ? { + researchHtml: renderMarkdown(usage.research_snapshot), + sources: sourceStatement.all(usage.id) as { + label: string; + url: string; + timecode_seconds: number | null; + }[], + tools: ( + toolStatement.all(usage.id) as { + name: string; + start_url: string; + work_url: string; + note: string; + }[] + ).map((tool) => ({ name: tool.name, note: tool.note })) + } + : {}) + })) + }; + }) + } + }; +} + +export async function getSharedMedia( + rawToken: string, + sessionToken: string | undefined, + snapshotMediaId: string +) { + const access = shareAccessState(rawToken, sessionToken); + if (access.status !== 'authorized' || !access.share) return null; + const row = getDatabase() + .prepare( + `SELECT pm.storage_name, pm.mime_type, pm.byte_size + FROM usage_media_snapshots ums + JOIN piece_usages u ON u.id = ums.usage_id + JOIN show_sections s ON s.id = u.show_section_id + JOIN piece_media pm ON pm.id = ums.original_media_id + WHERE ums.id = ? AND ums.is_presentation = 1 AND s.show_id = ? + AND s.is_reserve_bank = 0` + ) + .get(snapshotMediaId, access.share.show_id) as + { storage_name: string; mime_type: string; byte_size: number } | undefined; + if (!row) return null; + return { + data: await readFile(path.join(getServerPaths().uploads, row.storage_name)), + mimeType: row.mime_type, + byteSize: row.byte_size + }; +} diff --git a/src/lib/server/shows.spec.ts b/src/lib/server/shows.spec.ts index 1d4b475..132e1bb 100644 --- a/src/lib/server/shows.spec.ts +++ b/src/lib/server/shows.spec.ts @@ -253,7 +253,29 @@ describe('show builder data layer', () => { getDatabase() .prepare("UPDATE piece_usages SET status = 'skipped' WHERE id = ?") .run(skipped.id); - getDatabase().prepare("UPDATE shows SET status = 'completed' WHERE id = ?").run(completed.id); + getDatabase() + .prepare( + "UPDATE shows SET status = 'completed', completed_at_utc = starts_at_utc WHERE id = ?" + ) + .run(completed.id); + getDatabase() + .prepare( + `INSERT INTO show_finalizations + (show_id, command_id, source_revision, finalized_at_utc) + SELECT id, 'test-finalization', revision, starts_at_utc FROM shows WHERE id = ?` + ) + .run(completed.id); + getDatabase() + .prepare( + `INSERT INTO piece_usage_history + (usage_id, show_id, original_piece_id, show_title_snapshot, usage_title_snapshot, + used_at_utc, actual_duration_seconds) + SELECT u.id, sh.id, u.original_piece_id, sh.title, u.title_snapshot, + sh.starts_at_utc, 0 FROM piece_usages u + JOIN show_sections s ON s.id = u.show_section_id + JOIN shows sh ON sh.id = s.show_id WHERE u.id = ?` + ) + .run(done.id); const active = createShow({ title: 'Aktuelle Planung', startsAtUtc: null, templateId: null }); const activeSection = addShowSection(active.id, active.revision, { diff --git a/src/lib/server/shows.ts b/src/lib/server/shows.ts index 223ba66..c64418a 100644 --- a/src/lib/server/shows.ts +++ b/src/lib/server/shows.ts @@ -120,6 +120,7 @@ export type Show = { revision: number; createdAtUtc: string; updatedAtUtc: string; + completedAtUtc: string | null; sections: ShowSection[]; reserve: PieceUsage[]; totalDurationSeconds: number; @@ -193,6 +194,7 @@ type ShowRow = { revision: number; created_at_utc: string; updated_at_utc: string; + completed_at_utc: string | null; }; type SectionRow = { @@ -201,6 +203,8 @@ type SectionRow = { category_id: string | null; category: string | null; category_color: string | null; + category_name_snapshot: string | null; + category_color_snapshot: string | null; position: number; explicit_duration_seconds: number | null; is_reserve_bank: number; @@ -713,7 +717,11 @@ function copyUsageRelations(database: Database.Database, sourceUsageId: string, .run(usageId, sourceUsageId); } -export function listShows(): ShowListItem[] { +export function listShows(statuses?: ShowStatus[]): ShowListItem[] { + const requestedStatuses = statuses?.filter((status) => SHOW_STATUSES.includes(status)); + const where = requestedStatuses?.length + ? `WHERE sh.status IN (${requestedStatuses.map(() => '?').join(', ')})` + : ''; return ( getDatabase() .prepare( @@ -724,10 +732,11 @@ export function listShows(): ShowListItem[] { (SELECT sum(u.duration_seconds) FROM piece_usages u WHERE u.show_section_id = s.id)) ELSE 0 END), 0) AS total_duration_seconds FROM shows sh LEFT JOIN show_sections s ON s.show_id = sh.id + ${where} GROUP BY sh.id ORDER BY sh.starts_at_utc IS NULL, sh.starts_at_utc DESC, sh.created_at_utc DESC` ) - .all() as { + .all(...(requestedStatuses ?? [])) as { id: string; title: string; starts_at_utc: string | null; @@ -781,13 +790,14 @@ export function getShow(id: string): Show | null { const show = database .prepare( `SELECT id, title, starts_at_utc, twitch_title, announcement_text, status, revision, - created_at_utc, updated_at_utc FROM shows WHERE id = ?` + created_at_utc, updated_at_utc, completed_at_utc FROM shows WHERE id = ?` ) .get(id) as ShowRow | undefined; if (!show) return null; const sectionRows = database .prepare( `SELECT s.id, s.name, s.category_id, c.name AS category, c.color AS category_color, + s.category_name_snapshot, s.category_color_snapshot, s.position, s.explicit_duration_seconds, s.is_reserve_bank FROM show_sections s LEFT JOIN section_categories c ON c.id = s.category_id WHERE s.show_id = ? ORDER BY s.position` @@ -821,8 +831,14 @@ export function getShow(id: string): Show | null { id: row.id, name: row.name, categoryId: row.category_id, - category: row.category, - categoryColor: row.category_color, + category: + show.status === 'completed' + ? row.category_name_snapshot + : (row.category_name_snapshot ?? row.category), + categoryColor: + show.status === 'completed' + ? row.category_color_snapshot + : (row.category_color_snapshot ?? row.category_color), position: row.position, explicitDurationSeconds: row.explicit_duration_seconds, durationSeconds: @@ -842,6 +858,7 @@ export function getShow(id: string): Show | null { revision: show.revision, createdAtUtc: show.created_at_utc, updatedAtUtc: show.updated_at_utc, + completedAtUtc: show.completed_at_utc, sections, reserve: reserveRow ? (usagesBySection.get(reserveRow.id) ?? []) : [], totalDurationSeconds: sections.reduce((total, section) => total + section.durationSeconds, 0) @@ -868,14 +885,9 @@ export function getPieceUsageContexts( const database = getDatabase(); const history = database .prepare( - `SELECT u.id AS usage_id, u.original_piece_id, sh.id AS show_id, - sh.title AS show_title, COALESCE(sh.starts_at_utc, sh.updated_at_utc) AS used_at_utc - FROM piece_usages u - JOIN show_sections s ON s.id = u.show_section_id - JOIN shows sh ON sh.id = s.show_id - WHERE u.original_piece_id IS NOT NULL AND u.status = 'done' - AND sh.status = 'completed' AND s.is_reserve_bank = 0 - ORDER BY used_at_utc DESC, u.id DESC` + `SELECT usage_id, original_piece_id, show_id, + show_title_snapshot AS show_title, used_at_utc + FROM piece_usage_history ORDER BY used_at_utc DESC, usage_id DESC` ) .all() as { usage_id: string; @@ -976,6 +988,138 @@ export function createShow(input: CreateShowInput): CreatedMutationResult { return { id, revision: 1, updatedAtUtc: now }; } +export function duplicateCompletedShow( + sourceShowId: string, + commandId: string +): CreatedMutationResult { + if (!commandId || commandId.length > 200) throw new Error('INVALID_COMMAND'); + const database = getDatabase(); + return database.transaction(() => { + const duplicate = database + .prepare( + `SELECT created_show_id FROM show_duplication_commands + WHERE source_show_id = ? AND command_id = ?` + ) + .get(sourceShowId, commandId) as { created_show_id: string } | undefined; + if (duplicate) { + const show = database + .prepare('SELECT revision, updated_at_utc FROM shows WHERE id = ?') + .get(duplicate.created_show_id) as { revision: number; updated_at_utc: string }; + return { + id: duplicate.created_show_id, + revision: show.revision, + updatedAtUtc: show.updated_at_utc + }; + } + const source = database + .prepare( + `SELECT sh.title, sh.twitch_title, sh.announcement_text + FROM shows sh JOIN show_finalizations f ON f.show_id = sh.id WHERE sh.id = ?` + ) + .get(sourceShowId) as + { title: string; twitch_title: string; announcement_text: string } | undefined; + if (!source) throw new Error('NOT_FOUND'); + const id = randomUUID(); + const now = new Date().toISOString(); + const title = `${source.title} (Kopie)`.slice(0, 200); + database + .prepare( + `INSERT INTO shows (id, title, starts_at_utc, twitch_title, announcement_text, + status, revision, created_at_utc, updated_at_utc) + VALUES (?, ?, NULL, ?, ?, 'draft', 1, ?, ?)` + ) + .run(id, title, source.twitch_title, source.announcement_text, now, now); + + const sections = database + .prepare( + `SELECT id, name, category_id, category_name_snapshot, category_color_snapshot, + position, explicit_duration_seconds, is_reserve_bank + FROM show_sections WHERE show_id = ? ORDER BY position` + ) + .all(sourceShowId) as { + id: string; + name: string; + category_id: string | null; + category_name_snapshot: string | null; + category_color_snapshot: string | null; + position: number; + explicit_duration_seconds: number | null; + is_reserve_bank: number; + }[]; + const insertSection = database.prepare( + `INSERT INTO show_sections (id, show_id, name, category_id, category_name_snapshot, + category_color_snapshot, position, explicit_duration_seconds, is_reserve_bank) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)` + ); + const usages = database.prepare( + 'SELECT * FROM piece_usages WHERE show_section_id = ? ORDER BY position' + ); + const insertUsage = database.prepare( + `INSERT INTO piece_usages (id, show_section_id, original_piece_id, position, status, + duration_seconds, title_snapshot, output_title_snapshot, intro_snapshot, research_snapshot, + moderation_snapshot, interaction_snapshot, transition_snapshot, presentation_snapshot, + presentation_mode_snapshot, presentation_source_label_snapshot, + presentation_source_url_snapshot, presentation_source_timecode_snapshot, + presentation_title_meta_snapshot, presentation_description_snapshot, + presentation_image_url_snapshot, presentation_site_name_snapshot, + presentation_provider_snapshot, presentation_provider_id_snapshot, own_notes, is_reserve) + VALUES (?, ?, ?, ?, 'open', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` + ); + for (const section of sections) { + const sectionId = randomUUID(); + insertSection.run( + sectionId, + id, + section.name, + section.category_id, + section.category_name_snapshot, + section.category_color_snapshot, + section.position, + section.explicit_duration_seconds, + section.is_reserve_bank + ); + for (const usage of usages.all(section.id) as (UsageRow & { is_reserve: number })[]) { + const usageId = randomUUID(); + insertUsage.run( + usageId, + sectionId, + usage.original_piece_id, + usage.position, + usage.duration_seconds, + usage.title_snapshot, + usage.output_title_snapshot, + usage.intro_snapshot, + usage.research_snapshot, + usage.moderation_snapshot, + usage.interaction_snapshot, + usage.transition_snapshot, + usage.presentation_snapshot, + usage.presentation_mode_snapshot, + usage.presentation_source_label_snapshot, + usage.presentation_source_url_snapshot, + usage.presentation_source_timecode_snapshot, + usage.presentation_title_meta_snapshot, + usage.presentation_description_snapshot, + usage.presentation_image_url_snapshot, + usage.presentation_site_name_snapshot, + usage.presentation_provider_snapshot, + usage.presentation_provider_id_snapshot, + usage.own_notes, + usage.is_reserve + ); + copyUsageRelations(database, usage.id, usageId); + } + } + database + .prepare( + `INSERT INTO show_duplication_commands + (source_show_id, command_id, created_show_id, created_at_utc) VALUES (?, ?, ?, ?)` + ) + .run(sourceShowId, commandId, id, now); + return { id, revision: 1, updatedAtUtc: now }; + })(); +} + export function saveShowMetadata( showId: string, expectedRevision: number, @@ -1055,7 +1199,8 @@ export function updateShowSection( sectionBelongsToShow(database, showId, sectionId); database .prepare( - `UPDATE show_sections SET name = ?, category_id = ?, explicit_duration_seconds = ? + `UPDATE show_sections SET name = ?, category_id = ?, explicit_duration_seconds = ?, + category_name_snapshot = NULL, category_color_snapshot = NULL WHERE id = ?` ) .run(name, input.categoryId, duration, sectionId); diff --git a/src/lib/server/time.spec.ts b/src/lib/server/time.spec.ts new file mode 100644 index 0000000..73ea2a5 --- /dev/null +++ b/src/lib/server/time.spec.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest'; +import { berlinLocalToUtc } from './time.js'; + +describe('Berlin local time conversion', () => { + it('converts valid civil time and rejects normalized or nonexistent values', () => { + expect(berlinLocalToUtc('2026-10-25T20:00')).toBe('2026-10-25T19:00:00.000Z'); + expect(berlinLocalToUtc('')).toBeNull(); + expect(() => berlinLocalToUtc('2026-02-31T12:00')).toThrow('INVALID_TIMESTAMP'); + expect(() => berlinLocalToUtc('2026-03-29T02:30', 'INVALID_EXPIRY')).toThrow('INVALID_EXPIRY'); + expect(() => berlinLocalToUtc('2026-10-25T02:30', 'INVALID_EXPIRY')).toThrow('INVALID_EXPIRY'); + }); +}); diff --git a/src/lib/server/time.ts b/src/lib/server/time.ts new file mode 100644 index 0000000..3c9078a --- /dev/null +++ b/src/lib/server/time.ts @@ -0,0 +1,44 @@ +export function berlinLocalToUtc(value: string, errorCode = 'INVALID_TIMESTAMP'): string | null { + if (!value) return null; + const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/.exec(value); + if (!match) throw new Error(errorCode); + const parts = match.slice(1).map(Number); + const localMilliseconds = Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4]); + let candidate = localMilliseconds; + const formatter = new Intl.DateTimeFormat('en-CA', { + timeZone: 'Europe/Berlin', + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + hourCycle: 'h23' + }); + const localParts = (date: Date) => { + const formatted = formatter.formatToParts(date); + const read = (type: Intl.DateTimeFormatPartTypes) => + formatted.find((part) => part.type === type)?.value ?? ''; + return { + key: `${read('year')}-${read('month')}-${read('day')}T${read('hour')}:${read('minute')}`, + milliseconds: Date.UTC( + Number(read('year')), + Number(read('month')) - 1, + Number(read('day')), + Number(read('hour')), + Number(read('minute')) + ) + }; + }; + for (let attempt = 0; attempt < 2; attempt += 1) { + candidate += localMilliseconds - localParts(new Date(candidate)).milliseconds; + } + const result = new Date(candidate); + if (localParts(result).key !== value) throw new Error(errorCode); + if ( + localParts(new Date(candidate - 60 * 60 * 1000)).key === value || + localParts(new Date(candidate + 60 * 60 * 1000)).key === value + ) { + throw new Error(errorCode); + } + return result.toISOString(); +} diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index fd46542..2f5d4f2 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,7 +1,11 @@ import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = ({ locals, url }) => ({ - user: url.pathname.startsWith('/heinzlive/') ? null : locals.user, + user: + url.pathname.startsWith('/heinzlive/') || url.pathname.startsWith('/freigabe/') + ? null + : locals.user, presentationMode: url.pathname.startsWith('/heinzlive/'), + guestMode: url.pathname.startsWith('/freigabe/'), liveControllerMode: url.pathname === '/live' }); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d7be73a..def5ada 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -34,6 +34,17 @@ {#if data.presentationMode} {@render children()} +{:else if data.guestMode} +
+
+ Sendewerk +
+ {@render children()} +
{:else if data.user && data.liveControllerMode}
{@render children()}
{:else if data.user} diff --git a/src/routes/archiv/+page.server.ts b/src/routes/archiv/+page.server.ts index 6a076f0..913b695 100644 --- a/src/routes/archiv/+page.server.ts +++ b/src/routes/archiv/+page.server.ts @@ -1,11 +1,15 @@ -import { fail } from '@sveltejs/kit'; +import { fail, redirect } from '@sveltejs/kit'; +import { randomUUID } from 'node:crypto'; import type { Actions, PageServerLoad } from './$types'; import { requireUser } from '#lib/server/auth.js'; import { listPieces, setPieceArchived, setPieceDeleted } from '#lib/server/content.js'; +import { duplicateCompletedShow, listShows } from '#lib/server/shows.js'; export const load: PageServerLoad = ({ locals }) => { requireUser(locals.user); return { + completedShows: listShows(['completed']), + duplicateCommandId: randomUUID(), pieces: listPieces({ statuses: ['archived'] }), deletedPieces: listPieces({ statuses: ['inbox', 'preparing', 'ready', 'archived'], @@ -34,5 +38,19 @@ export const actions = { } catch { return fail(409, { error: 'Das Piece wurde inzwischen geändert. Bitte Seite neu laden.' }); } + }, + duplicateShow: async ({ locals, request }) => { + requireUser(locals.user); + const data = await request.formData(); + try { + const duplicate = duplicateCompletedShow( + String(data.get('showId') || ''), + String(data.get('commandId') || '') + ); + redirect(303, `/sendungen/${duplicate.id}`); + } catch (cause) { + if (cause && typeof cause === 'object' && 'status' in cause) throw cause; + return fail(400, { error: 'Die Sendung konnte nicht dupliziert werden.' }); + } } } satisfies Actions; diff --git a/src/routes/archiv/+page.svelte b/src/routes/archiv/+page.svelte index 68d1d29..b02155e 100644 --- a/src/routes/archiv/+page.svelte +++ b/src/routes/archiv/+page.svelte @@ -1,5 +1,7 @@ @@ -9,10 +11,29 @@

Archiv

-

Abgelegte Pieces; abgeschlossene Sendungen folgen in Etappe 6.

+

Stabile Sendungshistorie und bewusst abgelegte Fundus-Pieces.

{#if form?.error}{/if} {#if form?.success}
{form.success}
{/if} +
+

Abgeschlossene Sendungen

+ {#if data.completedShows.length}
+ {#each data.completedShows as show (show.id)}
+
+

{show.title}

+

+ {show.sectionCount} Abschnitte · {formatDuration(show.totalDurationSeconds)} Planzeit +

+
+
+ + + +
+
{/each} +
{:else}

Noch keine Sendung historisch abgeschlossen.

{/if} +
+

Archivierte Pieces

{#if data.pieces.length}
{#each data.pieces as piece (piece.id)}
diff --git a/src/routes/freigabe/[token]/+page.server.ts b/src/routes/freigabe/[token]/+page.server.ts new file mode 100644 index 0000000..c7fe24c --- /dev/null +++ b/src/routes/freigabe/[token]/+page.server.ts @@ -0,0 +1,31 @@ +import { fail } from '@sveltejs/kit'; +import type { Actions, PageServerLoad } from './$types'; +import { getAuthConfig } from '#lib/server/config.js'; +import { authenticateShare, getSharedShow, SHARE_SESSION_COOKIE } from '#lib/server/shares.js'; + +export const load: PageServerLoad = ({ params, cookies }) => { + return getSharedShow(params.token, cookies.get(SHARE_SESSION_COOKIE)); +}; + +export const actions = { + password: async ({ params, request, cookies, getClientAddress }) => { + const data = await request.formData(); + try { + const session = await authenticateShare( + params.token, + String(data.get('password') || ''), + getClientAddress() + ); + cookies.set(SHARE_SESSION_COOKIE, session.token, { + path: `/freigabe/${params.token}`, + httpOnly: true, + sameSite: 'strict', + secure: getAuthConfig().secureCookies, + expires: session.expires + }); + return { success: true }; + } catch { + return fail(401, { error: 'Passwort falsch oder Freigabe nicht mehr verfügbar.' }); + } + } +} satisfies Actions; diff --git a/src/routes/freigabe/[token]/+page.svelte b/src/routes/freigabe/[token]/+page.svelte new file mode 100644 index 0000000..b688c07 --- /dev/null +++ b/src/routes/freigabe/[token]/+page.svelte @@ -0,0 +1,141 @@ + + +Sendungsfreigabe | Sendewerk + +
+ {#if data.status === 'unavailable'} + + {:else if data.status === 'password'} + + {:else} + +
+ {#each data.share.sections as section (section)} +
+
+
+ {#if section.category}{section.category}{/if} +

{section.name}

+
+ {#if section.explicitDurationSeconds !== null}{formatDuration(section.explicitDurationSeconds)}{/if} +
+ {#each section.usages as usage (usage)} +
+
+
+ {usage.status === 'done' + ? 'Gesendet' + : usage.status === 'skipped' + ? 'Übersprungen' + : 'Offen'} +

{usage.title}

+
+ {formatDuration(usage.actualDurationSeconds || usage.durationSeconds)} +
+ {#if usage.media}{usage.media.alt_text}{/if} + {#if 'introHtml' in usage && usage.introHtml}
+ Einleitung + + +
{@html usage.introHtml}
+
{/if} + {#if 'moderationHtml' in usage && usage.moderationHtml}
+ Moderation + + +
{@html usage.moderationHtml}
+
{/if} + {#if 'interactionHtml' in usage && usage.interactionHtml}
+ Interaktion + + +
{@html usage.interactionHtml}
+
{/if} + {#if 'transitionHtml' in usage && usage.transitionHtml}
+ Überleitung + + +
{@html usage.transitionHtml}
+
{/if} + {#if 'researchHtml' in usage && usage.researchHtml}
+ Recherche + + +
{@html usage.researchHtml}
+
{/if} + {#if 'sources' in usage && usage.sources?.length}
+ Quellen + +
{/if} + {#if 'tools' in usage && usage.tools?.length}
+ Werkzeuge +
    + {#each usage.tools ?? [] as tool (tool)}
  • + {tool.name}{#if tool.note} + · {tool.note}{/if} +
  • {/each} +
+
{/if} +
+ {/each} +
+ {/each} +
+ {/if} +
diff --git a/src/routes/freigabe/[token]/media/[id]/+server.ts b/src/routes/freigabe/[token]/media/[id]/+server.ts new file mode 100644 index 0000000..529f188 --- /dev/null +++ b/src/routes/freigabe/[token]/media/[id]/+server.ts @@ -0,0 +1,18 @@ +import { error } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; +import { getSharedMedia, SHARE_SESSION_COOKIE } from '#lib/server/shares.js'; + +export const GET: RequestHandler = async ({ params, cookies }) => { + const media = await getSharedMedia(params.token, cookies.get(SHARE_SESSION_COOKIE), params.id); + if (!media) error(404, 'Bild nicht gefunden'); + return new Response(media.data, { + headers: { + 'Content-Type': media.mimeType, + 'Content-Length': String(media.byteSize), + 'Cache-Control': 'private, no-store', + 'Content-Security-Policy': "default-src 'none'; sandbox", + 'Cross-Origin-Resource-Policy': 'same-origin', + 'X-Content-Type-Options': 'nosniff' + } + }); +}; diff --git a/src/routes/live/+page.server.ts b/src/routes/live/+page.server.ts index 931b38b..52ae53c 100644 --- a/src/routes/live/+page.server.ts +++ b/src/routes/live/+page.server.ts @@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto'; import { error, fail } from '@sveltejs/kit'; import type { Actions, PageServerLoad } from './$types'; import { requireUser } from '#lib/server/auth.js'; +import { finalizeShow, getCompletionSummary } from '#lib/server/completion.js'; import { completeCurrent, clearPresentationChannel, @@ -74,7 +75,13 @@ export const load: PageServerLoad = ({ locals, url }) => { const showId = url.searchParams.get('show') || listShows()[0]?.id; if (!showId) error(404, 'Keine Sendung vorhanden'); try { - return { live: getLiveView(showId), commandId: randomUUID() }; + const live = getLiveView(showId); + return { + live, + commandId: randomUUID(), + completion: + live.endedAtUtc && live.show.status !== 'completed' ? getCompletionSummary(showId) : null + }; } catch (cause) { if (cause instanceof Error && cause.message === 'NOT_FOUND') error(404, 'Sendung nicht gefunden'); @@ -142,6 +149,20 @@ export const actions = { endLive(text(data, 'showId'), text(data, 'commandId'), Number(data.get('revision'))) ); }, + finalize: async ({ locals, request }) => { + const actor = requireUser(locals.user); + const data = await request.formData(); + return attempt('finalize', () => + finalizeShow({ + showId: text(data, 'showId'), + commandId: text(data, 'commandId'), + expectedShowRevision: Number(data.get('showRevision')), + expectedLiveRevision: Number(data.get('revision')), + archivePieceIds: data.getAll('archivePieceIds').map(String), + actor + }) + ); + }, resumeEnded: async ({ locals, request }) => { requireUser(locals.user); const data = await request.formData(); diff --git a/src/routes/live/+page.svelte b/src/routes/live/+page.svelte index 8f65f6f..a71de84 100644 --- a/src/routes/live/+page.svelte +++ b/src/routes/live/+page.svelte @@ -30,6 +30,7 @@ let railCloseButton = $state(); let liveMenuTrigger = $state(); let liveMenuPopover = $state(); + let completionDialog = $state(); let scrollPositions: Record = {}; let outputWindow: Window | null = null; let allUsages = $derived(data.live.sections.flatMap((section) => section.usages)); @@ -144,6 +145,10 @@ }); }); + $effect(() => { + if (!data.completion && completionDialog?.open) completionDialog.close(); + }); + onMount(() => { const key = `sendewerk-live-${data.live.show.id}`; try { @@ -611,34 +616,48 @@ > {:else if data.live.endedAtUtc} -
- - - - Diese Sendung ist historisch abgeschlossen und unveränderlich.

+ Historische Sendung öffnen -
-
{ - if ( - !confirm( - 'Neuen Durchlauf starten? Fortschritt und Zeitmessungen werden zurückgesetzt.' + {:else} + + + + + +
+
{ + if ( + !confirm( + 'Neuen Durchlauf starten? Fortschritt und Zeitmessungen werden zurückgesetzt.' + ) ) - ) - event.preventDefault(); - }} - > - - - - -
+ + + + + + + {/if} {:else}
{popupMessage}
{/if}
+ + +
+

Unveränderliche Historie

+

Sendung abschließen

+
+

+ Der aktuelle Ablauf wird dauerhaft eingefroren. Übersprungene Pieces zählen nicht als + gesendet. +

+ {#if data.completion} +
+ {data.completion.doneCount} gesendet{data.completion.skippedCount} übersprungen{data.completion.openCount} offen +
+
+ Originale bewusst in den Fundus-Archivbereich legen + {#each data.completion.candidates as candidate (candidate.pieceId)}{/each} + {#if !data.completion.candidates.length}

+ Keine archivierbaren Originale vorhanden. +

{/if} +
+ {/if} + + + + +
+ +
+ +
+
{#if viewedUsage}
- Number(formatted.find((part) => part.type === type)?.value); - const represented = Date.UTC( - read('year'), - read('month') - 1, - read('day'), - read('hour'), - read('minute') - ); - candidate += localMilliseconds - represented; - } - return new Date(candidate).toISOString(); -} +import { berlinLocalToUtc } from '#lib/server/time.js'; export const load: PageServerLoad = ({ locals, url }) => { const user = requireUser(locals.user); @@ -41,7 +11,7 @@ export const load: PageServerLoad = ({ locals, url }) => { const todayMonth = berlinMonthKey(today); const selectedMonth = normalizeMonthKey(url.searchParams.get('month'), todayMonth); return { - shows: listShows(), + shows: listShows(['draft', 'ready', 'live']), templates: listShowTemplates(), view: getShowOverviewView(user.id), selectedMonth, diff --git a/src/routes/sendungen/[id]/+page.server.ts b/src/routes/sendungen/[id]/+page.server.ts index 7fe9a9b..30d1b1e 100644 --- a/src/routes/sendungen/[id]/+page.server.ts +++ b/src/routes/sendungen/[id]/+page.server.ts @@ -1,13 +1,17 @@ import { randomUUID } from 'node:crypto'; -import { error, fail } from '@sveltejs/kit'; +import { error, fail, redirect } from '@sveltejs/kit'; import type { Actions, PageServerLoad } from './$types'; -import { requireUser } from '#lib/server/auth.js'; +import { requireBarkeeper, requireUser } from '#lib/server/auth.js'; +import { getHistoricalActualDurations } from '#lib/server/completion.js'; import { getTaxonomy, listPieces, listPieceTemplates } from '#lib/server/content.js'; +import { createShare, deleteShare, listShares, updateShare } from '#lib/server/shares.js'; +import { berlinLocalToUtc } from '#lib/server/time.js'; import { addPieceUsage, addShowSection, createAndAddPieceUsage, duplicatePieceUsage, + duplicateCompletedShow, getShow, getPieceUsageContexts, movePieceUsage, @@ -35,32 +39,6 @@ function pieceDurationInSeconds(data: FormData) { return Math.round(Number(data.get('durationMinutes')) * 60); } -function berlinLocalToUtc(value: string): string | null { - if (!value) return null; - const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/.exec(value); - if (!match) throw new Error('INVALID_TIMESTAMP'); - const parts = match.slice(1).map(Number); - const localMilliseconds = Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4]); - let candidate = localMilliseconds; - for (let attempt = 0; attempt < 2; attempt += 1) { - const formatted = new Intl.DateTimeFormat('en-CA', { - timeZone: 'Europe/Berlin', - year: 'numeric', - month: '2-digit', - day: '2-digit', - hour: '2-digit', - minute: '2-digit', - hourCycle: 'h23' - }).formatToParts(new Date(candidate)); - const read = (type: Intl.DateTimeFormatPartTypes) => - Number(formatted.find((part) => part.type === type)?.value); - candidate += - localMilliseconds - - Date.UTC(read('year'), read('month') - 1, read('day'), read('hour'), read('minute')); - } - return new Date(candidate).toISOString(); -} - function berlinInput(value: string | null) { if (!value) return ''; const parts = new Intl.DateTimeFormat('en-CA', { @@ -104,6 +82,12 @@ function actionError(cause: unknown) { }; if (code === 'INVALID_DURATION') return { status: 400, error: 'Piece-Dauern müssen zwischen 0 und 1.440 Minuten liegen.' }; + if (code === 'INVALID_PASSWORD') + return { status: 400, error: 'Ein Freigabepasswort benötigt mindestens 12 Zeichen.' }; + if (code === 'INVALID_EXPIRY') + return { status: 400, error: 'Der Ablaufzeitpunkt muss in der Zukunft liegen.' }; + if (code === 'INVALID_SCOPE' || code === 'INVALID_PASSWORD_MODE') + return { status: 400, error: 'Die Einstellungen der Freigabe sind ungültig.' }; if (code.startsWith('INVALID_')) return { status: 400, error: 'Mindestens ein Feld ist ungültig oder zu lang.' }; return { status: 500, error: 'Die Änderung konnte nicht gespeichert werden.' }; @@ -118,9 +102,26 @@ function attempt(intent: string, operation: () => unknown) { } export const load: PageServerLoad = ({ locals, params, url }) => { - requireUser(locals.user); + const actor = requireUser(locals.user); const show = getShow(params.id); if (!show) error(404, 'Sendung nicht gefunden'); + const shares = actor.role === 'barkeeper' ? listShares(actor, show.id) : []; + if (show.status === 'completed') { + return { + show, + shares, + canManageShares: actor.role === 'barkeeper', + historical: true, + actualDurations: getHistoricalActualDurations(show.id), + duplicateCommandId: randomUUID(), + startsAtLocal: berlinInput(show.startsAtUtc), + pieces: [], + taxonomy: getTaxonomy(), + pieceTemplates: [], + createCommandId: randomUUID(), + filters: { query: '', categoryId: '', includeUsed: false, includePlanned: false } + }; + } const pieces = listPieces({ statuses: ['ready'], query: url.searchParams.get('q') ?? '', @@ -132,6 +133,11 @@ export const load: PageServerLoad = ({ locals, params, url }) => { ); return { show, + shares, + canManageShares: actor.role === 'barkeeper', + historical: false, + actualDurations: {} as Record, + duplicateCommandId: randomUUID(), startsAtLocal: berlinInput(show.startsAtUtc), pieces: pieces.map((piece) => ({ ...piece, ...usageContexts[piece.id] })), taxonomy: getTaxonomy(), @@ -147,6 +153,66 @@ export const load: PageServerLoad = ({ locals, params, url }) => { }; export const actions = { + createShare: async ({ locals, params, request }) => { + const actor = requireBarkeeper(locals.user); + const data = await request.formData(); + try { + const share = await createShare(actor, { + showId: params.id, + scope: text(data, 'scope'), + password: text(data, 'password'), + expiresAtUtc: berlinLocalToUtc(text(data, 'expiresAt'), 'INVALID_EXPIRY') + }); + return { + intent: 'createShare', + success: 'Leselink angelegt. Der Link wird nur jetzt vollständig angezeigt.', + shareUrl: `/freigabe/${share.token}` + }; + } catch (cause) { + return fail(actionError(cause).status, { intent: 'createShare', ...actionError(cause) }); + } + }, + updateShare: async ({ locals, params, request }) => { + const actor = requireBarkeeper(locals.user); + const data = await request.formData(); + try { + await updateShare( + actor, + text(data, 'shareId'), + { + scope: text(data, 'scope'), + passwordMode: text(data, 'passwordMode'), + password: text(data, 'password'), + expiresAtUtc: berlinLocalToUtc(text(data, 'expiresAt'), 'INVALID_EXPIRY') + }, + params.id + ); + return { intent: 'updateShare', success: 'Freigabe aktualisiert.' }; + } catch (cause) { + return fail(actionError(cause).status, { intent: 'updateShare', ...actionError(cause) }); + } + }, + deleteShare: async ({ locals, params, request }) => { + const actor = requireBarkeeper(locals.user); + const data = await request.formData(); + try { + deleteShare(actor, text(data, 'shareId'), params.id); + return { intent: 'deleteShare', success: 'Freigabe gelöscht.' }; + } catch (cause) { + return fail(actionError(cause).status, { intent: 'deleteShare', ...actionError(cause) }); + } + }, + duplicateShow: async ({ locals, params, request }) => { + requireUser(locals.user); + const data = await request.formData(); + try { + const duplicate = duplicateCompletedShow(params.id, text(data, 'commandId')); + redirect(303, `/sendungen/${duplicate.id}`); + } catch (cause) { + if (cause && typeof cause === 'object' && 'status' in cause) throw cause; + return fail(actionError(cause).status, { intent: 'duplicateShow', ...actionError(cause) }); + } + }, saveMetadata: async ({ locals, params, request }) => { requireUser(locals.user); const data = await request.formData(); diff --git a/src/routes/sendungen/[id]/+page.svelte b/src/routes/sendungen/[id]/+page.svelte index ebe4b63..dea7457 100644 --- a/src/routes/sendungen/[id]/+page.svelte +++ b/src/routes/sendungen/[id]/+page.svelte @@ -7,6 +7,7 @@ import { onMount, untrack } from 'svelte'; import PieceUsageHistory from '#lib/components/PieceUsageHistory.svelte'; import AppIcon from '#lib/components/AppIcon.svelte'; + import ShareManagement from '#lib/components/ShareManagement.svelte'; import UsageEditorDialog from '#lib/components/UsageEditorDialog.svelte'; import { formatClock, formatDuration } from '#lib/format.js'; import type { PageProps } from './$types'; @@ -20,10 +21,10 @@ | null >(null); let dropTarget = $state(null); - let moveSectionForm: HTMLFormElement; - let moveUsageForm: HTMLFormElement; - let addUsageForm: HTMLFormElement; - let removeUsageForm: HTMLFormElement; + let moveSectionForm = $state(); + let moveUsageForm = $state(); + let addUsageForm = $state(); + let removeUsageForm = $state(); let draggedSectionId = $state(''); let draggedUsageId = $state(''); let draggedPieceId = $state(''); @@ -150,7 +151,7 @@ draggedSectionId = dragged.id; dropPosition = position; finishDrag(); - setTimeout(() => moveSectionForm.requestSubmit()); + setTimeout(() => moveSectionForm?.requestSubmit()); } function dropUsage(sectionId: string | null, position: number) { @@ -160,12 +161,12 @@ if (dragged.kind === 'piece') { draggedPieceId = dragged.id; finishDrag(); - setTimeout(() => addUsageForm.requestSubmit()); + setTimeout(() => addUsageForm?.requestSubmit()); return; } draggedUsageId = dragged.id; finishDrag(); - setTimeout(() => moveUsageForm.requestSubmit()); + setTimeout(() => moveUsageForm?.requestSubmit()); } function removeDraggedUsage() { @@ -176,7 +177,7 @@ } removedUsageId = dragged.id; finishDrag(); - setTimeout(() => removeUsageForm.requestSubmit()); + setTimeout(() => removeUsageForm?.requestSubmit()); } const enhanceCommand: SubmitFunction = ({ formElement, submitter }) => { @@ -205,719 +206,855 @@ {data.show.title} | Sendungen | Sendewerk -
-
-
- Sendungen -

{data.show.title}

-

- {data.show.startsAtUtc ? `${berlinDate(data.show.startsAtUtc)} Uhr` : 'Termin offen'} -

-
-
-
{plannedCount}Pieces
-
- {formatDuration(data.show.totalDurationSeconds)}Planzeit +{#if data.historical} +
+
+
+ Archiv +

Unveränderliche Sendungshistorie

+

{data.show.title}

+

+ {data.show.startsAtUtc ? `${berlinDate(data.show.startsAtUtc)} Uhr` : 'Termin offen'} +

- Live-Steuerung -
-
- - {#if form?.error}{/if} - {#if form?.success}
{form.success}
{/if} -
{copyMessage}
- -
- Sendungsdaten und Twitch-Texte -
- - - - - - -
- {copyMessage} -
-
-
- -
- - -
- -
-
{ - if (dragged?.kind !== 'usage') return; - event.preventDefault(); - dropTarget = 'source'; - }} - ondrop={(event) => { - event.preventDefault(); - removeDraggedUsage(); - }} - > -
+
+
{plannedCount}Pieces
-

Quelle

-

Sendefertiger Fundus

+ {formatDuration(data.show.totalDurationSeconds)}Planzeit
- {visiblePieces.length} -
- {#if dropTarget === 'source'}
- Aus Sendung entfernenDas Fundus-Original bleibt erhalten. -
{/if} -
- Neues Piece direkt einplanen -
- - - - - - + + +
-
-
- - - -
-
- -
-
- {#each visiblePieces as piece (piece.id)} -
-
startDrag('piece', piece.id, event)} - ondragend={finishDrag} - > -
- {piece.categories.map((item) => item.name).join(', ') || 'Offen'}{formatClock(piece.durationSeconds)} -
-

- {#if piece.isFavorite}{/if}{piece.title} -

-

- {piece.format} · {piece.topics.map((item) => item.name).join(', ') || 'Ohne Thema'} -

- {#if piece.isPlannedInCurrentShow}Eingeplant{/if} - - Ziehen oder Ziel auswählen + + {#if form?.error && !form.intent?.toLowerCase().includes('share')}{/if} + {#if data.canManageShares}{/if} +
+ {#each data.show.sections as section (section.id)}
+
+
+ {#if section.category}{section.category}{/if} +

{section.name}

-
- - - -
-
- {/each} -
- {#if !visiblePieces.length}
- -

Keine verfügbaren Pieces

-

- Suchfilter ändern, ausgeblendete Inhalte einblenden oder Pieces im Fundus sendefertig - markieren. -

-
{/if} -
- -
-
-
-

Sendungsplan

-

Ablauf

-
- {formatDuration(data.show.totalDurationSeconds)} -
-
- {#each data.show.sections as section, index (section.id)} - - {/each} - {#if dropPreview?.kind === 'section' && dropPreview.position === data.show.sections.length} -
event.preventDefault()} - ondrop={dropAtPreview} - > - Hier einfügen{draggedTitle()} -
- {/if} + {#if usage.intro}
+ Einleitung +

{usage.intro}

+
{/if} + {#if usage.moderation}
+ Moderation +

{usage.moderation}

+
{/if} + {#if usage.interaction}
+ Interaktion +

{usage.interaction}

+
{/if} + {#if usage.transition}
+ Überleitung +

{usage.transition}

+
{/if} + {#if usage.research}
+ Recherche +

{usage.research}

+
{/if} + {#if usage.ownNotes}
+ Eigene Notizen +

{usage.ownNotes}

+
{/if} +
{/each} + {/each} +
+
+{:else} +
+
+
+ Sendungen +

{data.show.title}

+

+ {data.show.startsAtUtc ? `${berlinDate(data.show.startsAtUtc)} Uhr` : 'Termin offen'} +

+
+
{plannedCount}Pieces
+
+ {formatDuration(data.show.totalDurationSeconds)}Planzeit +
+ Live-Steuerung +
+
+ + {#if form?.error && !form.intent?.toLowerCase().includes('share')}{/if} + {#if form?.success && !form.intent?.toLowerCase().includes('share')}
+ {form.success} +
{/if} +
{copyMessage}
+ +
+ Sendungsdaten und Twitch-Texte
-
+ {#if data.canManageShares}{/if} + +
+ + +
+ +
+
{ + if (dragged?.kind !== 'usage') return; + event.preventDefault(); + dropTarget = 'source'; + }} + ondrop={(event) => { + event.preventDefault(); + removeDraggedUsage(); + }} + > +
+
+

Quelle

+

Sendefertiger Fundus

+
+ {visiblePieces.length} +
+ {#if dropTarget === 'source'}
+ Aus Sendung entfernenDas Fundus-Original bleibt erhalten. +
{/if} +
+ Neues Piece direkt einplanen +
+ + + + + + +
+
+
+ + + +
+
+ + +
+
+ {#each visiblePieces as piece (piece.id)} +
startDrag('usage', usage.id, event)} + ondragstart={(event) => startDrag('piece', piece.id, event)} ondragend={finishDrag} > - {usage.title}{formatClock(usage.durationSeconds)} +
+ {piece.categories.map((item) => item.name).join(', ') || 'Offen'}{formatClock(piece.durationSeconds)} +
+

+ {#if piece.isFavorite}{/if}{piece.title} +

+

+ {piece.format} · {piece.topics.map((item) => item.name).join(', ') || + 'Ohne Thema'} +

+ {#if piece.isPlannedInCurrentShow}Eingeplant{/if} + + Ziehen oder Ziel auswählen
-
+ -
-
- !confirm('Piece aus dieser Sendung entfernen?') && event.preventDefault()} - > - +
- -
-
- {/each} - {#if showsUsagePreview(null, data.show.reserve.length)} -
- {draggedTitle()}Hier in die Reserve einfügen -
- {/if} - {#if !data.show.reserve.length}

- Pieces lassen sich hier parken, ohne die geplante Dauer zu erhöhen. -

{/if} + + {/each} +
+ {#if !visiblePieces.length}
+ +

Keine verfügbaren Pieces

+

+ Suchfilter ändern, ausgeblendete Inhalte einblenden oder Pieces im Fundus sendefertig + markieren. +

+
{/if} - - -
- -
-
- -
-
- -
-
- - -
- +
+
+
+

Sendungsplan

+

Ablauf

+
+ {formatDuration(data.show.totalDurationSeconds)} +
+
+ {#each data.show.sections as section, index (section.id)} + + {/each} + {#if dropPreview?.kind === 'section' && dropPreview.position === data.show.sections.length} +
event.preventDefault()} + ondrop={dropAtPreview} + > + Hier einfügen{draggedTitle()} +
+ {/if} +
+
+ +
+ +
previewUsage(event, null, data.show.reserve.length, false)} + ondrop={dropAtPreview} + > +
+
+ +
+ Reservebank{data.show.reserve.length} + {data.show.reserve.length === 1 ? 'Piece' : 'Pieces'} · zählt nicht zur Planzeit +
+
+
+ {#each data.show.reserve as usage, usageIndex (usage.id)} + + {/each} + {#if showsUsagePreview(null, data.show.reserve.length)} +
+ {draggedTitle()}Hier in die Reserve einfügen +
+ {/if} + {#if !data.show.reserve.length}

+ Pieces lassen sich hier parken, ohne die geplante Dauer zu erhöhen. +

{/if} +
+
+ + +
+ +
+
+ +
+
+ +
+
+ + +
+ +{/if} diff --git a/src/routes/verwaltung/+page.server.ts b/src/routes/verwaltung/+page.server.ts index b7e28f9..39b07e3 100644 --- a/src/routes/verwaltung/+page.server.ts +++ b/src/routes/verwaltung/+page.server.ts @@ -8,6 +8,9 @@ import { type TaxonomyKind } from '#lib/server/content.js'; import { getMailConfig } from '#lib/server/config.js'; +import { createShare, deleteShare, listShares, updateShare } from '#lib/server/shares.js'; +import { listShows } from '#lib/server/shows.js'; +import { berlinLocalToUtc } from '#lib/server/time.js'; import { createTool, listTools, updateTool, type ToolInput } from '#lib/server/tools.js'; import { inviteUser, @@ -28,6 +31,8 @@ export const load: PageServerLoad = ({ locals }) => { users: listUsers(), invitations: listOpenInvitations(), tools: listTools(true), + shows: listShows(), + shares: listShares(locals.user!), mailTransport: getMailConfig().transport }; }; @@ -50,6 +55,10 @@ function actionError(cause: unknown): string { if (code === 'INVALID_TOOL_FORMAT') return 'Mindestens ein gewähltes Format existiert nicht mehr.'; if (code === 'INVALID_TOOL') return 'Bitte die Werkzeugdaten prüfen.'; + if (code === 'INVALID_PASSWORD') return 'Ein Freigabepasswort benötigt mindestens 12 Zeichen.'; + if (code === 'INVALID_EXPIRY') return 'Der Ablaufzeitpunkt muss in der Zukunft liegen.'; + if (code === 'INVALID_SCOPE') return 'Der Informationsumfang ist ungültig.'; + if (code === 'INVALID_PASSWORD_MODE') return 'Die Passwortoption ist ungültig.'; return 'Die Aktion konnte nicht ausgeführt werden.'; } @@ -65,6 +74,50 @@ function toolInput(data: FormData): ToolInput { } export const actions = { + createShare: async ({ locals, request }) => { + const actor = requireBarkeeper(locals.user); + const data = await request.formData(); + try { + const share = await createShare(actor, { + showId: String(data.get('showId') || ''), + scope: String(data.get('scope') || ''), + password: String(data.get('password') || ''), + expiresAtUtc: berlinLocalToUtc(String(data.get('expiresAt') || ''), 'INVALID_EXPIRY') + }); + return { + intent: 'createShare', + success: 'Leselink angelegt. Der Link wird nur jetzt vollständig angezeigt.', + shareUrl: `/freigabe/${share.token}` + }; + } catch (cause) { + return fail(400, { intent: 'createShare', error: actionError(cause) }); + } + }, + updateShare: async ({ locals, request }) => { + const actor = requireBarkeeper(locals.user); + const data = await request.formData(); + try { + await updateShare(actor, String(data.get('shareId') || ''), { + scope: String(data.get('scope') || ''), + passwordMode: String(data.get('passwordMode') || ''), + password: String(data.get('password') || ''), + expiresAtUtc: berlinLocalToUtc(String(data.get('expiresAt') || ''), 'INVALID_EXPIRY') + }); + return { intent: 'updateShare', success: 'Freigabe aktualisiert.' }; + } catch (cause) { + return fail(400, { intent: 'updateShare', error: actionError(cause) }); + } + }, + deleteShare: async ({ locals, request }) => { + const actor = requireBarkeeper(locals.user); + const data = await request.formData(); + try { + deleteShare(actor, String(data.get('shareId') || '')); + return { intent: 'deleteShare', success: 'Freigabe gelöscht.' }; + } catch (cause) { + return fail(400, { intent: 'deleteShare', error: actionError(cause) }); + } + }, invite: async ({ locals, request }) => { const actor = requireBarkeeper(locals.user); const data = await request.formData(); diff --git a/src/routes/verwaltung/+page.svelte b/src/routes/verwaltung/+page.svelte index 20ad8b7..819cd89 100644 --- a/src/routes/verwaltung/+page.svelte +++ b/src/routes/verwaltung/+page.svelte @@ -1,6 +1,7 @@