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
+1 -1
View File
@@ -19,7 +19,7 @@ export class BlockedRelayLists extends RepositoryCollection<ReturnType<typeof re
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(BLOCKED_RELAYS)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [BLOCKED_RELAYS]}, relayHints)
}
getBlockedRelays = (pubkey: string) => getRelaysFromList(this.get(pubkey))
+1 -1
View File
@@ -18,6 +18,6 @@ export class BlossomServerLists extends RepositoryCollection<ReturnType<typeof r
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(BLOSSOM_SERVERS)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [BLOSSOM_SERVERS]}, relayHints)
}
}
+1 -1
View File
@@ -18,6 +18,6 @@ export class FollowLists extends RepositoryCollection<ReturnType<typeof readList
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(FOLLOWS)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [FOLLOWS]}, relayHints)
}
}
+1 -1
View File
@@ -19,6 +19,6 @@ export class MessagingRelayLists extends RepositoryCollection<ReturnType<typeof
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(MESSAGING_RELAYS)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [MESSAGING_RELAYS]}, relayHints)
}
}
+1 -1
View File
@@ -33,6 +33,6 @@ export class MuteLists extends RepositoryCollection<PublishedList> {
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(MUTES)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [MUTES]}, relayHints)
}
}
+1 -1
View File
@@ -18,6 +18,6 @@ export class PinLists extends RepositoryCollection<ReturnType<typeof readList>>
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(PINS)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [PINS]}, relayHints)
}
}
+1 -1
View File
@@ -18,7 +18,7 @@ export class Profiles extends RepositoryCollection<ReturnType<typeof readProfile
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(PROFILE)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [PROFILE]}, relayHints)
}
display = (pubkey: string | undefined) =>
+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),
])
}
}
+1 -1
View File
@@ -19,6 +19,6 @@ export class SearchRelayLists extends RepositoryCollection<ReturnType<typeof rea
}
fetch(pubkey: string, relayHints: string[] = []) {
return this.ctx.use(RelayLists).makeOutboxLoader(SEARCH_RELAYS)(pubkey, relayHints)
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [SEARCH_RELAYS]}, relayHints)
}
}
+1 -1
View File
@@ -345,4 +345,4 @@ export const makeLoader = (options: LoaderOptions) =>
return allRequests.map(r => resultsByRequest.get(r) || [])
}) as Loader
export const load = makeLoader({delay: 200, timeout: 3000, threshold: 0.5})
export const load = makeLoader({delay: 30, timeout: 3000, threshold: 0.5})