Get rid of domain module, allow app to override default event type

This commit is contained in:
Jon Staab
2024-08-13 15:45:20 -07:00
parent 5a63273b9d
commit 149c29472c
33 changed files with 201 additions and 285 deletions
+43
View File
@@ -0,0 +1,43 @@
import type {EventContent, CustomEvent} from './Events'
export type Encrypt = (x: string) => Promise<string>
export type DecryptedEvent = CustomEvent & {
plaintext: Partial<EventContent>
}
export const asDecryptedEvent = (event: CustomEvent, plaintext: Partial<EventContent>) =>
({...event, plaintext}) as DecryptedEvent
export class Encryptable<E extends Partial<EventContent>> {
constructor(readonly event: E, readonly updates: E) {}
async reconcile(encrypt: Encrypt) {
const encryptContent = () => {
if (!this.updates.content) return null
return encrypt(this.updates.content)
}
const encryptTags = () => {
if (!this.updates.tags) return null
return Promise.all(
this.updates.tags.map(async tag => {
tag[1] = await encrypt(tag[1])
return tag
})
)
}
const [content, tags] = await Promise.all([encryptContent(), encryptTags()])
// Updates are optional. If not provided, fall back to the event's content and tags.
return {
...this.event,
tags: tags || this.event.tags || [],
content: content || this.event.content || "",
}
}
}
+2 -1
View File
@@ -38,7 +38,8 @@ export type TrustedEvent = HashedEvent & {
[verifiedSymbol]?: boolean
}
export type ExtensibleTrustedEvent = TrustedEvent & Record<string, any>
/* eslint @typescript-eslint/no-empty-interface: 0 */
export interface CustomEvent extends TrustedEvent {}
export type CreateEventOpts = {
content?: string
+3 -4
View File
@@ -1,7 +1,6 @@
import {Event} from 'nostr-tools'
import {matchFilter as nostrToolsMatchFilter} from 'nostr-tools'
import {uniqBy, prop, mapVals, shuffle, avg, hash, groupBy, randomId, uniq} from '@welshman/lib'
import type {HashedEvent, TrustedEvent} from './Events'
import type {HashedEvent, CustomEvent, SignedEvent} from './Events'
import {isReplaceableKind} from './Kinds'
import {Address, getAddress} from './Address'
@@ -19,7 +18,7 @@ export type Filter = {
}
export const matchFilter = <E extends HashedEvent>(filter: Filter, event: E) => {
if (!nostrToolsMatchFilter(filter, event as unknown as Event)) {
if (!nostrToolsMatchFilter(filter, event as unknown as SignedEvent)) {
return false
}
@@ -156,7 +155,7 @@ export const getIdFilters = (idsOrAddresses: string[]) => {
return filters
}
export const getReplyFilters = (events: TrustedEvent[], filter: Filter) => {
export const getReplyFilters = (events: CustomEvent[], filter: Filter) => {
const a = []
const e = []
+48
View File
@@ -0,0 +1,48 @@
import {fromPairs, last, first, parseJson} from "@welshman/lib"
import {getAddress} from "./Address"
import {getAddressTags, getKindTagValues} from "./Tags"
import type {CustomEvent} from "./Events"
export type Handler = {
kind: number
name: string
about: string
image: string
identifier: string
event: CustomEvent
website?: string
lud16?: string
nip05?: string
}
export const readHandlers = (event: CustomEvent) => {
const {d: identifier} = fromPairs(event.tags)
const meta = parseJson(event.content)
const normalizedMeta = {
name: meta?.name || meta?.display_name || "",
image: meta?.image || meta?.picture || "",
about: meta?.about || "",
website: meta?.website || "",
lud16: meta?.lud16 || "",
nip05: meta?.nip05 || "",
}
// If our meta is missing important stuff, don't bother showing it
if (!normalizedMeta.name || !normalizedMeta.image) {
return []
}
return getKindTagValues(event.tags)
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler[]
}
export const getHandlerKey = (handler: Handler) => `${handler.kind}:${getAddress(handler.event)}`
export const displayHandler = (handler?: Handler, fallback = "") => handler?.name || fallback
export const getHandlerAddress = (event: CustomEvent) => {
const tags = getAddressTags(event.tags)
const tag = tags.find(t => last(t) === "web") || first(tags)
return tag?.[1]
}
+49
View File
@@ -0,0 +1,49 @@
import {parseJson, nth, nthEq} from "@welshman/lib"
import {Address} from "./Address"
import {isShareableRelayUrl} from "./Relay"
import {Encryptable, DecryptedEvent} from "./Encryptable"
export type ListParams = {
kind: number
}
export type List = ListParams & {
publicTags: string[][]
privateTags: string[][]
event?: DecryptedEvent
}
export type PublishedList = Omit<List, "event"> & {
event: DecryptedEvent
}
export const makeList = (list: ListParams & Partial<List>): List =>
({publicTags: [], privateTags: [], ...list})
const isValidTag = (tag: string[]) => {
if (tag[0] === "p") return tag[1]?.length === 64
if (tag[0] === "e") return tag[1]?.length === 64
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
if (tag[0] === "t") return tag[1]?.length > 0
if (tag[0] === "r") return isShareableRelayUrl(tag[1])
if (tag[0] === "relay") return isShareableRelayUrl(tag[1])
return true
}
export const readList = (event: DecryptedEvent): PublishedList => {
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
const privateTags = getTags(parseJson(event.plaintext?.content))
const publicTags = getTags(event.tags)
return {event, kind: event.kind, publicTags, privateTags}
}
export const createList = ({kind, publicTags = [], privateTags = []}: List) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const getListValues = (tagName: string, list: List | undefined) =>
[...list?.publicTags || [], ...list?.privateTags || []].filter(nthEq(0, tagName)).map(nth(1))
+72
View File
@@ -0,0 +1,72 @@
import {nip19} from "nostr-tools"
import {ellipsize, parseJson} from "@welshman/lib"
import {CustomEvent} from "./Events"
import {PROFILE} from "./Kinds"
export type Profile = {
name?: string
nip05?: string
lud06?: string
lud16?: string
about?: string
banner?: string
picture?: string
website?: string
display_name?: string
event?: CustomEvent
}
export type PublishedProfile = Omit<Profile, "event"> & {
event: CustomEvent
}
export const isPublishedProfile = (profile: Profile): profile is PublishedProfile =>
Boolean(profile.event)
export const makeProfile = (profile: Partial<Profile> = {}): Profile => ({
name: "",
nip05: "",
lud06: "",
lud16: "",
about: "",
banner: "",
picture: "",
website: "",
display_name: "",
...profile,
})
export const readProfile = (event: CustomEvent) => {
const profile = parseJson(event.content) || {}
return {...profile, event} as PublishedProfile
}
export const createProfile = ({event, ...profile}: Profile) => ({
kind: PROFILE,
content: JSON.stringify(profile),
})
export const editProfile = ({event, ...profile}: PublishedProfile) => ({
kind: PROFILE,
content: JSON.stringify(profile),
tags: event.tags,
})
export const displayPubkey = (pubkey: string) => {
const d = nip19.npubEncode(pubkey)
return d.slice(0, 8) + "…" + d.slice(-5)
}
export const displayProfile = (profile?: Profile, fallback = "") => {
const {display_name, name, event} = profile || {}
if (name) return ellipsize(name, 60)
if (display_name) return ellipsize(display_name, 60)
if (event) return displayPubkey(event.pubkey)
return fallback
}
export const profileHasName = (profile?: Profile) => Boolean(profile?.name || profile?.display_name)
+36 -9
View File
@@ -1,22 +1,45 @@
import {Emitter, normalizeUrl, sleep, stripProtocol} from '@welshman/lib'
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 {ExtensibleTrustedEvent} from './Events'
import type {CustomEvent} from './Events'
// Constants and types
export const LOCAL_RELAY_URL = "local://welshman.relay"
export const BOGUS_RELAY_URL = "bogus://welshman.relay"
export type RelayProfile = {
url: string
name?: string
pubkey?: string
contact?: string
description?: string
supported_nips?: number[]
limitation?: {
payment_required?: boolean
auth_required?: boolean
}
}
// Utils related to bare urls
export const isRelayUrl = (url: string) => {
try {
new URL(url)
} catch (e) {
return false
}
return true
}
export const isShareableRelayUrl = (url: string) =>
Boolean(
typeof url === 'string' &&
isRelayUrl(url) &&
// Is it actually a websocket url and has a dot
url.match(/^wss:\/\/.+\..+/) &&
// Sometimes bugs cause multiple relays to get concatenated
url.match(/:\/\//g)?.length === 1 &&
// It shouldn't have any whitespace, url-encoded or otherwise
!url.match(/\s|%/) &&
// Don't match stuff with a port number
!url.slice(6).match(/:\d+/) &&
// Don't match stuff with a numeric tld
@@ -48,6 +71,10 @@ export const normalizeRelayUrl = (url: string, {allowInsecure = false}: Normaliz
return prefix + url
}
export const displayRelayUrl = (url: string) => last(url.split("://")).replace(/\/$/, "")
// In-memory relay implementation backed by Repository
export class Relay extends Emitter {
subs = new Map<string, Filter[]>()
@@ -57,13 +84,13 @@ export class Relay extends Emitter {
send(type: string, ...message: any[]) {
switch(type) {
case 'EVENT': return this.handleEVENT(message as [ExtensibleTrustedEvent])
case 'EVENT': return this.handleEVENT(message as [CustomEvent])
case 'CLOSE': return this.handleCLOSE(message as [string])
case 'REQ': return this.handleREQ(message as [string, ...Filter[]])
}
}
handleEVENT([event]: [ExtensibleTrustedEvent]) {
handleEVENT([event]: [CustomEvent]) {
this.repository.publish(event)
// Callers generally expect async relays
+20 -20
View File
@@ -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 {ExtensibleTrustedEvent} from './Events'
import type {CustomEvent} from './Events'
export const DAY = 86400
const getDay = (ts: number) => Math.floor(ts / DAY)
export class Repository extends Emitter {
eventsById = new Map<string, ExtensibleTrustedEvent>()
eventsByWrap = new Map<string, ExtensibleTrustedEvent>()
eventsByAddress = new Map<string, ExtensibleTrustedEvent>()
eventsByTag = new Map<string, ExtensibleTrustedEvent[]>()
eventsByDay = new Map<number, ExtensibleTrustedEvent[]>()
eventsByAuthor = new Map<string, ExtensibleTrustedEvent[]>()
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[]>()
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: ExtensibleTrustedEvent[], chunkSize = 1000) => {
load = async (events: CustomEvent[], chunkSize = 1000) => {
this.clear()
const added = []
@@ -69,7 +69,7 @@ export class Repository extends Emitter {
: this.eventsById.get(idOrAddress)
}
hasEvent = (event: ExtensibleTrustedEvent) => {
hasEvent = (event: CustomEvent) => {
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: ExtensibleTrustedEvent[][] = []
const result: CustomEvent[][] = []
for (let filter of filters) {
let events: ExtensibleTrustedEvent[] = Array.from(this.eventsById.values())
let events: CustomEvent[] = Array.from(this.eventsById.values())
if (filter.ids) {
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as ExtensibleTrustedEvent[]
events = filter.ids!.map(id => this.eventsById.get(id)).filter(identity) as CustomEvent[]
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: ExtensibleTrustedEvent[] = []
for (const event of sortBy((e: ExtensibleTrustedEvent) => -e.created_at, events)) {
const chunk: CustomEvent[] = []
for (const event of sortBy((e: CustomEvent) => -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: ExtensibleTrustedEvent, {shouldNotify = true} = {}): boolean => {
publish = (event: CustomEvent, {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: ExtensibleTrustedEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
isDeletedByAddress = (event: CustomEvent) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
isDeletedById = (event: ExtensibleTrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
isDeletedById = (event: CustomEvent) => (this.deletes.get(event.id) || 0) > event.created_at
isDeleted = (event: ExtensibleTrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
isDeleted = (event: CustomEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
// Utilities
_updateIndex<K>(m: Map<K, ExtensibleTrustedEvent[]>, k: K, e: ExtensibleTrustedEvent, duplicate?: ExtensibleTrustedEvent) {
_updateIndex<K>(m: Map<K, CustomEvent[]>, k: K, e: CustomEvent, duplicate?: CustomEvent) {
let a = m.get(k) || []
if (duplicate) {
a = a.filter((x: ExtensibleTrustedEvent) => x !== duplicate)
a = a.filter((x: CustomEvent) => x !== duplicate)
}
a.push(e)
+8 -8
View File
@@ -1,6 +1,6 @@
import {first, splitAt, identity, sortBy, uniq, shuffle, pushToMapKey} from '@welshman/lib'
import {Tags} from './Tags'
import type {TrustedEvent} from './Events'
import type {CustomEvent} 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: TrustedEvent) =>
Event = (event: CustomEvent) =>
this.scenario(this.forceValue(event.id, [
this.getPubkeySelection(event.pubkey, RelayMode.Write),
...this.getContextSelections(Tags.fromEvent(event).context()),
]))
EventChildren = (event: TrustedEvent) =>
EventChildren = (event: CustomEvent) =>
this.scenario(this.forceValue(event.id, [
this.getPubkeySelection(event.pubkey, RelayMode.Read),
...this.getContextSelections(Tags.fromEvent(event).context()),
]))
EventAncestors = (event: TrustedEvent, type: "mentions" | "replies" | "roots") => {
EventAncestors = (event: CustomEvent, 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: TrustedEvent) => this.EventAncestors(event, "mentions")
EventMentions = (event: CustomEvent) => this.EventAncestors(event, "mentions")
EventParents = (event: TrustedEvent) => this.EventAncestors(event, "replies")
EventParents = (event: CustomEvent) => this.EventAncestors(event, "replies")
EventRoots = (event: TrustedEvent) => this.EventAncestors(event, "roots")
EventRoots = (event: CustomEvent) => this.EventAncestors(event, "roots")
PublishEvent = (event: TrustedEvent) => {
PublishEvent = (event: CustomEvent) => {
const tags = Tags.fromEvent(event)
const mentions = tags.values("p").valueOf()
+8 -4
View File
@@ -1,7 +1,7 @@
import {EventTemplate} from 'nostr-tools'
import type {OmitStatics} from '@welshman/lib'
import {Fluent, ensurePlural} from '@welshman/lib'
import {isShareableRelayUrl, normalizeRelayUrl} from './Relay'
import {isRelayUrl, normalizeRelayUrl} from './Relay'
import {Address, isContextAddress} from './Address'
import {GROUP, COMMUNITY} from './Kinds'
@@ -71,7 +71,7 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
entries = () => this.mapTo(t => t.entry())
relays = () => this.flatMap((t: Tag) => t.valueOf().filter(isShareableRelayUrl).map(url => normalizeRelayUrl(url))).uniq()
relays = () => this.flatMap((t: Tag) => t.valueOf().filter(isRelayUrl).map(url => normalizeRelayUrl(url))).uniq()
topics = () => this.whereKey("t").values().map((t: string) => t.replace(/^#/, ""))
@@ -240,10 +240,14 @@ export const getPubkeyTags = getTags(["p"], pk => pk.length === 64)
export const getPubkeyTagValues = getTagValues(["p"], pk => pk.length === 64)
export const getRelayTags = getTags(["r", "relay"], isShareableRelayUrl)
export const getRelayTags = getTags(["r", "relay"], isRelayUrl)
export const getRelayTagValues = getTagValues(["r", "relay"], isShareableRelayUrl)
export const getRelayTagValues = getTagValues(["r", "relay"], isRelayUrl)
export const getGroupTags = getTags(["h", "group"], h => Boolean(h.match(/^(.+)'(.+)$/)))
export const getGroupTagValues = getTagValues(["h", "group"], h => Boolean(h.match(/^(.+)'(.+)$/)))
export const getKindTags = getTags(["k"], h => Boolean(h.match(/^\d+$/)))
export const getKindTagValues = getTagValues(["k"], h => Boolean(h.match(/^\d+$/)))
+4 -4
View File
@@ -1,5 +1,5 @@
import {hexToBech32} from '@welshman/lib'
import type {TrustedEvent} from './Events'
import type {CustomEvent} from './Events'
import {Tags} from "./Tags"
const DIVISORS = {
@@ -81,12 +81,12 @@ export type Zapper = {
}
export type Zap = {
request: TrustedEvent
response: TrustedEvent,
request: CustomEvent
response: CustomEvent,
invoiceAmount: number
}
export const zapFromEvent = (response: TrustedEvent, zapper: Zapper) => {
export const zapFromEvent = (response: CustomEvent, zapper: Zapper) => {
const responseMeta = Tags.fromEvent(response).asObject()
let zap: Zap
+4
View File
@@ -1,8 +1,12 @@
export * from './Address'
export * from './Encryptable'
export * from './Events'
export * from './Filters'
export * from './Handler'
export * from './Kinds'
export * from './Links'
export * from './List'
export * from './Profile'
export * from './Relay'
export * from './Repository'
export * from './Router'