mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-07 21:32:51 +00:00
Consolidate profile metadata fetches
This commit is contained in:
1 parent
d698eff9cd
commit
89b8d124fa
6 files changed
+360
-148
No files matched your search
+11
-109
@@ -2,6 +2,7 @@
|
||||
|
||||
import { nostrClient } from "../nostrClientFacade.js";
|
||||
import { devLogger } from "./logger.js";
|
||||
import { fetchProfileMetadataBatch } from "../services/profileMetadataService.js";
|
||||
|
||||
const DEFAULT_PROFILE_IMAGE = "assets/svg/default-profile.svg";
|
||||
const HEX64_REGEX = /^[0-9a-f]{64}$/i;
|
||||
@@ -57,117 +58,18 @@ export async function batchFetchProfilesFromRelays({
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nostr?.pool || typeof nostr.pool.list !== "function") {
|
||||
logger.warn(
|
||||
"[batchFetchProfiles] Relay pool is not ready; skipping profile fetch.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
const profileResults = await fetchProfileMetadataBatch({
|
||||
pubkeys: toFetch,
|
||||
nostr,
|
||||
logger,
|
||||
defaultProfileImage,
|
||||
});
|
||||
|
||||
const filter = {
|
||||
kinds: [0],
|
||||
authors: toFetch,
|
||||
limit: toFetch.length,
|
||||
};
|
||||
|
||||
const newestProfiles = new Map();
|
||||
|
||||
const applyProfileEvent = (evt, relayUrl) => {
|
||||
if (!evt || typeof evt !== "object") {
|
||||
profileResults.forEach((entry, pubkey) => {
|
||||
if (!entry?.profile) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pubkey =
|
||||
typeof evt.pubkey === "string" && hex64Regex.test(evt.pubkey)
|
||||
? evt.pubkey
|
||||
: null;
|
||||
if (!pubkey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createdAt = Number.isFinite(evt.created_at) ? evt.created_at : 0;
|
||||
const previous = newestProfiles.get(pubkey);
|
||||
if (previous && previous.createdAt >= createdAt) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = JSON.parse(evt.content);
|
||||
const profile = {
|
||||
name: data?.name || data?.display_name || "Unknown",
|
||||
picture: data?.picture || defaultProfileImage,
|
||||
};
|
||||
|
||||
newestProfiles.set(pubkey, { createdAt, profile });
|
||||
setProfileCacheEntry(pubkey, profile);
|
||||
updateProfileInDOM(pubkey, profile);
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
`[batchFetchProfiles] Profile parse error for ${pubkey} from ${relayUrl}:`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const relayPromises = (Array.isArray(nostr.relays) ? nostr.relays : []).map(
|
||||
(relayUrl) =>
|
||||
nostr.pool
|
||||
.list([relayUrl], [filter])
|
||||
.then((events) => ({ relayUrl, events })),
|
||||
);
|
||||
|
||||
if (!relayPromises.length) {
|
||||
logger.warn(
|
||||
"[batchFetchProfiles] No relays configured; skipping profile fetch.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const results = await Promise.allSettled(relayPromises);
|
||||
|
||||
results.forEach((result, index) => {
|
||||
const relayUrl =
|
||||
result.status === "fulfilled"
|
||||
? result.value.relayUrl
|
||||
: nostr.relays[index];
|
||||
|
||||
if (result.status === "fulfilled") {
|
||||
const events = Array.isArray(result.value.events)
|
||||
? result.value.events
|
||||
: [];
|
||||
if (!events.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newestByPubkey = new Map();
|
||||
for (const evt of events) {
|
||||
if (!evt || typeof evt !== "object") {
|
||||
continue;
|
||||
}
|
||||
const pubkey =
|
||||
typeof evt.pubkey === "string" && hex64Regex.test(evt.pubkey)
|
||||
? evt.pubkey
|
||||
: null;
|
||||
if (!pubkey) {
|
||||
continue;
|
||||
}
|
||||
const createdAt = Number.isFinite(evt.created_at)
|
||||
? evt.created_at
|
||||
: 0;
|
||||
const prior = newestByPubkey.get(pubkey);
|
||||
if (!prior || createdAt > prior.createdAt) {
|
||||
newestByPubkey.set(pubkey, { createdAt, event: evt });
|
||||
}
|
||||
}
|
||||
|
||||
newestByPubkey.forEach(({ event }) => {
|
||||
applyProfileEvent(event, relayUrl);
|
||||
});
|
||||
} else if (relayUrl) {
|
||||
logger.warn(
|
||||
`[batchFetchProfiles] Failed to fetch profiles from relay ${relayUrl}:`,
|
||||
result.reason,
|
||||
);
|
||||
}
|
||||
setProfileCacheEntry(pubkey, entry.profile);
|
||||
updateProfileInDOM(pubkey, entry.profile);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user