From 976aa375cdadcfbd8062c806d4fda9c0efbea7db Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 21 Nov 2024 16:42:26 -0800 Subject: [PATCH] Get dvms working better --- packages/app/src/feeds.ts | 27 ++++++++++++++++++++++----- packages/dvm/src/request.ts | 9 +-------- packages/lib/src/Tools.ts | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/app/src/feeds.ts b/packages/app/src/feeds.ts index 6d00ded..c84ece5 100644 --- a/packages/app/src/feeds.ts +++ b/packages/app/src/feeds.ts @@ -1,8 +1,8 @@ -import {ctx, now} from '@welshman/lib' +import {ctx, nthEq, now} from '@welshman/lib' import {createEvent, getPubkeyTagValues} from '@welshman/util' import {Scope, FeedController} 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 {pubkey, signer} from './session' 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 event = await $signer.sign(createEvent(kind, {tags})) + const pubkey = await $signer.getPubkey() const relays = request.relays ? ctx.app.router.FromRelays(request.relays).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}) await new Promise(resolve => { - req.emitter.on("result", (url, event) => { + req.emitter.on(DVMEvent.Result, (url, event) => { onEvent(event) resolve() }) diff --git a/packages/dvm/src/request.ts b/packages/dvm/src/request.ts index dd2f351..56f1772 100644 --- a/packages/dvm/src/request.ts +++ b/packages/dvm/src/request.ts @@ -29,18 +29,11 @@ export const makeDvmRequest = (request: DVMRequestOptions) => { const {event, relays, timeout = 30_000, autoClose = true, reportProgress = true} = request const kind = event.kind + 1000 const kinds = reportProgress ? [kind, 7000] : [kind] - - // 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 filters: Filter[] = [{kinds, since: now() - 60, "#e": [event.id]}] const sub = subscribe({relays, timeout, filters}) const pub = publish({event, relays, timeout}) - sub.emitter.on(SubscriptionEvent.Event, (url: string, event: TrustedEvent) => { if (event.kind === 7000) { emitter.emit(DVMEvent.Progress, url, event) diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index b3fec8e..10d8f0d 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -25,7 +25,7 @@ export const identity = (x: T, ...args: unknown[]) => x export const always = (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) => x || 0