|
|
|
|
@@ -6449,8 +6449,11 @@ function shortHexPubkey(hex) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cache for discovered relays — rebuilt only when the contact list changes.
|
|
|
|
|
// The version suffix forces a rebuild when the handler code changes (e.g.
|
|
|
|
|
// adding servingNames).
|
|
|
|
|
let discoveredRelaysCache = null;
|
|
|
|
|
let discoveredRelaysCacheKey = '';
|
|
|
|
|
const DISCOVERED_RELAYS_CACHE_VERSION = 'v3-names-k0';
|
|
|
|
|
|
|
|
|
|
async function handleGetDiscoveredRelays(requestId, port) {
|
|
|
|
|
try {
|
|
|
|
|
@@ -6478,7 +6481,7 @@ async function handleGetDiscoveredRelays(requestId, port) {
|
|
|
|
|
? Array.from(kind3Events).sort((a, b) => (b.created_at || 0) - (a.created_at || 0))[0]
|
|
|
|
|
: null;
|
|
|
|
|
const followedPubkeys = latestKind3 ? extractFollowedPubkeysFromKind3(latestKind3) : [];
|
|
|
|
|
const cacheKey = `${latestKind3?.id || 'none'}_${latestKind3?.created_at || 0}`;
|
|
|
|
|
const cacheKey = `${DISCOVERED_RELAYS_CACHE_VERSION}_${latestKind3?.id || 'none'}_${latestKind3?.created_at || 0}`;
|
|
|
|
|
|
|
|
|
|
// Rebuild the relay→[pubkeys] mapping from follows' kind 10002 events
|
|
|
|
|
// in the Dexie cache if the contact list has changed.
|
|
|
|
|
@@ -6534,15 +6537,17 @@ async function handleGetDiscoveredRelays(requestId, port) {
|
|
|
|
|
|
|
|
|
|
// Look up profile names for all serving pubkeys from the Dexie
|
|
|
|
|
// profiles table so we can display usernames instead of pubkeys.
|
|
|
|
|
// Look up profile names for all serving pubkeys.
|
|
|
|
|
const pubkeyToName = new Map();
|
|
|
|
|
const allServingPubkeys = new Set();
|
|
|
|
|
for (const servingSet of relayServesPubkeys.values()) {
|
|
|
|
|
for (const pk of servingSet) allServingPubkeys.add(pk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Strategy 1: Try Dexie profiles table.
|
|
|
|
|
try {
|
|
|
|
|
const adapter = ndk?.cacheAdapter?.adapter;
|
|
|
|
|
if (adapter?.db?.profiles) {
|
|
|
|
|
const allServingPubkeys = new Set();
|
|
|
|
|
for (const servingSet of relayServesPubkeys.values()) {
|
|
|
|
|
for (const pk of servingSet) allServingPubkeys.add(pk);
|
|
|
|
|
}
|
|
|
|
|
// Query profiles table for all serving pubkeys.
|
|
|
|
|
for (const pk of allServingPubkeys) {
|
|
|
|
|
try {
|
|
|
|
|
const row = await adapter.db.profiles.get(pk);
|
|
|
|
|
@@ -6555,6 +6560,42 @@ async function handleGetDiscoveredRelays(requestId, port) {
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
|
|
|
|
// Strategy 2: If Dexie profiles didn't have names for everyone,
|
|
|
|
|
// query kind 0 events from the NDK cache for the missing pubkeys.
|
|
|
|
|
const missingPubkeys = Array.from(allServingPubkeys).filter(pk => !pubkeyToName.has(pk));
|
|
|
|
|
if (missingPubkeys.length > 0) {
|
|
|
|
|
try {
|
|
|
|
|
// Query in batches of 400.
|
|
|
|
|
for (let i = 0; i < missingPubkeys.length; i += 400) {
|
|
|
|
|
const batch = missingPubkeys.slice(i, i + 400);
|
|
|
|
|
const k0Events = await ndk.fetchEvents(
|
|
|
|
|
{ kinds: [0], authors: batch },
|
|
|
|
|
{ cacheUsage: 'ONLY_CACHE' }
|
|
|
|
|
);
|
|
|
|
|
if (k0Events && k0Events.size > 0) {
|
|
|
|
|
// For each author, keep only the latest kind 0.
|
|
|
|
|
const latestByAuthor = new Map();
|
|
|
|
|
for (const evt of k0Events) {
|
|
|
|
|
const existing = latestByAuthor.get(evt.pubkey);
|
|
|
|
|
if (!existing || (evt.created_at || 0) > (existing.created_at || 0)) {
|
|
|
|
|
latestByAuthor.set(evt.pubkey, evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const [pubkey, evt] of latestByAuthor) {
|
|
|
|
|
try {
|
|
|
|
|
const profile = typeof evt.content === 'string'
|
|
|
|
|
? JSON.parse(evt.content) : evt.content;
|
|
|
|
|
const name = profile?.display_name || profile?.name || '';
|
|
|
|
|
if (name) pubkeyToName.set(pubkey, name);
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('[Worker] getDiscoveredRelays: profile names resolved for', pubkeyToName.size, 'of', allServingPubkeys.size, 'pubkeys');
|
|
|
|
|
|
|
|
|
|
// Build the discovered relays list: relays that are NOT in the
|
|
|
|
|
// user's own kind 10002 but ARE in follows' kind 10002.
|
|
|
|
|
const relays = [];
|
|
|
|
|
|