// THE CHAT (module `chat`, region `chat`): a stream's live-chat replay in a
// column down one side of the footage, over the clips that ask for it
// (`umtool set
chat=on`; `render.chrome.chat` for the look). The
// messages are the video's own `live_chat.cues.json`, for the clip's SOURCE
// span, timed to the cut through the clip's gaps. chat.mjs holds the rules,
// chrome-chat.mjs writes the page.
import path from "node:path";
import { chatGeometry, chatHtml } from "../chrome-chat.mjs";
import { CHAT_DEFAULTS, CHAT_LIMITS, CHAT_POSITIONS, resolveChat, validateChat } from "../chat.mjs";
import { assertRegion, defineModule } from "./contract.mjs";
import { anonymiseChatClips, anonymousHandlesOf } from "../post-anon.mjs";
export default defineModule({
id: "chat",
version: 1,
z: 48,
kind: "overlay-whole",
slot: "panel",
title: "Live chat",
summary: "A stream's chat replay scrolled in a column beside the footage, over the clips that ask for it.",
configKey: "render.chrome.chat",
defaults: CHAT_DEFAULTS,
validate: (v) => validateChat(v),
resolve: resolveChat,
ui: [
{ key: "position", type: "enum", options: CHAT_POSITIONS, label: "Side", default: CHAT_DEFAULTS.position },
{ key: "lines", type: "number", min: CHAT_LIMITS.lines[0], max: CHAT_LIMITS.lines[1], label: "Messages shown", default: CHAT_DEFAULTS.lines },
{ key: "width", type: "number", min: CHAT_LIMITS.width[0], max: CHAT_LIMITS.width[1], label: "Width (px)", default: CHAT_DEFAULTS.width },
{ key: "maxChars", type: "number", min: CHAT_LIMITS.maxChars[0], max: CHAT_LIMITS.maxChars[1], label: "Characters per message", default: CHAT_DEFAULTS.maxChars },
{ key: "minGap", type: "number", min: CHAT_LIMITS.minGap[0], max: CHAT_LIMITS.minGap[1], step: 0.1, label: "Seconds between messages", default: CHAT_DEFAULTS.minGap },
],
docs: {
heading: "Live chat",
intro: "A stream's chat replay (`live_chat.cues.json`, kept beside a live video's transcript) in a column down one side of the footage box, author and text, newest at the bottom, over the clips with `chat: true` (`render.chrome.chat`, chat.mjs). A clip shows the messages of its own SOURCE span, timed to the cut through its gaps (a message said inside a gap is not shown). A busy chat is thinned to one message per `minGap`; a long message is cut at `maxChars` and clamped to four lines. A clip whose video has no chat file draws nothing and the build log says so -- it never fails the build. The column overlays the footage's edge (it does not reflow the footage); it shares the footage box with the stats and the status ledger, so put them in other corners.",
rows: [
["clip `chat`", "`on` · `off` · default off (a clip only)", "`umtool set chat=on` (`chat=` clears it)"],
["`chrome.chat.position`, `.width`", `\`right\` \`left\` · ${CHAT_LIMITS.width.join("–")} px · default \`right\`, ${CHAT_DEFAULTS.width}`, "`umtool look chat.position=left`"],
["`chrome.chat.lines`, `.maxChars`, `.minGap`", `${CHAT_LIMITS.lines.join("–")} messages · ${CHAT_LIMITS.maxChars.join("–")} characters · ${CHAT_LIMITS.minGap.join("–")} s · default ${CHAT_DEFAULTS.lines}, ${CHAT_DEFAULTS.maxChars}, ${CHAT_DEFAULTS.minGap}`, "`umtool look
chat='{\"lines\":8}'`"],
],
},
region: "chat",
workers: "narrow",
noun: "the chat's",
on: (schedule) => !!schedule?.chat?.clips?.length,
check: (schedule) => (!schedule.chat?.clips?.length
? "the chat region needs a schedule with a clip that asks for it (chat: true) and a live_chat.cues.json with messages in its span"
: null),
async page(ctx) {
const fonts = await ctx.fonts({ regular: "DeckSans", bold: "DeckSansBold" }, { strict: true });
// A chatter whose name is one of the video's anonymous handles (post-anon.mjs, the composer's pass): the shown name.
const anon = anonymousHandlesOf(ctx.manifest);
const clips = anonymiseChatClips(ctx.schedule?.chat?.clips, anon);
const schedule = clips === ctx.schedule?.chat?.clips ? ctx.schedule : { ...ctx.schedule, chat: { ...ctx.schedule.chat, clips } };
return chatHtml(schedule, ctx.render, { fonts, from: ctx.from, duration: ctx.duration });
},
regions(schedule, { render, outDir, frames = null }) {
return [assertRegion({
name: "chat", frames: frames ?? path.join(outDir, "chrome", "chat-frames"),
...chatGeometry(render, { feed: schedule.layout === "feed" }),
})];
},
// A cue interpreter: each state of the column an item, keyed only where it is shown.
frameKeys: { data: "chat-data", omit: ["init", "cues", "window", "total", "ids"] },
});