feat(sessions): material downloads unlock when the session ends

Private materials bucket + RLS read gate (admins/own speakers anytime, all
attendees after the scheduled slot end, event-local Amsterdam time); always
visible uploaded-state, locked download panel, authenticated client downloads,
admin signed URLs. 12 new assertions in material_time_lock_tests.sql.

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
committed 2026-10-21 18:57:15 +02:00
1 parent 7776145a77
commit a8f3f5b8e1
13 files changed
+334 -30

No files matched your search

+30 -6
View File
@@ -1,12 +1,12 @@
import { useRef } from 'react';
import {
clearPreference,
downloadSessionMaterial,
removeSessionMaterial,
sessionMaterialUrl,
setPreference,
uploadSessionMaterial,
} from '../lib/api';
import { fmtRange, slotStatus } from '../lib/time';
import { fmtRange, fmtTime, slotStatus } from '../lib/time';
import type { Session, Slot } from '../lib/types';
import { useToast } from '../context/ToastContext';
import { Sheet } from './ui';
@@ -100,6 +100,15 @@ export function SessionDetail({
}
}
async function handleDownloadMaterial() {
if (!session?.material_path) return;
try {
await downloadSessionMaterial(session.material_path);
} catch (err) {
toast(err instanceof Error ? err.message : 'Download failed', 'err');
}
}
const range = fmtRange(slot);
// Talk slots are auto-named "Slot 10:15–11:00", which merely repeats the
// time range shown next to it, and service slots are often named exactly
@@ -189,11 +198,26 @@ export function SessionDetail({
{picked ? 'Remove my pick' : 'Pick this session'}
</button>
)}
{/* Material download (all attendees) — pinned to the bottom. */}
{/* Material: its existence is ALWAYS surfaced once uploaded, but the
download stays locked until the session's scheduled end so slides
don't distract mid-session. `status === 'done'` (client) mirrors
the server-side storage RLS rule; speakers bypass the lock for
their own talk. After the end anyone can download — attendance is
not required, only the clock. */}
{session.material_path && (
<a className="btn primary" href={sessionMaterialUrl(session.material_path)} target="_blank" rel="noreferrer">
📎 Download material
</a>
isSpeaker || status === 'done' ? (
<button className="btn primary" data-testid="material-download" onClick={handleDownloadMaterial}>
📎 Download material
</button>
) : (
<div className="material-notice" data-testid="material-locked">
<div className="mn-title">📎 Material uploaded</div>
<div className="muted small">
The speaker has uploaded material for this session.
The download unlocks when the session ends at {fmtTime(slot.end_time)}.
</div>
</div>
)
)}
{/* Speakers can remove their own material to re-upload. */}
{isSpeaker && session.material_path && (
+22 -4
View File
@@ -236,10 +236,28 @@ export async function getMyPointsLog(email: string): Promise<PointsLogRow[]> {
return data as PointsLogRow[];
}
/** Public URL for a session material stored in the `materials` bucket. */
export function sessionMaterialUrl(path: string): string {
const { data } = supabase.storage.from('materials').getPublicUrl(path);
return data.publicUrl;
/** Filename of a material object path (`sessions/<id>/<ts>-<name>`). */
export function materialFilename(path: string): string {
const base = path.slice(path.lastIndexOf('/') + 1);
return base.replace(/^\d+-/, '');
}
/** Download a session material through authenticated storage. The materials
* bucket is private and server-side RLS only releases the file to the
* session's own speakers/admins at any time, and to everyone else once the
* session's scheduled end has passed — so this legitimately fails before the
* end for plain attendees even if the UI clock disagrees. */
export async function downloadSessionMaterial(path: string): Promise<void> {
const { data, error } = await supabase.storage.from('materials').download(path);
if (error) throw new ApiError('Download failed — material unlocks when the session ends.');
const url = URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = materialFilename(path);
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
/** Session ids the signed-in contact is a speaker of (for material upload). */
+2 -2
View File
@@ -3,7 +3,7 @@ import type { Session } from '@supabase/supabase-js';
import { LoaderCircle, Paperclip } from 'lucide-react';
import { supabase } from '../lib/supabase';
import { getSpeakerPortalSessions, speakerPortalSession, type SpeakerSession } from '../lib/speakerPortal';
import { removeSessionMaterial, sessionMaterialUrl, uploadSessionMaterial } from '../lib/api';
import { downloadSessionMaterial, removeSessionMaterial, uploadSessionMaterial } from '../lib/api';
const MAX_FILE_SIZE = 50 * 1024 * 1024;
const FILE_TYPES = '.pdf,.ppt,.pptx,.odp,.zip,.txt';
@@ -110,7 +110,7 @@ export function SpeakerPortal() {
<p className="small muted">{talk.event_name} · {talk.is_active ? 'Active event' : 'Inactive event'}</p>
<h3 className="h2">{talk.title}</h3>
<p className="small">{talk.slot_date} · {talk.start_time.slice(0, 5)} - {talk.end_time.slice(0, 5)}{talk.room && ` · ${talk.room}`}</p>
{talk.material_path ? <a className="speaker-download" href={sessionMaterialUrl(talk.material_path)} target="_blank" rel="noreferrer">Download material</a> : <p className="small muted">No material uploaded</p>}
{talk.material_path ? <button className="speaker-download" onClick={async () => { try { await downloadSessionMaterial(talk.material_path!); } catch (failure) { setError(messageOf(failure)); } }}>Download material</button> : <p className="small muted">No material uploaded</p>}
<label className="speaker-upload" htmlFor={`material-${talk.session_id}`}>
{workingTalk === talk.session_id ? <LoaderCircle aria-hidden="true" className="speaker-upload-spinner" /> : <Paperclip aria-hidden="true" />}
<span>{workingTalk === talk.session_id ? 'Saving material...' : talk.material_path ? 'Replace material' : 'Upload material'}</span>
+1 -1
View File
@@ -7,7 +7,7 @@
.speaker-talk { display: grid; gap: 10px; padding: 18px 0; border-top: 1px solid var(--line); }
.speaker-portal input { width: 100%; min-width: 0; }
.speaker-portal progress { width: 100%; accent-color: var(--accent-2); }
.speaker-download { color: var(--accent-2); }
.speaker-download { color: var(--accent-2); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; }
.speaker-upload {
position: relative;
display: flex;