// AN ACCOUNT'S ACTIVITY, FROM ITS ARCHIVED POSTS (lane x-console): posts per // UTC day over the archive's range, and its LONG SILENCES — the gaps between // two consecutive archived posts that are long for THIS account. // // A silence is a gap of at least SILENCE_MEDIAN_FACTOR (3) times the // account's median gap between consecutive posts AND at least // SILENCE_MIN_HOURS (48 h). Either rule alone is wrong: a prolific account's // median is minutes, so "3× the median" alone marks every night's sleep; a // once-a-week account's normal week would pass "48 h" alone. Together, a busy // account's silence is anything over two days, and a sparse one's is three of // its usual gaps. Mr Obvious's 5–8 Nov 2026 gap (the real-data case) is the // first kind: a median of minutes, and three days of nothing. // // IT IS A SILENCE IN THE ARCHIVE, not necessarily on the account: a fetch that // did not reach a stretch (an older walk not yet run below it, a capped run) // shows the same hole. The panel says "no archived post", not "the account was // silent". // // Pure and client-safe: the channel page reads the posts (lib/posts-server.ts) // and hands this the four fields it needs. export const SILENCE_MEDIAN_FACTOR = 3; export const SILENCE_MIN_HOURS = 48; // A post dated outside 2006-01-01 (X's first year) … a day from now is a // bogus date, not activity: one such date would stretch the strip to millions // of days. It is left out. export const ACTIVITY_EARLIEST = Date.parse("2006-01-01T00:00:00Z"); export type ActivityPost = { id: string; createdAt: string; url: string; text?: string; // The post's author's handle. author?: string; }; export type ActivityEnd = { id: string; createdAt: string; url: string; // The post's first line, short (a tooltip). snippet: string; }; export type ActivitySilence = { before: ActivityEnd; after: ActivityEnd; hours: number; // The gap over the median gap. ratio: number; }; export type ActivityDay = { day: string; count: number }; export type AccountActivity = { posts: number; // The account counted, when only its own posts were (a with-replies // timeline also archives the posts it answers, by others). author: string | null; firstAt: string; lastAt: string; // Every UTC day from the first post's to the last's, empty days included. days: ActivityDay[]; maxPerDay: number; medianGapMinutes: number; // max(SILENCE_MIN_HOURS, SILENCE_MEDIAN_FACTOR × the median gap), hours. thresholdHours: number; // Newest first. silences: ActivitySilence[]; // No archived post since the last one for at least the threshold, as of // `asOf` (the last fetch): open-ended, so it has no post after it. trailing: { since: ActivityEnd; hours: number; asOf: string } | null; }; const HOUR = 3_600_000; const DAY = 24 * HOUR; function snippet(text: string | undefined): string { const first = (text ?? "").split("\n").find((l) => l.trim()) ?? ""; const t = first.trim(); return t.length > 90 ? `${t.slice(0, 89)}…` : t; } function end(p: ActivityPost): ActivityEnd { return { id: p.id, createdAt: p.createdAt, url: p.url, snippet: snippet(p.text) }; } export function median(values: readonly number[]): number { if (values.length === 0) return 0; const s = [...values].sort((a, b) => a - b); const mid = Math.floor(s.length / 2); return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2; } function utcDay(ms: number): string { return new Date(ms).toISOString().slice(0, 10); } // null when there are fewer than two datable posts (no gap to measure). // // `author` (the channel's handle) counts only the account's own posts when // any carry it — a with-replies timeline archives the posts it answers too, // and another account's post is not this one's activity. No post by it (an X // search channel's many accounts, a forum thread): every post counts. export function accountActivity( input: readonly ActivityPost[], opts: { asOf?: string; minHours?: number; factor?: number; author?: string; now?: number } = {}, ): AccountActivity | null { const minHours = opts.minHours ?? SILENCE_MIN_HOURS; const factor = opts.factor ?? SILENCE_MEDIAN_FACTOR; const who = opts.author?.replace(/^@/, "").toLowerCase(); const own = who ? input.filter((p) => p.author?.replace(/^@/, "").toLowerCase() === who) : []; const author = own.length > 0 ? opts.author!.replace(/^@/, "") : null; const latest = (opts.now ?? Date.now()) + DAY; const posts = (own.length > 0 ? own : input) .map((p) => ({ p, t: Date.parse(p.createdAt) })) .filter((x) => Number.isFinite(x.t) && x.t >= ACTIVITY_EARLIEST && x.t <= latest) .sort((a, b) => a.t - b.t || a.p.id.localeCompare(b.p.id)); if (posts.length < 2) return null; const gaps: number[] = []; for (let i = 1; i < posts.length; i++) gaps.push(posts[i].t - posts[i - 1].t); const med = median(gaps); const thresholdMs = Math.max(minHours * HOUR, factor * med); const silences: ActivitySilence[] = []; for (let i = 1; i < posts.length; i++) { const gap = gaps[i - 1]; if (gap < thresholdMs) continue; silences.push({ before: end(posts[i - 1].p), after: end(posts[i].p), hours: gap / HOUR, ratio: med > 0 ? gap / med : Infinity, }); } silences.reverse(); // Days, first to last, zeros included. const counts = new Map(); for (const { t } of posts) { const d = utcDay(t); counts.set(d, (counts.get(d) ?? 0) + 1); } const first = posts[0].t; const last = posts[posts.length - 1].t; const days: ActivityDay[] = []; let maxPerDay = 0; for (let t = Date.parse(`${utcDay(first)}T00:00:00Z`); t <= last; t += DAY) { const day = utcDay(t); const count = counts.get(day) ?? 0; if (count > maxPerDay) maxPerDay = count; days.push({ day, count }); } let trailing: AccountActivity["trailing"] = null; const asOf = opts.asOf ? Date.parse(opts.asOf) : NaN; if (Number.isFinite(asOf) && asOf - last >= thresholdMs) { trailing = { since: end(posts[posts.length - 1].p), hours: (asOf - last) / HOUR, asOf: opts.asOf! }; } return { posts: posts.length, author, firstAt: posts[0].p.createdAt, lastAt: posts[posts.length - 1].p.createdAt, days, maxPerDay, medianGapMinutes: med / 60_000, thresholdHours: thresholdMs / HOUR, silences, trailing, }; } // "2 d 19 h", "52 h" → "2 d 4 h", "50 min". export function spanHours(hours: number): string { if (hours < 1) return `${Math.round(hours * 60)} min`; if (hours < 48) return `${Math.round(hours)} h`; const d = Math.floor(hours / 24); const h = Math.round(hours - d * 24); return h === 24 ? `${d + 1} d` : h > 0 ? `${d} d ${h} h` : `${d} d`; } // "22 min", "3.5 h", "2 d". export function medianText(minutes: number): string { if (minutes < 90) return `${Math.max(0, Math.round(minutes))} min`; const h = minutes / 60; if (h < 48) return `${Math.round(h * 10) / 10} h`; return spanHours(h); }