SPB Git

Repository Public

publication.ts 7.5 KB 149 lines typescript

1/**2 * Publication permissions. This is the single place that decides what a visitor may see.3 * Every page, the search index, the sitemap, structured data and the media routes consume the4 * `CourseView` built here; none of them reads the raw manifest.5 */6import type { Capsule, Manifest, Part, Resource, SectionKey, SectionRef, Session, Visibility } from "./schema";78export type Mode = "production" | "preview";910/** open: content is shown · upcoming: public plan only (« À venir ») · archived: withdrawn. */11export type SessionAccess = "open" | "upcoming" | "archived";1213export function canSee(visibility: Visibility, mode: Mode): boolean {14  if (visibility === "public") return true;15  if (visibility === "preview") return mode === "preview";16  return false;17}1819export function sessionAccess(session: Pick<Session, "publication">, mode: Mode): SessionAccess {20  switch (session.publication) {21    case "published": return "open";22    case "preview": return mode === "preview" ? "open" : "upcoming";23    case "archived": return "archived";24    default: return "upcoming"; // planned and draft never open, in any mode25  }26}2728/** A download is offered only when: the item is visible, downloads are enabled for it, and a licence is recorded. */29export function canDownload(item: { visibility: Visibility; download: boolean; license?: string | null; releaseAfter?: string }, courseLicense: string | null, mode: Mode, now = new Date()): boolean {30  if (!canSee(item.visibility, mode) || !item.download) return false;31  if (!(item.license ?? courseLicense)) return false;32  if (item.releaseAfter && new Date(item.releaseAfter) > now) return false;33  return true;34}3536export interface SessionView {37  id: string;38  number: number;39  slug: string;40  part: string;41  level: Session["level"];42  title: string;43  short: string;44  outcomes: string[];45  dependsOn: string[];46  /** Sessions that list this one as a prerequisite (derived). */47  unlocks: string[];48  tags: string[];49  access: SessionAccess;50  /** True when the session is open only because the preview mode is on. */51  previewOnly: boolean;52  /** True when the only thing the session offers so far is its slide deck: it is not called « published ». */53  slidesOnly: boolean;54  /**55   * True when the session offers its interactive lesson (with or without quiz, glossary, lab) but no video56   * capsule yet. Like a slides-only session it is online, and it is not called « published ».57   */58  lessonOnly: boolean;59  effortMinutes: Session["effortMinutes"];60  sections: Partial<Record<SectionKey, SectionRef>>;61  capsules: (Capsule & { previewOnly: boolean })[];62  resources: (Resource & { downloadable: boolean })[];63  revisions: Session["revisions"];64}6566export interface CourseView {67  mode: Mode;68  course: Manifest["course"];69  author: Manifest["author"];70  parts: Part[];71  sessions: SessionView[];72  diagrams: string[];73  counts: { sessions: number; parts: number; open: number; published: number; capsules: number; quizzes: number; slideDecks: number; lessons: number };74}7576/** French typography for display: a straight apostrophe between two letters becomes a typographic one. */77export const typo = (text: string) => text.replace(/(?<=[\p{L}])'(?=[\p{L}«])/gu, "’");7879export function buildView(manifest: Manifest, mode: Mode, now = new Date()): CourseView {80  const sessions: SessionView[] = [...manifest.sessions]81    .sort((a, b) => a.number - b.number)82    .map((s) => {83      const access = sessionAccess(s, mode);84      const open = access === "open";85      const previewOnly = open && s.publication !== "published";86      const sections: SessionView["sections"] = {};87      if (open) for (const [key, ref] of Object.entries(s.sections) as [SectionKey, SectionRef][]) if (canSee(ref.visibility, mode)) sections[key] = ref;88      const capsules = open ? s.capsules.filter((c) => canSee(c.visibility, mode)) : [];89      const slidesOnly = open && Boolean(sections.slides) && Object.keys(sections).length === 1 && capsules.length === 0;90      return {91        id: s.id, number: s.number, slug: s.slug, part: s.part, level: s.level, title: typo(s.title), short: typo(s.short),92        outcomes: s.outcomes.map(typo), dependsOn: s.dependsOn,93        unlocks: manifest.sessions.filter((o) => o.dependsOn.includes(s.id)).map((o) => o.id).sort(),94        tags: open || manifest.exposeTags ? s.tags : [],95        access, previewOnly, slidesOnly, lessonOnly: open && Boolean(sections.lesson) && capsules.length === 0,96        effortMinutes: open ? s.effortMinutes : null,97        sections,98        capsules: open ? s.capsules.filter((c) => canSee(c.visibility, mode)).sort((a, b) => a.order - b.order).map((c) => ({ ...c, download: canDownload(c, manifest.course.license, mode, now), previewOnly: previewOnly || c.visibility !== "public" })) : [],99        resources: open ? s.resources.filter((r) => r.kind !== "solution" || !r.releaseAfter || new Date(r.releaseAfter) <= now).filter((r) => canSee(r.visibility, mode)).map((r) => ({ ...r, downloadable: canDownload(r, manifest.course.license, mode, now) })) : [],100        revisions: open ? s.revisions : [],101      };102    });103  // Real counts only: a synthetic capsule or a preview-only session is never counted as published material.104  // The rule, stated on the site as well: a session whose slides alone are online, or whose interactive lesson is105  // online while no video capsule is, is counted as such — « diapositives en ligne », « leçon interactive en106  // ligne » — and not as a published session.107  const online = sessions.filter((s) => s.access === "open" && !s.previewOnly), published = online.filter((s) => !s.slidesOnly && !s.lessonOnly);108  return {109    mode,110    course: manifest.course,111    author: manifest.author,112    parts: manifest.parts.map((p) => ({ ...p, title: typo(p.title), short: typo(p.short) })),113    sessions,114    diagrams: Object.entries(manifest.diagrams).filter(([, v]) => canSee(v, mode)).map(([id]) => id),115    counts: {116      sessions: sessions.length,117      parts: manifest.parts.length,118      open: sessions.filter((s) => s.access === "open").length,119      published: published.length,120      capsules: published.reduce((n, s) => n + s.capsules.filter((c) => !c.synthetic && !c.previewOnly).length, 0),121      quizzes: online.filter((s) => s.sections.quiz).length,122      slideDecks: online.filter((s) => s.sections.slides).length,123      lessons: online.filter((s) => s.sections.lesson).length,124    },125  };126}127128/**129 * True when `key` designates media of a capsule the visitor may see: one of the files the capsule names130 * (MP4, poster, captions, transcript, chapters), or anything inside the directory of its HLS master131 * playlist (renditions and segments). Media sitting elsewhere — an earlier or a later encode of the same132 * capsule, a draft, a master — is not reachable, whatever its name.133 */134export function mediaAllowed(view: CourseView, key: string): boolean {135  if (!key || key.endsWith("/")) return false;136  for (const s of view.sessions) for (const c of s.capsules) {137    const m = c.media;138    if (key === m.mp4 || key === m.poster || key === m.transcript || key === m.chapters || m.captions.some((t) => t.path === key)) return true;139    const dir = m.hls ? m.hls.slice(0, m.hls.lastIndexOf("/") + 1) : "";140    if (dir && key.startsWith(dir) && key.length > dir.length) return true;141  }142  return false;143}144145/** True when the file belongs to a visible section of an open session. */146export function contentAllowed(view: CourseView, sessionId: string, key: SectionKey): boolean {147  return Boolean(view.sessions.find((s) => s.id === sessionId)?.sections[key]);148}149
Files main
Menu

Theme