Something went wrong. Try again.
A work-in-progress chat bot for Streamplace with chat overlay functionality
Something went wrong. Try again.
2.7 kB · 112 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import { getDidDocument, getRecord } from "./atcuteUtils.ts";import { getAtprotoHandle, getPdsEndpoint } from "@atcute/identity";
class DidResolver { private cache = new Map<Did, UserProfile>(); private pendingRequests = new Map<Did, Promise<UserProfile>>();
async resolve(did: Did): Promise<UserProfile> { // Check cache first const cached = this.cache.get(did); if (cached) { return cached; }
// Check if there's already a pending request for this DID const pending = this.pendingRequests.get(did); if (pending) { return pending; }
// Create new request const request = this.fetchProfile(did); this.pendingRequests.set(did, request);
try { const profile = await request; this.cache.set(did, profile); return profile; } finally { this.pendingRequests.delete(did); } }
private async fetchProfile(did: Did): Promise<UserProfile> { try { const didDocument = await getDidDocument(did); const pdsHostUrl = getPdsEndpoint(didDocument); const handle = getAtprotoHandle(didDocument);
if (!pdsHostUrl || !handle) { throw new Error(`Error fetching PDS host or handle for ${did}`); }
const [actorProfileResult, chatProfileResult] = await Promise .allSettled([ getRecord(pdsHostUrl, { repo: did, collection: "app.bsky.actor.profile", rkey: "self", }), getRecord(pdsHostUrl, { repo: did, collection: "place.stream.chat.profile", rkey: "self", }), ]);
// Extract values from results, handling failures gracefully const actorProfile = actorProfileResult.status === "fulfilled" ? actorProfileResult.value?.value : null; const chatProfile = chatProfileResult.status === "fulfilled" ? chatProfileResult.value?.value : null;
const userProfile: UserProfile = { handle: handle as Handle, pdsEndpoint: pdsHostUrl as `${string}:${string}`, };
if (actorProfile) { userProfile.actorProfile = actorProfile as ActorProfile; }
if (chatProfile) { userProfile.chatProfile = chatProfile as ChatProfile; }
return userProfile; } catch (error) { console.error(`Error fetching profile for ${did}:`, error); throw new Error( `Failed to fetch profile for ${did}: ${ error instanceof Error ? error.message : "Unknown error" }`, ); } }
getUser(did: Did): UserProfile | undefined { return this.cache.get(did); }
setUser(did: Did, profile: UserProfile) { this.cache.set(did, profile); }
clearUserFromCache(did: Did) { this.cache.delete(did); }
clearCache() { this.cache.clear(); }
getCacheSize(): number { return this.cache.size; }}
// Export singleton instanceexport const didResolver = new DidResolver();