Something went wrong. Try again.
A live media wall for your Bluesky follows
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171import { moderateProfile, type Agent, type AppBskyActorDefs, type ModerationOpts} from '@atproto/api';
export const PROFILE_CONTEXT = Symbol('profiles');export const PROFILE_NAVIGATION_CONTEXT = Symbol('profile-navigation');
export function profileFromUrl(url: URL): string { const actor = url.searchParams.get('profile') ?? ''; return /^did:[a-z0-9]+:[A-Za-z0-9._:%-]+$/.test(actor) ? actor : '';}
export function profileUrl(current: URL, actor: string): URL { const url = new URL(current); url.searchParams.delete('profile'); if (actor && /^did:[a-z0-9]+:[A-Za-z0-9._:%-]+$/.test(actor)) url.searchParams.set('profile', actor); return url;}
export type ProfileCard = { did: string; handle: string; displayName?: string; pronouns?: string; description?: string; avatar?: string; banner?: string; links: { url: string; title: string }[]; labels: { value: string; source: string }[]; restricted: boolean; following?: string;};
export type ProfileStore = { peek(actor: string): ProfileCard | undefined; get(actor: string): Promise<ProfileCard>; close(): void;};
type Profile = AppBskyActorDefs.ProfileViewDetailed;
function safeUrl(value: string | undefined, image = false): string | undefined { if (!value) return undefined; try { const url = new URL(value); if (url.username || url.password) return undefined; if (url.protocol === 'https:' || (!image && url.protocol === 'http:')) return url.href; } catch {} return undefined;}
export function buildProfileCard( profile: Profile, moderation: ModerationOpts | undefined): ProfileCard { const card: ProfileCard = { did: profile.did, handle: profile.handle, links: [], labels: (profile.labels ?? []).map((label) => ({ value: label.val, source: label.src })), restricted: true }; if (!moderation) return card; try { const decision = moderateProfile(profile, moderation); const hidden = (context: 'avatar' | 'banner' | 'profileView' | 'displayName') => { const ui = decision.ui(context); return ui.filter || ui.blur; }; const viewer = profile.viewer; if ( hidden('profileView') || viewer?.muted || viewer?.mutedByList || viewer?.blocking || viewer?.blockingByList || viewer?.blockedBy ) return card; const links: ProfileCard['links'] = []; for (const link of profile.links ?? []) { const url = safeUrl(link.url); if (url) links.push({ url, title: link.title || url }); } if (!links.length) { const url = safeUrl(profile.website); if (url) links.push({ url, title: url }); } return { ...card, restricted: false, following: profile.viewer?.following, displayName: hidden('displayName') ? undefined : profile.displayName, pronouns: profile.pronouns, description: profile.description, avatar: hidden('avatar') ? undefined : safeUrl(profile.avatar, true), banner: hidden('banner') ? undefined : safeUrl(profile.banner, true), links }; } catch { return card; }}
export function createProfileStore( agent: Pick<Agent, 'getProfile'>, getModeration: () => ModerationOpts | undefined): ProfileStore { const cache = new Map<string, { profile: Profile; time: number }>(); const pending = new Map<string, { promise: Promise<Profile>; reject: (error: Error) => void }>(); let closed = false; return { peek(actor) { if (closed) return undefined; const cached = cache.get(actor); return cached ? buildProfileCard(cached.profile, getModeration()) : undefined; }, async get(actor) { if (closed) throw new Error('Profile store is closed'); if (!/^did:[a-z0-9]+:[A-Za-z0-9._:%-]+$/.test(actor)) throw new Error('Expected an actor DID'); const cached = cache.get(actor); if (cached && Date.now() - cached.time < 60_000) { cache.delete(actor); cache.set(actor, cached); return buildProfileCard(cached.profile, getModeration()); } let request = pending.get(actor); if (!request) { if (pending.size >= 8) throw new Error('Too many pending profile requests'); let resolve!: (profile: Profile) => void; let reject!: (error: Error) => void; const promise = new Promise<Profile>((accept, fail) => { resolve = accept; reject = fail; }); request = { promise, reject }; pending.set(actor, request); void Promise.resolve() .then(async () => { if (closed) return; const { data } = await agent.getProfile({ actor }); if (closed) return; if (data.did !== actor) throw new Error('Profile DID does not match requested actor'); cache.set(actor, { profile: data, time: Date.now() }); if (cache.size > 100) cache.delete(cache.keys().next().value!); pending.delete(actor); resolve(data); }) .catch((error) => { pending.delete(actor); reject(error); }); } const profile = await request.promise; if (closed) throw new Error('Profile store is closed'); return buildProfileCard(profile, getModeration()); }, close() { closed = true; cache.clear(); for (const request of pending.values()) request.reject(new Error('Profile store is closed')); pending.clear(); } };}