Fix load bypassing freshness

This commit is contained in:
Jon Staab
2024-09-02 15:57:40 -07:00
parent 9a5a423b3e
commit b910569fc0
8 changed files with 80 additions and 61 deletions
+32 -6
View File
@@ -1,9 +1,14 @@
import {RELAYS, getRelayTags, normalizeRelayUrl, type TrustedEvent} from '@welshman/util'
import {uniq} from '@welshman/lib'
import {INBOX_RELAYS, RELAYS, getRelayTags, normalizeRelayUrl, type TrustedEvent} from '@welshman/util'
import {type SubscribeRequest} from "@welshman/net"
import {deriveEvents} from '@welshman/store'
import {load, repository} from './core'
import {deriveEvents, withGetter} from '@welshman/store'
import {env, load, repository} from './core'
import {collection} from './collection'
export const getRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || [])
.map((t: string[]) => normalizeRelayUrl(t[1]))
export const getReadRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || [])
.filter((t: string[]) => !t[2] || t[2] === "read")
@@ -14,7 +19,7 @@ export const getWriteRelayUrls = (event?: TrustedEvent): string[] =>
.filter((t: string[]) => !t[2] || t[2] === "write")
.map((t: string[]) => normalizeRelayUrl(t[1]))
export const relaySelections = deriveEvents(repository, {filters: [{kinds: [RELAYS]}]})
export const relaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [RELAYS]}]}))
export const {
indexStore: relaySelectionsByPubkey,
@@ -24,10 +29,31 @@ export const {
name: "relaySelections",
store: relaySelections,
getKey: relaySelections => relaySelections.pubkey,
load: (pubkey: string, relays: string[], request: Partial<SubscribeRequest> = {}) =>
load: (pubkey: string, request: Partial<SubscribeRequest> = {}) =>
load({
...request,
relays,
filters: [{kinds: [RELAYS], authors: [pubkey]}],
relays: [...env.BOOTSTRAP_RELAYS, ...request.relays || []],
}),
})
export const getHintsForPubkey = async (pubkey: string, relays: string[] = []) =>
uniq([...relays, ...env.BOOTSTRAP_RELAYS, ...getWriteRelayUrls(await loadRelaySelections(pubkey, {relays}))])
export const inboxRelaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [RELAYS]}]}))
export const {
indexStore: inboxRelaySelectionsByPubkey,
deriveItem: deriveInboxRelaySelections,
loadItem: loadInboxRelaySelections,
} = collection({
name: "inboxRelaySelections",
store: inboxRelaySelections,
getKey: inboxRelaySelections => inboxRelaySelections.pubkey,
load: async (pubkey: string, request: Partial<SubscribeRequest> = {}) =>
load({
...request,
filters: [{kinds: [INBOX_RELAYS], authors: [pubkey]}],
relays: await getHintsForPubkey(pubkey, request.relays),
}),
})