// PICTURE-ONLY POSTS (umtool/docs/report-video.md, "Picture-only posts"; operator, 5 Nov, walking the Mr. Obvious mid // cut at 26:53: "the "It's time to stop" gif shows while Obvious is speaking rather than it being a picture-only // response like the MW post, we need to handle this because it's a standard type of post"; and, of Kirsche's reply // that is only a picture of another post: "better inserted inline in Kirsche's message, almost as if it's text, // becuase it's treated like such"). // // A post whose words are nothing but @mentions and links -- "@ObviousRises" and a GIF -- says what it says WITH its // picture. Such a post (an item, a post it answers, the post it quotes) is PICTURE-ONLY: // // - its attachment is drawn INSIDE its own card, where its words would be (the card's width, its aspect kept, a // GIF or a video playing), in the thread's column like any post -- never in the side slot, never while another // post's read plays; its mention-only words are drawn small, as the reply-to line X shows; // - in a thread read, a picture-only context post the read does not read gets its own BEAT before the read: // lit and shown alone (a GIF or a video its own length, clamped; an image `image` s), the read starting after // it -- the take is never re-made: the read's start moves (the item's clock gains the beat, as a join's); // - an item that is picture-only and not narrated lasts its beat. // // THE KNOBS: `pictureOnly: true | false` on an item, on `parents[i]`, on `quoted` (absent: the rule above), and the // cut's `render.posts.pictureReply: { on, image, min, max }` (on: the rule at all; the beat's length). // // PURE and dependency-free: the plan, the card, the build, Play, the bench and the writers all read it from here. const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const str = (v) => (typeof v === "string" ? v : ""); const r3 = (v) => Math.round(v * 1000) / 1000; const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v)); /** The cut's rule (`render.posts.pictureReply`): on; an image's beat; a GIF's or a video's own length clamped to [min, max]. */ export const PICTURE_REPLY_DEFAULTS = Object.freeze({ on: true, image: 2, min: 1.5, max: 4, maxSound: 8 }); export const PICTURE_REPLY_LIMITS = Object.freeze({ image: [0.5, 8], min: [0.5, 8], max: [0.5, 12], maxSound: [0.5, 30] }); /** * Whether a picture-only post's attachment SPEAKS: a video with its sound (not `audio: false`, not `mute`) -- its beat * plays it (it is the post's message) and lasts its whole length up to the rule's `maxSound`. A GIF never does. */ export const pictureHasSound = (m) => isObj(m) && m.kind === "video" && m.audio !== false && m.mute !== true; /** A word that is not words: an @mention, a link (a bare domain link too: pic.x.com/…, t.co/…). */ const MENTION_RE = /^[(\[]?@[A-Za-z0-9_.]{1,64}[)\],.:;!?]*$/; const URL_RE = /^(?:https?:\/\/\S+|(?:www\.|pic\.(?:x|twitter)\.com\/|t\.co\/)\S*)$/i; /** Whether `text` has no words but @mentions and links (whitespace aside). An empty text has none. */ export function onlyMentions(text) { return str(text).split(/\s+/).filter(Boolean).every((w) => MENTION_RE.test(w) || URL_RE.test(w)); } /** A post's attachments (an item's `media`, a context post's), the drawable ones. */ export const pictureMediaOf = (post) => (Array.isArray(post?.media) ? post.media.filter((m) => isObj(m) && typeof m.src === "string" && ["image", "gif", "video"].includes(m.kind)) : []); /** * Whether a post -- an item, a context post (a parent, the quoted post) -- is PICTURE-ONLY: its own `pictureOnly` * (true needs an attachment), else its words (its paraphrase, when it has one: then it has words) are only @mentions * and links AND it carries at least one attachment. `text`: the words its card shows (cardText / contextText). * @param {Record | null | undefined} post * @param {{ text?: string }} [o] */ export function isPictureOnly(post, { text = undefined } = {}) { if (!isObj(post)) return false; if (post.pictureOnly === false) return false; if (!pictureMediaOf(post).length) return false; if (post.pictureOnly === true) return true; if (str(post.paraphrase).trim()) return false; return onlyMentions(text === undefined ? str(post.text) : text); } /** The cut's picture-reply rule (`render.posts.pictureReply`), over the defaults. */ export function pictureReplyKnobs(render) { const v = isObj(render?.posts) && isObj(render.posts.pictureReply) ? render.posts.pictureReply : {}; const out = { ...PICTURE_REPLY_DEFAULTS }; if (typeof v.on === "boolean") out.on = v.on; for (const k of ["image", "min", "max", "maxSound"]) if (fin(v[k])) out[k] = v[k]; if (out.max < out.min) out.max = out.min; if (out.maxSound < out.min) out.maxSound = out.min; return out; } /** Every reason `render.posts.pictureReply` cannot be built. */ export function validatePictureReplyRender(render) { const v = isObj(render?.posts) ? render.posts.pictureReply : undefined; if (v === undefined) return []; if (!isObj(v)) return ["render.posts.pictureReply must be { on?, image?, min?, max?, maxSound? } (picture-only posts)"]; const errors = Object.keys(v).filter((k) => !(k in PICTURE_REPLY_DEFAULTS)).map((k) => `render.posts.pictureReply.${k} is not a picture-reply knob (on, image, min, max, maxSound)`); if (v.on !== undefined && typeof v.on !== "boolean") errors.push("render.posts.pictureReply.on must be true or false"); for (const k of ["image", "min", "max", "maxSound"]) { const [lo, hi] = PICTURE_REPLY_LIMITS[k]; if (v[k] !== undefined && !(fin(v[k]) && v[k] >= lo && v[k] <= hi)) errors.push(`render.posts.pictureReply.${k} must be ${lo}–${hi} seconds`); } if (fin(v.min) && fin(v.max) && v.max < v.min) errors.push("render.posts.pictureReply.max must be at least its min"); if (fin(v.min) && fin(v.maxSound) && v.maxSound < v.min) errors.push("render.posts.pictureReply.maxSound must be at least its min"); return errors; } /** A post item's `pictureOnly` knobs' errors (its own, each parent's, the quoted post's). */ export function validatePictureOnly(e) { const w = `timeline ${e?.id ?? "?"}`; const errors = []; const check = (v, where) => { if (v !== undefined && typeof v !== "boolean") errors.push(`${w}.${where} must be true or false (absent: a post of only @mentions and links with an attachment is picture-only)`); }; check(e?.pictureOnly, "pictureOnly"); (Array.isArray(e?.parents) ? e.parents : []).forEach((p, i) => check(p?.pictureOnly, `parents[${i}].pictureOnly`)); if (isObj(e?.quoted)) check(e.quoted.pictureOnly, "quoted.pictureOnly"); return errors; } /** * The item with the CUT's rule laid on: with `render.posts.pictureReply.on` false, every post that does not say * otherwise is not picture-only (`pictureOnly: false` set where it is absent); else the item as it is. */ export function pictureRuleEntry(entry, render) { if (!isObj(entry) || pictureReplyKnobs(render).on) return entry; const off = (p) => (isObj(p) && p.pictureOnly === undefined ? { ...p, pictureOnly: false } : p); return { ...off(entry), ...(Array.isArray(entry.parents) ? { parents: entry.parents.map(off) } : {}), ...(isObj(entry.quoted) ? { quoted: off(entry.quoted) } : {}), }; } /** One attachment's own length (a GIF's pass, a video's trimmed span), else null (an image). */ function ownLength(m) { if (m?.kind !== "gif" && m?.kind !== "video") return null; const len = fin(m.seconds) && m.seconds > 0 ? m.seconds : null; const from = fin(m.start) ? Math.max(0, m.start) : 0; const to = fin(m.end) ? m.end : len; return fin(to) && to > from ? Math.min(to, len ?? to) - from : null; } /** * A picture-only post's BEAT, seconds: its attachments' own lengths (a GIF's pass, a video's span; an image the * rule's `image`), together, clamped to the rule's [min, max] -- [min, maxSound] when one of them has its sound (a * video's punchline is never cut: operator, 5 Nov, the "It's time to stop" meme with its audio). * @param {Array>} media * @param {Record} [render] */ export function pictureBeatSeconds(media, render = undefined) { const k = pictureReplyKnobs(render); const list = (Array.isArray(media) ? media : []).filter(isObj); if (!list.length) return 0; const sum = list.reduce((n, m) => n + (ownLength(m) ?? k.image), 0); return r3(clamp(sum, k.min, list.some(pictureHasSound) ? k.maxSound : k.max)); }