import { NextResponse } from "next/server"; import { judgeXTrendingAction } from "../../../channels/[slug]/xTrendingActions"; import { OpsInputError, ops, opsFail, optBool, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, set: [{id | thread, state, note?}, …], by?, archive? } // -> { ok: true, slug, written: [{id | thread, state}], // skipped: [{id | thread, reason}], kept: [, …], // archive?: {jobId, ids} | {error, ids} } // // JUDGE POSTS OF AN X TRENDING LIBRARY (lane x-trending-2), written to // `channels//trending/judgements.json` by its one writer: // // id a post of the library } one of them // thread a thread's ROOT post id: every post of the thread with no // judgement of its own is taken to be the thread's // state keep | drop | later, or undecided (the judgement is removed: no // entry is undecided) // note up to 2000 characters, kept with the judgement // by "agent" (the default here) or "operator" (the channel page's // buttons). IT IS THE CALLER'S WORD: this route cannot tell who is // asking, so an agent is trusted to say "agent" (the operator's // hand is the page's buttons, and slice 3's judge view). // AN AGENT'S JUDGEMENT IS A SUGGESTION: an agent's write over a // judgement the operator made is skipped for that id and named in // `skipped`; the rest of the ask is written. // // WHICH JUDGEMENT IS IN FORCE, one rule: the operator's entry, for the post or // for its thread, always outranks an agent's about the same post, whatever // the order in time; between two entries of the same hand, the post's own // outranks its thread's. So an agent's post-level write is skipped whenever // the post's thread is the operator's, and an operator's thread judgement // overrides an agent's older entry for one of its posts at once (that entry // stays in the file and decides nothing). // // `kept` names the posts whose judgement BECAME keep by this ask (their own, // or their thread's). A KEEP ARCHIVES THEM: one `capture-posts` job on X's // queue (a screenshot, the context, a reply's thread shot, the media — // `archive.jobId`), unless `archive: false`. When that job is refused (a // stale jar, the X alert, X cooling down) the judgements are written all the // same and `archive.error` says why nothing was started; `capture-posts // {slug, ids, thread: true, keptMedia: true}` archives them later. // DROP DELETES NOTHING: the post, its capture and its media stay, and a // later keep puts it back. // // Refused with a sentence: no such channel, not a library, an ill-shaped set // (an unknown key, both or neither of id and thread, a state that is none of // the four, a note on "undecided", an id judged twice). An id the library // does not hold is not a refusal: it is skipped by name. export async function POST(request: Request) { return ops(request, ["slug", "set", "by", "archive"], async (body) => { const archive = optBool(body, "archive"); const by = body.by === undefined ? "agent" : body.by; if (body.set === undefined) throw new OpsInputError(`"set" is a list of judgements: [{"id" | "thread", "state", "note"?}, …]`); const result = await judgeXTrendingAction({ slug: reqSlug(body, "slug"), by, set: body.set, ...(archive !== undefined ? { archive } : {}), }); if (!result.ok) return opsFail(result.error); return NextResponse.json(result); }); }