Fix relay profile loading, add getTopicTags, allow mapping events to multiple items

This commit is contained in:
Jon Staab
2024-09-11 09:15:03 -07:00
parent bece689530
commit 4e113faa1a
7 changed files with 62 additions and 40 deletions
+4 -6
View File
@@ -80,20 +80,18 @@ export const {
load: batcher(800, async (nip05s: string[]) => {
const fresh = await fetchHandles(uniq(nip05s))
const stale = handlesByNip05.get()
const items: Handle[] = nip05s.map(nip05 => {
for (const nip05 of nip05s) {
const newHandle = fresh.get(nip05)
const oldHandle = stale.get(nip05)
if (newHandle) {
stale.set(nip05, {...newHandle, nip05})
}
return {...oldHandle, ...newHandle, nip05}
})
}
handles.set(Array.from(stale.values()))
return items
return nip05s
}),
})
+15 -12
View File
@@ -1,6 +1,6 @@
import {writable, derived} from 'svelte/store'
import {withGetter} from '@welshman/store'
import {ctx, groupBy, indexBy, batch, now, uniq, uniqBy, batcher, postJson} from '@welshman/lib'
import {ctx, groupBy, indexBy, batch, now, uniq, batcher, postJson} from '@welshman/lib'
import type {RelayProfile} from "@welshman/util"
import {AuthStatus, asMessage, type Connection, type SocketMessage} from '@welshman/net'
import {createSearch} from './util'
@@ -43,14 +43,14 @@ export const relaysByPubkey = derived(relays, $relays =>
),
)
export const fetchRelayProfiles = (urls: string[]) => {
export const fetchRelayProfiles = async (urls: string[]) => {
const base = ctx.app.dufflepudUrl!
if (!base) {
throw new Error("ctx.app.dufflepudUrl is required to fetch relay metadata")
}
const res: any = postJson(`${base}/relay/info`, {urls})
const res: any = await postJson(`${base}/relay/info`, {urls})
const profilesByUrl = new Map<string, RelayProfile>()
for (const {url, info} of res?.data || []) {
@@ -69,18 +69,21 @@ export const {
store: relays,
getKey: (relay: Relay) => relay.url,
load: batcher(800, async (urls: string[]) => {
const profilesByUrl = await fetchRelayProfiles(uniq(urls))
const index = relaysByUrl.get()
const items: Relay[] = urls.map(url => {
const relay = index.get(url)
const profile = profilesByUrl.get(url) || relay?.profile
const fresh = await fetchRelayProfiles(uniq(urls))
const stale = relaysByUrl.get()
return {...relay, profile, url}
})
for (const url of urls) {
const relay = stale.get(url)
const profile = fresh.get(url)
relays.update($relays => uniqBy($relay => $relay.url, [...$relays, ...items]))
if (profile) {
stale.set(url, {...relay, profile, url})
}
}
return items
relays.set(Array.from(stale.values()))
return urls
}),
})
+10
View File
@@ -131,4 +131,14 @@ export const storageAdapters = {
fromPairs(data.map(({key, value}) => [key, value])),
}),
}),
fromMapStore: <T>(store: Writable<Map<string, T>>) => ({
keyPath: "key",
store: adapter({
store: store,
forward: ($data: Map<string, T>) =>
Array.from($data.entries()).map(([key, value]) => ({key, value})),
backward: (data: {key: string, value: T}[]) =>
new Map(data.map(({key, value}) => [key, value])),
}),
}),
}
+4 -6
View File
@@ -54,20 +54,18 @@ export const {
load: batcher(800, async (lnurls: string[]) => {
const fresh = await fetchZappers(uniq(lnurls))
const stale = zappersByLnurl.get()
const items: Zapper[] = lnurls.map(lnurl => {
for (const lnurl of lnurls) {
const newZapper = fresh.get(lnurl)
const oldZapper = stale.get(lnurl)
if (newZapper) {
stale.set(lnurl, {...newZapper, lnurl})
}
return {...oldZapper, ...newZapper, lnurl}
})
}
zappers.set(Array.from(stale.values()))
return items
return lnurls
}),
})