diff --git a/packages/feeds/README.md b/packages/feeds/README.md index c7fc409..43132a5 100644 --- a/packages/feeds/README.md +++ b/packages/feeds/README.md @@ -14,7 +14,7 @@ const loader = new FeedLoader({ }) // Define a feed using set operations -const feed = intersectFeed( +const feed = intersectionFeed( unionFeed( dvmFeed({ kind: 5300, @@ -22,10 +22,8 @@ const feed = intersectFeed( }), listFeed("10003:19ba654f26afd4930fd3d51baf4e26f1413b7aeec7190cd6c0cdf4d2f14cec6b:"), ) - filterFeed({ - min_wot: 0.1, - scopes: ["global"], - }), + wotFeed({min: 0.1}), + scopeFeed("global"), ) // Load notes using the feed diff --git a/packages/feeds/compiler.ts b/packages/feeds/compiler.ts index d91e963..5c7c418 100644 --- a/packages/feeds/compiler.ts +++ b/packages/feeds/compiler.ts @@ -1,7 +1,7 @@ import {uniq, flatten, pushToMapKey, intersection, ensureNumber, tryCatch, now} from '@welshman/lib' import type {Rumor, Filter} from '@welshman/util' import {Tags, intersectFilters, getAddress, getIdFilters, unionFilters} from '@welshman/util' -import type {RequestItem, TagFilterMapping, ListItem, DVMItem, Scope, Feed, FeedOptions} from './core' +import type {WOTItem, RequestItem, TagFilterMapping, ListItem, DVMItem, Scope, Feed, FeedOptions} from './core' import {FeedType, getSubFeeds} from './core' export class FeedCompiler { @@ -19,7 +19,7 @@ export class FeedCompiler { switch(type) { case FeedType.Union: case FeedType.Intersection: - return getSubFeeds([type, ...feed] as Feed).every(this.canCompile) + return getSubFeeds([type, ...feed] as Feed).every(f => this.canCompile(f)) case FeedType.Address: case FeedType.Author: case FeedType.DVM: @@ -30,10 +30,7 @@ export class FeedCompiler { case FeedType.Scope: case FeedType.Since: case FeedType.SinceAgo: - case FeedType.ATag: - case FeedType.ETag: - case FeedType.PTag: - case FeedType.TTag: + case FeedType.Tag: case FeedType.Until: case FeedType.UntilAgo: case FeedType.WOT: @@ -56,14 +53,11 @@ export class FeedCompiler { case FeedType.Scope: return this._compileScopes(feed as Scope[]) case FeedType.Since: return this._compileFilter("since", feed[0] as number) case FeedType.SinceAgo: return this._compileFilter("since", now() - (feed[0] as number)) - case FeedType.ATag: return this._compileFilter("#a", feed as string[]) - case FeedType.ETag: return this._compileFilter("#e", feed as string[]) - case FeedType.PTag: return this._compileFilter("#p", feed as string[]) - case FeedType.TTag: return this._compileFilter("#t", feed as string[]) + case FeedType.Tag: return this._compileFilter(feed[0] as string, feed.slice(1) as string[]) case FeedType.Until: return this._compileFilter("until", feed[0] as number) case FeedType.UntilAgo: return this._compileFilter("until", now() - (feed[0] as number)) case FeedType.Union: return await this._compileUnion(feed as Feed[]) - case FeedType.WOT: return this._compileWot(feed[0] as number, feed[1] as number) + case FeedType.WOT: return this._compileWot(feed as WOTItem) default: throw new Error(`Unable to convert feed of type ${type} to filters`) } @@ -81,8 +75,8 @@ export class FeedCompiler { return [{filters: [{authors: uniq(scopes.flatMap(this.options.getPubkeysForScope))}]}] } - _compileWot(min_wot: number, max_wot: number) { - return [{filters: [{authors: this.options.getPubkeysForWotRange(min_wot, max_wot)}]}] + _compileWot({min = 0, max = 1}) { + return [{filters: [{authors: this.options.getPubkeysForWotRange(min, max)}]}] } async _compileDvms(items: DVMItem[]): Promise { @@ -109,7 +103,7 @@ export class FeedCompiler { } async _compileIntersection(feeds: Feed[]): Promise { - const [head, ...tail] = await Promise.all(feeds.map(this.compile)) + const [head, ...tail] = await Promise.all(feeds.map(f => this.compile(f))) const result = [] diff --git a/packages/feeds/core.ts b/packages/feeds/core.ts index f8f71fe..3f8364a 100644 --- a/packages/feeds/core.ts +++ b/packages/feeds/core.ts @@ -15,10 +15,7 @@ export enum FeedType { Since = "since", SinceAgo = "since_ago", SymmetricDifference = "symmetric_difference", - ATag = "#a", - ETag = "#e", - PTag = "#p", - TTag = "#t", + Tag = "tag", Union = "union", Until = "until", UntilAgo = "until_ago", @@ -31,20 +28,13 @@ export enum Scope { Self = "self", } -export type TagFeedType = - FeedType.ATag | - FeedType.ETag | - FeedType.PTag | - FeedType.TTag - - export type FilterFeedType = FeedType.ID | FeedType.Address | FeedType.Author | FeedType.Kind | FeedType.Relay | - TagFeedType + FeedType.Tag export type TagFilterMapping = [string, FilterFeedType] @@ -61,8 +51,8 @@ export type ListItem = { } export type WOTItem = { - min: number, - max: number, + min?: number, + max?: number, } export type AddressFeed = [type: FeedType.Address, ...addresses: string[]] @@ -79,10 +69,7 @@ export type ScopeFeed = [type: FeedType.Scope, ...scopes: Scope[]] export type SinceAgoFeed = [type: FeedType.SinceAgo, since_ago: number] export type SinceFeed = [type: FeedType.Since, since: number] export type SymmetricDifferenceFeed = [type: FeedType.SymmetricDifference, ...feeds: Feed[]] -export type ATagFeed = [type: FeedType.ATag, ...addresses: string[]] -export type ETagFeed = [type: FeedType.ETag, ...ids: string[]] -export type PTagFeed = [type: FeedType.PTag, ...pubkeys: string[]] -export type TTagFeed = [type: FeedType.TTag, ...topics: string[]] +export type TagFeed = [type: FeedType.Tag, key: string, ...values: string[]] export type UnionFeed = [type: FeedType.Union, ...feeds: Feed[]] export type UntilAgoFeed = [type: FeedType.UntilAgo, until_ago: number] export type UntilFeed = [type: FeedType.Until, until: number] @@ -102,10 +89,7 @@ export type Feed = SinceAgoFeed | SinceFeed | SymmetricDifferenceFeed | - ATagFeed | - ETagFeed | - PTagFeed | - TTagFeed | + TagFeed | UnionFeed | UntilAgoFeed | UntilFeed @@ -124,10 +108,7 @@ export const scopeFeed = (...scopes: Scope[]): ScopeFeed => [FeedType.Scope, ... export const sinceAgoFeed = (since_ago: number): SinceAgoFeed => [FeedType.SinceAgo, since_ago] export const sinceFeed = (since: number): SinceFeed => [FeedType.Since, since] export const symmetricDifferenceFeed = (...feeds: Feed[]): SymmetricDifferenceFeed => [FeedType.SymmetricDifference, ...feeds] -export const aTagFeed = (...values: string[]): ATagFeed => [FeedType.ATag, ...values] -export const eTagFeed = (...values: string[]): ETagFeed => [FeedType.ETag, ...values] -export const pTagFeed = (...values: string[]): PTagFeed => [FeedType.PTag, ...values] -export const tTagFeed = (...values: string[]): TTagFeed => [FeedType.TTag, ...values] +export const tagFeed = (key: string, ...values: string[]): TagFeed => [FeedType.Tag, key, ...values] export const unionFeed = (...feeds: Feed[]): UnionFeed => [FeedType.Union, ...feeds] export const untilAgoFeed = (until_ago: number): UntilAgoFeed => [FeedType.UntilAgo, until_ago] export const untilFeed = (until: number): UntilFeed => [FeedType.Until, until] @@ -141,10 +122,7 @@ export const feedsFromFilter = (filter: Filter) => { else if (k === 'authors') feeds.push(authorFeed(...v as string[])) else if (k === 'since') feeds.push(sinceFeed(v as number)) else if (k === 'until') feeds.push(untilFeed(v as number)) - else if (k === "#a") feeds.push(aTagFeed(...v as string[])) - else if (k === "#e") feeds.push(eTagFeed(...v as string[])) - else if (k === "#p") feeds.push(pTagFeed(...v as string[])) - else if (k === "#t") feeds.push(tTagFeed(...v as string[])) + else if (k.startsWith('#')) feeds.push(tagFeed(k as string, ...v as string[])) else throw new Error(`Unable to create feed from filter ${k}: ${v}`) }