Add label feeds

This commit is contained in:
Jon Staab
2024-06-05 14:59:29 -07:00
parent b06348ba35
commit e72b692a02
3 changed files with 54 additions and 2 deletions
+38 -2
View File
@@ -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<E extends TrustedEvent> {
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<E extends TrustedEvent> {
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<E extends TrustedEvent> {
return this._compileUnion(feeds)
}
async _compileLabels(labelItems: LabelItem[]): Promise<RequestItem[]> {
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)
}
}
+10
View File
@@ -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 |
+6
View File
@@ -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[]