-
Notifications
You must be signed in to change notification settings - Fork 0
/
nostr.ts
45 lines (40 loc) · 1.17 KB
/
nostr.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
import {
NostrEvent,
} from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/4671ee04a8abe16dc6fcedf76d74508863b4b79f/nostr.ts";
import { ConnectionPool } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/4671ee04a8abe16dc6fcedf76d74508863b4b79f/relay.ts";
const pool = new ConnectionPool();
/* optional await */ pool.addRelayURLs([
"wss://nos.lol",
"wss://relay.nostr.band",
]);
export async function getPost(pubkey: string, identifire: string) {
const subId = pubkey.slice(0, 4) + ":" + identifire;
const subscription = await pool.newSub(subId, {
kinds: [30023],
limit: 1,
authors: [pubkey],
"#d": [identifire],
});
if (subscription instanceof Error) {
// handle the error
console.log(subscription);
return null;
}
const { chan } = subscription;
let event: NostrEvent | null = null;
for await (const { res: msg, url } of chan) {
if (msg.type === "EVENT") {
if (!event) {
event = msg.event;
} else {
if (msg.event.created_at > event.created_at) {
event = msg.event;
}
}
}
if (msg.type === "EOSE") {
pool.closeSub(subId);
}
}
return event;
}