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
|
import config from "$config";
import { db, g } from "$lib/db";
import { l, warn } from "$lib/logging";
import { getUser } from "$lib/utils";
import { Relay } from "nostr-tools/relay";
import webpush from "web-push";
if (config.vapid) {
webpush.setVapidDetails(`mailto:${config.support}`, config.vapid.pk, config.vapid.sk);
}
const handleDmEvent = async (event: any) => {
const pTag = event.tags.find((t: any) => t[0] === "p");
l(`dm event ${event.id.slice(0, 8)} p=${pTag?.[1]?.slice(0, 12) || "none"}`);
if (!pTag) return;
const recipientPubkey = pTag[1];
try {
const user = await getUser(recipientPubkey);
if (!user?.id) return;
const subscriptions = await db.sMembers(`${user.id}:subscriptions`);
if (!subscriptions?.length) return;
l(`dm push to ${user.username} (${subscriptions.length} subs)`);
const payload = JSON.stringify({
title: "New message",
body: "You have a new encrypted message",
url: "/messages",
});
for (const s of subscriptions as any) {
webpush
.sendNotification(JSON.parse(s as string), payload)
.catch((e) => {
warn("dm push failed", e.message);
db.sRem(`${user.id}:subscriptions`, s);
});
}
} catch (e) {
// User not found for this pubkey - not a coinos user
}
};
const subscribeRelay = (url: string) => {
Relay.connect(url)
.then((relay) => {
relay.subscribe([{ kinds: [1059], since: Math.floor(Date.now() / 1000) }], {
onevent: handleDmEvent,
});
l(`listening for DM notifications on ${url}`);
})
.catch((e) => {
warn(`DM listener failed ${url}`, e.message);
setTimeout(() => subscribeRelay(url), 5000);
});
};
export const listenForDMs = () => {
const relays = new Set([config.nostr, ...config.relays.filter((r: string) => r.startsWith("wss://"))]);
for (const url of relays) subscribeRelay(url);
};
|