import { NextResponse } from "next/server"; import { organiseXTrendingAction } from "../../../channels/[slug]/xTrendingActions"; import { oneOf, ops, opsFail, optBool, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, op: "rename", topic, title, by? } // -> { ok: true, slug, op, topic, title, was, changed, sentence } // POST { slug, op: "move", to, ids: [..] | thread, create?, by? } // -> { ok: true, slug, op, to: {id, title, created}, moved: [ids], // skipped: [{id, reason}], sentence } // POST { slug, op: "merge", into, from: [topic, …], by: "operator" } // -> { ok: true, slug, op, into: {id, title}, merged: [{id, title, posts, // pages}], posts, pages, watch: {stands?: {from, sentence}, dropped}, // published: {carried?, left}, sentence } // // ORGANISE AN X TRENDING LIBRARY'S TOPICS (lane x-trending-publish). `by` is // who asks ("agent" unless told; umtool's Posts library page says // "operator"), and it is recorded. // // RENAME changes a topic's name. Its id stays, so its watch, its posts and // their judgements are untouched. Refused: a name another topic answers to, // and A READ OR A REPARSE OF THE LIBRARY QUEUED OR RUNNING (a read may name // its topic by its name, and would make a new topic of the old one). // // MOVE puts posts — `ids`, or every post of one `thread` (its root post's // id) — into the topic `to` (an id or a name; `create: true` makes a topic of // that name, with no trending page, when there is none). Judgements and // captures are untouched; a later read never moves a post back. Safe beside a // read or a capture: not refused for one. // // MERGE puts every post, page and read of the `from` topics into `into` and // removes them. THERE IS NO UNMERGE, so it is the OPERATOR's: an agent's ask // is refused. Refused too while a read or a reparse of the library is queued // or running. The merged topics' ids keep meaning `into`. Judgements are // untouched. ONE watch stands: `into`'s live watch with its own terms, else // the first merged topic's moves to it; `sentence` says which, and that it // now reads every page in turn. A topic has ONE published channel too // (`published: {carried?, left}`): `into`'s stays its own; when it has none, // the first merged topic's becomes its own (`carried`: its next publish adds // to it); any other merged topic's channel is `left` as it is and never // added to again, and the sentence names it. // // Refused with a sentence: no such channel, not a library, no such topic. export async function POST(request: Request) { return ops(request, ["slug", "op", "by", "topic", "title", "into", "from", "to", "ids", "thread", "create"], async (body) => { const op = oneOf(body, "op", ["rename", "move", "merge"] as const); const create = optBool(body, "create"); const result = await organiseXTrendingAction({ slug: reqSlug(body, "slug"), op, by: body.by === undefined ? "agent" : body.by, ...(body.topic !== undefined ? { topic: body.topic } : {}), ...(body.title !== undefined ? { title: body.title } : {}), ...(body.into !== undefined ? { into: body.into } : {}), ...(body.from !== undefined ? { from: body.from } : {}), ...(body.to !== undefined ? { to: body.to } : {}), ...(body.ids !== undefined ? { ids: body.ids } : {}), ...(body.thread !== undefined ? { thread: body.thread } : {}), ...(create !== undefined ? { create } : {}), }); if (!result.ok) return opsFail(result.error); return NextResponse.json(result); }); }