// AN X SEARCH CHANNEL'S LEADS (release 38 lane S): the accounts its matching // posts came from, ranked by how many — the "who keeps saying this" view the // operator acts on (Archive this account / Never archive). Pure: posts in, // rows out; no model. controller/xSearchLeads.ts reads the posts and the // channels it needs. import type { Post } from "../posts"; import { xLeadHandle } from "./config"; export type XSearchLeadSample = { id: string; url: string; text: string; createdAt: string; }; export type XSearchLead = { // The handle as the newest post writes it; `key` is its lower case. handle: string; key: string; name?: string; // Matching posts by this account (its reposts among them). count: number; reposts: number; first: string; // ISO, the oldest matching post last: string; // ISO, the newest // The newest matching post the account wrote itself (else its newest repost). sample: XSearchLeadSample; // Posts channels already archiving this account (an X channel whose handle // is this one), when there are. archivedAs?: string[]; }; export type XSearchLeads = { leads: XSearchLead[]; // Authors hidden by "Never archive", with their counts. hidden: Array<{ handle: string; count: number }>; // Matching posts read, and distinct authors among them (hidden included). posts: number; authors: number; // Leads past `limit` that were not listed. more: number; }; export const X_SEARCH_LEADS_DEFAULT_LIMIT = 100; export const X_SEARCH_LEAD_SAMPLE_CHARS = 280; function sampleOf(p: Post): XSearchLeadSample { const text = p.text.replace(/\s+/g, " ").trim(); return { id: p.id, url: p.url, text: text.length > X_SEARCH_LEAD_SAMPLE_CHARS ? `${text.slice(0, X_SEARCH_LEAD_SAMPLE_CHARS - 1)}…` : text, createdAt: p.createdAt, }; } // Ranked: most matching posts first, then the most recent, then by handle. // A post is counted once by id, whatever its shard. export function rankXSearchLeads( posts: ReadonlyArray, opts: { exclude?: ReadonlyArray; // Lower-case handle → the slugs of X channels that archive it. archived?: ReadonlyMap; limit?: number; } = {}, ): XSearchLeads { const exclude = new Set((opts.exclude ?? []).map((h) => h.toLowerCase())); type Acc = { lead: XSearchLead; own?: Post; anyNewest: Post; newestAt: string }; const byKey = new Map(); const seen = new Set(); for (const p of posts) { if (seen.has(p.id)) continue; seen.add(p.id); const key = xLeadHandle(p.author); if (!key) continue; let acc = byKey.get(key); if (!acc) { acc = { lead: { handle: p.author.replace(/^@/, ""), key, count: 0, reposts: 0, first: p.createdAt, last: p.createdAt, sample: sampleOf(p), }, anyNewest: p, newestAt: p.createdAt, }; byKey.set(key, acc); } const lead = acc.lead; lead.count++; if (p.isRepost) lead.reposts++; if (p.createdAt < lead.first) lead.first = p.createdAt; if (p.createdAt > lead.last) lead.last = p.createdAt; if (p.createdAt >= acc.newestAt) { acc.newestAt = p.createdAt; acc.anyNewest = p; lead.handle = p.author.replace(/^@/, ""); if (p.authorName) lead.name = p.authorName; } else if (!lead.name && p.authorName) { lead.name = p.authorName; } if (!p.isRepost && (!acc.own || p.createdAt > acc.own.createdAt)) acc.own = p; } const all = [...byKey.values()].map((acc) => { acc.lead.sample = sampleOf(acc.own ?? acc.anyNewest); const slugs = opts.archived?.get(acc.lead.key); if (slugs && slugs.length > 0) acc.lead.archivedAs = [...slugs].sort(); return acc.lead; }); all.sort( (a, b) => b.count - a.count || (b.last > a.last ? 1 : b.last < a.last ? -1 : 0) || a.key.localeCompare(b.key), ); const shown = all.filter((l) => !exclude.has(l.key)); const hidden = all.filter((l) => exclude.has(l.key)).map((l) => ({ handle: l.handle, count: l.count })); // A handle excluded that matched nothing (yet) is still listed as hidden. for (const h of exclude) if (!byKey.has(h)) hidden.push({ handle: h, count: 0 }); const limit = opts.limit ?? X_SEARCH_LEADS_DEFAULT_LIMIT; return { leads: shown.slice(0, limit), hidden, posts: seen.size, authors: byKey.size, more: Math.max(0, shown.length - limit), }; } // The channel an "Archive this account" makes: `-x`, on no site. The // New channel form's field names (create-channel's `fields`). export function xLeadChannelSlug(handle: string): string { const key = xLeadHandle(handle); if (!key) throw new Error(`"${handle}" is not an X handle (letters, digits and "_")`); // A slug starts with a letter or a digit; a handle may start with "_". return /^[a-z0-9]/.test(key) ? `${key}-x` : `x${key}-x`; } export function xLeadChannelFields(lead: { handle: string; name?: string }): Record { const handle = lead.handle.replace(/^@/, ""); return { name: `${lead.name?.trim() || handle} (X)`, handling: "transcribe", url: `https://x.com/${handle}`, platform: "twitter", sourceKind: "social", postFetcher: "x-gallery-dl", socialHandle: handle, }; } // WHAT "ARCHIVE THIS ACCOUNT" DOES BESIDE MAKING THE CHANNEL, by where the // lead came from (lane x-trending-publish, review 1 M1). // // From an X SEARCH channel's leads, as always: the first fetch starts unless // `fetch: false`, and the channel syncs like any other. // // From an X TRENDING LIBRARY's accounts: NOTHING IS ASKED OF X unless `fetch: // true`. Not starting the first fetch is not enough: a channel with a URL // that has never synced is the most overdue one the scheduler knows, and the // next tick would fetch it. So the channel is made with SYNC OFF: its // priority tier `paused` (lib/channelPriority.ts), the one switch the sync // scheduler, Sync all and the four lanes all ask (`isChannelPaused`). export function xLeadArchiveMode(o: { library: boolean; fetch?: boolean }): { fetchNow: boolean; syncOff: boolean } { if (o.library) return o.fetch === true ? { fetchNow: true, syncOff: false } : { fetchNow: false, syncOff: true }; return { fetchNow: o.fetch !== false, syncOff: false }; } // What is said of a channel made with sync off: the page, the ops answer and // the CLI print this, whole. export function xLeadSyncOffSentence(slug: string, handle: string): string { return ( `Made the channel "${slug}" for @${handle.replace(/^@/, "")}, on no site, with sync off (its priority is paused): nothing was asked of X, and nothing will be until you say so. ` + `To turn sync on, set its priority to normal on /channels (pnpm ops channel-priority --json '{"slugs":["${slug}"],"tier":"normal"}'). ` + `Its first fetch is Fetch posts on its page (pnpm ops fetch-posts --json '{"slug":"${slug}"}').` ); }