Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141import type { Did, Handle } from "@atcute/lexicons/syntax";import { getDevAccounts } from "../atproto/env.ts";import { getCachedProfile, setCachedProfile,} from "../server/db/queries/index.ts";import { getDidDocumentResolver, getHandleResolver } from "./identity.ts";
export interface ProfileInfo { handle: string | null; displayName: string | null; avatar: string | null;}
// Bluesky's sentinel for a handle that failed its own bidirectional check.const INVALID_HANDLE = "handle.invalid";
// "" is not a handle: it renders as nothing and builds /@/slug, which resolves// to no DID. `??` doesn't catch it, so every handle passes through here.function handleOrNull(value: string | null | undefined): string | null { if (!value || value === INVALID_HANDLE) return null; return value;}
// A DID document is written by its own controller, so the handle it claims is// unverified until it resolves back to that DID.async function verifyClaimedHandle( did: string, claimed: string | null,): Promise<string | null> { if (!claimed || claimed.startsWith("did:")) return null; return (await resolveHandleToDid(claimed)) === did ? claimed : null;}
// Bluesky's /img/avatar_thumbnail/ ~150px variant; we render at 16–64px, so the full asset is wasted bytes.export function thumbnailAvatar(url: string | null): string | null { if (!url) return null; return url.replace("/img/avatar/", "/img/avatar_thumbnail/");}
export async function resolveHandleToDid( handleOrDid: string,): Promise<string | null> { const normalized = handleOrDid.startsWith("@") ? handleOrDid.slice(1) : handleOrDid; if (normalized.startsWith("did:")) return normalized; // Dev mode: resolve known test handles without network calls. const devAccounts = getDevAccounts(); if (devAccounts) { const account = Object.values(devAccounts).find( (a) => a.handle === normalized, ); if (account) return account.did; } try { return await getHandleResolver().resolve(normalized as Handle); } catch { return null; }}
// Profile data comes from the Bluesky AppView (the de-facto standard); never throws.export async function resolveProfile( did: string, fetchFn: typeof fetch = fetch,): Promise<ProfileInfo> { // Dev DIDs don't exist on plc.directory; short-circuit to the known handle. const devAccounts = getDevAccounts(); if (devAccounts) { const account = Object.values(devAccounts).find((a) => a.did === did); if (account) { return { handle: account.handle, displayName: null, avatar: null }; } }
// Skip the cache when fetchFn is injected (tests). if (fetchFn === fetch) { const cached = getCachedProfile(did); if (cached) { return { handle: handleOrNull(cached.handle), displayName: cached.display_name, avatar: thumbnailAvatar(cached.avatar), }; } }
try { const doc = await getDidDocumentResolver().resolve(did as Did); const alsoKnownAs = doc.alsoKnownAs ?? []; const atHandle = alsoKnownAs.find((uri) => uri.startsWith("at://")); const claimed = atHandle?.slice("at://".length) ?? null; const handle = await verifyClaimedHandle(did, claimed);
const url = `https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(did)}`; const res = await fetchFn(url, { signal: AbortSignal.timeout(5000) }); if (!res.ok) { if (fetchFn === fetch) setCachedProfile(did, handle, null, null); return { handle, displayName: null, avatar: null }; } const profile = (await res.json()) as { handle?: string; displayName?: string; avatar?: string; };
const result: ProfileInfo = { // The AppView runs the same bidirectional check, so its handle is the // fallback — never the DID document's unverified claim. handle: handle ?? handleOrNull(profile.handle), displayName: profile.displayName ?? null, avatar: thumbnailAvatar(profile.avatar ?? null), };
if (fetchFn === fetch) { setCachedProfile(did, result.handle, result.displayName, result.avatar); }
return result; } catch (err) { console.warn(`Failed to resolve profile for ${did}:`, err); // Without a row nothing records that we tried, so nothing ever retries. if (fetchFn === fetch) setCachedProfile(did, null, null, null); return { handle: null, displayName: null, avatar: null }; }}
export async function resolveProfiles( dids: string[], fetchFn: typeof fetch = fetch,): Promise<Map<string, ProfileInfo>> { const unique = [...new Set(dids)]; const results = await Promise.all( unique.map( async (did) => [did, await resolveProfile(did, fetchFn)] as const, ), ); return new Map(results);}