Get dvms working better
This commit is contained in:
@@ -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<void>(resolve => {
|
||||
req.emitter.on("result", (url, event) => {
|
||||
req.emitter.on(DVMEvent.Result, (url, event) => {
|
||||
onEvent(event)
|
||||
resolve()
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -25,7 +25,7 @@ export const identity = <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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user