Browse Source

fixed profile pics on second-order search results

master
silberengel 7 months ago
parent
commit
0468ed6e16
  1. 99
      src/lib/utils/subscription_search.ts
  2. 48
      src/routes/events/+page.svelte

99
src/lib/utils/subscription_search.ts

@ -1245,6 +1245,10 @@ async function processContentEoseResults( @@ -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<Sear @@ -1286,6 +1290,12 @@ async function processTTagEoseResults(searchState: any, ndk?: NDK): Promise<Sear
ndk
);
// AI-NOTE: 2025-01-24 - Attach profile data to t-tag events for display
// This ensures profile pictures and other metadata are available in the UI
if (ndk) {
await attachProfileDataToEvents(prioritizedEvents, ndk);
}
return {
events: prioritizedEvents,
secondOrder: [],
@ -1522,6 +1532,10 @@ async function performSecondOrderSearchInBackground( @@ -1522,6 +1532,10 @@ async function performSecondOrderSearchInBackground(
);
}
// AI-NOTE: 2025-01-24 - Attach profile data to second-order events for display
// This ensures profile pictures and other metadata are available in the UI
await attachProfileDataToEvents(prioritizedSecondOrder, ndk);
// Update the search results with second-order events
const result: SearchResult = {
events: firstOrderEvents,
@ -1555,3 +1569,88 @@ async function performSecondOrderSearchInBackground( @@ -1555,3 +1569,88 @@ async function performSecondOrderSearchInBackground(
);
}
}
/**
* Attach profile data to events for display purposes
* This function fetches and attaches profile information to events so they can display profile pictures and other metadata
* @param events Array of events to attach profile data to
* @param ndk NDK instance for fetching profile data
* @returns Promise that resolves when profile data is attached
*/
async function attachProfileDataToEvents(events: NDKEvent[], ndk: NDK): Promise<void> {
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<string>();
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<string, any>();
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);
}
}

48
src/routes/events/+page.svelte

@ -177,32 +177,7 @@ @@ -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 @@ @@ -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<string>();
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;

Loading…
Cancel
Save