// A MEDIA SCENE (umtool/docs/report-video.md, "Media scenes"; operator, 4 Nov, // on the Mr. Obvious cut: "show all these 'jiggle' messages in a way that makes // it obvious he's posted multiple different messages about this same // picture"): CONSECUTIVE post items of a cut that carry the same attachment // are one scene. The attachment is shown ONCE and stays put -- the same // picture in the same box from the scene's first frame to its last, never // pushed in, never crossfaded in again -- and each message about it stacks // in beside it: the one being read in full and lit, the ones before it shrunk // and dimmed above it, the one just read giving way as the next comes in // (the thread read's parent → reply motion, post-thread.mjs, on its knobs). // // WHAT IS "THE SAME ATTACHMENT" is the combo counter's question, answered by // its matcher (post-combo.mjs repeatGroups: X's media id, then the bytes, // then -- `match: "audio"` -- the sound): two items are in one scene when they // are next to each other in the cut and share a repeat group. The planner // takes those groups; it never matches files itself. // // render.posts.scene = { on } the cut's: scenes at all (default on) // timeline[i].scene = false this item is never in a scene (it breaks one) // | "join" this item, with no attachment of its own in // the group, joins the scene beside it (a post // that talks about the picture without carrying it) // // THE ATTACHMENT in the scene is the scene's ANCHOR's -- its first item that // carries it: its file, its crop, its censor (set them there; a later item's // own are not drawn while it is in the scene). A video or a GIF keeps each // item's own in and out point (its read is timed to it); an image is held. // // THE CARD: an item after the first is drawn as a column of tiles (the thread // read's: read-follow.mjs pans it, post-item.mjs draws it) -- the scene's // earlier messages oldest first, then its own (and, narrated, the posts it // answers and quotes, as its thread read draws them). The column rides on // read-follow (on by default): with it off, each card is drawn alone and only // the attachment carries the scene. A thread read's own tile for a post that // is also an earlier message of the scene (the post it answers, just before // it) is drawn once, as the thread's. // // A THREAD SCENE and a media scene on the same item: THE THREAD SCENE WINS. A // conversation drawn across items (lane/thread-tree's post-conversation.mjs // `planContext(timeline, i)` → `{ prev, scene }`) takes the card; the media // scene keeps only the attachment (sceneEntry). The call sites pass this // module's plan as its own option (`mediaScene`), beside `prev`, so the two // meet only where read-follow.mjs builds the column. // // PURE and dependency-free: the build, Play, the card's route, the bench, the // lint and `umtool posts scenes` read the same plan from here. export const SCENE_DEFAULTS = Object.freeze({ on: true }); /** A post item's own `scene`: false (never in one) or "join" (joins the scene beside it). */ export const SCENE_ITEM_VALUES = Object.freeze([false, "join"]); /** The card fields an earlier message of the scene is drawn from (its tile). */ const CARD_KEYS = ["id", "post", "platform", "author", "handle", "badge", "date", "text", "url", "repostedBy", "parents", "replyingTo", "subjects", "redact", "engagement"]; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const r3 = (v) => Math.round(v * 1000) / 1000; // --------------------------------------------------------------------------- // The knobs. // --------------------------------------------------------------------------- /** The cut's scene knobs, the defaults filled in. */ export function sceneKnobs(render) { const r = isObj(render?.posts) && isObj(render.posts.scene) ? render.posts.scene : {}; return { ...SCENE_DEFAULTS, ...(typeof r.on === "boolean" ? { on: r.on } : {}) }; } /** Every reason `render.posts.scene` cannot be built. */ export function validatePostSceneRender(render) { const v = render?.posts?.scene; if (v === undefined) return []; if (!isObj(v)) return ["render.posts.scene must be { on } (media scenes: consecutive post items with the same attachment)"]; const errors = Object.keys(v).filter((k) => !(k in SCENE_DEFAULTS)).map((k) => `render.posts.scene.${k} is not a scene knob (${Object.keys(SCENE_DEFAULTS).join(", ")})`); if (v.on !== undefined && typeof v.on !== "boolean") errors.push("render.posts.scene.on must be true or false"); return errors; } /** Every reason one post item's `scene` cannot be built. */ export function validatePostScene(e) { if (e?.scene === undefined) return []; return SCENE_ITEM_VALUES.includes(e.scene) ? [] : [`timeline ${e?.id ?? "?"}.scene must be false (never in a media scene) or "join" (joins the scene beside it); absent: in one when it shares the attachment`]; } // --------------------------------------------------------------------------- // THE SCENES of a cut. // --------------------------------------------------------------------------- /** One item's groups: `Map` (the repeat groups it is a member of). */ function groupsById(groups) { const out = new Map(); for (const g of Array.isArray(groups) ? groups : []) { for (const m of Array.isArray(g?.members) ? g.members : []) { if (!out.has(m.id)) out.set(m.id, new Map()); if (!out.get(m.id).has(g.key)) out.get(m.id).set(g.key, m.i); } } return out; } const MEMO = new WeakMap(); /** * THE SCENES, pure and memoised (per timeline, groups and the cut's `on`): * `{ scenes: [{ key, group, at: [timeline indices], ids, anchor: { id, at, * media }, members: [{ id, at, media }] }], byAt: Map }` -- each * a run of at least two neighbouring post items sharing a repeat group (and * the `join` items beside it), with at least one that carries the attachment. * `groups`: post-combo.mjs repeatGroups' (its members' `id` and `i`). * * A run is broken by anything that is not a post item of the group: a clip, a * post with another attachment, `scene: false`. A `join` item after a scene * joins it; one before the first scene of a run joins the scene that follows. * * @param {Array>} timeline * @param {Array<{ key: string, members: Array<{ id: string, i: number }> }>} groups * @param {Record} [render] */ export function mediaScenes(timeline, groups, render = undefined) { const tl = Array.isArray(timeline) ? timeline : []; const on = sceneKnobs(render).on; const memo = MEMO.get(tl); if (memo && memo.groups === groups && memo.on === on) return memo.value; const value = planScenes(tl, groups, on); MEMO.set(tl, { groups, on, value }); return value; } function planScenes(tl, groups, on) { const byAt = new Map(); const scenes = []; if (!on) return { scenes, byAt }; const gOf = groupsById(groups); const order = (Array.isArray(groups) ? groups : []).map((g) => g.key); /** null: breaks a run; "J": joins; else its groups. */ const label = (e) => { if (!isObj(e) || e.type !== "post" || typeof e.id !== "string" || e.scene === false) return null; const own = gOf.get(e.id); if (own?.size) return own; return e.scene === "join" ? "J" : null; }; const labels = tl.map(label); let run = null; let pending = []; const close = () => { if (run && run.at.length >= 2 && run.at.some((k) => labels[k] !== "J")) { const members = run.at.map((k) => ({ id: tl[k].id, at: k, media: labels[k] === "J" ? null : labels[k].get(run.group) })); const a = members.find((m) => m.media !== null); const scene = { key: `s${scenes.length + 1}`, group: run.group, at: [...run.at], ids: members.map((m) => m.id), anchor: { id: a.id, at: a.at, media: a.media }, members }; scenes.push(scene); for (const k of run.at) byAt.set(k, scene); } run = null; }; /** The group a run starting at `k` takes: the one it shares with the next item that carries one, else its first. */ const groupFrom = (k) => { const mine = labels[k]; let j = k + 1; while (j < labels.length && labels[j] === "J") j += 1; const next = labels[j]; const shared = next instanceof Map ? order.filter((g) => mine.has(g) && next.has(g)) : []; return shared[0] ?? order.find((g) => mine.has(g)); }; labels.forEach((lab, k) => { if (lab === null) { close(); pending = []; return; } if (lab === "J") { if (run) run.at.push(k); else pending.push(k); return; } if (run && lab.has(run.group)) { run.at.push(k); return; } close(); run = { group: groupFrom(k), at: [...pending, k] }; pending = []; }); close(); return { scenes, byAt }; } /** The card fields of an item (its tile in a later item's column). */ export const sceneCardOf = (e) => Object.fromEntries(CARD_KEYS.filter((k) => e?.[k] !== undefined).map((k) => [k, e[k]])); /** * ITEM `i`'s SCENE, pure (memoised through mediaScenes): `{ key, n, of, ids, * media, anchor: { id, media, entry: { id, post, media } }, before: [cards] }` * -- `n` its place (0: the first), `media` the index of the shared attachment * in its own media (null: a join item), `before` the earlier messages' cards, * oldest first. Null when the item is in no scene. * * @param {Array>} timeline * @param {number} i * @param {Array>} groups * @param {Record} [render] */ export function mediaSceneOf(timeline, i, groups, render = undefined) { const tl = Array.isArray(timeline) ? timeline : []; const s = mediaScenes(tl, groups, render).byAt.get(i); if (!s) return null; const n = s.at.indexOf(i); const a = tl[s.anchor.at]; return { key: s.key, n, of: s.at.length, ids: s.ids, media: s.members[n].media, anchor: { id: a.id, media: s.anchor.media, entry: { id: a.id, post: a.post, media: a.media } }, before: s.at.slice(0, n).map((k) => sceneCardOf(tl[k])), }; } /** * The scene of the item with id `id` in a timeline (mediaSceneOf by id). * * @param {Array>} timeline * @param {string} id * @param {Array>} groups * @param {Record} [render] */ export function mediaSceneById(timeline, id, groups, render = undefined) { const tl = Array.isArray(timeline) ? timeline : []; const at = tl.findIndex((e) => e?.id === id); return at < 0 ? null : mediaSceneOf(tl, at, groups, render); } // --------------------------------------------------------------------------- // THE ITEM AS THE SCENE PLAYS IT. // --------------------------------------------------------------------------- /** An attachment's timing (its own in a scene: its read is timed to it). */ const TIMING = ["start", "end", "mute", "words", "seconds", "audio"]; /** * ITEM `entry` AS ITS SCENE PLAYS IT, pure: the shared attachment drawn as * the anchor's (its file, size, crop and censor), with the item's own timing * (in and out, sound, words) and never pushed in; a join item gains it as its * first attachment (the anchor's timing). `censor(entry)` lays a channel's * markup on the anchor first (post-censor.mjs censorEntry). The entry itself * when it is in no scene. * * @param {Record} entry * @param {ReturnType} scene * @param {{ censor?: (e: Record) => Record }} [o] */ export function sceneEntry(entry, scene, { censor = null } = {}) { if (!scene || !isObj(entry)) return entry; const anchor = censor ? censor(scene.anchor.entry) : scene.anchor.entry; const am = Array.isArray(anchor?.media) ? anchor.media[scene.anchor.media] : null; if (!isObj(am)) return entry; const look = Object.fromEntries(Object.entries(am).filter(([k]) => !TIMING.includes(k) && k !== "push")); const own = Array.isArray(entry.media) ? entry.media : []; if (scene.media === null) { const { push, ...m } = am; void push; return { ...entry, media: [m, ...own].slice(0, 4) }; } if (!isObj(own[scene.media])) return entry; const media = own.map((m, k) => { if (k !== scene.media) return m; const time = Object.fromEntries(Object.entries(m).filter(([kk]) => TIMING.includes(kk))); return { ...look, ...time }; }); return { ...entry, media }; } // --------------------------------------------------------------------------- // THE COLUMN: the scene's earlier messages as tiles over the card's own. // --------------------------------------------------------------------------- /** The archive id of an item's post (`/` → ``). */ const postIdOf = (e) => String(e?.post ?? "").split("/")[1] ?? ""; /** * THE SCENE's TILES over item `entry`'s card, pure: `{ on, scene, tiles, * roles }` -- the scene's earlier messages (`scene:`, oldest first; the * one just before it `carry`, in full as that item ended, the rest * `context`), then the card's own tiles: `base` (post-thread.mjs threadLayout, * a narrated item's: the posts it answers and quotes as its read draws them) * or, without one, the posts it answers as `context`, its post in full, the * post it quotes. Each scene tile carries its message's card (`entry`). A post * the thread draws (the post it answers, carried) is not drawn twice. `base` * itself when the item is the scene's first (nothing above it), or in none. * * @param {Record | null} base * @param {Record} entry * @param {ReturnType} scene */ export function sceneLayout(base, entry, scene) { if (!scene || scene.n === 0 || !scene.before.length) return base; const parents = (Array.isArray(entry?.parents) ? entry.parents : []).filter(isObj).slice(0, 8); const drawn = new Set(); if (base?.tiles) { for (const t of base.tiles) { const m = /^parent:(\d)$/.exec(t.scope); if (m && t.mode !== "hidden" && parents[Number(m[1])]?.id) drawn.add(String(parents[Number(m[1])].id)); } } const baseCarries = !!base?.tiles?.some((t) => t.mode === "carry"); const scenes = []; scene.before.forEach((card0, j) => { if (drawn.has(postIdOf(card0))) return; // A message answering the one drawn just above it says so by its place: no "Replying to" line (as its own // thread read drew it, the post it answers a tile over it). const above = j > 0 ? postIdOf(scene.before[j - 1]) : ""; const answers = (Array.isArray(card0.parents) ? card0.parents : [])[0]?.id; const card = above && answers !== undefined && String(answers) === above ? (({ parents, replyingTo, ...rest }) => (void parents, void replyingTo, rest))(card0) : card0; scenes.push({ scope: `scene:${j}`, mode: "context", read: false, entry: card }); }); if (!baseCarries && scenes.length && scene.before.length - 1 === Number(scenes[scenes.length - 1].scope.slice(6))) scenes[scenes.length - 1].mode = "carry"; let own; if (base?.tiles) own = base.tiles; else { // Un-narrated: the posts it answers small and dim above its words (an earlier message carried is drawn once). const carried = new Set(scene.before.map(postIdOf)); own = []; for (let i = parents.length - 1; i >= 0; i -= 1) { const p = parents[i]; const words = typeof p.paraphrase === "string" && p.paraphrase.trim() ? p.paraphrase : typeof p.text === "string" ? p.text : ""; if (!words.trim() || carried.has(String(p.id ?? ""))) continue; own.push({ scope: `parent:${i}`, mode: "context", read: false }); } own.push({ scope: "post", mode: "full", read: true }); if (isObj(entry?.quoted) && String(entry.quoted.paraphrase ?? entry.quoted.text ?? "").trim()) own.push({ scope: "quoted", mode: "full", read: false }); } if (!scenes.length) return base; return { on: true, scene: scene.key, tiles: [...scenes, ...own], roles: base?.roles ?? [] }; } /** * THE SCHEDULE of an UN-NARRATED item's column (a narrated one's is the thread * read's, threadSchedule, over sceneLayout's tiles), pure, in the thread's * shape (read-follow.mjs pans it, Play and the build draw it): the message * before it in full as that item ended, shrinking and dimming (`knobs`: the * thread read's shrink, dim, seconds) as its words come in under it -- from * its first block's start, less `lead` -- the older ones small and dim. * * @param {{ shrink: number, dim: number, seconds: number, pan: number, reveal?: boolean }} knobs * @param {{ layout: Record, blocks: Array<{ start: number | null }>, lead?: number }} o */ export function sceneSchedule(knobs, { layout, blocks, lead = 0.4 }) { if (!layout?.on) return null; const starts = (blocks ?? []).map((b) => b.start).filter(fin); const T = starts.length ? Math.min(...starts) : 0; const t0 = r3(Math.max(0, T - lead)); const win = { t0, t1: r3(t0 + knobs.seconds) }; const small = [knobs.shrink, knobs.dim]; const tiles = []; for (const t of layout.tiles) { if (t.mode === "hidden") continue; const row = { scope: t.scope, mode: t.mode, read: !!t.read, from: [1, 1], to: [1, 1], t0: 0, t1: 0 }; if (t.mode === "context") { row.from = t.scope === "quoted" ? [1, knobs.dim] : small; row.to = row.from; } else if (t.mode === "carry") Object.assign(row, { to: small, ...win }); else if (t.scope === "post" && knobs.reveal !== false) Object.assign(row, { from: [1, 0], to: [1, 1], ...win }); tiles.push(row); } return { shrink: knobs.shrink, dim: knobs.dim, seconds: knobs.seconds, pan: knobs.pan, contextRoom: 0.3, tiles }; } /** A schedule's rows with the scene's messages on them (`entry`: what a `scene:` tile draws), and the scene's key. */ export function sceneRows(thread, layout) { if (!thread || !layout?.scene) return thread; const cards = new Map(layout.tiles.filter((t) => t.entry).map((t) => [t.scope, t.entry])); return { ...thread, scene: layout.scene, tiles: thread.tiles.map((r) => (cards.has(r.scope) ? { ...r, entry: cards.get(r.scope) } : r)) }; } /** The scene's lines for a person (`umtool posts scenes`): each scene, its items, the anchor. */ export function sceneLines(scenes, timeline) { const tl = Array.isArray(timeline) ? timeline : []; return scenes.map((s) => { const items = s.members.map((m) => `${m.id}${m.media === null ? " (joins)" : ""}${m.id === s.anchor.id ? " (anchor)" : ""}`); const a = tl[s.anchor.at]?.media?.[s.anchor.media]; return `${s.key}: ${items.join(" → ")}${a?.src ? ` -- ${a.src}` : ""}`; }); }