import { askEditor } from "@/lib/articles/generation"; import { listAccounts } from "@/lib/library/client.mjs"; import { unavailableSentence } from "@/lib/library/load"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; export const dynamic = "force-dynamic"; // GET /api/library/accounts?lib=&topic= -> { ok: true, accounts: [{handle, name?, posts, kept, keptByOperator, // topics, last, archivedAs?}], total, posts, more } or { ok: false, sentence } // // THE LIBRARY'S ACCOUNTS (lane x-trending-publish), read THROUGH THE EDITOR // (GET /api/ops/trending?accounts=1) when the page's Accounts panel is // opened. READ ONLY, and a private library's: a cross-site request is // refused, and nothing caches it. const PRIVATE = { "cache-control": "private, no-store", "cross-origin-resource-policy": "same-origin" }; const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; export async function GET(request: Request) { if (crossSiteRefusal(request.headers)) return Response.json({ error: "refused: the library is read by umtool's own pages only" }, { status: 403, headers: PRIVATE }); const sp = new URL(request.url).searchParams; const lib = sp.get("lib") ?? ""; const topic = sp.get("topic") ?? ""; if (!SLUG.test(lib)) return Response.json({ ok: false, sentence: `"lib" is the library's slug` }, { status: 400, headers: PRIVATE }); try { const ask = (route: string) => askEditor>(route, { fresh: true }); const answer = await listAccounts(ask, { slug: lib, ...(topic ? { topic } : {}), limit: 200 }); return Response.json({ ok: true, accounts: answer.accounts ?? [], total: answer.total ?? 0, posts: answer.posts ?? 0, more: answer.more ?? 0 }, { headers: PRIVATE }); } catch (err) { return Response.json({ ok: false, ...unavailableSentence(err) }, { headers: PRIVATE }); } }