From 0468ed6e16c196e13fbddcf925619b6370068f01 Mon Sep 17 00:00:00 2001 From: silberengel Date: Sun, 24 Aug 2025 09:41:11 +0200 Subject: [PATCH] fixed profile pics on second-order search results --- src/lib/utils/subscription_search.ts | 99 ++++++++++++++++++++++++++++ src/routes/events/+page.svelte | 48 +------------- 2 files changed, 101 insertions(+), 46 deletions(-) diff --git a/src/lib/utils/subscription_search.ts b/src/lib/utils/subscription_search.ts index 814fc63..caacf21 100644 --- a/src/lib/utils/subscription_search.ts +++ b/src/lib/utils/subscription_search.ts @@ -1245,6 +1245,10 @@ async function processContentEoseResults( ndk ); + // AI-NOTE: 2025-01-24 - Attach profile data to first-order events for display + // This ensures profile pictures and other metadata are available in the UI + await attachProfileDataToEvents(prioritizedEvents, ndk); + // Perform second-order search for d-tag searches if (dedupedEvents.length > 0) { performSecondOrderSearchInBackground( @@ -1286,6 +1290,12 @@ async function processTTagEoseResults(searchState: any, ndk?: NDK): Promise { + if (events.length === 0) { + return; + } + + console.log(`subscription_search: Attaching profile data to ${events.length} events`); + + try { + // Import user list functions dynamically to avoid circular dependencies + const { fetchCurrentUserLists, isPubkeyInUserLists } = await import("./user_lists.ts"); + + // Get current user's lists for user list status + const userLists = await fetchCurrentUserLists(undefined, ndk); + + // Get unique pubkeys from events + const uniquePubkeys = new Set(); + events.forEach((event) => { + if (event.pubkey) { + uniquePubkeys.add(event.pubkey); + } + }); + + console.log(`subscription_search: Found ${uniquePubkeys.size} unique pubkeys to fetch profiles for`); + + // Fetch profile data for each unique pubkey + const profilePromises = Array.from(uniquePubkeys).map(async (pubkey) => { + try { + // Import getUserMetadata dynamically to avoid circular dependencies + const { getUserMetadata } = await import("./nostrUtils.ts"); + const npub = await import("./nostrUtils.ts").then(m => m.toNpub(pubkey)); + + if (npub) { + const profileData = await getUserMetadata(npub, ndk, true); + if (profileData) { + // Check if this pubkey is in user's lists + const isInLists = isPubkeyInUserLists(pubkey, userLists); + + // Return profile data with user list status + return { + pubkey, + profileData: { + ...profileData, + isInUserLists: isInLists + } + }; + } + } + } catch (error) { + console.warn(`subscription_search: Failed to fetch profile for ${pubkey}:`, error); + } + return null; + }); + + const profileResults = await Promise.allSettled(profilePromises); + + // Create a map of pubkey to profile data + const profileMap = new Map(); + profileResults.forEach((result) => { + if (result.status === "fulfilled" && result.value) { + profileMap.set(result.value.pubkey, result.value.profileData); + } + }); + + console.log(`subscription_search: Successfully fetched ${profileMap.size} profiles`); + + // Attach profile data to each event + events.forEach((event) => { + if (event.pubkey && profileMap.has(event.pubkey)) { + (event as any).profileData = profileMap.get(event.pubkey); + } + }); + + console.log(`subscription_search: Profile data attachment complete`); + } catch (error) { + console.error("subscription_search: Error attaching profile data:", error); + } +} diff --git a/src/routes/events/+page.svelte b/src/routes/events/+page.svelte index bec76db..2298ecb 100644 --- a/src/routes/events/+page.svelte +++ b/src/routes/events/+page.svelte @@ -177,32 +177,7 @@ } } - // AI-NOTE: 2025-01-24 - Function to update profile data with user list information - async function updateProfileDataWithUserLists(events: NDKEvent[]) { - try { - const userLists = await fetchCurrentUserLists(undefined, ndk); - - for (const event of events) { - if (event.kind === 0 && event.pubkey) { - const existingProfileData = - (event as any).profileData || parseProfileContent(event); - if (existingProfileData) { - const isInLists = isPubkeyInUserLists(event.pubkey, userLists); - (event as any).profileData = { - ...existingProfileData, - isInUserLists: isInLists, - }; - } - } - } - } catch (error) { - console.warn( - "[Events Page] Failed to update profile data with user lists:", - error, - ); - } - } // Use Svelte 5 idiomatic effect to update searchValue and searchType based on URL parameters $effect(() => { @@ -330,30 +305,11 @@ checkCommunityStatusForResults(tTagEvents); } - // AI-NOTE: 2025-01-24 - Cache profiles for all search results - cacheProfilesForEvents([...results, ...secondOrder, ...tTagEvents]); + // AI-NOTE: 2025-01-24 - Profile data is now handled in subscription_search.ts + // No need to cache profiles here as they're already attached to events } - // AI-NOTE: 2025-01-24 - Function to cache profiles for multiple events - async function cacheProfilesForEvents(events: NDKEvent[]) { - const uniquePubkeys = new Set(); - events.forEach((event) => { - if (event.pubkey) { - uniquePubkeys.add(event.pubkey); - } - }); - - // Cache profiles in parallel - const cachePromises = Array.from(uniquePubkeys).map((pubkey) => - cacheProfileForPubkey(pubkey), - ); - await Promise.allSettled(cachePromises); - // AI-NOTE: 2025-01-24 - Update profile data with user list information for cached events - await updateProfileDataWithUserLists(events); - - console.log(`[Events Page] Profile caching complete`); - } function handleClear() { searchType = null;