···101101 // Get the DID
102102 const authorDid = entry.did;
103103104104+ // Determine the best handle to use
105105+ let authorHandle: string;
106106+104107 // First check if we have a valid handle in the database record
105108 // and it's not "unknown"
106106- let authorHandle: string;
107109 if (entry.handle && entry.handle !== 'unknown') {
108110 // Use the handle from the database
109111 authorHandle = entry.handle;
···111113112114 // Store in cache for future use
113115 dbHandleCache.set(authorDid, authorHandle);
114114- } else if (dbHandleCache.has(authorDid)) {
116116+ }
117117+ // Next, check if we have it in the cache
118118+ else if (dbHandleCache.has(authorDid)) {
115119 // Use cached handle from previous database entries
116120 authorHandle = dbHandleCache.get(authorDid)!;
117121 console.log(`Using cached DB handle for ${authorDid}: ${authorHandle}`);
118118- } else {
119119- // Resolve handle from DID
120120- authorHandle = await resolveDidToHandle(authorDid);
121121- console.log(`Resolved handle for ${authorDid}: ${authorHandle}`);
122122+ }
123123+ // If not in database or cache, try to resolve it
124124+ else {
125125+ // Try to resolve the handle from PLC directory
126126+ const resolvedHandle = await resolveDidToHandle(authorDid);
127127+128128+ // Only use the resolved handle if it's not in the user.xyz format (our fallback format)
129129+ if (!resolvedHandle.startsWith('user.')) {
130130+ authorHandle = resolvedHandle;
131131+ console.log(`Successfully resolved handle for ${authorDid}: ${authorHandle}`);
132132+133133+ // Also update the database with the resolved handle if possible
134134+ try {
135135+ if (supabaseUrl && supabaseKey) {
136136+ const supabase = createClient(supabaseUrl, supabaseKey);
137137+ await supabase
138138+ .from('flushing_records')
139139+ .update({ handle: authorHandle })
140140+ .eq('did', authorDid);
141141+ console.log(`Updated database with resolved handle for ${authorDid}: ${authorHandle}`);
142142+ }
143143+ } catch (updateError) {
144144+ console.error(`Failed to update handle in database for ${authorDid}:`, updateError);
145145+ }
146146+ } else {
147147+ // If resolution failed, still use the resolved handle (which will be our fallback format)
148148+ authorHandle = resolvedHandle;
149149+ console.log(`Could not resolve real handle for ${authorDid}, using: ${authorHandle}`);
150150+ }
122151 }
123152124153 if (containsBannedWords(entry.text)) {
···196225 // Get the DID
197226 const authorDid = entry.did;
198227228228+ // Determine the best handle to use
229229+ let authorHandle: string;
230230+199231 // First check if we have a valid handle in the database record
200232 // and it's not "unknown"
201201- let authorHandle: string;
202233 if (entry.handle && entry.handle !== 'unknown') {
203234 // Use the handle from the database
204235 authorHandle = entry.handle;
···206237207238 // Store in cache for future use
208239 dbHandleCache.set(authorDid, authorHandle);
209209- } else if (dbHandleCache.has(authorDid)) {
240240+ }
241241+ // Next, check if we have it in the cache
242242+ else if (dbHandleCache.has(authorDid)) {
210243 // Use cached handle from previous database entries
211244 authorHandle = dbHandleCache.get(authorDid)!;
212245 console.log(`Using cached DB handle for ${authorDid}: ${authorHandle}`);
213213- } else {
214214- // Resolve handle from DID
215215- authorHandle = await resolveDidToHandle(authorDid);
216216- console.log(`Resolved handle for ${authorDid}: ${authorHandle}`);
246246+ }
247247+ // If not in database or cache, try to resolve it
248248+ else {
249249+ // Try to resolve the handle from PLC directory
250250+ const resolvedHandle = await resolveDidToHandle(authorDid);
251251+252252+ // Only use the resolved handle if it's not in the user.xyz format (our fallback format)
253253+ if (!resolvedHandle.startsWith('user.')) {
254254+ authorHandle = resolvedHandle;
255255+ console.log(`Successfully resolved handle for ${authorDid}: ${authorHandle}`);
256256+257257+ // Also update the database with the resolved handle if possible
258258+ try {
259259+ if (supabaseUrl && supabaseKey) {
260260+ const supabase = createClient(supabaseUrl, supabaseKey);
261261+ await supabase
262262+ .from('flushing_records')
263263+ .update({ handle: authorHandle })
264264+ .eq('did', authorDid);
265265+ console.log(`Updated database with resolved handle for ${authorDid}: ${authorHandle}`);
266266+ }
267267+ } catch (updateError) {
268268+ console.error(`Failed to update handle in database for ${authorDid}:`, updateError);
269269+ }
270270+ } else {
271271+ // If resolution failed, still use the resolved handle (which will be our fallback format)
272272+ authorHandle = resolvedHandle;
273273+ console.log(`Could not resolve real handle for ${authorDid}, using: ${authorHandle}`);
274274+ }
217275 }
218276219277 // Skip entries with banned content
···340398 const plcResolver = async () => {
341399 console.log(`Trying PLC directory for DID: ${did}`);
342400401401+ // Function to extract handle from PLC data
402402+ const extractHandleFromPlcData = (plcData: any): string | null => {
403403+ if (!plcData || !plcData.alsoKnownAs || !Array.isArray(plcData.alsoKnownAs)) {
404404+ return null;
405405+ }
406406+407407+ // Loop through alsoKnownAs and find entries starting with "at://"
408408+ for (const aka of plcData.alsoKnownAs) {
409409+ if (typeof aka === 'string' && aka.startsWith('at://')) {
410410+ // Extract the handle (everything after "at://")
411411+ const handle = aka.split('//')[1];
412412+ if (handle) {
413413+ return handle;
414414+ }
415415+ }
416416+ }
417417+418418+ return null;
419419+ };
420420+343421 try {
344422 // Try main PLC endpoint
423423+ console.log(`Fetching from https://plc.directory/${did}`);
345424 const controller = new AbortController();
346425 const timeoutId = setTimeout(() => controller.abort(), 3000);
347426 const plcResponse = await fetch(`https://plc.directory/${did}`, {
···351430352431 if (plcResponse.ok) {
353432 const plcData = await plcResponse.json();
354354- if (plcData && plcData.alsoKnownAs && plcData.alsoKnownAs.length > 0) {
355355- // alsoKnownAs contains values like 'at://user.bsky.social'
356356- for (const aka of plcData.alsoKnownAs) {
357357- if (aka.startsWith('at://')) {
358358- const handle = aka.split('//')[1];
359359- if (handle) {
360360- console.log(`Resolved ${did} to handle ${handle} via PLC directory`);
361361- didResolutionCache.set(did, handle);
362362- return handle;
363363- }
364364- }
365365- }
433433+ console.log(`PLC response for ${did}:`, JSON.stringify(plcData).substring(0, 200) + '...');
434434+435435+ const handle = extractHandleFromPlcData(plcData);
436436+ if (handle) {
437437+ console.log(`Successfully resolved ${did} to handle ${handle} via PLC directory`);
438438+ didResolutionCache.set(did, handle);
439439+ return handle;
440440+ } else {
441441+ console.log(`PLC data for ${did} did not contain a valid handle in alsoKnownAs:`, plcData.alsoKnownAs);
366442 }
443443+ } else {
444444+ console.log(`PLC response not OK: ${plcResponse.status} ${plcResponse.statusText}`);
367445 }
368446 } catch (err) {
369447 console.warn(`Error with main PLC endpoint for ${did}:`, err);
···371449372450 try {
373451 // Try alternate PLC endpoint
452452+ console.log(`Fetching from https://plc.directory/${did}/data`);
374453 const altController = new AbortController();
375454 const altTimeoutId = setTimeout(() => altController.abort(), 3000);
376455 const altPlcResponse = await fetch(`https://plc.directory/${did}/data`, {
···380459381460 if (altPlcResponse.ok) {
382461 const altPlcData = await altPlcResponse.json();
383383- if (altPlcData && altPlcData.alsoKnownAs && altPlcData.alsoKnownAs.length > 0) {
384384- for (const aka of altPlcData.alsoKnownAs) {
385385- if (aka.startsWith('at://')) {
386386- const handle = aka.split('//')[1];
387387- if (handle) {
388388- console.log(`Resolved ${did} to handle ${handle} via PLC directory (alternate endpoint)`);
389389- didResolutionCache.set(did, handle);
390390- return handle;
391391- }
392392- }
393393- }
462462+ console.log(`PLC alternate response for ${did}:`, JSON.stringify(altPlcData).substring(0, 200) + '...');
463463+464464+ const handle = extractHandleFromPlcData(altPlcData);
465465+ if (handle) {
466466+ console.log(`Successfully resolved ${did} to handle ${handle} via PLC directory (alternate endpoint)`);
467467+ didResolutionCache.set(did, handle);
468468+ return handle;
469469+ } else {
470470+ console.log(`PLC alternate data for ${did} did not contain a valid handle in alsoKnownAs:`, altPlcData.alsoKnownAs);
394471 }
472472+ } else {
473473+ console.log(`PLC alternate response not OK: ${altPlcResponse.status} ${altPlcResponse.statusText}`);
395474 }
396475 } catch (err) {
397476 console.warn(`Error with alternate PLC endpoint for ${did}:`, err);