Tweak outbox loader
This commit is contained in:
@@ -19,7 +19,7 @@ export class BlockedRelayLists extends RepositoryCollection<ReturnType<typeof re
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
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))
|
getBlockedRelays = (pubkey: string) => getRelaysFromList(this.get(pubkey))
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ export class BlossomServerLists extends RepositoryCollection<ReturnType<typeof r
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ export class FollowLists extends RepositoryCollection<ReturnType<typeof readList
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
fetch(pubkey: string, relayHints: string[] = []) {
|
||||||
return this.ctx.use(RelayLists).makeOutboxLoader(FOLLOWS)(pubkey, relayHints)
|
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [FOLLOWS]}, relayHints)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ export class MessagingRelayLists extends RepositoryCollection<ReturnType<typeof
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,6 @@ export class MuteLists extends RepositoryCollection<PublishedList> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
fetch(pubkey: string, relayHints: string[] = []) {
|
||||||
return this.ctx.use(RelayLists).makeOutboxLoader(MUTES)(pubkey, relayHints)
|
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [MUTES]}, relayHints)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ export class PinLists extends RepositoryCollection<ReturnType<typeof readList>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
fetch(pubkey: string, relayHints: string[] = []) {
|
||||||
return this.ctx.use(RelayLists).makeOutboxLoader(PINS)(pubkey, relayHints)
|
return this.ctx.use(RelayLists).loadUsingOutbox(pubkey, {kinds: [PINS]}, relayHints)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export class Profiles extends RepositoryCollection<ReturnType<typeof readProfile
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
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) =>
|
display = (pubkey: string | undefined) =>
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {
|
|||||||
asDecryptedEvent,
|
asDecryptedEvent,
|
||||||
readList,
|
readList,
|
||||||
getRelaysFromList,
|
getRelaysFromList,
|
||||||
isPlainReplaceableKind,
|
|
||||||
sortEventsDesc,
|
sortEventsDesc,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import type {Filter, TrustedEvent, PublishedList} 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
|
* NIP-65 relay lists, keyed by pubkey. This is the routing substrate every other
|
||||||
* outbox-model load depends on, so it also exposes `loadUsingOutbox` /
|
* outbox-model load depends on, so it also exposes `loadUsingOutbox` for other
|
||||||
* `makeOutboxLoader` for other collections to build their fetchers on. It and the
|
* collections to use as their fetcher.
|
||||||
* Router reference each other lazily via `ctx.use`, so the cycle never bites.
|
|
||||||
*/
|
*/
|
||||||
export class RelayLists extends RepositoryCollection<PublishedList> {
|
export class RelayLists extends RepositoryCollection<PublishedList> {
|
||||||
constructor(ctx: IClient) {
|
constructor(ctx: IClient) {
|
||||||
@@ -31,34 +29,34 @@ export class RelayLists extends RepositoryCollection<PublishedList> {
|
|||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
fetch(pubkey: string, relayHints: string[] = []) {
|
||||||
const filters = [{kinds: [RELAYS], authors: [pubkey], limit: 1}]
|
const filters = [{kinds: [RELAYS], authors: [pubkey], limit: 1}]
|
||||||
|
const networking = this.ctx.use(Networking)
|
||||||
const router = this.ctx.use(Router)
|
const router = this.ctx.use(Router)
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
this.ctx.use(Networking).load({filters, relays: router.FromRelays(relayHints).getUrls()}),
|
networking.load({filters, relays: router.FromRelays(relayHints).getUrls()}),
|
||||||
this.ctx.use(Networking).load({filters, relays: router.FromPubkey(pubkey).getUrls()}),
|
networking.load({filters, relays: router.FromPubkey(pubkey).getUrls()}),
|
||||||
this.ctx.use(Networking).load({filters, relays: router.Index().getUrls()}),
|
networking.load({filters, relays: router.Index().getUrls()}),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
getRelaysForPubkey = (pubkey: string, mode?: RelayMode) =>
|
getRelaysForPubkey = (pubkey: string, mode?: RelayMode) =>
|
||||||
getRelaysFromList(this.get(pubkey), mode)
|
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 = {}) => {
|
loadUsingOutbox = async (pubkey: string, filter: Filter = {}, relayHints: string[] = []) => {
|
||||||
const filters: Filter[] = [{...filter, kinds: [kind], authors: [pubkey]}]
|
const filters: Filter[] = [{...filter, authors: [pubkey]}]
|
||||||
const writeRelays = getRelaysFromList(await this.load(pubkey), RelayMode.Write)
|
const writeRelays = getRelaysFromList(await this.load(pubkey), RelayMode.Write)
|
||||||
const allRelays = this.ctx
|
const allRelays = this.ctx
|
||||||
.use(Router)
|
.use(Router)
|
||||||
.FromRelays(writeRelays)
|
.FromRelays([...relayHints, ...writeRelays])
|
||||||
.policy(addMinimalFallbacks)
|
.policy(addMinimalFallbacks)
|
||||||
.limit(8)
|
.limit(8)
|
||||||
.getUrls()
|
.getUrls()
|
||||||
|
|
||||||
if (isPlainReplaceableKind(kind)) {
|
|
||||||
filters[0].limit = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const relays of chunk(2, allRelays)) {
|
for (const relays of chunk(2, allRelays)) {
|
||||||
const events = await this.ctx.use(Networking).load({filters, relays})
|
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),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ export class SearchRelayLists extends RepositoryCollection<ReturnType<typeof rea
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetch(pubkey: string, relayHints: string[] = []) {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -345,4 +345,4 @@ export const makeLoader = (options: LoaderOptions) =>
|
|||||||
return allRequests.map(r => resultsByRequest.get(r) || [])
|
return allRequests.map(r => resultsByRequest.get(r) || [])
|
||||||
}) as Loader
|
}) as Loader
|
||||||
|
|
||||||
export const load = makeLoader({delay: 200, timeout: 3000, threshold: 0.5})
|
export const load = makeLoader({delay: 30, timeout: 3000, threshold: 0.5})
|
||||||
|
|||||||
Reference in New Issue
Block a user