import type { Paths } from "../lib/paths"; import { getSite, listSiteIds, patchSite, type Site } from "../lib/site"; // A DELETED CHANNEL LEAVES NO SITE NAMING IT (lane editor-gaps; 9 Nov: // deleting `mooler` left it in efapalyzer's site.json, fixed by hand). Every // site's pool (`channels`) and cited scope (`citedChannels`) drop the slug, // through the one site writer (writeSite), the way a rename re-points them // (renameChannel.ts, step 5), each as a patchSite under the site's lock. A // site that does not name it is not written. // // Best-effort per site, because the channel is already gone: a site whose // write is refused (its own file no longer valid) is named in `failed` with // the writer's sentence and the others still go through. export type SiteDropResult = { updated: string[]; failed: { siteId: string; error: string }[]; }; export async function dropChannelFromSites(paths: Paths, slug: string): Promise { const result: SiteDropResult = { updated: [], failed: [] }; for (const siteId of [...listSiteIds(paths)].sort()) { try { // A cheap look first, so a site that does not name it is not written; // the edit itself is a patch, read again INSIDE the site's lock, so a // concurrent edit of the site (a Reports-tab pages patch) is not // written over with this copy. if (!namesChannel(getSite(siteId, paths), slug)) continue; const saved = await patchSite( siteId, (current) => ({ channels: current.channels.filter((c) => c.slug !== slug), citedChannels: (current.citedChannels ?? []).filter((c) => c.slug !== slug), }), paths, ); if (saved) result.updated.push(siteId); } catch (err) { result.failed.push({ siteId, error: (err as Error).message }); } } return result; } function namesChannel(site: Site, slug: string): boolean { return ( site.channels.some((c) => c.slug === slug) || (site.citedChannels ?? []).some((c) => c.slug === slug) ); }