// The Bluesky conversation attached to a document. // // `site.standard.document` may carry `bskyPostRef`, a strongRef to the post // where the article is being discussed. It is the only cross-app signal in // the record: standard.site readers do not own a comment system, and the // post's thread is where the conversation already is. // // The counts come from the public appview, unauthenticated, so this works // signed out and needs no scope. The post itself is not copied or stored; // only its three counts are read, and only for the page on screen. import { bskyProfileUrl, parseAtUri } from './atproto' import { type RequestOptions, requestJson } from './http' import type { DocumentRecord } from './types' const POST_COLLECTION = 'app.bsky.feed.post' const PUBLIC_APPVIEW = 'https://public.api.bsky.app' /** Counts are cosmetic; a slow appview must not hold the popup open. */ const OPTIONS: RequestOptions = { timeoutMs: 5_000, retries: 1 } export interface BskyPostRef { uri: string } export interface DiscussionCounts { replies: number reposts: number likes: number } /** * The post a document points at, if the record carries a well-formed * `bskyPostRef`. Anything else — another collection, a malformed uri, a * non-string — is no reference at all. */ export function bskyPostRef(record: DocumentRecord): BskyPostRef | undefined { const raw = record.bskyPostRef if (!raw || typeof raw !== 'object') return undefined const uri = (raw as { uri?: unknown }).uri if (typeof uri !== 'string') return undefined const parsed = parseAtUri(uri) if (!parsed || parsed.collection !== POST_COLLECTION) return undefined return { uri } } /** Both bsky.app and the appview accept a DID in the profile slot. */ export function bskyThreadUrl(ref: BskyPostRef): string | undefined { const parsed = parseAtUri(ref.uri) if (!parsed) return undefined return `${bskyProfileUrl(parsed.did)}/post/${encodeURIComponent(parsed.rkey)}` } interface PostView { uri?: unknown replyCount?: unknown repostCount?: unknown likeCount?: unknown } function count(value: unknown): number { return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0 } /** * The thread's counts, or null when the post is gone or the appview cannot be * asked. Null means "show the link without numbers", never zero. */ export async function discussionCounts(ref: BskyPostRef): Promise { const u = new URL(`${PUBLIC_APPVIEW}/xrpc/app.bsky.feed.getPosts`) u.searchParams.append('uris', ref.uri) try { const { posts } = await requestJson<{ posts?: PostView[] }>(u.toString(), OPTIONS) const post = posts?.find((p) => p.uri === ref.uri) if (!post) return null return { replies: count(post.replyCount), reposts: count(post.repostCount), likes: count(post.likeCount), } } catch (err) { console.debug('[substandard] discussion counts unavailable', ref.uri, err) return null } } /** "12 replies · 3 reposts · 40 likes", omitting zero counts. */ export function discussionSummary(counts: DiscussionCounts): string { const parts: string[] = [] const add = (one: string, many: string, n: number) => { if (n > 0) parts.push(`${n} ${n === 1 ? one : many}`) } add('reply', 'replies', counts.replies) add('repost', 'reposts', counts.reposts) add('like', 'likes', counts.likes) return parts.join(' · ') }