// THE AUTHORS' PICTURES of a cut's post items and threads (umtool/docs/ // report-video.md, "Avatars"): the manifest's `postAvatars`, one picture per // handle, used by every card that draws that author -- // // "postAvatars": { "ObviousRises": { "src": "posts-media/_avatars/ObviousRises.jpg", // "source": "archive" | "manual", // "url": "https://pbs.twimg.com/…_400x400.jpg", "capturedAt": "…" } } // // `source: "archive"`: copied in from the posts archive's own store when a post // of that author went in (a channel's `posts-media/_avatars/avatars.json`, // written by the editor's capture-posts); `"manual"`: `umtool posts avatar`, // which an archive copy never replaces. The file is IN the project (never a // link out of it). A REDACTED author never shows one, whatever is here: the // card draws a blurred stand-in (post-item.mjs avatarHtml). // // PURE: the validator, the files the lint checks, and the map a card is drawn // with. The copying is lib/report/postReel.mjs's. /** Where a project keeps the pictures, and where a channel's posts archive keeps its own. */ export const AVATARS_DIR = "posts-media/_avatars"; /** The archive's store, inside a channel's posts-media/ (the editor's capture-posts writes it). */ export const ARCHIVE_AVATARS_DIR = "_avatars"; export const ARCHIVE_AVATARS_FILE = "avatars.json"; export const AVATAR_SOURCES = Object.freeze(["archive", "manual"]); export const AVATAR_EXT_RE = /\.(?:jpe?g|png|webp|gif)$/i; const HANDLE_RE = /^[A-Za-z0-9_.-]{1,64}$/; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); /** A handle as a key: no `@`, compared without regard to case. */ export const handleKey = (h) => String(h ?? "").replace(/^@/, "").toLowerCase(); /** The manifest's entry for `handle` (`[key, value]`, matched without regard to case), or null. */ export function avatarEntryOf(manifest, handle) { const map = isObj(manifest?.postAvatars) ? manifest.postAvatars : {}; const k = handleKey(handle); for (const [h, v] of Object.entries(map)) if (handleKey(h) === k && isObj(v)) return [h, v]; return null; } /** Every reason `postAvatars` cannot be built (the writers' check and the build's lint). */ export function validatePostAvatars(manifest) { const v = manifest?.postAvatars; if (v === undefined) return []; if (!isObj(v)) return ["postAvatars must be { : { src, source } }"]; const errors = []; const seen = new Set(); for (const [h, a] of Object.entries(v)) { const w = `postAvatars.${h}`; if (!HANDLE_RE.test(h.replace(/^@/, ""))) errors.push(`${w}: "${h}" is not a handle`); if (seen.has(handleKey(h))) errors.push(`${w}: a second picture for the same handle (handles are matched without regard to case)`); seen.add(handleKey(h)); if (!isObj(a)) { errors.push(`${w} must be { src, source }`); continue; } if (typeof a.src !== "string" || !a.src.startsWith(`${AVATARS_DIR}/`) || a.src.split("/").includes("..") || !AVATAR_EXT_RE.test(a.src)) { errors.push(`${w}.src must be a picture under ${AVATARS_DIR}/ (jpg, png, webp or gif), got ${JSON.stringify(a.src)}`); } if (a.source !== undefined && !AVATAR_SOURCES.includes(a.source)) errors.push(`${w}.source must be ${AVATAR_SOURCES.join(" or ")}`); if (a.url !== undefined && (typeof a.url !== "string" || !/^https?:\/\//.test(a.url))) errors.push(`${w}.url must be an http(s) link`); } return errors; } /** The files `postAvatars` names, as `{ where, src }` (the lint checks each is there). */ export function postAvatarFiles(manifest) { const map = isObj(manifest?.postAvatars) ? manifest.postAvatars : {}; return Object.entries(map) .filter(([, a]) => isObj(a) && typeof a.src === "string") .map(([h, a]) => ({ where: `postAvatars.${h}`, src: a.src })); } /** `{ : url }` for a card: each picture's `src` through `toUrl` (a file url, a route's). */ export function avatarUrls(manifest, toUrl) { const out = {}; const map = isObj(manifest?.postAvatars) ? manifest.postAvatars : {}; for (const [h, a] of Object.entries(map)) if (isObj(a) && typeof a.src === "string") out[h] = toUrl(a.src); return out; }