Get dvms working better

This commit is contained in:
Jon Staab
2024-11-21 16:42:26 -08:00
parent 2fbcfc59a1
commit 976aa375cd
3 changed files with 24 additions and 14 deletions
+22 -5
View File
@@ -1,8 +1,8 @@
import {ctx, now} from '@welshman/lib' import {ctx, nthEq, now} from '@welshman/lib'
import {createEvent, getPubkeyTagValues} from '@welshman/util' import {createEvent, getPubkeyTagValues} from '@welshman/util'
import {Scope, FeedController} from '@welshman/feeds' import {Scope, FeedController} from '@welshman/feeds'
import type {RequestOpts, FeedOptions, DVMOpts, Feed} from '@welshman/feeds' import type {RequestOpts, FeedOptions, DVMOpts, Feed} from '@welshman/feeds'
import {makeDvmRequest} from '@welshman/dvm' import {makeDvmRequest, DVMEvent} from '@welshman/dvm'
import {makeSecret, Nip01Signer} from '@welshman/signer' import {makeSecret, Nip01Signer} from '@welshman/signer'
import {pubkey, signer} from './session' import {pubkey, signer} from './session'
import {getFilterSelections} from './router' import {getFilterSelections} from './router'
@@ -29,18 +29,35 @@ export const requestDVM = async ({kind, onEvent, ...request}: DVMOpts) => {
} }
} }
const tags = [...request.tags || [], ["expiration", String(now() + 60)]] const tags = request.tags || []
const $signer = signer.get() || new Nip01Signer(makeSecret()) const $signer = signer.get() || new Nip01Signer(makeSecret())
const event = await $signer.sign(createEvent(kind, {tags})) const pubkey = await $signer.getPubkey()
const relays = const relays =
request.relays request.relays
? ctx.app.router.FromRelays(request.relays).getUrls() ? ctx.app.router.FromRelays(request.relays).getUrls()
: ctx.app.router.FromPubkeys(getPubkeyTagValues(tags)).getUrls() : ctx.app.router.FromPubkeys(getPubkeyTagValues(tags)).getUrls()
if (!tags.some(nthEq(0, 'expiration'))) {
tags.push(["expiration", String(now() + 60)])
}
if (!tags.some(nthEq(0, 'relays'))) {
tags.push(["relays", ...relays])
}
if (!tags.some(nthEq(1, 'user'))) {
tags.push(["param", "user", pubkey])
}
if (!tags.some(nthEq(1, 'max_results'))) {
tags.push(["param", "max_results", "200"])
}
const event = await $signer.sign(createEvent(kind, {tags}))
const req = makeDvmRequest({event, relays}) const req = makeDvmRequest({event, relays})
await new Promise<void>(resolve => { await new Promise<void>(resolve => {
req.emitter.on("result", (url, event) => { req.emitter.on(DVMEvent.Result, (url, event) => {
onEvent(event) onEvent(event)
resolve() resolve()
}) })
+1 -8
View File
@@ -29,18 +29,11 @@ export const makeDvmRequest = (request: DVMRequestOptions) => {
const {event, relays, timeout = 30_000, autoClose = true, reportProgress = true} = request const {event, relays, timeout = 30_000, autoClose = true, reportProgress = true} = request
const kind = event.kind + 1000 const kind = event.kind + 1000
const kinds = reportProgress ? [kind, 7000] : [kind] const kinds = reportProgress ? [kind, 7000] : [kind]
const filters: Filter[] = [{kinds, since: now() - 60, "#e": [event.id]}]
// DVMs seem to no longer be responding to requests, but lots of events exist for their
// pubkeys. So query both, at least until people figure out how dvms are supposed to work
const filters: Filter[] = [
{kinds, authors: getPubkeyTagValues(event.tags)},
{kinds, since: now() - 60, "#e": [event.id]},
]
const sub = subscribe({relays, timeout, filters}) const sub = subscribe({relays, timeout, filters})
const pub = publish({event, relays, timeout}) const pub = publish({event, relays, timeout})
sub.emitter.on(SubscriptionEvent.Event, (url: string, event: TrustedEvent) => { sub.emitter.on(SubscriptionEvent.Event, (url: string, event: TrustedEvent) => {
if (event.kind === 7000) { if (event.kind === 7000) {
emitter.emit(DVMEvent.Progress, url, event) emitter.emit(DVMEvent.Progress, url, event)
+1 -1
View File
@@ -25,7 +25,7 @@ export const identity = <T>(x: T, ...args: unknown[]) => x
export const always = <T>(x: T, ...args: unknown[]) => () => x export const always = <T>(x: T, ...args: unknown[]) => () => x
export const not = (x: boolean, ...args: unknown[]) => !x export const not = (x: any, ...args: unknown[]) => !x
export const num = (x: Maybe<number>) => x || 0 export const num = (x: Maybe<number>) => x || 0