Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798// 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<DiscussionCounts | null> { 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(' · ')}