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.
This commit is contained in:
1 parent
a2b87602b4
commit
d4e4637e7c
13 files changed
+334
-30
No files matched your search
@@ -538,9 +538,12 @@ export async function removeSessionMaterial(sessionId: string): Promise<void> {
|
||||
if (error) throw new ApiError(error.message);
|
||||
}
|
||||
|
||||
export function sessionMaterialUrl(path: string): string {
|
||||
const { data } = supabase.storage.from('materials').getPublicUrl(path);
|
||||
return data.publicUrl;
|
||||
/** Open a session material in a new tab via a short-lived signed URL. The
|
||||
* materials bucket is private; storage RLS always allows admins. */
|
||||
export async function openSessionMaterial(path: string): Promise<void> {
|
||||
const { data, error } = await supabase.storage.from('materials').createSignedUrl(path, 600);
|
||||
if (error) throw new ApiError(error.message);
|
||||
window.open(data.signedUrl, '_blank');
|
||||
}
|
||||
|
||||
async function feedbackSidecarFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
getSessions,
|
||||
getSlots,
|
||||
removeSessionMaterial,
|
||||
sessionMaterialUrl,
|
||||
openSessionMaterial,
|
||||
setSessionSpeakers,
|
||||
uploadSessionMaterial,
|
||||
} from '../lib/api';
|
||||
@@ -573,9 +573,19 @@ function SessionModal({
|
||||
<label>Material</label>
|
||||
{session.material_path ? (
|
||||
<div className="row" style={{ gap: 10 }}>
|
||||
<a className="btn small" href={sessionMaterialUrl(session.material_path)} target="_blank" rel="noreferrer">
|
||||
<button
|
||||
className="btn small"
|
||||
disabled={busy}
|
||||
onClick={async () => {
|
||||
try {
|
||||
await openSessionMaterial(session.material_path!);
|
||||
} catch (e) {
|
||||
onSaved(e instanceof Error ? e.message : 'Could not open material');
|
||||
}
|
||||
}}
|
||||
>
|
||||
📎 View current
|
||||
</a>
|
||||
</button>
|
||||
<button
|
||||
className="btn small danger"
|
||||
disabled={busy}
|
||||
|
||||
@@ -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 && (
|
||||
|
||||
@@ -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). */
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user