import { NextResponse } from "next/server"; import { archiveXSearchLeadAction, hideXSearchLeadAction, } from "../../../channels/[slug]/xSearchActions"; import { oneOf, ops, opsFail, optBool, optString, reqSlug, reqString } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, handle, action: "archive" | "never" | "restore", name?, fetch?, dryRun? } // -> archive: { ok: true, slug, jobIds, jobId? } (dryRun: { ok, slug, fields, dryRun }) // -> never | restore: { ok: true, exclude, changed } // // An X search channel's leads view, over HTTP (release 38 lane S). "archive" // is Archive this account: a `-x` X channel on NO site, created by // the New channel form's path with its first post fetch started (`fetch: // false` skips it), named `name` (or the handle) + " (X)". Refused: a channel // that is not an X search channel, a handle that is not one, an account an X // channel archives already, a slug taken. "never" is Never archive — the // handle into the channel's `xSearch.exclude`, hidden from the leads — and // "restore" undoes it. Read the leads with `get channel --leads`. // // AN X TRENDING LIBRARY'S ACCOUNTS ARE LEADS TOO (lane x-trending-publish): // with `slug` a library, "archive" makes `-x` for an account that // wrote a post the library holds — on no site, naming nothing of the library // — and ASKS X NOTHING, now or later: no first fetch is started, and the // channel is made with SYNC OFF (its priority tier `paused`, which the sync // scheduler, Sync all and the lanes all honour; a never-synced channel with a // URL would otherwise be fetched at the scheduler's next tick). The answer // then carries `syncOff: true` and `sentence`, which says how to turn sync on // (`pnpm ops channel-priority`, tier "normal") and run the first fetch // (`pnpm ops fetch-posts`). `fetch: true` asks for the ordinary behaviour // instead: the first fetch started, sync on. "never" and "restore" are a // search channel's only. Read the accounts with `get trending // --accounts`. export async function POST(request: Request) { return ops(request, ["slug", "handle", "action", "name", "fetch", "dryRun"], async (body) => { const slug = reqSlug(body, "slug"); const handle = reqString(body, "handle"); const action = oneOf(body, "action", ["archive", "never", "restore"] as const); if (action === "archive") { const name = optString(body, "name"); const res = await archiveXSearchLeadAction(slug, handle, { ...(name ? { name } : {}), ...(optBool(body, "dryRun") ? { dryRun: true } : {}), ...(optBool(body, "fetch") !== undefined ? { fetch: optBool(body, "fetch") === true } : {}), }); if (!res.ok) return opsFail(res.error); if (res.dryRun) return NextResponse.json({ ok: true, dryRun: true, slug: res.slug, fields: res.fields }); const jobIds = res.jobIds ?? []; return NextResponse.json({ ok: true, slug: res.slug, jobIds, ...(jobIds.length === 1 ? { jobId: jobIds[0] } : {}), ...(res.syncOff ? { syncOff: true, sentence: res.sentence } : {}), }); } if (body.dryRun !== undefined || body.name !== undefined || body.fetch !== undefined) { return opsFail(`"dryRun", "name" and "fetch" apply only to action "archive"`); } const res = await hideXSearchLeadAction(slug, handle, action === "never"); if (!res.ok) return opsFail(res.error); return NextResponse.json({ ok: true, exclude: res.exclude ?? [], changed: res.changed === true }); }); }