// The conversation under an article's Bluesky post, as a small tree. // // `getPostThread` returns the root and its replies already nested. The popup // shows a preview — the root post and a few replies — and links to Bluesky for // the rest. Every node is third-party data, so the shape is validated here // rather than trusted by the renderer. // // A thread can be huge, so the preview has a node budget as well as a depth: // the root, a handful of its replies, and a couple below each of those. import type { BskyPostRef } from './discussion' import { type RequestOptions, requestJson } from './http' const PUBLIC_APPVIEW = 'https://public.api.bsky.app' const OPTIONS: RequestOptions = { timeoutMs: 8_000, retries: 1 } const ROOT_REPLIES = 5 const CHILD_REPLIES = 2 const MAX_NODES = 24 const MAX_DEPTH = 2 export interface ThreadAuthor { did: string handle?: string displayName?: string avatarUrl?: string } export interface ThreadPost { uri: string author: ThreadAuthor text: string likeCount: number replyCount: number repostCount: number replies: ThreadPost[] } export interface ThreadView { root: ThreadPost /** More replies exist at the root than the preview asked for. */ more: boolean } function count(value: unknown): number { return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0 } function text(value: unknown): string | undefined { if (typeof value !== 'string') return undefined const trimmed = value.trim() return trimmed || undefined } function author(raw: unknown): ThreadAuthor | undefined { if (!raw || typeof raw !== 'object') return undefined const value = raw as { did?: unknown handle?: unknown displayName?: unknown avatar?: unknown } if (typeof value.did !== 'string' || !value.did.startsWith('did:')) return undefined return { did: value.did, ...(text(value.handle) ? { handle: text(value.handle) } : {}), ...(text(value.displayName) ? { displayName: text(value.displayName) } : {}), ...(typeof value.avatar === 'string' && value.avatar.startsWith('https://') ? { avatarUrl: value.avatar } : {}), } } /** postView, or null when the node is not one the preview can draw. */ function postFrom(raw: unknown): ThreadPost | null { if (!raw || typeof raw !== 'object') return null const value = raw as { uri?: unknown author?: unknown record?: unknown likeCount?: unknown replyCount?: unknown repostCount?: unknown replies?: unknown } if (typeof value.uri !== 'string') return null const who = author(value.author) const record = value.record as { text?: unknown } | undefined if (!who || !record || typeof record.text !== 'string') return null return { uri: value.uri, author: who, text: record.text.trim(), likeCount: count(value.likeCount), replyCount: count(value.replyCount), repostCount: count(value.repostCount), replies: [], } } interface NodeResult { post: ThreadPost /** Replies the server returned, whether or not the preview kept them. */ totalReplies: number } /** * One `threadViewPost`, with replies mapped to `depth`. Mutates `budget` so a * broad thread cannot make the popup build an unbounded tree. */ function nodeFrom(raw: unknown, depth: number, budget: { left: number }): NodeResult | null { if (!raw || typeof raw !== 'object' || budget.left <= 0) return null const node = raw as { $type?: unknown; post?: unknown; replies?: unknown } if (node.$type !== 'app.bsky.feed.defs#threadViewPost') return null const post = postFrom(node.post) if (!post) return null budget.left-- const replies = Array.isArray(node.replies) ? node.replies : [] if (depth >= MAX_DEPTH) return { post, totalReplies: replies.length } const keep = Math.min(replies.length, depth === 0 ? ROOT_REPLIES : CHILD_REPLIES) for (let i = 0; i < keep && budget.left > 0; i++) { const child = nodeFrom(replies[i], depth + 1, budget) if (child) post.replies.push(child.post) } return { post, totalReplies: replies.length } } /** * The preview for a thread, or null when the appview has no root (deleted, * blocked, or not a thread). Throws when the request itself fails. */ export async function fetchThread(ref: BskyPostRef): Promise { const u = new URL(`${PUBLIC_APPVIEW}/xrpc/app.bsky.feed.getPostThread`) u.searchParams.set('uri', ref.uri) u.searchParams.set('depth', String(MAX_DEPTH)) u.searchParams.set('parentHeight', '0') const data = await requestJson<{ thread?: unknown }>(u.toString(), OPTIONS) const root = nodeFrom(data.thread, 0, { left: MAX_NODES }) if (!root) return null return { root: root.post, more: root.totalReplies > root.post.replies.length, } }