Remove CustomEvent
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type {EventContent, CustomEvent} from './Events'
|
||||
import type {EventContent, TrustedEvent} from './Events'
|
||||
|
||||
export type Encrypt = (x: string) => Promise<string>
|
||||
|
||||
@@ -14,11 +14,11 @@ export type EncryptableResult = {
|
||||
content: string
|
||||
}
|
||||
|
||||
export type DecryptedEvent = CustomEvent & {
|
||||
export type DecryptedEvent = TrustedEvent & {
|
||||
plaintext: Partial<EventContent>
|
||||
}
|
||||
|
||||
export const asDecryptedEvent = (event: CustomEvent, plaintext: Partial<EventContent>) =>
|
||||
export const asDecryptedEvent = (event: TrustedEvent, plaintext: Partial<EventContent>) =>
|
||||
({...event, plaintext}) as DecryptedEvent
|
||||
|
||||
export class Encryptable {
|
||||
|
||||
@@ -12,10 +12,13 @@ export type EventContent = {
|
||||
|
||||
export type EventTemplate = EventContent & {
|
||||
kind: number
|
||||
}
|
||||
|
||||
export type StampedEvent = EventTemplate & {
|
||||
created_at: number
|
||||
}
|
||||
|
||||
export type OwnedEvent = EventTemplate & {
|
||||
export type OwnedEvent = StampedEvent & {
|
||||
pubkey: string
|
||||
}
|
||||
|
||||
@@ -38,9 +41,6 @@ export type TrustedEvent = HashedEvent & {
|
||||
[verifiedSymbol]?: boolean
|
||||
}
|
||||
|
||||
/* eslint @typescript-eslint/no-empty-interface: 0 */
|
||||
export interface CustomEvent extends TrustedEvent {}
|
||||
|
||||
export type CreateEventOpts = {
|
||||
content?: string
|
||||
tags?: string[][]
|
||||
@@ -51,10 +51,13 @@ export const createEvent = (kind: number, {content = "", tags = [], created_at =
|
||||
({kind, content, tags, created_at})
|
||||
|
||||
export const isEventTemplate = (e: EventTemplate): e is EventTemplate =>
|
||||
Boolean(typeof e.kind === "number" && e.tags && typeof e.content === "string" && e.created_at)
|
||||
Boolean(typeof e.kind === "number" && e.tags && typeof e.content === "string")
|
||||
|
||||
export const isStampedEvent = (e: StampedEvent): e is StampedEvent =>
|
||||
Boolean(isEventTemplate(e) && e.created_at)
|
||||
|
||||
export const isOwnedEvent = (e: OwnedEvent): e is OwnedEvent =>
|
||||
Boolean(isEventTemplate(e) && e.pubkey)
|
||||
Boolean(isStampedEvent(e) && e.pubkey)
|
||||
|
||||
export const isHashedEvent = (e: HashedEvent): e is HashedEvent =>
|
||||
Boolean(isOwnedEvent(e) && e.id)
|
||||
@@ -69,6 +72,9 @@ export const isTrustedEvent = (e: TrustedEvent): e is TrustedEvent =>
|
||||
isSignedEvent(e) || isUnwrappedEvent(e)
|
||||
|
||||
export const asEventTemplate = (e: EventTemplate): EventTemplate =>
|
||||
pick(['kind', 'tags', 'content'], e)
|
||||
|
||||
export const asStampedEvent = (e: StampedEvent): StampedEvent =>
|
||||
pick(['kind', 'tags', 'content', 'created_at'], e)
|
||||
|
||||
export const asOwnedEvent = (e: OwnedEvent): OwnedEvent =>
|
||||
@@ -118,7 +124,7 @@ export const isPlainReplaceable = (e: EventTemplate) => isPlainReplaceableKind(e
|
||||
|
||||
export const isParameterizedReplaceable = (e: EventTemplate) => isParameterizedReplaceableKind(e.kind)
|
||||
|
||||
export const isChildOf = (child: EventTemplate, parent: HashedEvent) => {
|
||||
export const isChildOf = (child: EventContent, parent: HashedEvent) => {
|
||||
const {roots, replies} = Tags.fromEvent(child).ancestors()
|
||||
const parentIds = (replies.exists() ? replies : roots).values().valueOf()
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {matchFilter as nostrToolsMatchFilter} from 'nostr-tools'
|
||||
import {uniqBy, prop, mapVals, shuffle, avg, hash, groupBy, randomId, uniq} from '@welshman/lib'
|
||||
import type {HashedEvent, CustomEvent, SignedEvent} from './Events'
|
||||
import type {HashedEvent, TrustedEvent, SignedEvent} from './Events'
|
||||
import {isReplaceableKind} from './Kinds'
|
||||
import {Address, getAddress} from './Address'
|
||||
|
||||
@@ -155,7 +155,7 @@ export const getIdFilters = (idsOrAddresses: string[]) => {
|
||||
return filters
|
||||
}
|
||||
|
||||
export const getReplyFilters = (events: CustomEvent[], filter: Filter) => {
|
||||
export const getReplyFilters = (events: TrustedEvent[], filter: Filter) => {
|
||||
const a = []
|
||||
const e = []
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {fromPairs, last, first, parseJson} from "@welshman/lib"
|
||||
import {getAddress} from "./Address"
|
||||
import {getAddressTags, getKindTagValues} from "./Tags"
|
||||
import type {CustomEvent} from "./Events"
|
||||
import type {TrustedEvent} from "./Events"
|
||||
|
||||
export type Handler = {
|
||||
kind: number
|
||||
@@ -9,13 +9,13 @@ export type Handler = {
|
||||
about: string
|
||||
image: string
|
||||
identifier: string
|
||||
event: CustomEvent
|
||||
event: TrustedEvent
|
||||
website?: string
|
||||
lud16?: string
|
||||
nip05?: string
|
||||
}
|
||||
|
||||
export const readHandlers = (event: CustomEvent) => {
|
||||
export const readHandlers = (event: TrustedEvent) => {
|
||||
const {d: identifier} = fromPairs(event.tags)
|
||||
const meta = parseJson(event.content)
|
||||
const normalizedMeta = {
|
||||
@@ -40,7 +40,7 @@ export const getHandlerKey = (handler: Handler) => `${handler.kind}:${getAddress
|
||||
|
||||
export const displayHandler = (handler?: Handler, fallback = "") => handler?.name || fallback
|
||||
|
||||
export const getHandlerAddress = (event: CustomEvent) => {
|
||||
export const getHandlerAddress = (event: TrustedEvent) => {
|
||||
const tags = getAddressTags(event.tags)
|
||||
const tag = tags.find(t => last(t) === "web") || first(tags)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {nip19} from "nostr-tools"
|
||||
import {ellipsize, parseJson} from "@welshman/lib"
|
||||
import {CustomEvent} from "./Events"
|
||||
import {TrustedEvent} from "./Events"
|
||||
import {PROFILE} from "./Kinds"
|
||||
|
||||
export type Profile = {
|
||||
@@ -13,11 +13,11 @@ export type Profile = {
|
||||
picture?: string
|
||||
website?: string
|
||||
display_name?: string
|
||||
event?: CustomEvent
|
||||
event?: TrustedEvent
|
||||
}
|
||||
|
||||
export type PublishedProfile = Omit<Profile, "event"> & {
|
||||
event: CustomEvent
|
||||
event: TrustedEvent
|
||||
}
|
||||
|
||||
export const isPublishedProfile = (profile: Profile): profile is PublishedProfile =>
|
||||
@@ -36,7 +36,7 @@ export const makeProfile = (profile: Partial<Profile> = {}): Profile => ({
|
||||
...profile,
|
||||
})
|
||||
|
||||
export const readProfile = (event: CustomEvent) => {
|
||||
export const readProfile = (event: TrustedEvent) => {
|
||||
const profile = parseJson(event.content) || {}
|
||||
|
||||
return {...profile, event} as PublishedProfile
|
||||
|
||||
@@ -2,7 +2,7 @@ import {last, Emitter, normalizeUrl, sleep, stripProtocol} from '@welshman/lib'
|
||||
import {matchFilters} from './Filters'
|
||||
import type {Repository} from './Repository'
|
||||
import type {Filter} from './Filters'
|
||||
import type {CustomEvent} from './Events'
|
||||
import type {TrustedEvent} from './Events'
|
||||
|
||||
// Constants and types
|
||||
|
||||
@@ -84,13 +84,13 @@ export class Relay extends Emitter {
|
||||
|
||||
send(type: string, ...message: any[]) {
|
||||
switch(type) {
|
||||
case 'EVENT': return this.handleEVENT(message as [CustomEvent])
|
||||
case 'EVENT': return this.handleEVENT(message as [TrustedEvent])
|
||||
case 'CLOSE': return this.handleCLOSE(message as [string])
|
||||
case 'REQ': return this.handleREQ(message as [string, ...Filter[]])
|
||||
}
|
||||
}
|
||||
|
||||
handleEVENT([event]: [CustomEvent]) {
|
||||
handleEVENT([event]: [TrustedEvent]) {
|
||||
this.repository.publish(event)
|
||||
|
||||
// Callers generally expect async relays
|
||||
|
||||
@@ -4,19 +4,19 @@ import {EPOCH, matchFilter} from './Filters'
|
||||
import {isReplaceable, isTrustedEvent} from './Events'
|
||||
import {getAddress} from './Address'
|
||||
import type {Filter} from './Filters'
|
||||
import type {CustomEvent} from './Events'
|
||||
import type {TrustedEvent} from './Events'
|
||||
|
||||
export const DAY = 86400
|
||||
|
||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||
|
||||
export class Repository extends Emitter {
|
||||
eventsById = new Map<string, CustomEvent>()
|
||||
eventsByWrap = new Map<string, CustomEvent>()
|
||||
eventsByAddress = new Map<string, CustomEvent>()
|
||||
eventsByTag = new Map<string, CustomEvent[]>()
|
||||
eventsByDay = new Map<number, CustomEvent[]>()
|
||||
eventsByAuthor = new Map<string, CustomEvent[]>()
|
||||
eventsById = new Map<string, TrustedEvent>()
|
||||
eventsByWrap = new Map<string, TrustedEvent>()
|
||||
eventsByAddress = new Map<string, TrustedEvent>()
|
||||
eventsByTag = new Map<string, TrustedEvent[]>()
|
||||
eventsByDay = new Map<number, TrustedEvent[]>()
|
||||
eventsByAuthor = new Map<string, TrustedEvent[]>()
|
||||
deletes = new Map<string, number>()
|
||||
|
||||
// Dump/load/clear
|
||||
@@ -25,7 +25,7 @@ export class Repository extends Emitter {
|
||||
return Array.from(this.eventsById.values())
|
||||
}
|
||||
|
||||
load = async (events: CustomEvent[], chunkSize = 1000) => {
|
||||
load = async (events: TrustedEvent[], chunkSize = 1000) => {
|
||||
this.clear()
|
||||
|
||||
const added = []
|
||||
@@ -69,7 +69,7 @@ export class Repository extends Emitter {
|
||||
: this.eventsById.get(idOrAddress)
|
||||
}
|
||||
|
||||
hasEvent = (event: CustomEvent) => {
|
||||
hasEvent = (event: TrustedEvent) => {
|
||||
const duplicate = (
|
||||
this.eventsById.get(event.id) ||
|
||||
this.eventsByAddress.get(getAddress(event))
|
||||
@@ -79,12 +79,12 @@ export class Repository extends Emitter {
|
||||
}
|
||||
|
||||
query = (filters: Filter[], {includeDeleted = false} = {}) => {
|
||||
const result: CustomEvent[][] = []
|
||||
const result: TrustedEvent[][] = []
|
||||
for (let filter of filters) {
|
||||
let events: CustomEvent[] = Array.from(this.eventsById.values())
|
||||
let events: TrustedEvent[] = Array.from(this.eventsById.values())
|
||||
|
||||
if (filter.ids) {
|
||||
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as CustomEvent[]
|
||||
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as TrustedEvent[]
|
||||
filter = omit(['ids'], filter)
|
||||
} else if (filter.authors) {
|
||||
events = uniq(filter.authors!.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || []))
|
||||
@@ -112,8 +112,8 @@ export class Repository extends Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
const chunk: CustomEvent[] = []
|
||||
for (const event of sortBy((e: CustomEvent) => -e.created_at, events)) {
|
||||
const chunk: TrustedEvent[] = []
|
||||
for (const event of sortBy((e: TrustedEvent) => -e.created_at, events)) {
|
||||
if (filter.limit && chunk.length >= filter.limit) {
|
||||
break
|
||||
}
|
||||
@@ -133,7 +133,7 @@ export class Repository extends Emitter {
|
||||
return uniq(flatten(result))
|
||||
}
|
||||
|
||||
publish = (event: CustomEvent, {shouldNotify = true} = {}): boolean => {
|
||||
publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
|
||||
if (!isTrustedEvent(event)) {
|
||||
throw new Error("Invalid event published to Repository", event)
|
||||
}
|
||||
@@ -203,19 +203,19 @@ export class Repository extends Emitter {
|
||||
return true
|
||||
}
|
||||
|
||||
isDeletedByAddress = (event: CustomEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
isDeletedByAddress = (event: TrustedEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
|
||||
isDeletedById = (event: CustomEvent) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
isDeletedById = (event: TrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
|
||||
isDeleted = (event: CustomEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
isDeleted = (event: TrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
|
||||
// Utilities
|
||||
|
||||
_updateIndex<K>(m: Map<K, CustomEvent[]>, k: K, e: CustomEvent, duplicate?: CustomEvent) {
|
||||
_updateIndex<K>(m: Map<K, TrustedEvent[]>, k: K, e: TrustedEvent, duplicate?: TrustedEvent) {
|
||||
let a = m.get(k) || []
|
||||
|
||||
if (duplicate) {
|
||||
a = a.filter((x: CustomEvent) => x !== duplicate)
|
||||
a = a.filter((x: TrustedEvent) => x !== duplicate)
|
||||
}
|
||||
|
||||
a.push(e)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {first, splitAt, identity, sortBy, uniq, shuffle, pushToMapKey} from '@welshman/lib'
|
||||
import {Tags} from './Tags'
|
||||
import type {CustomEvent} from './Events'
|
||||
import type {TrustedEvent} from './Events'
|
||||
import {isShareableRelayUrl} from './Relay'
|
||||
import {isCommunityAddress, isGroupAddress} from './Address'
|
||||
|
||||
@@ -172,19 +172,19 @@ export class Router {
|
||||
this.getPubkeySelection(pubkey, RelayMode.Inbox),
|
||||
]).policy(this.addMinimalFallbacks)
|
||||
|
||||
Event = (event: CustomEvent) =>
|
||||
Event = (event: TrustedEvent) =>
|
||||
this.scenario(this.forceValue(event.id, [
|
||||
this.getPubkeySelection(event.pubkey, RelayMode.Write),
|
||||
...this.getContextSelections(Tags.fromEvent(event).context()),
|
||||
]))
|
||||
|
||||
EventChildren = (event: CustomEvent) =>
|
||||
EventChildren = (event: TrustedEvent) =>
|
||||
this.scenario(this.forceValue(event.id, [
|
||||
this.getPubkeySelection(event.pubkey, RelayMode.Read),
|
||||
...this.getContextSelections(Tags.fromEvent(event).context()),
|
||||
]))
|
||||
|
||||
EventAncestors = (event: CustomEvent, type: "mentions" | "replies" | "roots") => {
|
||||
EventAncestors = (event: TrustedEvent, type: "mentions" | "replies" | "roots") => {
|
||||
const tags = Tags.fromEvent(event)
|
||||
const ancestors = tags.ancestors()[type]
|
||||
const pubkeys = tags.values("p").valueOf()
|
||||
@@ -201,13 +201,13 @@ export class Router {
|
||||
return this.product(ancestors.values().valueOf(), relays)
|
||||
}
|
||||
|
||||
EventMentions = (event: CustomEvent) => this.EventAncestors(event, "mentions")
|
||||
EventMentions = (event: TrustedEvent) => this.EventAncestors(event, "mentions")
|
||||
|
||||
EventParents = (event: CustomEvent) => this.EventAncestors(event, "replies")
|
||||
EventParents = (event: TrustedEvent) => this.EventAncestors(event, "replies")
|
||||
|
||||
EventRoots = (event: CustomEvent) => this.EventAncestors(event, "roots")
|
||||
EventRoots = (event: TrustedEvent) => this.EventAncestors(event, "roots")
|
||||
|
||||
PublishEvent = (event: CustomEvent) => {
|
||||
PublishEvent = (event: TrustedEvent) => {
|
||||
const tags = Tags.fromEvent(event)
|
||||
const mentions = tags.values("p").valueOf()
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import {EventTemplate} from 'nostr-tools'
|
||||
import type {OmitStatics} from '@welshman/lib'
|
||||
import {Fluent, nth, nthEq, ensurePlural} from '@welshman/lib'
|
||||
import {isRelayUrl, normalizeRelayUrl} from './Relay'
|
||||
@@ -42,9 +41,9 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
||||
|
||||
static wrap = (p: Iterable<string[]>) => new Tags(Array.from(p).map(Tag.from))
|
||||
|
||||
static fromEvent = (event: Pick<EventTemplate, "tags">) => Tags.wrap(event.tags || [])
|
||||
static fromEvent = (event: {tags: string[][]}) => Tags.wrap(event.tags || [])
|
||||
|
||||
static fromEvents = (events: Pick<EventTemplate, "tags">[]) => Tags.wrap(events.flatMap(e => e.tags || []))
|
||||
static fromEvents = (events: {tags: string[][]}[]) => Tags.wrap(events.flatMap(e => e.tags || []))
|
||||
|
||||
static fromIMeta = (imeta: string[]) => Tags.wrap(imeta.map((m: string) => m.split(" ")))
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {hexToBech32} from '@welshman/lib'
|
||||
import type {CustomEvent} from './Events'
|
||||
import type {TrustedEvent} from './Events'
|
||||
import {Tags} from "./Tags"
|
||||
|
||||
const DIVISORS = {
|
||||
@@ -81,12 +81,12 @@ export type Zapper = {
|
||||
}
|
||||
|
||||
export type Zap = {
|
||||
request: CustomEvent
|
||||
response: CustomEvent,
|
||||
request: TrustedEvent
|
||||
response: TrustedEvent,
|
||||
invoiceAmount: number
|
||||
}
|
||||
|
||||
export const zapFromEvent = (response: CustomEvent, zapper: Zapper) => {
|
||||
export const zapFromEvent = (response: TrustedEvent, zapper: Zapper) => {
|
||||
const responseMeta = Tags.fromEvent(response).asObject()
|
||||
|
||||
let zap: Zap
|
||||
|
||||
Reference in New Issue
Block a user