// WHAT A LIBRARY HAS PUBLISHED (lane x-trending-publish, slice 4): // // channels//trending/published.json topic id → {slug, firstAt, // lastAt, posts: {post id → when it was published}, identity?: {ino, // birthtimeMs} of the channel's directory, pending?} // // THE RECORD IS THE LIBRARY'S, AND THE ONLY ONE. The channel a topic is // published into carries no mark of it: nothing in that channel names the // library, a judgement, a note or a watch. "This channel was made by this // publish" is therefore answered from here, and from the channel's own // content (controller/publishXTrending.ts, the three-part proof). // // `slug` is fixed at the first publish: renaming the topic afterwards does // not fork its channel. `posts` is written BEFORE the posts are, so at any // moment the channel holds no post this record does not name. // // A library file like judgements.json: behind every door, never in a build. // ONE WRITER (this module), under the file's lock. SERVER-ONLY (node:fs). import path from "node:path"; import { readJsonFile, withJsonFileLock, writeJsonAtomic } from "../jsonFile-server"; const TRENDING_DIRNAME = "trending"; export const X_TRENDING_PUBLISHED_FILENAME = "published.json"; // WHICH DIRECTORY THE CHANNEL IS (review 1 S2): the channel directory's inode // and birth time, read when this publish made it. A channel deleted and made // again — by hand, of the very same shape, even empty — is another directory, // and is refused. The DIRECTORY, not config.json: a config is rewritten // through a temp file and a rename, so its inode changes with every edit. // Nothing is written into the channel for this. export type XTrendingPublishedIdentity = { ino: number; birthtimeMs: number }; export type XTrendingPublishedTopic = { slug: string; firstAt: string; lastAt: string; posts: Record; identity?: XTrendingPublishedIdentity; // A publish wrote this record and had not yet recorded the directory it // made (it writes the record first): the next publish may adopt a channel // of the slug only while it holds no post. pending?: true; }; type PublishedFile = { version: 1; topics: Record }; export function xTrendingPublishedPath(channelRoot: string): string { return path.join(channelRoot, TRENDING_DIRNAME, X_TRENDING_PUBLISHED_FILENAME); } const str = (v: unknown): v is string => typeof v === "string" && v !== ""; const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; export async function readXTrendingPublished(channelRoot: string): Promise> { const read = await readJsonFile(xTrendingPublishedPath(channelRoot)); const topics = read.ok ? (read.value as { topics?: unknown } | null)?.topics : null; const out: Record = {}; if (!topics || typeof topics !== "object" || Array.isArray(topics)) return out; for (const [id, raw] of Object.entries(topics as Record | null>)) { if (!id || !raw || typeof raw !== "object" || !str(raw.slug) || !SLUG.test(raw.slug) || raw.slug === "..") continue; const posts: Record = {}; if (raw.posts && typeof raw.posts === "object" && !Array.isArray(raw.posts)) { for (const [post, at] of Object.entries(raw.posts)) if (/^\d+$/.test(post) && str(at)) posts[post] = at; } const ident = raw.identity as Partial | undefined; const identity = ident && typeof ident === "object" && Number.isFinite(ident.ino) && Number.isFinite(ident.birthtimeMs) ? { ino: Number(ident.ino), birthtimeMs: Number(ident.birthtimeMs) } : undefined; out[id] = { slug: raw.slug, firstAt: str(raw.firstAt) ? raw.firstAt : "", lastAt: str(raw.lastAt) ? raw.lastAt : "", posts, ...(identity ? { identity } : {}), ...(raw.pending === true ? { pending: true as const } : {}), }; } return out; } // Every write: the file read under its lock, changed, written. export async function mutateXTrendingPublished( channelRoot: string, change: (topics: Record) => { write: boolean; result: T }, ): Promise { return withJsonFileLock(xTrendingPublishedPath(channelRoot), async () => { const topics = await readXTrendingPublished(channelRoot); const { write, result } = change(topics); if (write) await writeJsonAtomic(xTrendingPublishedPath(channelRoot), { version: 1, topics } satisfies PublishedFile, { mkdir: true }); return result; }); }