forked from adamritter/nostr-relaypool-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.ts
80 lines (75 loc) · 1.88 KB
/
event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type {Kind, Event} from "nostr-tools";
import {Author} from "./author";
import type {RelayPool} from "./relay-pool";
import type {OnEvent} from "./on-event-filters";
export class EventObject implements Event {
id: string;
kind: Kind;
pubkey: string;
tags: string[][];
created_at: number;
content: string;
relayPool: RelayPool;
relays: string[] | undefined;
sig: string;
constructor(
event: Event,
relayPool: RelayPool,
relays: string[] | undefined
) {
this.id = event.id;
this.kind = event.kind;
this.pubkey = event.pubkey;
this.tags = event.tags;
this.created_at = event.created_at;
this.content = event.content;
this.relayPool = relayPool;
this.relays = relays;
this.sig = event.sig;
}
referencedAuthors(): Author[] {
const r: Author[] = [];
for (const tag of this.tags) {
if (tag[0] === "p") {
r.push(new Author(this.relayPool, undefined, tag[1]));
}
}
return r;
}
referencedEvents(maxDelayms: number): Promise<EventObject>[] {
const r: Promise<EventObject>[] = [];
for (const tag of this.tags) {
if (tag[0] === "e") {
let relays = this.relays;
if (tag[2]) {
relays = [tag[2], ...(relays || [])];
}
r.push(
this.relayPool
// @ts-ignore
.getEventById(tag[1], relays, maxDelayms)
.then((e) => new EventObject(e, this.relayPool, this.relays))
);
}
}
return r;
}
thread(cb: OnEvent, maxDelayms: number): () => void {
let relays = this.relays;
let ids: string[] = [];
for (const tag of this.tags) {
if (tag[0] === "e") {
if (tag[2]) {
relays = [tag[2], ...(relays || [])];
}
ids.push(tag[1]);
}
}
return this.relayPool.subscribe(
[{ids}, {"#e": ids}],
relays,
cb,
maxDelayms
);
}
}