Files
bitvid/js/utils/profileBatchFetcher.js
T
google-labs-jules[bot]andPR0M3TH3AN a084bb59de refactor: deduplicate HEX64_REGEX to js/utils/hex.js
Moves the repeatedly defined HEX64_REGEX constant to a shared utility
module to improve maintainability and consistency. Updates all consumers
to import from the new location.

Signed-off-by: Jules <[email protected]>

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-07 21:52:51 +00:00

77 lines
1.9 KiB
JavaScript

// js/utils/profileBatchFetcher.js
import { nostrClient } from "../nostrClientFacade.js";
import { devLogger } from "./logger.js";
import { fetchProfileMetadataBatch } from "../services/profileMetadataService.js";
import { HEX64_REGEX } from "./hex.js";
const DEFAULT_PROFILE_IMAGE = "assets/svg/default-profile.svg";
export async function batchFetchProfilesFromRelays({
authorSet,
getProfileCacheEntry,
setProfileCacheEntry,
updateProfileInDOM,
nostr = nostrClient,
logger = devLogger,
hex64Regex = HEX64_REGEX,
defaultProfileImage = DEFAULT_PROFILE_IMAGE,
} = {}) {
if (typeof getProfileCacheEntry !== "function") {
throw new TypeError("getProfileCacheEntry callback is required");
}
if (typeof setProfileCacheEntry !== "function") {
throw new TypeError("setProfileCacheEntry callback is required");
}
if (typeof updateProfileInDOM !== "function") {
throw new TypeError("updateProfileInDOM callback is required");
}
const pubkeys = Array.isArray(authorSet)
? authorSet
: Array.from(authorSet || []);
if (!pubkeys.length) {
return;
}
const toFetch = [];
pubkeys.forEach((pubkey) => {
const normalizedPubkey =
typeof pubkey === "string" ? pubkey.trim() : "";
const hasValidPubkey =
normalizedPubkey && hex64Regex.test(normalizedPubkey);
if (!hasValidPubkey) {
return;
}
const cacheEntry = getProfileCacheEntry(normalizedPubkey);
if (cacheEntry) {
updateProfileInDOM(normalizedPubkey, cacheEntry.profile);
} else {
toFetch.push(normalizedPubkey);
}
});
if (!toFetch.length) {
return;
}
const profileResults = await fetchProfileMetadataBatch({
pubkeys: toFetch,
nostr,
logger,
defaultProfileImage,
});
profileResults.forEach((entry, pubkey) => {
if (!entry?.profile) {
return;
}
setProfileCacheEntry(pubkey, entry.profile);
updateProfileInDOM(pubkey, entry.profile);
});
}