import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { rankOf, rankedOrder, type RankPlacement, } from "yt-dlp-transcript-common/lib/channelPriority"; import { channelExists } from "yt-dlp-transcript-common/controller/channels"; import { setChannelRankAction } from "../../../channels/actions"; import { OpsInputError, ops, opsFail, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, rank: | null } | { slug, top: true } | { slug, bottom: true } // | { slug, before: } | { slug, after: } // -> { ok, slug, rank, order: [{slug, rank}, …] } // // ONE CHANNEL PLACED IN THE ORDER — the rank inside its tier that every lane // and the sync scheduler follow (one rank per channel; unranked after every // ranked channel, then by slug). Before this only /channels' "Add to top" // moved a channel, and it also sets the tier to normal and turns the download // lane on. This moves the rank alone, through the deck's one writer // (saveChannelPriorityAction), which recompiles the four lane roots in the // same write, under the settings lock. Every other channel keeps its order: // the ones after it move down only as far as they must, ties stay tied. // // Exactly one placement. `rank: null` removes the rank. Refused before any // write: an unknown channel or anchor, an anchor with no rank (nothing to be // next to), a rank that is not a whole number ≥ 0, a channel paused for every // operation (it is in no order; its rank would not be kept). // The answer is the saved rank and the whole ranked order. const PLACEMENTS = ["rank", "top", "bottom", "before", "after"] as const; export async function POST(request: Request) { return ops(request, ["slug", ...PLACEMENTS], async (body) => { const slug = reqSlug(body, "slug"); const given = PLACEMENTS.filter((k) => body[k] !== undefined); if (given.length !== 1) { throw new OpsInputError( `give exactly one placement — "rank" (a number, or null to unrank), "top": true, "bottom": true, ` + `"before": "" or "after": ""${given.length ? `; got ${given.map((k) => `"${k}"`).join(", ")}` : ""}`, ); } const paths = getPaths(); if (!(await channelExists(paths, slug))) { return opsFail(`Channel "${slug}" not found`, 404); } const place = await readPlacement(body, given[0], async (anchor) => { if (!(await channelExists(paths, anchor))) { throw new OpsInputError(`Channel "${anchor}" not found (the "${given[0]}" anchor)`); } }); const result = await setChannelRankAction(slug, place); if (result?.error) return opsFail(result.error); const saved = getSettings().channelPriority; return NextResponse.json({ ok: true, slug, rank: rankOf(saved, slug), order: rankedOrder(saved) }); }); } async function readPlacement( body: Record, key: (typeof PLACEMENTS)[number], checkAnchor: (slug: string) => Promise, ): Promise { const v = body[key]; if (key === "rank") { if (v === null) return { kind: "unrank" }; if (typeof v !== "number" || !Number.isInteger(v) || v < 0) { throw new OpsInputError('"rank" must be a whole number, 0 or more (0 = first), or null to remove the rank'); } return { kind: "rank", rank: v }; } if (key === "top" || key === "bottom") { if (v !== true) throw new OpsInputError(`"${key}" must be true`); return { kind: key }; } const anchor = reqSlug(body, key); await checkAnchor(anchor); return { kind: key, slug: anchor }; } export function GET() { return NextResponse.json({ ok: false, error: "POST only" }, { status: 405 }); }