// 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 } = {}, ): Promise { 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 }>(); 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() }; 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) }; } // Whether the library holds a post by this account (its handle's lower case), // however many accounts it holds: asked of the posts, not of a capped listing. export async function xTrendingHoldsAccount(channelRoot: string, key: string): Promise { const [membership, posts] = await Promise.all([readXTrendingMembership(channelRoot), readXTrendingPosts(channelRoot)]); return posts.some((p) => membership[p.id] && xLeadHandle(p.author) === key); }