From e72b692a02d8e316128376af0a74fc046371d005 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Wed, 5 Jun 2024 14:59:29 -0700 Subject: [PATCH] Add label feeds --- packages/feeds/compiler.ts | 40 ++++++++++++++++++++++++++++++++++++-- packages/feeds/core.ts | 10 ++++++++++ packages/feeds/utils.ts | 6 ++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/packages/feeds/compiler.ts b/packages/feeds/compiler.ts index 79d2d29..d7e9db7 100644 --- a/packages/feeds/compiler.ts +++ b/packages/feeds/compiler.ts @@ -1,7 +1,7 @@ import {uniq, identity, flatten, pushToMapKey, intersection, tryCatch, now} from '@welshman/lib' import type {TrustedEvent, Filter} from '@welshman/util' -import {Tags, intersectFilters, getAddress, getIdFilters, unionFilters} from '@welshman/util' -import type {CreatedAtItem, RequestItem, ListItem, WOTItem, DVMItem, Scope, Feed, FeedOptions} from './core' +import {Tags, intersectFilters, matchFilter, getAddress, getIdFilters, unionFilters} from '@welshman/util' +import type {CreatedAtItem, RequestItem, ListItem, LabelItem, WOTItem, DVMItem, Scope, Feed, FeedOptions} from './core' import {getFeedArgs, feedsFromTags} from './utils' import {FeedType} from './core' @@ -20,6 +20,7 @@ export class FeedCompiler { case FeedType.ID: case FeedType.Kind: case FeedType.List: + case FeedType.Label: case FeedType.Relay: case FeedType.Scope: case FeedType.Search: @@ -39,6 +40,7 @@ export class FeedCompiler { case FeedType.DVM: return await this._compileDvms(getFeedArgs(feed)) case FeedType.Intersection: return await this._compileIntersection(getFeedArgs(feed)) case FeedType.List: return await this._compileLists(getFeedArgs(feed)) + case FeedType.Label: return await this._compileLabels(getFeedArgs(feed)) case FeedType.Union: return await this._compileUnion(getFeedArgs(feed)) case FeedType.Address: return this._compileAddresses(getFeedArgs(feed)) case FeedType.CreatedAt: return this._compileCreatedAt(getFeedArgs(feed)) @@ -240,4 +242,38 @@ export class FeedCompiler { return this._compileUnion(feeds) } + + async _compileLabels(labelItems: LabelItem[]): Promise { + const events: E[] = [] + + await Promise.all( + labelItems.map(({mappings, relays, ...filter}) => + this.options.request({ + relays, + filters: [{kinds: [1985], ...filter}], + onEvent: (e: E) => events.push(e), + }) + ) + ) + + const feeds = flatten( + await Promise.all( + labelItems.map(({mappings, relays, ...filter}) => { + const tags: string[][] = [] + + for (const event of events) { + if (matchFilter(filter, event)) { + for (const tag of event.tags) { + tags.push(tag) + } + } + } + + return feedsFromTags(Tags.wrap(tags), mappings) + }) + ) + ) + + return this._compileUnion(feeds) + } } diff --git a/packages/feeds/core.ts b/packages/feeds/core.ts index 27af9cc..64badbc 100644 --- a/packages/feeds/core.ts +++ b/packages/feeds/core.ts @@ -10,6 +10,7 @@ export enum FeedType { Intersection = "intersection", Kind = "kind", List = "list", + Label = "label", WOT = "wot", Relay = "relay", Scope = "scope", @@ -48,6 +49,13 @@ export type ListItem = { mappings?: TagFeedMapping[], } +export type LabelItem = { + relays?: string[], + authors?: string[] + [key: `#${string}`]: string[] + mappings?: TagFeedMapping[], +} + export type WOTItem = { min?: number, max?: number, @@ -68,6 +76,7 @@ export type IDFeed = [type: FeedType.ID, ...ids: string[]] export type IntersectionFeed = [type: FeedType.Intersection, ...feeds: Feed[]] export type KindFeed = [type: FeedType.Kind, ...kinds: number[]] export type ListFeed = [type: FeedType.List, ...items: ListItem[]] +export type LabelFeed = [type: FeedType.Label, ...items: LabelItem[]] export type WOTFeed = [type: FeedType.WOT, ...items: WOTItem[]] export type RelayFeed = [type: FeedType.Relay, ...urls: string[]] export type ScopeFeed = [type: FeedType.Scope, ...scopes: Scope[]] @@ -86,6 +95,7 @@ export type Feed = IntersectionFeed | KindFeed | ListFeed | + LabelFeed | WOTFeed | RelayFeed | ScopeFeed | diff --git a/packages/feeds/utils.ts b/packages/feeds/utils.ts index 6f1cc9a..2d6d43b 100644 --- a/packages/feeds/utils.ts +++ b/packages/feeds/utils.ts @@ -13,6 +13,7 @@ import { IntersectionFeed, KindFeed, ListFeed, + LabelFeed, WOTFeed, RelayFeed, ScopeFeed, @@ -25,6 +26,7 @@ import { WOTItem, DVMItem, ListItem, + LabelItem, CreatedAtItem, } from './core' @@ -37,6 +39,7 @@ export const makeIDFeed = (...ids: string[]): IDFeed export const makeIntersectionFeed = (...feeds: Feed[]): IntersectionFeed => [FeedType.Intersection, ...feeds] export const makeKindFeed = (...kinds: number[]): KindFeed => [FeedType.Kind, ...kinds] export const makeListFeed = (...items: ListItem[]): ListFeed => [FeedType.List, ...items] +export const makeLabelFeed = (...items: LabelItem[]): LabelFeed => [FeedType.Label, ...items] export const makeWOTFeed = (...items: WOTItem[]): WOTFeed => [FeedType.WOT, ...items] export const makeRelayFeed = (...urls: string[]): RelayFeed => [FeedType.Relay, ...urls] export const makeScopeFeed = (...scopes: Scope[]): ScopeFeed => [FeedType.Scope, ...scopes] @@ -54,6 +57,7 @@ export const isIDFeed = (feed: Feed): feed is IDFeed export const isIntersectionFeed = (feed: Feed): feed is IntersectionFeed => feed[0] === FeedType.Intersection export const isKindFeed = (feed: Feed): feed is KindFeed => feed[0] === FeedType.Kind export const isListFeed = (feed: Feed): feed is ListFeed => feed[0] === FeedType.List +export const isLabelFeed = (feed: Feed): feed is LabelFeed => feed[0] === FeedType.Label export const isWOTFeed = (feed: Feed): feed is WOTFeed => feed[0] === FeedType.WOT export const isRelayFeed = (feed: Feed): feed is RelayFeed => feed[0] === FeedType.Relay export const isScopeFeed = (feed: Feed): feed is ScopeFeed => feed[0] === FeedType.Scope @@ -66,6 +70,7 @@ export function getFeedArgs(feed: IntersectionFeed | UnionFeed | DifferenceFeed export function getFeedArgs(feed: AddressFeed | AuthorFeed | IDFeed | RelayFeed | SearchFeed): string[] export function getFeedArgs(feed: CreatedAtFeed): CreatedAtItem[] export function getFeedArgs(feed: ListFeed): ListItem[] +export function getFeedArgs(feed: LabelFeed): LabelItem[] export function getFeedArgs(feed: DVMFeed): DVMItem[] export function getFeedArgs(feed: WOTFeed): WOTItem[] export function getFeedArgs(feed: ScopeFeed): Scope[] @@ -85,6 +90,7 @@ export function getFeedArgs(feed: Feed) { case FeedType.Tag: return feed.slice(1) as [string, ...string[]] case FeedType.CreatedAt: return feed.slice(1) as CreatedAtItem[] case FeedType.List: return feed.slice(1) as ListItem[] + case FeedType.Label: return feed.slice(1) as LabelItem[] case FeedType.DVM: return feed.slice(1) as DVMItem[] case FeedType.WOT: return feed.slice(1) as WOTItem[] case FeedType.Scope: return feed.slice(1) as Scope[]