import { NextResponse } from "next/server"; import { purgeXTrendingAction } from "../../../channels/[slug]/xTrendingActions"; import { ops, opsFail, optBool, optString, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, dryRun: true, topic?, ids? } // -> { ok: true, slug, dryRun: true, posts, ids, files, bytes, blobsFreed, // blobsKept, gone: [{id, at}], agentDropped, skipped: [{id, reason}], // confirm, captureInFlight?, sentence } // POST { slug, confirm, by: "operator", topic?, ids? } // -> the same, dryRun: false: removed // // PURGE THE CAPTURES OF DROPPED POSTS OF AN X TRENDING LIBRARY (lane // posts-library): the screenshot, the record and the media under // `channels//trending/posts-media//` of every post whose judgement // IN FORCE is drop BY THE OPERATOR (its own entry, or its thread's) — in one // `topic`, the whole library, or the `ids` named among them (an id that is // not such a post is named in `skipped`, never removed). AN AGENT'S DROP IS A // SUGGESTION AND IS NEVER PURGED: `agentDropped` counts the posts an agent // dropped that have a capture, left alone. `gone` names the posts to be // removed that are gone from the page: their capture is the only copy, and // `sentence` says that removing it cannot be undone. // // WHAT STAYS: the judgements, the posts, the reads; a post marked later, kept // or undecided; and every media file another post still uses (media is kept // once, by content: `blobsKept`). Addresses remembered as over the size cap // are not forgotten. A later keep captures the post again. // // TWO STEPS, AND THE SECOND IS THE OPERATOR'S. `dryRun: true` removes nothing: // it counts (`posts`, `files`, `bytes`) and answers `confirm`, a digest of the // exact posts. The removal carries that `confirm` and `by: "operator"`; if the // set changed in between (a post dropped or kept, a capture added) it is // refused and nothing is removed — checked again inside the media index's // lock, immediately before the first file goes. An agent's removal is refused: an agent may // ask for the dry run. // // Refused with a sentence: no such channel, not a library, no such topic, a // removal with no `confirm` or not by the operator, a `confirm` that no // longer matches, and A CAPTURE OF THE LIBRARY QUEUED OR RUNNING (it writes // these directories; a dry run then says `captureInFlight: true`). export async function POST(request: Request) { return ops(request, ["slug", "topic", "ids", "dryRun", "confirm", "by"], async (body) => { const dryRun = optBool(body, "dryRun"); const topic = optString(body, "topic"); const confirm = optString(body, "confirm"); const result = await purgeXTrendingAction({ slug: reqSlug(body, "slug"), ...(topic !== undefined ? { topic } : {}), ...(body.ids !== undefined ? { ids: body.ids } : {}), ...(dryRun !== undefined ? { dryRun } : {}), ...(confirm !== undefined ? { confirm } : {}), ...(body.by !== undefined ? { by: body.by } : {}), }); if (!result.ok) return opsFail(result.error); return NextResponse.json(result); }); }