Improve DVM feeds

This commit is contained in:
Jon Staab
2024-04-25 15:42:48 -07:00
parent d2ac9d1839
commit b88c074d86
11 changed files with 71 additions and 28 deletions
+19 -7
View File
@@ -1,4 +1,4 @@
import {uniq, now, isNil} from '@welshman/lib'
import {uniq, tryCatch, now, isNil} from '@welshman/lib'
import type {Rumor, Filter} from '@welshman/util'
import {Tags, getIdFilters, mergeFilters} from '@welshman/util'
import type {RequestItem, DVMItem, Scope, Feed, DynamicFilter, FeedOptions} from './core'
@@ -82,9 +82,10 @@ export class FeedCompiler<E extends Rumor> {
await this.options.request({
relays: [],
filters: getIdFilters(addresses),
onEvent: events.push,
onEvent: (e: E) => events.push(e),
})
return {
relays: [],
filters: this._getFiltersFromTags(Tags.fromEvents(events)),
@@ -92,21 +93,27 @@ export class FeedCompiler<E extends Rumor> {
}
async _compileDvms(requests: DVMItem[]): Promise<RequestItem> {
const events: E[] = []
const responseTags: Tags[] = []
await Promise.all(
requests.map(request =>
this.options.requestDvm({
tags: [],
...request,
onEvent: events.push,
onEvent: async (e: E) => {
const tags = Tags.fromEvent(e)
const {id, pubkey} = await tryCatch(() => JSON.parse(tags.get("request")?.value())) || {}
responseTags.push(tags.filterByKey(["t", "p", "e", "a"]).rejectByValue([id, pubkey]))
},
})
)
)
const mergedTags = Tags.from(responseTags.flatMap(tags => tags.valueOf()))
return {
relays: [],
filters: this._getFiltersFromTags(Tags.fromEvents(events)),
relays: mergedTags.relays().valueOf(),
filters: this._getFiltersFromTags(mergedTags),
}
}
@@ -160,6 +167,11 @@ export class FeedCompiler<E extends Rumor> {
}
}
// If we don't have any filters, return nothing instead of everything
if (filters.length === 0) {
filters.push({authors: []})
}
return filters
}
}
+1
View File
@@ -90,6 +90,7 @@ export type RequestOpts<E> = RequestItem & {
export type DVMItem = {
kind: number
tags?: string[][]
relays?: string[]
}
export type DVMOpts<E> = DVMItem & {
+5
View File
@@ -39,6 +39,11 @@ export class FeedLoader<E extends Rumor> {
}
async _getRequestLoader({relays, filters}: RequestItem, {onEvent, onExhausted}: LoadOpts<E>) {
// Make sure we have some kind of filter to send if we've been given an empty one, as happens with relay feeds
if (filters.length === 0) {
filters = [{}]
}
const untils = filters.flatMap((filter: Filter) => filter.until ? [filter.until] : [])
const sinces = filters.flatMap((filter: Filter) => filter.since ? [filter.since] : [])
const maxUntil = untils.length === filters.length ? max(untils) : now()
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/feeds",
"version": "0.0.1",
"version": "0.0.2",
"author": "hodlbod",
"license": "MIT",
"description": "Utilities for building dynamic nostr feeds.",
@@ -31,6 +31,6 @@
"typescript": "~5.1.6"
},
"dependencies": {
"@welshman/util": "0.0.1"
"@welshman/util": "0.0.2"
}
}