diff --git a/packages/feeds/compiler.ts b/packages/feeds/compiler.ts index 5c7c418..b1ca500 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 {uniq, identity, 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 {WOTItem, RequestItem, TagFilterMapping, ListItem, DVMItem, Scope, Feed, FeedOptions} from './core' +import type {WOTItem, CreatedAtItem, RequestItem, TagFilterMapping, ListItem, DVMItem, Scope, Feed, FeedOptions} from './core' import {FeedType, getSubFeeds} from './core' export class FeedCompiler { @@ -22,17 +22,15 @@ export class FeedCompiler { return getSubFeeds([type, ...feed] as Feed).every(f => this.canCompile(f)) case FeedType.Address: case FeedType.Author: + case FeedType.CreatedAt: case FeedType.DVM: case FeedType.ID: case FeedType.Kind: case FeedType.List: case FeedType.Relay: case FeedType.Scope: - case FeedType.Since: - case FeedType.SinceAgo: + case FeedType.Search: case FeedType.Tag: - case FeedType.Until: - case FeedType.UntilAgo: case FeedType.WOT: return true default: @@ -43,6 +41,7 @@ export class FeedCompiler { async compile([type, ...feed]: Feed): Promise { switch(type) { case FeedType.Address: return this._compileAddresses(feed as string[]) + case FeedType.CreatedAt: return this._compileCreatedAt(feed as CreatedAtItem[]) case FeedType.Author: return this._compileFilter("authors", feed as string[]) case FeedType.DVM: return await this._compileDvms(feed as DVMItem[]) case FeedType.ID: return this._compileFilter("ids", feed as string[]) @@ -51,11 +50,8 @@ export class FeedCompiler { case FeedType.List: return await this._compileLists(feed as ListItem[]) case FeedType.Relay: return [{relays: feed as string[]}] 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.Search: return this._compileSearches(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 as WOTItem) default: @@ -71,10 +67,38 @@ export class FeedCompiler { return [{filters: [{[key]: value} as Filter]}] } + _compileCreatedAt(items: CreatedAtItem[]) { + const filters = items + .map(({since, until, relative}) => { + if (relative) { + if (typeof since === 'number') { + since = now() - since + } + + if (typeof until === 'number') { + until = now() - until + } + } + + if (since && until) return {since, until} + if (since) return {since} + if (until) return {until} + + return null + }) + .filter(identity) + + return [{filters: filters as Filter[]}] + } + _compileScopes(scopes: Scope[]) { return [{filters: [{authors: uniq(scopes.flatMap(this.options.getPubkeysForScope))}]}] } + _compileSearches(searches: string[]) { + return [{filters: searches.map(search => ({search}))}] + } + _compileWot({min = 0, max = 1}) { return [{filters: [{authors: this.options.getPubkeysForWotRange(min, max)}]}] } diff --git a/packages/feeds/core.ts b/packages/feeds/core.ts index 3f8364a..26e0699 100644 --- a/packages/feeds/core.ts +++ b/packages/feeds/core.ts @@ -3,6 +3,7 @@ import type {Filter} from '@welshman/util' export enum FeedType { Address = "address", Author = "author", + CreatedAt = "created_at", DVM = "dvm", Difference = "difference", ID = "id", @@ -12,13 +13,10 @@ export enum FeedType { WOT = "wot", Relay = "relay", Scope = "scope", - Since = "since", - SinceAgo = "since_ago", + Search = "search", SymmetricDifference = "symmetric_difference", Tag = "tag", Union = "union", - Until = "until", - UntilAgo = "until_ago", } export enum Scope { @@ -55,8 +53,15 @@ export type WOTItem = { max?: number, } +export type CreatedAtItem = { + since?: number, + until?: number, + relative?: boolean, +} + export type AddressFeed = [type: FeedType.Address, ...addresses: string[]] export type AuthorFeed = [type: FeedType.Author, ...pubkeys: string[]] +export type CreatedAtFeed = [type: FeedType.CreatedAt, ...items: CreatedAtItem[]] export type DVMFeed = [type: FeedType.DVM, ...items: DVMItem[]] export type DifferenceFeed = [type: FeedType.Difference, ...feeds: Feed[]] export type IDFeed = [type: FeedType.ID, ...ids: string[]] @@ -66,17 +71,15 @@ export type ListFeed = [type: FeedType.List, ...items: ListItem[]] export type WOTFeed = [type: FeedType.WOT, ...items: WOTItem[]] export type RelayFeed = [type: FeedType.Relay, ...urls: string[]] 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 SearchFeed = [type: FeedType.Search, ...searches: string[]] export type SymmetricDifferenceFeed = [type: FeedType.SymmetricDifference, ...feeds: Feed[]] 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] export type Feed = AddressFeed | AuthorFeed | + CreatedAtFeed | DVMFeed | DifferenceFeed | IDFeed | @@ -86,16 +89,14 @@ export type Feed = WOTFeed | RelayFeed | ScopeFeed | - SinceAgoFeed | - SinceFeed | + SearchFeed | SymmetricDifferenceFeed | TagFeed | - UnionFeed | - UntilAgoFeed | - UntilFeed + UnionFeed export const addressFeed = (...addresses: string[]): AddressFeed => [FeedType.Address, ...addresses] export const authorFeed = (...pubkeys: string[]): AuthorFeed => [FeedType.Author, ...pubkeys] +export const createdAtFeed = (...items: CreatedAtItem[]): CreatedAtFeed => [FeedType.CreatedAt, ...items] export const dvmFeed = (...items: DVMItem[]): DVMFeed => [FeedType.DVM, ...items] export const differenceFeed = (...feeds: Feed[]): DifferenceFeed => [FeedType.Difference, ...feeds] export const idFeed = (...ids: string[]): IDFeed => [FeedType.ID, ...ids] @@ -105,23 +106,26 @@ export const listFeed = (...items: ListItem[]): ListFeed => [FeedType.List, ...i export const wotFeed = (...items: WOTItem[]): WOTFeed => [FeedType.WOT, ...items] export const relayFeed = (...urls: string[]): RelayFeed => [FeedType.Relay, ...urls] export const scopeFeed = (...scopes: Scope[]): ScopeFeed => [FeedType.Scope, ...scopes] -export const sinceAgoFeed = (since_ago: number): SinceAgoFeed => [FeedType.SinceAgo, since_ago] -export const sinceFeed = (since: number): SinceFeed => [FeedType.Since, since] +export const searchFeed = (...searches: string[]): SearchFeed => [FeedType.Search, ...searches] export const symmetricDifferenceFeed = (...feeds: Feed[]): SymmetricDifferenceFeed => [FeedType.SymmetricDifference, ...feeds] 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] -export const feedsFromFilter = (filter: Filter) => { +export const feedsFromFilter = ({since, until, ...filter}: Filter) => { const feeds = [] + if (since && until) { + feeds.push(createdAtFeed({since, until})) + } else if (since) { + feeds.push(createdAtFeed({since})) + } else if (until) { + feeds.push(createdAtFeed({until})) + } + for (const [k, v] of Object.entries(filter)) { if (k === 'ids') feeds.push(idFeed(...v as string[])) else if (k === 'kinds') feeds.push(kindFeed(...v as number[])) 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.startsWith('#')) feeds.push(tagFeed(k as string, ...v as string[])) else throw new Error(`Unable to create feed from filter ${k}: ${v}`) } @@ -131,7 +135,24 @@ export const feedsFromFilter = (filter: Filter) => { export const feedFromFilter = (filter: Filter) => intersectionFeed(...feedsFromFilter(filter)) -export const hasSubFeeds = ([type]: [FeedType]) => +export const isAddressFeed = (feed: Feed) => feed[0] === FeedType.Address +export const isAuthorFeed = (feed: Feed) => feed[0] === FeedType.Author +export const isCreatedAtFeed = (feed: Feed) => feed[0] === FeedType.CreatedAt +export const isDvmFeed = (feed: Feed) => feed[0] === FeedType.DVM +export const isDifferenceFeed = (feed: Feed) => feed[0] === FeedType.Difference +export const isIdFeed = (feed: Feed) => feed[0] === FeedType.ID +export const isIntersectionFeed = (feed: Feed) => feed[0] === FeedType.Intersection +export const isKindFeed = (feed: Feed) => feed[0] === FeedType.Kind +export const isListFeed = (feed: Feed) => feed[0] === FeedType.List +export const isWotFeed = (feed: Feed) => feed[0] === FeedType.WOT +export const isRelayFeed = (feed: Feed) => feed[0] === FeedType.Relay +export const isScopeFeed = (feed: Feed) => feed[0] === FeedType.Scope +export const isSearchFeed = (feed: Feed) => feed[0] === FeedType.Search +export const isSymmetricDifferenceFeed = (feed: Feed) => feed[0] === FeedType.SymmetricDifference +export const isTagFeed = (feed: Feed) => feed[0] === FeedType.Tag +export const isUnionFeed = (feed: Feed) => feed[0] === FeedType.Union + +export const hasSubFeeds = ([type]: [FeedType, ...any[]]) => [ FeedType.Union, FeedType.Intersection, diff --git a/packages/util/Router.ts b/packages/util/Router.ts index 071b6cf..ffc926b 100644 --- a/packages/util/Router.ts +++ b/packages/util/Router.ts @@ -82,7 +82,7 @@ export class Router { sortRelaySelections = (relaySelections: RelayValues[]) => { const scores = new Map() - const getScore = (relayValues: RelayValues) => scores.get(relayValues.relay) || 0 + const getScore = (relayValues: RelayValues) => -(scores.get(relayValues.relay) || 0) for (const relayValues of relaySelections) { scores.set(relayValues.relay, this.scoreRelaySelection(relayValues)) diff --git a/packages/util/Tags.ts b/packages/util/Tags.ts index e8aa31a..166f0ad 100644 --- a/packages/util/Tags.ts +++ b/packages/util/Tags.ts @@ -98,7 +98,11 @@ export class Tags extends (Fluent as OmitStatics, 'from' const dispatchTags = (thisTags: Tags) => thisTags.forEach((t: Tag, i: number) => { if (t.mark() === 'root') { - roots.push(t.valueOf()) + if (thisTags.whereMark("reply").count() === 0) { + replies.push(t.valueOf()) + } else { + roots.push(t.valueOf()) + } } else if (t.mark() === 'reply') { replies.push(t.valueOf()) } else if (t.mark() === 'mention') {