// AN X SEARCH CHANNEL'S LEADS, read and acted on (release 38 lane S): the // accounts its matching posts came from (lib/xSearch/leads.ts ranks them), // and the two things the operator does with one — archive the account as its // own X channel (on no site; the editor's create-channel path does it, from // planArchiveXLead's fields) or never archive it (`xSearch.exclude`, which // hides it from the view). No model, no request: everything is on disk. import path from "node:path"; import type { Paths } from "../lib/paths"; import type { ChannelConfig } from "../lib/channelConfig"; import { readAllPosts, readPostFetchState } from "../lib/posts-server"; import { describeXSearch, isXSearchChannel, xLeadHandle, } from "../lib/xSearch/config"; import { describeXSearchWalk } from "../lib/xSearch/walk"; import { rankXSearchLeads, xLeadChannelFields, xLeadChannelSlug, type XSearchLeads, } from "../lib/xSearch/leads"; import { handleFromAccountUrl } from "../social/fetchers"; import { isXTrendingChannel, NOT_AN_X_TRENDING_LIBRARY } from "../lib/xTrending/config"; import { listXTrendingAccounts, xTrendingHoldsAccount, type XTrendingAccounts } from "../lib/xTrending/accounts"; import { channelExists, listChannelConfigs, patchChannelConfig, readChannelConfig } from "./channels"; export const NOT_AN_X_SEARCH_CHANNEL = (slug: string) => `${slug} is not an X search channel (its config has no xSearch query).`; // X channels that archive ONE account (not a search), by lower-case handle. export async function archivedXAccounts(paths: Paths): Promise> { const out = new Map(); for (const { slug, config } of await listChannelConfigs(paths)) { if (config.sourceKind !== "social" || config.platform !== "twitter" || isXSearchChannel(config)) continue; const handle = xLeadHandle(config.socialHandle ?? handleFromAccountUrl(config.url ?? "") ?? ""); if (!handle) continue; out.set(handle, [...(out.get(handle) ?? []), slug]); } return out; } export type XSearchLeadsView = XSearchLeads & { ok: true; slug: string; query: string; search: string; // describeXSearch walk: string; // describeXSearchWalk }; export async function readXSearchLeads( paths: Paths, slug: string, opts: { limit?: number } = {}, ): Promise { const config = await readChannelConfig(paths, slug); if (!config) return { ok: false, error: `No such channel: ${slug}` }; if (!isXSearchChannel(config) || !config.xSearch) return { ok: false, error: NOT_AN_X_SEARCH_CHANNEL(slug) }; const channelRoot = path.join(paths.channelsDir, slug); const [posts, state, archived] = await Promise.all([ readAllPosts(channelRoot), readPostFetchState(channelRoot), archivedXAccounts(paths), ]); return { ok: true, slug, query: config.xSearch.query, search: describeXSearch(config.xSearch), walk: describeXSearchWalk(state?.search), ...rankXSearchLeads(posts, { exclude: config.xSearch.exclude, archived, ...(opts.limit !== undefined ? { limit: opts.limit } : {}), }), }; } // "Never archive" (hidden = true) and its undo: the handle into, or out of, // the channel's `xSearch.exclude`, re-read and written under the config lock. export async function setXSearchLeadHidden( paths: Paths, slug: string, handle: string, hidden: boolean, ): Promise<{ ok: true; exclude: string[]; changed: boolean } | { ok: false; error: string }> { const key = xLeadHandle(handle); 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) }; let changed = false; let exclude: string[] = []; const written = await patchChannelConfig(paths, slug, {}, { derive: (current: ChannelConfig) => { if (!current.xSearch) return {}; const was = current.xSearch.exclude ?? []; exclude = hidden ? (was.includes(key) ? was : [...was, key]) : was.filter((h) => h !== key); changed = exclude.length !== was.length; const { exclude: _old, ...rest } = current.xSearch; void _old; return { xSearch: exclude.length > 0 ? { ...rest, exclude } : rest }; }, }); if (!written) return { ok: false, error: `No such channel: ${slug}` }; return { ok: true, exclude, changed }; } export const LEAD_ALREADY_ARCHIVED = (handle: string, slugs: string[]) => `@${handle} is already archived: ${slugs.join(", ")}.`; // What "Archive this account" creates: `-x`, an X account channel on // NO site, with the New channel form's fields. Refused when the account is // already archived by an X channel, or the slug is taken. export async function planArchiveXLead( paths: Paths, slug: string, handle: string, name?: string, ): Promise<{ ok: true; slug: string; fields: Record } | { ok: false; error: string }> { const key = xLeadHandle(handle); 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}` }; // 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)) { // Asked of the library's posts, not of the listing (which stops at 500 accounts). if (!(await xTrendingHoldsAccount(path.join(paths.channelsDir, slug), 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); if (await channelExists(paths, newSlug)) { return { ok: false, error: `A channel "${newSlug}" already exists; create @${key}'s channel by hand with another slug.` }; } 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 })) }; }