Add wrap manager for tracking gift wraps
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
export * from "./repository.js"
|
||||
export * from "./relay.js"
|
||||
export * from "./repository.js"
|
||||
export * from "./tracker.js"
|
||||
export * from "./wrapManager.js"
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import {Emitter, sleep} from "@welshman/lib"
|
||||
import {Filter, TrustedEvent, HashedEvent, matchFilters} from "@welshman/util"
|
||||
import {Filter, TrustedEvent, matchFilters} from "@welshman/util"
|
||||
import {Repository} from "./repository.js"
|
||||
|
||||
export class LocalRelay<E extends HashedEvent = TrustedEvent> extends Emitter {
|
||||
export class LocalRelay extends Emitter {
|
||||
subs = new Map<string, Filter[]>()
|
||||
|
||||
constructor(readonly repository: Repository<E>) {
|
||||
constructor(readonly repository: Repository) {
|
||||
super()
|
||||
}
|
||||
|
||||
send(type: string, ...message: any[]) {
|
||||
switch (type) {
|
||||
case "EVENT":
|
||||
return this.handleEVENT(message as [E])
|
||||
return this.handleEVENT(message as [TrustedEvent])
|
||||
case "CLOSE":
|
||||
return this.handleCLOSE(message as [string])
|
||||
case "REQ":
|
||||
@@ -20,7 +20,7 @@ export class LocalRelay<E extends HashedEvent = TrustedEvent> extends Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
handleEVENT([event]: [E]) {
|
||||
handleEVENT([event]: [TrustedEvent]) {
|
||||
this.repository.publish(event)
|
||||
|
||||
// Callers generally expect async relays
|
||||
|
||||
@@ -1,47 +1,32 @@
|
||||
import {
|
||||
DAY,
|
||||
Emitter,
|
||||
flatten,
|
||||
pluck,
|
||||
sortBy,
|
||||
inc,
|
||||
chunk,
|
||||
uniq,
|
||||
omit,
|
||||
now,
|
||||
range,
|
||||
} from "@welshman/lib"
|
||||
import {DAY, Emitter, flatten, pluck, sortBy, inc, uniq, omit, now, range} from "@welshman/lib"
|
||||
import {
|
||||
DELETE,
|
||||
EPOCH,
|
||||
matchFilter,
|
||||
isReplaceable,
|
||||
isUnwrappedEvent,
|
||||
getAddress,
|
||||
Filter,
|
||||
TrustedEvent,
|
||||
HashedEvent,
|
||||
} from "@welshman/util"
|
||||
|
||||
export const LOCAL_RELAY_URL = "local://welshman.relay/"
|
||||
|
||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||
|
||||
export let repositorySingleton: Repository<TrustedEvent>
|
||||
export let repositorySingleton: Repository
|
||||
|
||||
export type RepositoryUpdate = {
|
||||
added: TrustedEvent[]
|
||||
removed: Set<string>
|
||||
}
|
||||
|
||||
export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
eventsById = new Map<string, E>()
|
||||
eventsByWrap = new Map<string, E>()
|
||||
eventsByAddress = new Map<string, E>()
|
||||
eventsByTag = new Map<string, E[]>()
|
||||
eventsByDay = new Map<number, E[]>()
|
||||
eventsByAuthor = new Map<string, E[]>()
|
||||
eventsByKind = new Map<number, E[]>()
|
||||
export class Repository extends Emitter {
|
||||
eventsById = new Map<string, TrustedEvent>()
|
||||
eventsByAddress = new Map<string, TrustedEvent>()
|
||||
eventsByTag = new Map<string, TrustedEvent[]>()
|
||||
eventsByDay = new Map<number, TrustedEvent[]>()
|
||||
eventsByAuthor = new Map<string, TrustedEvent[]>()
|
||||
eventsByKind = new Map<number, TrustedEvent[]>()
|
||||
deletes = new Map<string, number>()
|
||||
expired = new Map<string, number>()
|
||||
|
||||
@@ -65,11 +50,10 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
return Array.from(this.eventsById.values())
|
||||
}
|
||||
|
||||
load = (events: E[], chunkSize = 1000) => {
|
||||
load = (events: TrustedEvent[]) => {
|
||||
const stale = new Set(this.eventsById.keys())
|
||||
|
||||
this.eventsById.clear()
|
||||
this.eventsByWrap.clear()
|
||||
this.eventsByAddress.clear()
|
||||
this.eventsByTag.clear()
|
||||
this.eventsByDay.clear()
|
||||
@@ -80,13 +64,11 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
|
||||
const added = []
|
||||
|
||||
for (const eventsChunk of chunk(chunkSize, events)) {
|
||||
for (const event of eventsChunk) {
|
||||
if (this.publish(event, {shouldNotify: false})) {
|
||||
// Don't send duplicate events to subscribers
|
||||
if (!stale.has(event.id)) {
|
||||
added.push(event)
|
||||
}
|
||||
for (const event of events) {
|
||||
if (this.publish(event, {shouldNotify: false})) {
|
||||
// Don't send duplicate events to subscribers
|
||||
if (!stale.has(event.id)) {
|
||||
added.push(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,7 +103,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
: this.eventsById.get(idOrAddress)
|
||||
}
|
||||
|
||||
hasEvent = (event: E) => {
|
||||
hasEvent = (event: TrustedEvent) => {
|
||||
const duplicate = this.eventsById.get(event.id) || this.eventsByAddress.get(getAddress(event))
|
||||
|
||||
return duplicate && duplicate.created_at >= event.created_at
|
||||
@@ -132,11 +114,6 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
|
||||
if (event) {
|
||||
this.eventsById.delete(event.id)
|
||||
|
||||
for (const wrap of event.wraps || []) {
|
||||
this.eventsByWrap.delete(wrap.id)
|
||||
}
|
||||
|
||||
this.eventsByAddress.delete(getAddress(event))
|
||||
|
||||
for (const [k, v] of event.tags) {
|
||||
@@ -157,7 +134,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
filters: Filter[],
|
||||
{includeDeleted = false, includeExpired = false, shouldSort = true} = {},
|
||||
) => {
|
||||
const result: E[][] = []
|
||||
const result: TrustedEvent[][] = []
|
||||
for (const originalFilter of filters) {
|
||||
if (originalFilter.limit !== undefined && !shouldSort) {
|
||||
throw new Error("Unable to skip sorting if limit is defined")
|
||||
@@ -169,7 +146,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
const events = applied ? this._getEvents(applied!.ids) : this.dump()
|
||||
const sorted = this._sortEvents(shouldSort && Boolean(filter.limit), events)
|
||||
|
||||
const chunk: E[] = []
|
||||
const chunk: TrustedEvent[] = []
|
||||
for (const event of sorted) {
|
||||
if (filter.limit && chunk.length >= filter.limit) {
|
||||
break
|
||||
@@ -197,7 +174,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
return this._sortEvents(shouldSortAll, uniq(flatten(result)))
|
||||
}
|
||||
|
||||
publish = (event: E, {shouldNotify = true} = {}): boolean => {
|
||||
publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
|
||||
if (!event?.id) {
|
||||
console.warn("Attempted to publish invalid event to repository", event)
|
||||
|
||||
@@ -234,11 +211,6 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
this.eventsByAddress.set(address, event)
|
||||
}
|
||||
|
||||
// Save wrapper index
|
||||
for (const wrap of event.wraps || []) {
|
||||
this.eventsByWrap.set(wrap.id, event)
|
||||
}
|
||||
|
||||
// Update our timestamp and author indexes
|
||||
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate)
|
||||
this._updateIndex(this.eventsByAuthor, event.pubkey, event, duplicate)
|
||||
@@ -279,13 +251,14 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
return true
|
||||
}
|
||||
|
||||
isDeletedByAddress = (event: E) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
isDeletedByAddress = (event: TrustedEvent) =>
|
||||
(this.deletes.get(getAddress(event)) || 0) > event.created_at
|
||||
|
||||
isDeletedById = (event: E) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
isDeletedById = (event: TrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
|
||||
|
||||
isDeleted = (event: E) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
isDeleted = (event: TrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
|
||||
|
||||
isExpired = (event: E) => {
|
||||
isExpired = (event: TrustedEvent) => {
|
||||
const ts = this.expired.get(event.id)
|
||||
|
||||
return Boolean(ts && ts < now())
|
||||
@@ -293,14 +266,19 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
|
||||
// Utilities
|
||||
|
||||
_sortEvents = (shouldSort: boolean, events: E[]) =>
|
||||
_sortEvents = (shouldSort: boolean, events: TrustedEvent[]) =>
|
||||
shouldSort ? sortBy(e => -e.created_at, events) : events
|
||||
|
||||
_updateIndex = <K>(m: Map<K, E[]>, k: K, add?: E, remove?: E) => {
|
||||
_updateIndex = <K>(
|
||||
m: Map<K, TrustedEvent[]>,
|
||||
k: K,
|
||||
add?: TrustedEvent,
|
||||
remove?: TrustedEvent,
|
||||
) => {
|
||||
let a = m.get(k) || []
|
||||
|
||||
if (remove) {
|
||||
a = a.filter((x: E) => x !== remove)
|
||||
a = a.filter((x: TrustedEvent) => x !== remove)
|
||||
}
|
||||
|
||||
if (add) {
|
||||
@@ -311,7 +289,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
||||
}
|
||||
|
||||
_getEvents = (ids: Iterable<string>) => {
|
||||
const events: E[] = []
|
||||
const events: TrustedEvent[] = []
|
||||
|
||||
for (const id of ids) {
|
||||
const event = this.eventsById.get(id)
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import {Emitter, addToMapKey} from "@welshman/lib"
|
||||
|
||||
export class Tracker extends Emitter {
|
||||
relaysById = new Map<string, Set<string>>()
|
||||
idsByRelay = new Map<string, Set<string>>()
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
this.setMaxListeners(100)
|
||||
}
|
||||
|
||||
getIds = (relay: string) => this.idsByRelay.get(relay) || new Set<string>()
|
||||
|
||||
getRelays = (eventId: string) => this.relaysById.get(eventId) || new Set<string>()
|
||||
|
||||
hasRelay = (eventId: string, relay: string) => this.relaysById.get(eventId)?.has(relay)
|
||||
|
||||
addRelay = (eventId: string, relay: string) => {
|
||||
let relays = this.relaysById.get(eventId)
|
||||
let ids = this.idsByRelay.get(relay)
|
||||
|
||||
if (relays?.has(relay) && ids?.has(eventId)) return
|
||||
|
||||
if (!relays) {
|
||||
relays = new Set()
|
||||
}
|
||||
|
||||
if (!ids) {
|
||||
ids = new Set()
|
||||
}
|
||||
|
||||
relays.add(relay)
|
||||
ids.add(eventId)
|
||||
|
||||
this.relaysById.set(eventId, relays)
|
||||
this.idsByRelay.set(relay, ids)
|
||||
|
||||
this.emit("add", eventId, relay)
|
||||
}
|
||||
|
||||
removeRelay = (eventId: string, relay: string) => {
|
||||
const didDeleteRelay = this.relaysById.get(eventId)?.delete(relay)
|
||||
const didDeleteId = this.idsByRelay.get(relay)?.delete(eventId)
|
||||
|
||||
if (!didDeleteRelay && !didDeleteId) return
|
||||
|
||||
this.emit("remove", eventId, relay)
|
||||
}
|
||||
|
||||
track = (eventId: string, relay: string) => {
|
||||
const seen = this.relaysById.has(eventId)
|
||||
|
||||
this.addRelay(eventId, relay)
|
||||
|
||||
return seen
|
||||
}
|
||||
|
||||
copy = (eventId1: string, eventId2: string) => {
|
||||
for (const relay of this.getRelays(eventId1)) {
|
||||
this.addRelay(eventId2, relay)
|
||||
}
|
||||
}
|
||||
|
||||
load = (relaysById: Tracker["relaysById"]) => {
|
||||
this.relaysById.clear()
|
||||
this.idsByRelay.clear()
|
||||
|
||||
for (const [id, relays] of relaysById.entries()) {
|
||||
for (const relay of relays) {
|
||||
addToMapKey(this.relaysById, id, relay)
|
||||
addToMapKey(this.idsByRelay, relay, id)
|
||||
}
|
||||
}
|
||||
|
||||
this.emit("load")
|
||||
}
|
||||
|
||||
clear = () => {
|
||||
this.relaysById.clear()
|
||||
this.idsByRelay.clear()
|
||||
|
||||
this.emit("clear")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import {Emitter, remove, omit} from "@welshman/lib"
|
||||
import {HashedEvent, SignedEvent} from "@welshman/util"
|
||||
import {Tracker} from "./tracker.js"
|
||||
import {LocalRelay} from "./relay.js"
|
||||
|
||||
export type WrapItem = Omit<HashedEvent, "content"> & {
|
||||
rumorId: string
|
||||
recipient: string
|
||||
}
|
||||
|
||||
export type WrapReference = string[]
|
||||
|
||||
export type WrapManagerOptions = {
|
||||
relay: LocalRelay
|
||||
tracker: Tracker
|
||||
}
|
||||
|
||||
export class WrapManager extends Emitter {
|
||||
_wrapIndex = new Map<string, WrapItem>()
|
||||
_rumorIndex = new Map<string, WrapReference>()
|
||||
_recipientIndex = new Map<string, WrapReference>()
|
||||
|
||||
constructor(readonly options: WrapManagerOptions) {
|
||||
super()
|
||||
}
|
||||
|
||||
getRumor = (id: string) => {
|
||||
const wrapItem = this._wrapIndex.get(id)
|
||||
|
||||
if (wrapItem) {
|
||||
return this.options.relay.repository.getEvent(wrapItem.rumorId)
|
||||
}
|
||||
}
|
||||
|
||||
// Adding/importing
|
||||
|
||||
load = (wrapItems: WrapItem[]) => {
|
||||
this._wrapIndex.clear()
|
||||
this._rumorIndex.clear()
|
||||
this._recipientIndex.clear()
|
||||
|
||||
for (const wrapItem of wrapItems) {
|
||||
this._add(wrapItem)
|
||||
}
|
||||
|
||||
this.emit("load")
|
||||
}
|
||||
|
||||
add = ({recipient, rumor, wrap}: {recipient: string; rumor: HashedEvent; wrap: SignedEvent}) => {
|
||||
const wrapItem = {
|
||||
...omit(["content"], wrap),
|
||||
rumorId: rumor.id,
|
||||
recipient,
|
||||
}
|
||||
|
||||
this._add(wrapItem)
|
||||
|
||||
// Send via our relay so that listeners get notified
|
||||
this.options.relay.send("EVENT", rumor)
|
||||
|
||||
// Mark the rumor as having come from the wrap's urls
|
||||
this.options.tracker.copy(wrap.id, rumor.id)
|
||||
|
||||
this.emit("add", wrapItem)
|
||||
}
|
||||
|
||||
// Removing
|
||||
|
||||
remove = (id: string) => {
|
||||
const wrapItem = this._wrapIndex.get(id)
|
||||
|
||||
if (wrapItem) {
|
||||
this._remove(wrapItem)
|
||||
this.options.relay.repository.removeEvent(wrapItem.rumorId)
|
||||
this.emit("remove", wrapItem)
|
||||
}
|
||||
}
|
||||
|
||||
removeByRumorId = (rumorId: string) => {
|
||||
for (const id of this._rumorIndex.get(rumorId) || []) {
|
||||
this.remove(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Utils
|
||||
|
||||
_add = (wrapItem: WrapItem) => {
|
||||
this._wrapIndex.set(wrapItem.id, wrapItem)
|
||||
this._addReference(this._rumorIndex, wrapItem.rumorId, wrapItem.id)
|
||||
this._addReference(this._recipientIndex, wrapItem.recipient, wrapItem.id)
|
||||
}
|
||||
|
||||
_addReference = (index: Map<string, WrapReference>, key: string, wrapId: string) => {
|
||||
const reference = index.get(key)
|
||||
|
||||
if (reference) {
|
||||
reference.push(wrapId)
|
||||
} else {
|
||||
index.set(key, [wrapId])
|
||||
}
|
||||
}
|
||||
|
||||
_remove = (wrapItem: WrapItem) => {
|
||||
this._wrapIndex.delete(wrapItem.id)
|
||||
this._removeReference(this._rumorIndex, wrapItem.rumorId, wrapItem.id)
|
||||
this._removeReference(this._recipientIndex, wrapItem.recipient, wrapItem.id)
|
||||
}
|
||||
|
||||
_removeReference = (index: Map<string, WrapReference>, key: string, wrapId: string) => {
|
||||
const reference = index.get(key)
|
||||
|
||||
if (reference) {
|
||||
const wraps = remove(wrapId, reference)
|
||||
|
||||
if (wraps.length > 0) {
|
||||
index.set(key, wraps)
|
||||
} else {
|
||||
index.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user