mirror of
https://github.com/dergigi/boris.git
synced 2025-12-17 22:54:30 +01:00
fix: add periodic re-checking of eventStore for async profile arrivals
- Poll eventStore every 200ms for up to 2 seconds after fetchProfiles - Accumulate resolved labels across checks instead of resetting - Add detailed logging to diagnose why profiles aren't resolving - Fixes issue where profiles arrive asynchronously after fetchProfiles completes
This commit is contained in:
@@ -81,31 +81,63 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
|
|||||||
|
|
||||||
// Fetch missing profiles asynchronously
|
// Fetch missing profiles asynchronously
|
||||||
if (pubkeysToFetch.length > 0 && relayPool && eventStore) {
|
if (pubkeysToFetch.length > 0 && relayPool && eventStore) {
|
||||||
console.log('[npub-resolve] Fetching', pubkeysToFetch.length, 'missing profiles')
|
console.log('[npub-resolve] Fetching', pubkeysToFetch.length, 'missing profiles:', pubkeysToFetch.slice(0, 3).map(p => p.slice(0, 8) + '...'))
|
||||||
fetchProfiles(relayPool, eventStore as unknown as IEventStore, pubkeysToFetch)
|
fetchProfiles(relayPool, eventStore as unknown as IEventStore, pubkeysToFetch)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// Re-check eventStore for all profiles (including ones we just fetched)
|
// Re-check eventStore periodically as profiles arrive asynchronously
|
||||||
// This ensures we get profiles even if fetchProfiles didn't return them in the array
|
let checkCount = 0
|
||||||
const updatedLabels = new Map(labels)
|
const maxChecks = 10
|
||||||
profileData.forEach(({ encoded, pubkey }) => {
|
const checkInterval = 200 // ms
|
||||||
if (!updatedLabels.has(encoded) && eventStore) {
|
|
||||||
const profileEvent = eventStore.getEvent(pubkey + ':0')
|
// Keep track of resolved labels across checks
|
||||||
if (profileEvent) {
|
let currentLabels = new Map(labels)
|
||||||
try {
|
|
||||||
const profileData = JSON.parse(profileEvent.content || '{}') as { name?: string; display_name?: string; nip05?: string }
|
const checkForProfiles = () => {
|
||||||
const displayName = profileData.display_name || profileData.name || profileData.nip05
|
checkCount++
|
||||||
if (displayName) {
|
const updatedLabels = new Map(currentLabels)
|
||||||
updatedLabels.set(encoded, `@${displayName}`)
|
let newlyResolvedCount = 0
|
||||||
console.log('[npub-resolve] Resolved profile:', encoded, '->', displayName)
|
let withEventsCount = 0
|
||||||
|
let withoutNamesCount = 0
|
||||||
|
|
||||||
|
profileData.forEach(({ encoded, pubkey }) => {
|
||||||
|
if (!updatedLabels.has(encoded) && eventStore) {
|
||||||
|
const profileEvent = eventStore.getEvent(pubkey + ':0')
|
||||||
|
if (profileEvent) {
|
||||||
|
withEventsCount++
|
||||||
|
try {
|
||||||
|
const profileData = JSON.parse(profileEvent.content || '{}') as { name?: string; display_name?: string; nip05?: string }
|
||||||
|
const displayName = profileData.display_name || profileData.name || profileData.nip05
|
||||||
|
if (displayName) {
|
||||||
|
updatedLabels.set(encoded, `@${displayName}`)
|
||||||
|
newlyResolvedCount++
|
||||||
|
console.log('[npub-resolve] Resolved profile:', encoded.slice(0, 20) + '...', '->', displayName)
|
||||||
|
} else {
|
||||||
|
withoutNamesCount++
|
||||||
|
if (checkCount === 1) { // Only log once on first check
|
||||||
|
console.log('[npub-resolve] Profile has no name:', encoded.slice(0, 20) + '...', 'content keys:', Object.keys(profileData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[npub-resolve] Error parsing profile:', encoded.slice(0, 20) + '...', err)
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
// ignore parse errors
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
currentLabels = updatedLabels
|
||||||
|
console.log('[npub-resolve] Check', checkCount, '- resolved:', updatedLabels.size, 'total,', newlyResolvedCount, 'new,', withEventsCount, 'with events,', withoutNamesCount, 'without names')
|
||||||
|
setProfileLabels(updatedLabels)
|
||||||
|
|
||||||
|
// Continue checking if we haven't resolved all profiles and haven't exceeded max checks
|
||||||
|
if (updatedLabels.size < profileData.length && checkCount < maxChecks) {
|
||||||
|
setTimeout(checkForProfiles, checkInterval)
|
||||||
|
} else if (updatedLabels.size < profileData.length) {
|
||||||
|
console.warn('[npub-resolve] Stopped checking after', checkCount, 'attempts. Resolved', updatedLabels.size, 'out of', profileData.length)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
console.log('[npub-resolve] After fetch, resolved:', updatedLabels.size, 'out of', profileData.length)
|
|
||||||
setProfileLabels(updatedLabels)
|
// Start checking immediately, then periodically
|
||||||
|
checkForProfiles()
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error('[npub-resolve] Error fetching profiles:', err)
|
console.error('[npub-resolve] Error fetching profiles:', err)
|
||||||
|
|||||||
Reference in New Issue
Block a user