commit 1b3a88b504b097692806cdb5a4bee282a362e97d
parent 6dbd6357a4cab2d18b55eb360786b65146b473ff
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Wed, 18 Nov 2026 10:36:21 -0500
common, editor: x-trending slice 4, accounts -- a library lists its accounts like a search channel's leads, and "Archive this account" plans <handle>-x for one of them
listXTrendingAccounts (lib/xTrending/accounts.ts): handle, name, posts,
kept (and the operator's among them), topics, newest post; the controller
adds the X channels that already archive each. planArchiveXLead takes a
library too, for an account that wrote a post the library holds: the same
fields, naming nothing of the library. From a library the x-search-lead
archive creates the channel on no site and asks X nothing: the first
fetch is started only with "fetch": true.
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat:
5 files changed, 178 insertions(+), 4 deletions(-)
diff --git a/common/controller/xSearchLeads.test.ts b/common/controller/xSearchLeads.test.ts
@@ -17,7 +17,9 @@ process.env.SETTINGS_FILE = path.join(ROOT, "settings.json");
const { getPaths } = await import("../lib/paths");
const { writePosts } = await import("../lib/posts-server");
-const { readXSearchLeads, setXSearchLeadHidden, planArchiveXLead } = await import("./xSearchLeads");
+const { readXSearchLeads, setXSearchLeadHidden, planArchiveXLead, readXTrendingAccounts } = await import("./xSearchLeads");
+const { judgeXTrending } = await import("../lib/xTrending/judgements");
+const { recordXTrendingRead, writeXTrendingPosts } = await import("../lib/xTrending/store");
const { writeSite, X_SEARCH_NOT_ON_A_SITE } = await import("../lib/site");
const paths = getPaths();
@@ -152,3 +154,52 @@ test("no site may carry an X search channel, as a member or a cited channel", as
{ message: "demo-search is an X search channel: it is never put on a site." },
);
});
+
+test("an X trending library's accounts: handle, posts, kept, topics, who archives it; Archive this account plans <handle>-x for an account in the library only", async () => {
+ const libRoot = await channel("demo-library", { sourceKind: "social", platform: "twitter", xTrending: {} });
+ const lp = (id: string, author: string, name: string, at: string) => ({
+ id, slug: `demo-library/${id}`, channelSlug: "demo-library", author, authorName: name, createdAt: at, uploadDate: at.slice(0, 10).replace(/-/g, ""),
+ text: `kettle ${id}`, url: `https://x.com/${author}/status/${id}`, platform: "twitter" as const, isReply: false, isRepost: false, links: [],
+ });
+ await writeXTrendingPosts(libRoot, [
+ lp("901", "Dora", "Dora D", "2026-11-16T10:00:00.000Z"),
+ lp("902", "dora", "Dora Now", "2026-11-16T12:00:00.000Z"),
+ lp("903", "bob", "Bob", "2026-11-16T11:00:00.000Z"),
+ lp("904", "Dora", "Dora D", "2026-11-16T09:00:00.000Z"),
+ lp("905", "erin", "Erin", "2026-11-16T09:00:00.000Z"),
+ ]);
+ const ref = (n: number) => ({ trendId: `190000000000000000${n}`, url: `https://x.com/i/trending/190000000000000000${n}` });
+ await recordXTrendingRead(libRoot, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: ref(1), topic: "Kettles", postIds: ["901", "902", "903"], added: 3 });
+ await recordXTrendingRead(libRoot, { readId: "tr-01JB0000000000000000000000", at: "2026-11-17T11:00:00.000Z", ref: ref(2), topic: "Cups", postIds: ["904", "905"], added: 2 });
+ await judgeXTrending(libRoot, { by: "operator", set: [{ id: "901", state: "keep" }, { id: "903", state: "drop" }] });
+ await judgeXTrending(libRoot, { by: "agent", set: [{ id: "904", state: "keep" }] });
+
+ const view = await readXTrendingAccounts(paths, "demo-library");
+ assert.ok(view.ok);
+ assert.deepEqual([view.total, view.posts, view.more], [3, 5, 0]);
+ assert.deepEqual(view.accounts, [
+ { handle: "dora", key: "dora", name: "Dora Now", posts: 3, kept: 2, keptByOperator: 1, topics: ["kettles", "cups"], last: "2026-11-16T12:00:00.000Z" },
+ { handle: "bob", key: "bob", name: "Bob", posts: 1, kept: 0, keptByOperator: 0, topics: ["kettles"], last: "2026-11-16T11:00:00.000Z", archivedAs: ["bob-x"] },
+ { handle: "erin", key: "erin", name: "Erin", posts: 1, kept: 0, keptByOperator: 0, topics: ["cups"], last: "2026-11-16T09:00:00.000Z" },
+ ]);
+ const cups = await readXTrendingAccounts(paths, "demo-library", { topic: "cups", limit: 1 });
+ assert.ok(cups.ok);
+ assert.deepEqual([cups.accounts.map((a) => [a.key, a.posts]), cups.total, cups.more], [[["dora", 1]], 2, 1]);
+ assert.deepEqual(await readXTrendingAccounts(paths, "demo-search"), { ok: false, error: 'demo-search is not an X trending library (create one with platform "x-trending").' });
+
+ // The plan: the leads' own, and it names nothing of the library.
+ const plan = await planArchiveXLead(paths, "demo-library", "@Dora", "Dora Now");
+ assert.deepEqual(plan, {
+ ok: true,
+ slug: "dora-x",
+ fields: { name: "Dora Now (X)", handling: "transcribe", url: "https://x.com/Dora", platform: "twitter", sourceKind: "social", postFetcher: "x-gallery-dl", socialHandle: "Dora" },
+ });
+ assert.doesNotMatch(JSON.stringify(plan), /demo-library|trending/i);
+ assert.deepEqual(await planArchiveXLead(paths, "demo-library", "bob"), { ok: false, error: "@bob is already archived: bob-x." });
+ assert.deepEqual(await planArchiveXLead(paths, "demo-library", "alice"), {
+ ok: false,
+ error: 'demo-library holds no post by @alice: "Archive this account" is for an account in the library.',
+ });
+ // "Never archive" stays a search channel's.
+ assert.equal((await setXSearchLeadHidden(paths, "demo-library", "dora", true)).ok, false);
+});
diff --git a/common/controller/xSearchLeads.ts b/common/controller/xSearchLeads.ts
@@ -22,6 +22,8 @@ import {
type XSearchLeads,
} from "../lib/xSearch/leads";
import { handleFromAccountUrl } from "../social/fetchers";
+import { isXTrendingChannel, NOT_AN_X_TRENDING_LIBRARY } from "../lib/xTrending/config";
+import { listXTrendingAccounts, X_TRENDING_ACCOUNTS_MAX, type XTrendingAccounts } from "../lib/xTrending/accounts";
import { channelExists, listChannelConfigs, patchChannelConfig, readChannelConfig } from "./channels";
export const NOT_AN_X_SEARCH_CHANNEL = (slug: string) =>
@@ -121,7 +123,15 @@ export async function planArchiveXLead(
if (!key) return { ok: false, error: `"${handle}" is not an X handle (letters, digits and "_")` };
const config = await readChannelConfig(paths, slug);
if (!config) return { ok: false, error: `No such channel: ${slug}` };
- if (!isXSearchChannel(config)) return { ok: false, error: NOT_AN_X_SEARCH_CHANNEL(slug) };
+ // AN X TRENDING LIBRARY'S ACCOUNTS ARE LEADS TOO (lane x-trending-publish):
+ // the same plan, for an account that wrote a post the library holds. The
+ // channel made names nothing of the library.
+ if (isXTrendingChannel(config)) {
+ const { accounts } = await listXTrendingAccounts(path.join(paths.channelsDir, slug), { limit: X_TRENDING_ACCOUNTS_MAX });
+ if (!accounts.some((a) => a.key === key)) {
+ return { ok: false, error: `${slug} holds no post by @${handle.replace(/^@/, "")}: "Archive this account" is for an account in the library.` };
+ }
+ } else if (!isXSearchChannel(config)) return { ok: false, error: NOT_AN_X_SEARCH_CHANNEL(slug) };
const archived = (await archivedXAccounts(paths)).get(key);
if (archived && archived.length > 0) return { ok: false, error: LEAD_ALREADY_ARCHIVED(handle.replace(/^@/, ""), archived) };
const newSlug = xLeadChannelSlug(key);
@@ -130,3 +140,18 @@ export async function planArchiveXLead(
}
return { ok: true, slug: newSlug, fields: xLeadChannelFields({ handle: handle.replace(/^@/, ""), ...(name ? { name } : {}) }) };
}
+
+// A LIBRARY'S ACCOUNTS, with the X channels that already archive each (lane
+// x-trending-publish): what `get trending --accounts`, the MCP's
+// x_trending_accounts and the Posts library page list.
+export async function readXTrendingAccounts(
+ paths: Paths,
+ slug: string,
+ opts: { topic?: string; limit?: number } = {},
+): Promise<({ ok: true; slug: string } & XTrendingAccounts) | { ok: false; error: string }> {
+ const config = await readChannelConfig(paths, slug);
+ if (!config) return { ok: false, error: `No such channel: ${slug}` };
+ if (!isXTrendingChannel(config)) return { ok: false, error: NOT_AN_X_TRENDING_LIBRARY(slug) };
+ const archived = await archivedXAccounts(paths);
+ return { ok: true, slug, ...(await listXTrendingAccounts(path.join(paths.channelsDir, slug), { ...opts, archived })) };
+}
diff --git a/common/lib/xTrending/accounts.ts b/common/lib/xTrending/accounts.ts
@@ -0,0 +1,84 @@
+// A LIBRARY'S ACCOUNTS (lane x-trending-publish, slice 4): who wrote the
+// posts a library holds — like an X search channel's leads
+// (lib/xSearch/leads.ts): the handle, how many posts, how many of them are
+// kept (the judgement in force, either hand's), and the topics they are in.
+// Read from the library's own files; no index, no request. A library file's
+// reader like the listing: behind every door. SERVER-ONLY (node:fs).
+
+import { xLeadHandle } from "../xSearch/config";
+import {
+ readXTrendingJudgementsFile,
+ readXTrendingMembership,
+ readXTrendingPosts,
+ readXTrendingTopics,
+ xTrendingEffectiveJudgement,
+ xTrendingThreadOf,
+} from "./store";
+
+export type XTrendingAccount = {
+ // The handle as the newest post writes it; `key` is its lower case.
+ handle: string;
+ key: string;
+ name?: string;
+ posts: number;
+ kept: number;
+ // Of the kept, the operator's (what a publish would take).
+ keptByOperator: number;
+ topics: string[];
+ last: string; // ISO, its newest post in the library
+ // X channels already archiving this account (filled by the controller).
+ archivedAs?: string[];
+};
+
+export type XTrendingAccounts = { accounts: XTrendingAccount[]; total: number; posts: number; more: number };
+
+export const X_TRENDING_ACCOUNTS_DEFAULT = 100;
+export const X_TRENDING_ACCOUNTS_MAX = 500;
+
+// Most posts first, then most kept, then by handle. `topic` narrows to one.
+export async function listXTrendingAccounts(
+ channelRoot: string,
+ opts: { topic?: string; limit?: number; archived?: ReadonlyMap<string, string[]> } = {},
+): Promise<XTrendingAccounts> {
+ const [topics, membership, posts, judgements] = await Promise.all([
+ readXTrendingTopics(channelRoot),
+ readXTrendingMembership(channelRoot),
+ readXTrendingPosts(channelRoot),
+ readXTrendingJudgementsFile(channelRoot),
+ ]);
+ const want = opts.topic ? (topics.find((t) => t.id === opts.topic) ?? topics.find((t) => t.aliases?.includes(opts.topic!)))?.id ?? opts.topic : undefined;
+ const byId = new Map(posts.filter((p) => membership[p.id]).map((p) => [p.id, p]));
+ const rows = new Map<string, XTrendingAccount & { topicSet: Set<string> }>();
+ let counted = 0;
+ for (const post of byId.values()) {
+ const m = membership[post.id];
+ if (want && m.topic !== want) continue;
+ const key = xLeadHandle(post.author);
+ if (!key) continue;
+ counted++;
+ const row = rows.get(key) ?? { handle: post.author.replace(/^@/, ""), key, posts: 0, kept: 0, keptByOperator: 0, topics: [], last: "", topicSet: new Set<string>() };
+ row.posts++;
+ const j = xTrendingEffectiveJudgement(judgements.posts[post.id], judgements.threads[xTrendingThreadOf(post, membership, byId)]);
+ if (j?.state === "keep") {
+ row.kept++;
+ if (j.by === "operator") row.keptByOperator++;
+ }
+ row.topicSet.add(m.topic);
+ if (post.createdAt > row.last) {
+ row.last = post.createdAt;
+ row.handle = post.author.replace(/^@/, "");
+ if (post.authorName) row.name = post.authorName;
+ }
+ rows.set(key, row);
+ }
+ const order = new Map(topics.map((t, i) => [t.id, i]));
+ const all = [...rows.values()]
+ .map(({ topicSet, ...row }) => ({
+ ...row,
+ topics: [...topicSet].sort((a, b) => (order.get(a) ?? 1e9) - (order.get(b) ?? 1e9)),
+ ...(opts.archived?.get(row.key)?.length ? { archivedAs: opts.archived.get(row.key) } : {}),
+ }))
+ .sort((a, b) => b.posts - a.posts || b.kept - a.kept || (a.key < b.key ? -1 : 1));
+ const limit = Math.min(Math.max(1, opts.limit ?? X_TRENDING_ACCOUNTS_DEFAULT), X_TRENDING_ACCOUNTS_MAX);
+ return { accounts: all.slice(0, limit), total: all.length, posts: counted, more: Math.max(0, all.length - limit) };
+}
diff --git a/editor/app/api/ops/x-search-lead/route.ts b/editor/app/api/ops/x-search-lead/route.ts
@@ -19,6 +19,14 @@ export const dynamic = "force-dynamic";
// 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 <slug> --leads`.
+//
+// AN X TRENDING LIBRARY'S ACCOUNTS ARE LEADS TOO (lane x-trending-publish):
+// with `slug` a library, "archive" makes `<handle>-x` for an account that
+// wrote a post the library holds — on no site, naming nothing of the library
+// — and ASKS X NOTHING: there the first fetch is not started unless `fetch:
+// true` (it is a separate press, `pnpm ops fetch-posts`). "never" and
+// "restore" are a search channel's only. Read the accounts with
+// `get trending <slug> --accounts`.
export async function POST(request: Request) {
return ops(request, ["slug", "handle", "action", "name", "fetch", "dryRun"], async (body) => {
const slug = reqSlug(body, "slug");
@@ -29,7 +37,7 @@ export async function POST(request: Request) {
const res = await archiveXSearchLeadAction(slug, handle, {
...(name ? { name } : {}),
...(optBool(body, "dryRun") ? { dryRun: true } : {}),
- ...(optBool(body, "fetch") === false ? { fetch: false } : {}),
+ ...(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 });
diff --git a/editor/app/channels/[slug]/xSearchActions.ts b/editor/app/channels/[slug]/xSearchActions.ts
@@ -15,6 +15,7 @@ import {
setXSearchLeadHidden,
type XSearchLeadsView,
} from "yt-dlp-transcript-common/controller/xSearchLeads";
+import { isXTrendingLibrary } from "yt-dlp-transcript-common/lib/xTrending/library-server";
import { safeRevalidate } from "../../lib/safeRevalidate";
import { applyChannelFormPatch } from "../components/channelConfigToForm";
import { createChannelFromForm } from "../actions";
@@ -44,7 +45,12 @@ export async function archiveXSearchLeadAction(
const fd = new FormData();
applyChannelFormPatch(fd, plan.fields);
fd.set("slug", plan.slug);
- if (opts.fetch !== false) fd.set("fetchPostsNow", "on");
+ // From an X TRENDING LIBRARY's accounts (lane x-trending-publish) the press
+ // makes the channel and asks X nothing: the first fetch is its own press
+ // (`fetch: true` asks for it here). From a search channel's leads it starts,
+ // as it always did, unless `fetch: false`.
+ const library = await isXTrendingLibrary(getPaths().channelsDir, slug);
+ if (opts.fetch ?? !library) fd.set("fetchPostsNow", "on");
const created = await createChannelFromForm(fd);
if (!created.ok) return { ok: false, error: created.error };
safeRevalidate([`/channels/${slug}`, `/channels/${created.slug}`, "/channels"]);