Remove generics for event type
This commit is contained in:
Generated
+23
-4
@@ -601,6 +601,10 @@
|
|||||||
"resolved": "packages/content",
|
"resolved": "packages/content",
|
||||||
"link": true
|
"link": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@welshman/domain": {
|
||||||
|
"resolved": "packages/domain",
|
||||||
|
"link": true
|
||||||
|
},
|
||||||
"node_modules/@welshman/dvm": {
|
"node_modules/@welshman/dvm": {
|
||||||
"resolved": "packages/dvm",
|
"resolved": "packages/dvm",
|
||||||
"link": true
|
"link": true
|
||||||
@@ -3269,11 +3273,26 @@
|
|||||||
},
|
},
|
||||||
"packages/content": {
|
"packages/content": {
|
||||||
"name": "@welshman/content",
|
"name": "@welshman/content",
|
||||||
"version": "0.0.6",
|
"version": "0.0.7",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@braintree/sanitize-url": "^7.0.2",
|
"@braintree/sanitize-url": "^7.0.2",
|
||||||
"nostr-tools": "^2.7.0"
|
"nostr-tools": "^2.7.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"gts": "^5.0.1",
|
||||||
|
"tsc-multi": "^1.1.0",
|
||||||
|
"typescript": "~5.1.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"packages/domain": {
|
||||||
|
"name": "@welshman/domain",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@welshman/lib": "0.0.14",
|
||||||
|
"@welshman/util": "0.0.25",
|
||||||
|
"nostr-tools": "^2.7.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"gts": "^5.0.1",
|
"gts": "^5.0.1",
|
||||||
@@ -3289,7 +3308,7 @@
|
|||||||
"@welshman/lib": "0.0.14",
|
"@welshman/lib": "0.0.14",
|
||||||
"@welshman/net": "0.0.18",
|
"@welshman/net": "0.0.18",
|
||||||
"@welshman/util": "0.0.25",
|
"@welshman/util": "0.0.25",
|
||||||
"nostr-tools": "^2.7.0"
|
"nostr-tools": "^2.7.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"gts": "^5.0.1",
|
"gts": "^5.0.1",
|
||||||
@@ -3408,7 +3427,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@welshman/lib": "0.0.14",
|
"@welshman/lib": "0.0.14",
|
||||||
"nostr-tools": "^2.3.2"
|
"nostr-tools": "^2.7.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"gts": "^5.0.1",
|
"gts": "^5.0.1",
|
||||||
|
|||||||
@@ -32,6 +32,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@braintree/sanitize-url": "^7.0.2",
|
"@braintree/sanitize-url": "^7.0.2",
|
||||||
"nostr-tools": "^2.7.0"
|
"nostr-tools": "^2.7.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
"typescript": "~5.1.6"
|
"typescript": "~5.1.6"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"nostr-tools": "^2.3.2",
|
"nostr-tools": "^2.7.2",
|
||||||
"@welshman/lib": "0.0.14",
|
"@welshman/lib": "0.0.14",
|
||||||
"@welshman/util": "0.0.25"
|
"@welshman/util": "0.0.25"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
import {fromPairs, parseJson} from "@welshman/lib"
|
import {fromPairs, parseJson} from "@welshman/lib"
|
||||||
import {getAddress, Tags} from "@welshman/util"
|
import {getAddress, Tags} from "@welshman/util"
|
||||||
import type {TrustedEvent} from "@welshman/util"
|
import type {ExtensibleTrustedEvent} from "@welshman/util"
|
||||||
|
|
||||||
export type Handler<E extends TrustedEvent> = {
|
export type Handler = {
|
||||||
kind: number
|
kind: number
|
||||||
name: string
|
name: string
|
||||||
about: string
|
about: string
|
||||||
image: string
|
image: string
|
||||||
identifier: string
|
identifier: string
|
||||||
event: E
|
event: ExtensibleTrustedEvent
|
||||||
website?: string
|
website?: string
|
||||||
lud16?: string
|
lud16?: string
|
||||||
nip05?: string
|
nip05?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const readHandlers = <E extends TrustedEvent>(event: E) => {
|
export const readHandlers = (event: ExtensibleTrustedEvent) => {
|
||||||
const {d: identifier} = fromPairs(event.tags)
|
const {d: identifier} = fromPairs(event.tags)
|
||||||
const meta = parseJson(event.content)
|
const meta = parseJson(event.content)
|
||||||
const normalizedMeta = {
|
const normalizedMeta = {
|
||||||
@@ -35,14 +35,14 @@ export const readHandlers = <E extends TrustedEvent>(event: E) => {
|
|||||||
.whereKey("k")
|
.whereKey("k")
|
||||||
.values()
|
.values()
|
||||||
.valueOf()
|
.valueOf()
|
||||||
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler<E>[]
|
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getHandlerKey = <E extends TrustedEvent>(handler: Handler<E>) => `${handler.kind}:${getAddress(handler.event)}`
|
export const getHandlerKey = (handler: Handler) => `${handler.kind}:${getAddress(handler.event)}`
|
||||||
|
|
||||||
export const displayHandler = <E extends TrustedEvent>(handler?: Handler<E>, fallback = "") => handler?.name || fallback
|
export const displayHandler = (handler?: Handler, fallback = "") => handler?.name || fallback
|
||||||
|
|
||||||
export const getHandlerAddress = <E extends TrustedEvent>(event: E) => {
|
export const getHandlerAddress = (event: ExtensibleTrustedEvent) => {
|
||||||
const tags = Tags.fromEvent(event).whereKey("a")
|
const tags = Tags.fromEvent(event).whereKey("a")
|
||||||
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
|
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
import {parseJson} from "@welshman/lib"
|
import {parseJson} from "@welshman/lib"
|
||||||
import {Address, isShareableRelayUrl, TrustedEvent} from "@welshman/util"
|
import {Address, isShareableRelayUrl} from "@welshman/util"
|
||||||
import {Encryptable, DecryptedEvent} from "./util"
|
import {Encryptable, DecryptedEvent} from "./util"
|
||||||
|
|
||||||
export type ListParams = {
|
export type ListParams = {
|
||||||
kind: number
|
kind: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type List<E extends TrustedEvent> = ListParams & {
|
export type List = ListParams & {
|
||||||
publicTags: string[][]
|
publicTags: string[][]
|
||||||
privateTags: string[][]
|
privateTags: string[][]
|
||||||
event?: DecryptedEvent<E>
|
event?: DecryptedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PublishedList<E extends TrustedEvent> = Omit<List<E>, "event"> & {
|
export type PublishedList = Omit<List, "event"> & {
|
||||||
event: DecryptedEvent<E>
|
event: DecryptedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
export const makeList = <E extends TrustedEvent>(list: ListParams & Partial<List<E>>): List<E> =>
|
export const makeList = (list: ListParams & Partial<List>): List =>
|
||||||
({publicTags: [], privateTags: [], ...list})
|
({publicTags: [], privateTags: [], ...list})
|
||||||
|
|
||||||
const isValidTag = (tag: string[]) => {
|
const isValidTag = (tag: string[]) => {
|
||||||
@@ -30,7 +30,7 @@ const isValidTag = (tag: string[]) => {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): PublishedList<E> => {
|
export const readList = (event: DecryptedEvent): PublishedList => {
|
||||||
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
|
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
|
||||||
const privateTags = getTags(parseJson(event.plaintext?.content))
|
const privateTags = getTags(parseJson(event.plaintext?.content))
|
||||||
const publicTags = getTags(event.tags)
|
const publicTags = getTags(event.tags)
|
||||||
@@ -38,8 +38,8 @@ export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): Publ
|
|||||||
return {event, kind: event.kind, publicTags, privateTags}
|
return {event, kind: event.kind, publicTags, privateTags}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: List<E>) =>
|
export const createList = ({kind, publicTags = [], privateTags = []}: List) =>
|
||||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||||
|
|
||||||
export const editList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: PublishedList<E>) =>
|
export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) =>
|
||||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {nip19} from "nostr-tools"
|
import {nip19} from "nostr-tools"
|
||||||
import {ellipsize, parseJson} from "@welshman/lib"
|
import {ellipsize, parseJson} from "@welshman/lib"
|
||||||
import {PROFILE, TrustedEvent} from "@welshman/util"
|
import {PROFILE, ExtensibleTrustedEvent} from "@welshman/util"
|
||||||
|
|
||||||
export type Profile<E extends TrustedEvent> = {
|
export type Profile = {
|
||||||
name?: string
|
name?: string
|
||||||
nip05?: string
|
nip05?: string
|
||||||
lud06?: string
|
lud06?: string
|
||||||
@@ -12,17 +12,17 @@ export type Profile<E extends TrustedEvent> = {
|
|||||||
picture?: string
|
picture?: string
|
||||||
website?: string
|
website?: string
|
||||||
display_name?: string
|
display_name?: string
|
||||||
event?: E
|
event?: ExtensibleTrustedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PublishedProfile<E extends TrustedEvent> = Omit<Profile<E>, "event"> & {
|
export type PublishedProfile = Omit<Profile, "event"> & {
|
||||||
event: E
|
event: ExtensibleTrustedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isPublishedProfile = <E extends TrustedEvent>(profile: Profile<E>): profile is PublishedProfile<E> =>
|
export const isPublishedProfile = (profile: Profile): profile is PublishedProfile =>
|
||||||
Boolean(profile.event)
|
Boolean(profile.event)
|
||||||
|
|
||||||
export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>> = {}): Profile<E> => ({
|
export const makeProfile = (profile: Partial<Profile> = {}): Profile => ({
|
||||||
name: "",
|
name: "",
|
||||||
nip05: "",
|
nip05: "",
|
||||||
lud06: "",
|
lud06: "",
|
||||||
@@ -35,18 +35,18 @@ export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>>
|
|||||||
...profile,
|
...profile,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const readProfile = <E extends TrustedEvent>(event: E) => {
|
export const readProfile = (event: ExtensibleTrustedEvent) => {
|
||||||
const profile = parseJson(event.content) || {}
|
const profile = parseJson(event.content) || {}
|
||||||
|
|
||||||
return {...profile, event} as PublishedProfile<E>
|
return {...profile, event} as PublishedProfile
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createProfile = <E extends TrustedEvent>({event, ...profile}: Profile<E>) => ({
|
export const createProfile = ({event, ...profile}: Profile) => ({
|
||||||
kind: PROFILE,
|
kind: PROFILE,
|
||||||
content: JSON.stringify(profile),
|
content: JSON.stringify(profile),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const editProfile = <E extends TrustedEvent>({event, ...profile}: PublishedProfile<E>) => ({
|
export const editProfile = ({event, ...profile}: PublishedProfile) => ({
|
||||||
kind: PROFILE,
|
kind: PROFILE,
|
||||||
content: JSON.stringify(profile),
|
content: JSON.stringify(profile),
|
||||||
tags: event.tags,
|
tags: event.tags,
|
||||||
@@ -58,7 +58,7 @@ export const displayPubkey = (pubkey: string) => {
|
|||||||
return d.slice(0, 8) + "…" + d.slice(-5)
|
return d.slice(0, 8) + "…" + d.slice(-5)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fallback = "") => {
|
export const displayProfile = (profile?: Profile, fallback = "") => {
|
||||||
const {display_name, name, event} = profile || {}
|
const {display_name, name, event} = profile || {}
|
||||||
|
|
||||||
if (name) return ellipsize(name, 60)
|
if (name) return ellipsize(name, 60)
|
||||||
@@ -68,4 +68,4 @@ export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fal
|
|||||||
return fallback
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
export const profileHasName = <E extends TrustedEvent>(profile?: Profile<E>) => Boolean(profile?.name || profile?.display_name)
|
export const profileHasName = (profile?: Profile) => Boolean(profile?.name || profile?.display_name)
|
||||||
|
|||||||
@@ -1,21 +1,16 @@
|
|||||||
import type {TrustedEvent} from "@welshman/util"
|
import type {EventContent, ExtensibleTrustedEvent} from "@welshman/util"
|
||||||
|
|
||||||
export type Encrypt = (x: string) => Promise<string>
|
export type Encrypt = (x: string) => Promise<string>
|
||||||
|
|
||||||
export type EventContent = {
|
export type DecryptedEvent = ExtensibleTrustedEvent & {
|
||||||
content?: string
|
plaintext: Partial<EventContent>
|
||||||
tags?: string[][]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DecryptedEvent<E extends TrustedEvent> = E & {
|
export const asDecryptedEvent = (event: ExtensibleTrustedEvent, plaintext: Partial<EventContent>) =>
|
||||||
plaintext: EventContent
|
({...event, plaintext}) as DecryptedEvent
|
||||||
}
|
|
||||||
|
|
||||||
export const asDecryptedEvent = <E extends TrustedEvent>(event: E, plaintext: EventContent) =>
|
export class Encryptable<E extends Partial<EventContent>> {
|
||||||
({...event, plaintext}) as DecryptedEvent<E>
|
constructor(readonly event: E, readonly updates: E) {}
|
||||||
|
|
||||||
export class Encryptable<E extends TrustedEvent> {
|
|
||||||
constructor(readonly event: Partial<E>, readonly updates: EventContent) {}
|
|
||||||
|
|
||||||
async reconcile(encrypt: Encrypt) {
|
async reconcile(encrypt: Encrypt) {
|
||||||
const encryptContent = () => {
|
const encryptContent = () => {
|
||||||
@@ -41,8 +36,8 @@ export class Encryptable<E extends TrustedEvent> {
|
|||||||
// Updates are optional. If not provided, fall back to the event's content and tags.
|
// Updates are optional. If not provided, fall back to the event's content and tags.
|
||||||
return {
|
return {
|
||||||
...this.event,
|
...this.event,
|
||||||
tags: tags || this.event.tags,
|
tags: tags || this.event.tags || [],
|
||||||
content: content || this.event.content,
|
content: content || this.event.content || "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,6 @@
|
|||||||
"@welshman/lib": "0.0.14",
|
"@welshman/lib": "0.0.14",
|
||||||
"@welshman/net": "0.0.18",
|
"@welshman/net": "0.0.18",
|
||||||
"@welshman/util": "0.0.25",
|
"@welshman/util": "0.0.25",
|
||||||
"nostr-tools": "^2.7.0"
|
"nostr-tools": "^2.7.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import {hexToBytes} from '@noble/hashes/utils'
|
import {hexToBytes} from '@noble/hashes/utils'
|
||||||
import {getPublicKey, finalizeEvent} from 'nostr-tools'
|
import {getPublicKey, finalizeEvent} from 'nostr-tools'
|
||||||
import {now} from '@welshman/lib'
|
import {now} from '@welshman/lib'
|
||||||
import type {TrustedEvent, EventTemplate, Filter} from '@welshman/util'
|
import type {ExtensibleTrustedEvent, EventTemplate, Filter} from '@welshman/util'
|
||||||
import {subscribe, publish} from '@welshman/net'
|
import {subscribe, publish} from '@welshman/net'
|
||||||
|
|
||||||
export type DVMHandler = {
|
export type DVMHandler = {
|
||||||
stop?: () => void
|
stop?: () => void
|
||||||
handleEvent: (e: TrustedEvent) => AsyncGenerator<EventTemplate>
|
handleEvent: (e: ExtensibleTrustedEvent) => AsyncGenerator<EventTemplate>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CreateDVMHandler = (dvm: DVM) => DVMHandler
|
export type CreateDVMHandler = (dvm: DVM) => DVMHandler
|
||||||
@@ -49,7 +49,7 @@ export class DVM {
|
|||||||
const filters = [filter]
|
const filters = [filter]
|
||||||
const sub = subscribe({relays, filters})
|
const sub = subscribe({relays, filters})
|
||||||
|
|
||||||
sub.emitter.on('event', (url: string, e: TrustedEvent) => this.onEvent(e))
|
sub.emitter.on('event', (url: string, e: ExtensibleTrustedEvent) => this.onEvent(e))
|
||||||
sub.emitter.on('complete', () => resolve())
|
sub.emitter.on('complete', () => resolve())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ export class DVM {
|
|||||||
this.active = false
|
this.active = false
|
||||||
}
|
}
|
||||||
|
|
||||||
async onEvent(request: TrustedEvent) {
|
async onEvent(request: ExtensibleTrustedEvent) {
|
||||||
console.log(request)
|
console.log(request)
|
||||||
const {expireAfter = 60 * 60} = this.opts
|
const {expireAfter = 60 * 60} = this.opts
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ export class DVM {
|
|||||||
if (event.kind !== 7000) {
|
if (event.kind !== 7000) {
|
||||||
event.tags.push(['request', JSON.stringify(request)])
|
event.tags.push(['request', JSON.stringify(request)])
|
||||||
|
|
||||||
const inputTag = request.tags.find(t => t[0] === 'i')
|
const inputTag = request.tags.find((t: string[]) => t[0] === 'i')
|
||||||
|
|
||||||
if (inputTag) {
|
if (inputTag) {
|
||||||
event.tags.push(inputTag)
|
event.tags.push(inputTag)
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import {uniq, identity, flatten, pushToMapKey, intersection, tryCatch, now} from '@welshman/lib'
|
import {uniq, identity, flatten, pushToMapKey, intersection, tryCatch, now} from '@welshman/lib'
|
||||||
import type {TrustedEvent, Filter} from '@welshman/util'
|
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
|
||||||
import {Tags, intersectFilters, matchFilter, getAddress, getIdFilters, unionFilters} from '@welshman/util'
|
import {Tags, intersectFilters, matchFilter, getAddress, getIdFilters, unionFilters} from '@welshman/util'
|
||||||
import type {CreatedAtItem, RequestItem, ListItem, LabelItem, WOTItem, DVMItem, Scope, Feed, FeedOptions} from './core'
|
import type {CreatedAtItem, RequestItem, ListItem, LabelItem, WOTItem, DVMItem, Scope, Feed, FeedOptions} from './core'
|
||||||
import {getFeedArgs, feedsFromTags} from './utils'
|
import {getFeedArgs, feedsFromTags} from './utils'
|
||||||
import {FeedType} from './core'
|
import {FeedType} from './core'
|
||||||
|
|
||||||
export class FeedCompiler<E extends TrustedEvent> {
|
export class FeedCompiler {
|
||||||
constructor(readonly options: FeedOptions<E>) {}
|
constructor(readonly options: FeedOptions) {}
|
||||||
|
|
||||||
canCompile(feed: Feed): boolean {
|
canCompile(feed: Feed): boolean {
|
||||||
switch(feed[0]) {
|
switch(feed[0]) {
|
||||||
@@ -109,7 +109,7 @@ export class FeedCompiler<E extends TrustedEvent> {
|
|||||||
items.map(({mappings, ...request}) =>
|
items.map(({mappings, ...request}) =>
|
||||||
this.options.requestDVM({
|
this.options.requestDVM({
|
||||||
...request,
|
...request,
|
||||||
onEvent: async (e: E) => {
|
onEvent: async (e: ExtensibleTrustedEvent) => {
|
||||||
const tags = Tags.wrap(await tryCatch(() => JSON.parse(e.content)) || [])
|
const tags = Tags.wrap(await tryCatch(() => JSON.parse(e.content)) || [])
|
||||||
|
|
||||||
for (const feed of feedsFromTags(tags, mappings)) {
|
for (const feed of feedsFromTags(tags, mappings)) {
|
||||||
@@ -215,11 +215,11 @@ export class FeedCompiler<E extends TrustedEvent> {
|
|||||||
|
|
||||||
async _compileLists(listItems: ListItem[]): Promise<RequestItem[]> {
|
async _compileLists(listItems: ListItem[]): Promise<RequestItem[]> {
|
||||||
const addresses = uniq(listItems.flatMap(({addresses}) => addresses))
|
const addresses = uniq(listItems.flatMap(({addresses}) => addresses))
|
||||||
const eventsByAddress = new Map<string, E>()
|
const eventsByAddress = new Map<string, ExtensibleTrustedEvent>()
|
||||||
|
|
||||||
await this.options.request({
|
await this.options.request({
|
||||||
filters: getIdFilters(addresses),
|
filters: getIdFilters(addresses),
|
||||||
onEvent: (e: E) => eventsByAddress.set(getAddress(e), e),
|
onEvent: (e: ExtensibleTrustedEvent) => eventsByAddress.set(getAddress(e), e),
|
||||||
})
|
})
|
||||||
|
|
||||||
const feeds = flatten(
|
const feeds = flatten(
|
||||||
@@ -246,14 +246,14 @@ export class FeedCompiler<E extends TrustedEvent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _compileLabels(labelItems: LabelItem[]): Promise<RequestItem[]> {
|
async _compileLabels(labelItems: LabelItem[]): Promise<RequestItem[]> {
|
||||||
const events: E[] = []
|
const events: ExtensibleTrustedEvent[] = []
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
labelItems.map(({mappings, relays, ...filter}) =>
|
labelItems.map(({mappings, relays, ...filter}) =>
|
||||||
this.options.request({
|
this.options.request({
|
||||||
relays,
|
relays,
|
||||||
filters: [{kinds: [1985], ...filter}],
|
filters: [{kinds: [1985], ...filter}],
|
||||||
onEvent: (e: E) => events.push(e),
|
onEvent: (e: ExtensibleTrustedEvent) => events.push(e),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type {Filter} from '@welshman/util'
|
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
|
||||||
|
|
||||||
export enum FeedType {
|
export enum FeedType {
|
||||||
Address = "address",
|
Address = "address",
|
||||||
@@ -109,8 +109,8 @@ export type RequestItem = {
|
|||||||
filters?: Filter[]
|
filters?: Filter[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RequestOpts<E> = RequestItem & {
|
export type RequestOpts = RequestItem & {
|
||||||
onEvent: (event: E) => void
|
onEvent: (event: ExtensibleTrustedEvent) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DVMRequest = {
|
export type DVMRequest = {
|
||||||
@@ -119,13 +119,13 @@ export type DVMRequest = {
|
|||||||
relays?: string[],
|
relays?: string[],
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DVMOpts<E> = DVMRequest & {
|
export type DVMOpts = DVMRequest & {
|
||||||
onEvent: (event: E) => void
|
onEvent: (event: ExtensibleTrustedEvent) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FeedOptions<E> = {
|
export type FeedOptions = {
|
||||||
request: (opts: RequestOpts<E>) => Promise<void>
|
request: (opts: RequestOpts) => Promise<void>
|
||||||
requestDVM: (opts: DVMOpts<E>) => Promise<void>
|
requestDVM: (opts: DVMOpts) => Promise<void>
|
||||||
getPubkeysForScope: (scope: Scope) => string[]
|
getPubkeysForScope: (scope: Scope) => string[]
|
||||||
getPubkeysForWOTRange: (minWOT: number, maxWOT: number) => string[]
|
getPubkeysForWOTRange: (minWOT: number, maxWOT: number) => string[]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
import {inc, max, min, now} from '@welshman/lib'
|
import {inc, max, min, now} from '@welshman/lib'
|
||||||
import type {TrustedEvent, Filter} from '@welshman/util'
|
import type {ExtensibleTrustedEvent, Filter} from '@welshman/util'
|
||||||
import {EPOCH, trimFilters, guessFilterDelta} from '@welshman/util'
|
import {EPOCH, trimFilters, guessFilterDelta} from '@welshman/util'
|
||||||
import type {Feed, RequestItem, FeedOptions} from './core'
|
import type {Feed, RequestItem, FeedOptions} from './core'
|
||||||
import {FeedType} from './core'
|
import {FeedType} from './core'
|
||||||
import {FeedCompiler} from './compiler'
|
import {FeedCompiler} from './compiler'
|
||||||
|
|
||||||
export type LoadOpts<E> = {
|
export type LoadOpts = {
|
||||||
onEvent?: (event: E) => void
|
onEvent?: (event: ExtensibleTrustedEvent) => void
|
||||||
onExhausted?: () => void
|
onExhausted?: () => void
|
||||||
useWindowing?: boolean
|
useWindowing?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Loader = (limit: number) => Promise<void>
|
export type Loader = (limit: number) => Promise<void>
|
||||||
|
|
||||||
export class FeedLoader<E extends TrustedEvent> {
|
export class FeedLoader {
|
||||||
compiler: FeedCompiler<E>
|
compiler: FeedCompiler
|
||||||
|
|
||||||
constructor(readonly options: FeedOptions<E>) {
|
constructor(readonly options: FeedOptions) {
|
||||||
this.compiler = new FeedCompiler(options)
|
this.compiler = new FeedCompiler(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
async getLoader([type, ...feed]: Feed, loadOpts: LoadOpts<E>) {
|
async getLoader([type, ...feed]: Feed, loadOpts: LoadOpts) {
|
||||||
if (this.compiler.canCompile([type, ...feed] as Feed)) {
|
if (this.compiler.canCompile([type, ...feed] as Feed)) {
|
||||||
return this.getRequestsLoader(await this.compiler.compile([type, ...feed] as Feed), loadOpts)
|
return this.getRequestsLoader(await this.compiler.compile([type, ...feed] as Feed), loadOpts)
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRequestsLoader(requests: RequestItem[], loadOpts: LoadOpts<E>) {
|
async getRequestsLoader(requests: RequestItem[], loadOpts: LoadOpts) {
|
||||||
const seen = new Set()
|
const seen = new Set()
|
||||||
const exhausted = new Set()
|
const exhausted = new Set()
|
||||||
const loaders = await Promise.all(
|
const loaders = await Promise.all(
|
||||||
@@ -64,7 +64,7 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getRequestLoader({relays, filters}: RequestItem, {useWindowing = true, onEvent, onExhausted}: LoadOpts<E>) {
|
async _getRequestLoader({relays, filters}: RequestItem, {useWindowing = true, onEvent, onExhausted}: LoadOpts) {
|
||||||
// Make sure we have some kind of filter to send if we've been given an empty one, as happens with relay feeds
|
// 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 || filters.length === 0) {
|
if (!filters || filters.length === 0) {
|
||||||
filters = [{}]
|
filters = [{}]
|
||||||
@@ -101,7 +101,7 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
await this.options.request({
|
await this.options.request({
|
||||||
relays,
|
relays,
|
||||||
filters: trimFilters(requestFilters),
|
filters: trimFilters(requestFilters),
|
||||||
onEvent: (event: E) => {
|
onEvent: (event: ExtensibleTrustedEvent) => {
|
||||||
count += 1
|
count += 1
|
||||||
until = Math.min(until, event.created_at - 1)
|
until = Math.min(until, event.created_at - 1)
|
||||||
onEvent?.(event)
|
onEvent?.(event)
|
||||||
@@ -127,17 +127,17 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getDifferenceLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
|
async _getDifferenceLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
|
||||||
const exhausted = new Set<number>()
|
const exhausted = new Set<number>()
|
||||||
const skip = new Set<string>()
|
const skip = new Set<string>()
|
||||||
const events: E[] = []
|
const events: ExtensibleTrustedEvent[] = []
|
||||||
const seen = new Set()
|
const seen = new Set()
|
||||||
|
|
||||||
const loaders = await Promise.all(
|
const loaders = await Promise.all(
|
||||||
feeds.map((feed: Feed, i: number) =>
|
feeds.map((feed: Feed, i: number) =>
|
||||||
this.getLoader(feed, {
|
this.getLoader(feed, {
|
||||||
onExhausted: () => exhausted.add(i),
|
onExhausted: () => exhausted.add(i),
|
||||||
onEvent: (event: E) => {
|
onEvent: (event: ExtensibleTrustedEvent) => {
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
events.push(event)
|
events.push(event)
|
||||||
} else {
|
} else {
|
||||||
@@ -172,17 +172,17 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getIntersectionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
|
async _getIntersectionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
|
||||||
const exhausted = new Set<number>()
|
const exhausted = new Set<number>()
|
||||||
const counts = new Map<string, number>()
|
const counts = new Map<string, number>()
|
||||||
const events: E[] = []
|
const events: ExtensibleTrustedEvent[] = []
|
||||||
const seen = new Set()
|
const seen = new Set()
|
||||||
|
|
||||||
const loaders = await Promise.all(
|
const loaders = await Promise.all(
|
||||||
feeds.map((feed: Feed, i: number) =>
|
feeds.map((feed: Feed, i: number) =>
|
||||||
this.getLoader(feed, {
|
this.getLoader(feed, {
|
||||||
onExhausted: () => exhausted.add(i),
|
onExhausted: () => exhausted.add(i),
|
||||||
onEvent: (event: E) => {
|
onEvent: (event: ExtensibleTrustedEvent) => {
|
||||||
events.push(event)
|
events.push(event)
|
||||||
counts.set(event.id, inc(counts.get(event.id)))
|
counts.set(event.id, inc(counts.get(event.id)))
|
||||||
},
|
},
|
||||||
@@ -214,7 +214,7 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getUnionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts<E>) {
|
async _getUnionLoader(feeds: Feed[], {onEvent, onExhausted}: LoadOpts) {
|
||||||
const exhausted = new Set<number>()
|
const exhausted = new Set<number>()
|
||||||
const seen = new Set()
|
const seen = new Set()
|
||||||
|
|
||||||
@@ -222,7 +222,7 @@ export class FeedLoader<E extends TrustedEvent> {
|
|||||||
feeds.map((feed: Feed, i: number) =>
|
feeds.map((feed: Feed, i: number) =>
|
||||||
this.getLoader(feed, {
|
this.getLoader(feed, {
|
||||||
onExhausted: () => exhausted.add(i),
|
onExhausted: () => exhausted.add(i),
|
||||||
onEvent: (event: E) => {
|
onEvent: (event: ExtensibleTrustedEvent) => {
|
||||||
if (!seen.has(event.id)) {
|
if (!seen.has(event.id)) {
|
||||||
onEvent?.(event)
|
onEvent?.(event)
|
||||||
seen.add(event.id)
|
seen.add(event.id)
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import {Emitter} from '@welshman/lib'
|
import {Emitter} from '@welshman/lib'
|
||||||
import type {TrustedEvent} from '@welshman/util'
|
|
||||||
import {Relay, LOCAL_RELAY_URL} from '@welshman/util'
|
import {Relay, LOCAL_RELAY_URL} from '@welshman/util'
|
||||||
import type {Message} from '../Socket'
|
import type {Message} from '../Socket'
|
||||||
|
|
||||||
export class Local<T extends TrustedEvent> extends Emitter {
|
export class Local extends Emitter {
|
||||||
constructor(readonly relay: Relay<T>) {
|
constructor(readonly relay: Relay) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
relay.on('*', this.onMessage)
|
relay.on('*', this.onMessage)
|
||||||
|
|||||||
+14
-14
@@ -4,7 +4,7 @@ import type {Readable, Writable} from "svelte/store"
|
|||||||
import {identity, batch, partition, first} from "@welshman/lib"
|
import {identity, batch, partition, first} from "@welshman/lib"
|
||||||
import type {Repository} from "@welshman/util"
|
import type {Repository} from "@welshman/util"
|
||||||
import {matchFilters, getIdAndAddress, getIdFilters} from "@welshman/util"
|
import {matchFilters, getIdAndAddress, getIdFilters} from "@welshman/util"
|
||||||
import type {Filter, TrustedEvent} from "@welshman/util"
|
import type {Filter, ExtensibleTrustedEvent} from "@welshman/util"
|
||||||
|
|
||||||
export const getter = <T>(store: Readable<T>) => {
|
export const getter = <T>(store: Readable<T>) => {
|
||||||
let value: T
|
let value: T
|
||||||
@@ -68,8 +68,8 @@ export function withGetter<T>(store: Readable<T> | Writable<T>) {
|
|||||||
export const throttled = <T>(delay: number, store: Readable<T>) =>
|
export const throttled = <T>(delay: number, store: Readable<T>) =>
|
||||||
custom(set => store.subscribe(throttle(delay, set)))
|
custom(set => store.subscribe(throttle(delay, set)))
|
||||||
|
|
||||||
export const createEventStore = <E extends TrustedEvent>(repository: Repository<E>) => {
|
export const createEventStore = (repository: Repository) => {
|
||||||
let subs: Sub<E[]>[] = []
|
let subs: Sub<ExtensibleTrustedEvent[]>[] = []
|
||||||
|
|
||||||
const onUpdate = throttle(300, () => {
|
const onUpdate = throttle(300, () => {
|
||||||
const $events = repository.dump()
|
const $events = repository.dump()
|
||||||
@@ -81,8 +81,8 @@ export const createEventStore = <E extends TrustedEvent>(repository: Repository<
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
get: () => repository.dump(),
|
get: () => repository.dump(),
|
||||||
set: (events: E[]) => repository.load(events),
|
set: (events: ExtensibleTrustedEvent[]) => repository.load(events),
|
||||||
subscribe: (f: Sub<E[]>) => {
|
subscribe: (f: Sub<ExtensibleTrustedEvent[]>) => {
|
||||||
f(repository.dump())
|
f(repository.dump())
|
||||||
|
|
||||||
subs.push(f)
|
subs.push(f)
|
||||||
@@ -102,7 +102,7 @@ export const createEventStore = <E extends TrustedEvent>(repository: Repository<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveEventsMapped = <E extends TrustedEvent, T>({
|
export const deriveEventsMapped = <T>({
|
||||||
filters,
|
filters,
|
||||||
repository,
|
repository,
|
||||||
eventToItem,
|
eventToItem,
|
||||||
@@ -110,9 +110,9 @@ export const deriveEventsMapped = <E extends TrustedEvent, T>({
|
|||||||
includeDeleted = false,
|
includeDeleted = false,
|
||||||
}: {
|
}: {
|
||||||
filters: Filter[]
|
filters: Filter[]
|
||||||
repository: Repository<E>,
|
repository: Repository,
|
||||||
eventToItem: (event: E) => T
|
eventToItem: (event: ExtensibleTrustedEvent) => T
|
||||||
itemToEvent: (item: T) => E
|
itemToEvent: (item: T) => ExtensibleTrustedEvent
|
||||||
includeDeleted?: boolean
|
includeDeleted?: boolean
|
||||||
}) =>
|
}) =>
|
||||||
custom<T[]>(setter => {
|
custom<T[]>(setter => {
|
||||||
@@ -120,7 +120,7 @@ export const deriveEventsMapped = <E extends TrustedEvent, T>({
|
|||||||
|
|
||||||
setter(data)
|
setter(data)
|
||||||
|
|
||||||
const onUpdate = batch(300, (updates: {added: E[]; removed: Set<string>}[]) => {
|
const onUpdate = batch(300, (updates: {added: ExtensibleTrustedEvent[]; removed: Set<string>}[]) => {
|
||||||
const removed = new Set()
|
const removed = new Set()
|
||||||
const added = new Map()
|
const added = new Map()
|
||||||
|
|
||||||
@@ -170,15 +170,15 @@ export const deriveEventsMapped = <E extends TrustedEvent, T>({
|
|||||||
return () => repository.off("update", onUpdate)
|
return () => repository.off("update", onUpdate)
|
||||||
})
|
})
|
||||||
|
|
||||||
export const deriveEvents = <E extends TrustedEvent>(repository: Repository<E>, opts: {filters: Filter[]; includeDeleted?: boolean}) =>
|
export const deriveEvents = (repository: Repository, opts: {filters: Filter[]; includeDeleted?: boolean}) =>
|
||||||
deriveEventsMapped<E, E>({
|
deriveEventsMapped<ExtensibleTrustedEvent>({
|
||||||
...opts,
|
...opts,
|
||||||
repository,
|
repository,
|
||||||
eventToItem: identity,
|
eventToItem: identity,
|
||||||
itemToEvent: identity,
|
itemToEvent: identity,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const deriveEvent = <E extends TrustedEvent>(repository: Repository<E>, idOrAddress: string) =>
|
export const deriveEvent = (repository: Repository, idOrAddress: string) =>
|
||||||
derived(
|
derived(
|
||||||
deriveEvents(repository, {
|
deriveEvents(repository, {
|
||||||
filters: getIdFilters([idOrAddress]),
|
filters: getIdFilters([idOrAddress]),
|
||||||
@@ -187,7 +187,7 @@ export const deriveEvent = <E extends TrustedEvent>(repository: Repository<E>, i
|
|||||||
first
|
first
|
||||||
)
|
)
|
||||||
|
|
||||||
export const deriveIsDeletedByAddress = <E extends TrustedEvent>(repository: Repository<E>, event: E) =>
|
export const deriveIsDeletedByAddress = (repository: Repository, event: ExtensibleTrustedEvent) =>
|
||||||
custom<boolean>(setter => {
|
custom<boolean>(setter => {
|
||||||
setter(repository.isDeletedByAddress(event))
|
setter(repository.isDeletedByAddress(event))
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@welshman/lib": "0.0.14",
|
"@welshman/lib": "0.0.14",
|
||||||
"nostr-tools": "^2.3.2"
|
"nostr-tools": "^2.7.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ import {Tags} from './Tags'
|
|||||||
import {getAddress} from './Address'
|
import {getAddress} from './Address'
|
||||||
import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds'
|
import {isEphemeralKind, isReplaceableKind, isPlainReplaceableKind, isParameterizedReplaceableKind} from './Kinds'
|
||||||
|
|
||||||
export type EventTemplate = {
|
export type EventContent = {
|
||||||
kind: number
|
|
||||||
tags: string[][]
|
tags: string[][]
|
||||||
content: string
|
content: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type EventTemplate = EventContent & {
|
||||||
|
kind: number
|
||||||
created_at: number
|
created_at: number
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +38,8 @@ export type TrustedEvent = HashedEvent & {
|
|||||||
[verifiedSymbol]?: boolean
|
[verifiedSymbol]?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ExtensibleTrustedEvent = TrustedEvent & Record<string, any>
|
||||||
|
|
||||||
export type CreateEventOpts = {
|
export type CreateEventOpts = {
|
||||||
content?: string
|
content?: string
|
||||||
tags?: string[][]
|
tags?: string[][]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import {Emitter, normalizeUrl, sleep, stripProtocol} from '@welshman/lib'
|
|||||||
import {matchFilters} from './Filters'
|
import {matchFilters} from './Filters'
|
||||||
import type {Repository} from './Repository'
|
import type {Repository} from './Repository'
|
||||||
import type {Filter} from './Filters'
|
import type {Filter} from './Filters'
|
||||||
import type {TrustedEvent} from './Events'
|
import type {ExtensibleTrustedEvent} from './Events'
|
||||||
|
|
||||||
export const LOCAL_RELAY_URL = "local://welshman.relay"
|
export const LOCAL_RELAY_URL = "local://welshman.relay"
|
||||||
|
|
||||||
@@ -48,22 +48,22 @@ export const normalizeRelayUrl = (url: string, {allowInsecure = false}: Normaliz
|
|||||||
return prefix + url
|
return prefix + url
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Relay<T extends TrustedEvent> extends Emitter {
|
export class Relay extends Emitter {
|
||||||
subs = new Map<string, Filter[]>()
|
subs = new Map<string, Filter[]>()
|
||||||
|
|
||||||
constructor(readonly repository: Repository<T>) {
|
constructor(readonly repository: Repository) {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
|
|
||||||
send(type: string, ...message: any[]) {
|
send(type: string, ...message: any[]) {
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case 'EVENT': return this.handleEVENT(message as [T])
|
case 'EVENT': return this.handleEVENT(message as [ExtensibleTrustedEvent])
|
||||||
case 'CLOSE': return this.handleCLOSE(message as [string])
|
case 'CLOSE': return this.handleCLOSE(message as [string])
|
||||||
case 'REQ': return this.handleREQ(message as [string, ...Filter[]])
|
case 'REQ': return this.handleREQ(message as [string, ...Filter[]])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleEVENT([event]: [T]) {
|
handleEVENT([event]: [ExtensibleTrustedEvent]) {
|
||||||
this.repository.publish(event)
|
this.repository.publish(event)
|
||||||
|
|
||||||
// Callers generally expect async relays
|
// Callers generally expect async relays
|
||||||
|
|||||||
@@ -4,19 +4,19 @@ import {EPOCH, matchFilter} from './Filters'
|
|||||||
import {isReplaceable, isTrustedEvent} from './Events'
|
import {isReplaceable, isTrustedEvent} from './Events'
|
||||||
import {getAddress} from './Address'
|
import {getAddress} from './Address'
|
||||||
import type {Filter} from './Filters'
|
import type {Filter} from './Filters'
|
||||||
import type {TrustedEvent} from './Events'
|
import type {ExtensibleTrustedEvent} from './Events'
|
||||||
|
|
||||||
export const DAY = 86400
|
export const DAY = 86400
|
||||||
|
|
||||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||||
|
|
||||||
export class Repository<T extends TrustedEvent> extends Emitter {
|
export class Repository extends Emitter {
|
||||||
eventsById = new Map<string, T>()
|
eventsById = new Map<string, ExtensibleTrustedEvent>()
|
||||||
eventsByWrap = new Map<string, T>()
|
eventsByWrap = new Map<string, ExtensibleTrustedEvent>()
|
||||||
eventsByAddress = new Map<string, T>()
|
eventsByAddress = new Map<string, ExtensibleTrustedEvent>()
|
||||||
eventsByTag = new Map<string, T[]>()
|
eventsByTag = new Map<string, ExtensibleTrustedEvent[]>()
|
||||||
eventsByDay = new Map<number, T[]>()
|
eventsByDay = new Map<number, ExtensibleTrustedEvent[]>()
|
||||||
eventsByAuthor = new Map<string, T[]>()
|
eventsByAuthor = new Map<string, ExtensibleTrustedEvent[]>()
|
||||||
deletes = new Map<string, number>()
|
deletes = new Map<string, number>()
|
||||||
|
|
||||||
// Dump/load/clear
|
// Dump/load/clear
|
||||||
@@ -25,7 +25,7 @@ export class Repository<T extends TrustedEvent> extends Emitter {
|
|||||||
return Array.from(this.eventsById.values())
|
return Array.from(this.eventsById.values())
|
||||||
}
|
}
|
||||||
|
|
||||||
load = async (events: T[], chunkSize = 1000) => {
|
load = async (events: ExtensibleTrustedEvent[], chunkSize = 1000) => {
|
||||||
this.clear()
|
this.clear()
|
||||||
|
|
||||||
const added = []
|
const added = []
|
||||||
@@ -69,7 +69,7 @@ export class Repository<T extends TrustedEvent> extends Emitter {
|
|||||||
: this.eventsById.get(idOrAddress)
|
: this.eventsById.get(idOrAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
hasEvent = (event: T) => {
|
hasEvent = (event: ExtensibleTrustedEvent) => {
|
||||||
const duplicate = (
|
const duplicate = (
|
||||||
this.eventsById.get(event.id) ||
|
this.eventsById.get(event.id) ||
|
||||||
this.eventsByAddress.get(getAddress(event))
|
this.eventsByAddress.get(getAddress(event))
|
||||||
@@ -79,12 +79,12 @@ export class Repository<T extends TrustedEvent> extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query = (filters: Filter[], {includeDeleted = false} = {}) => {
|
query = (filters: Filter[], {includeDeleted = false} = {}) => {
|
||||||
const result: T[][] = []
|
const result: ExtensibleTrustedEvent[][] = []
|
||||||
for (let filter of filters) {
|
for (let filter of filters) {
|
||||||
let events: T[] = Array.from(this.eventsById.values())
|
let events: ExtensibleTrustedEvent[] = Array.from(this.eventsById.values())
|
||||||
|
|
||||||
if (filter.ids) {
|
if (filter.ids) {
|
||||||
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as T[]
|
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as ExtensibleTrustedEvent[]
|
||||||
filter = omit(['ids'], filter)
|
filter = omit(['ids'], filter)
|
||||||
} else if (filter.authors) {
|
} else if (filter.authors) {
|
||||||
events = uniq(filter.authors!.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || []))
|
events = uniq(filter.authors!.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || []))
|
||||||
@@ -112,8 +112,8 @@ export class Repository<T extends TrustedEvent> extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunk: T[] = []
|
const chunk: ExtensibleTrustedEvent[] = []
|
||||||
for (const event of sortBy((e: T) => -e.created_at, events)) {
|
for (const event of sortBy((e: ExtensibleTrustedEvent) => -e.created_at, events)) {
|
||||||
if (filter.limit && chunk.length >= filter.limit) {
|
if (filter.limit && chunk.length >= filter.limit) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -133,7 +133,7 @@ export class Repository<T extends TrustedEvent> extends Emitter {
|
|||||||
return uniq(flatten(result))
|
return uniq(flatten(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
publish = (event: T, {shouldNotify = true} = {}): boolean => {
|
publish = (event: ExtensibleTrustedEvent, {shouldNotify = true} = {}): boolean => {
|
||||||
if (!isTrustedEvent(event)) {
|
if (!isTrustedEvent(event)) {
|
||||||
throw new Error("Invalid event published to Repository", event)
|
throw new Error("Invalid event published to Repository", event)
|
||||||
}
|
}
|
||||||
@@ -203,19 +203,19 @@ export class Repository<T extends TrustedEvent> extends Emitter {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
isDeletedByAddress = (event: T) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
isDeletedByAddress = (event: ExtensibleTrustedEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||||
|
|
||||||
isDeletedById = (event: T) => (this.deletes.get(event.id) || 0) > event.created_at
|
isDeletedById = (event: ExtensibleTrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||||
|
|
||||||
isDeleted = (event: T) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
isDeleted = (event: ExtensibleTrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
|
|
||||||
_updateIndex<K>(m: Map<K, T[]>, k: K, e: T, duplicate?: T) {
|
_updateIndex<K>(m: Map<K, ExtensibleTrustedEvent[]>, k: K, e: ExtensibleTrustedEvent, duplicate?: ExtensibleTrustedEvent) {
|
||||||
let a = m.get(k) || []
|
let a = m.get(k) || []
|
||||||
|
|
||||||
if (duplicate) {
|
if (duplicate) {
|
||||||
a = a.filter((x: T) => x !== duplicate)
|
a = a.filter((x: ExtensibleTrustedEvent) => x !== duplicate)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.push(e)
|
a.push(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user