Tweak outbox loader

This commit is contained in:
Jon Staab
2026-06-16 15:00:01 -07:00
parent 9094d30b89
commit 163d2dc355
10 changed files with 22 additions and 36 deletions
+13 -27
View File
@@ -5,7 +5,6 @@ import {
asDecryptedEvent,
readList,
getRelaysFromList,
isPlainReplaceableKind,
sortEventsDesc,
} from "@welshman/util"
import type {Filter, TrustedEvent, PublishedList} from "@welshman/util"
@@ -16,9 +15,8 @@ import type {IClient} from "./client.js"
/**
* NIP-65 relay lists, keyed by pubkey. This is the routing substrate every other
* outbox-model load depends on, so it also exposes `loadUsingOutbox` /
* `makeOutboxLoader` for other collections to build their fetchers on. It and the
* Router reference each other lazily via `ctx.use`, so the cycle never bites.
* outbox-model load depends on, so it also exposes `loadUsingOutbox` for other
* collections to use as their fetcher.
*/
export class RelayLists extends RepositoryCollection<PublishedList> {
constructor(ctx: IClient) {
@@ -31,34 +29,34 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
fetch(pubkey: string, relayHints: string[] = []) {
const filters = [{kinds: [RELAYS], authors: [pubkey], limit: 1}]
const networking = this.ctx.use(Networking)
const router = this.ctx.use(Router)
return Promise.all([
this.ctx.use(Networking).load({filters, relays: router.FromRelays(relayHints).getUrls()}),
this.ctx.use(Networking).load({filters, relays: router.FromPubkey(pubkey).getUrls()}),
this.ctx.use(Networking).load({filters, relays: router.Index().getUrls()}),
networking.load({filters, relays: router.FromRelays(relayHints).getUrls()}),
networking.load({filters, relays: router.FromPubkey(pubkey).getUrls()}),
networking.load({filters, relays: router.Index().getUrls()}),
])
}
getRelaysForPubkey = (pubkey: string, mode?: RelayMode) =>
getRelaysFromList(this.get(pubkey), mode)
// Load a pubkey's events using their advertised write relays (outbox model)
// Load a pubkey's events using their advertised write relays (outbox model),
// plus any explicit relay hints. This is the fetcher outbox-loaded collections
// (profiles, follows, mutes, …) delegate to — it's a stable method, so calling
// it doesn't build a fresh loader per call.
loadUsingOutbox = async (kind: number, pubkey: string, filter: Filter = {}) => {
const filters: Filter[] = [{...filter, kinds: [kind], authors: [pubkey]}]
loadUsingOutbox = async (pubkey: string, filter: Filter = {}, relayHints: string[] = []) => {
const filters: Filter[] = [{...filter, authors: [pubkey]}]
const writeRelays = getRelaysFromList(await this.load(pubkey), RelayMode.Write)
const allRelays = this.ctx
.use(Router)
.FromRelays(writeRelays)
.FromRelays([...relayHints, ...writeRelays])
.policy(addMinimalFallbacks)
.limit(8)
.getUrls()
if (isPlainReplaceableKind(kind)) {
filters[0].limit = 1
}
for (const relays of chunk(2, allRelays)) {
const events = await this.ctx.use(Networking).load({filters, relays})
@@ -67,16 +65,4 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
}
}
}
makeOutboxLoader =
(kind: number, filter: Filter = {}) =>
async (pubkey: string, relayHints: string[] = []) => {
const filters: Filter[] = [{...filter, kinds: [kind], authors: [pubkey]}]
const relays = this.ctx.use(Router).FromRelays(relayHints).getUrls()
await Promise.all([
this.ctx.use(Networking).load({filters, relays}),
this.loadUsingOutbox(kind, pubkey, filter),
])
}
}