diff options
| author | Adam Soltys <[email protected]> | 2025-02-11 20:07:21 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-02-11 20:07:21 -0800 |
| commit | eb3c2aad4cef9b86294bc9a13a36da4918529209 (patch) | |
| tree | 33c58529ee50bf875620a5a33d97e8ab57b673ae /lib/notifications.ts | |
| parent | 004ac3ccbfb6a12227a81d6e0dc0beaf9a740827 (diff) | |
| download | coinos-server-eb3c2aad4cef9b86294bc9a13a36da4918529209.tar.gz coinos-server-eb3c2aad4cef9b86294bc9a13a36da4918529209.zip | |
nwc notifications
Diffstat (limited to 'lib/notifications.ts')
| -rw-r--r-- | lib/notifications.ts | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/lib/notifications.ts b/lib/notifications.ts index 8891d875..e988a728 100644 --- a/lib/notifications.ts +++ b/lib/notifications.ts @@ -1,10 +1,13 @@ import config from "$config"; import { db } from "$lib/db"; +import ln from "$lib/ln"; import { err, warn } from "$lib/logging"; import { mail, templates } from "$lib/mail"; import mqtt from "$lib/mqtt"; +import { publish, serverSecret } from "$lib/nostr"; import { emit } from "$lib/sockets"; -import { f, fiat, fmt, link, t } from "$lib/utils"; +import { f, fiat, fmt, getUser, link, nada, t } from "$lib/utils"; +import { finalizeEvent } from "nostr-tools"; import webpush from "web-push"; if (config.vapid) { @@ -74,3 +77,45 @@ export const notify = async (p, user, withdrawal) => { ); } }; + +export const nwcNotify = async (p) => { + try { + const user = await getUser(p.uid); + const pubkeys = await db.sMembers(`${user.id}:apps`); + if (pubkeys.length) { + let payment_hash = ""; + if (p.type === "lightning") ({ payment_hash } = await ln.decode(p.hash)); + for (const pubkey of pubkeys) { + const notification = { + type: p.amount > 0 ? "incoming" : "outgoing", + invoice: p.hash, + description: p.memo, + preimage: p.ref, + payment_hash: payment_hash, + amount: Math.abs(p.amount) * 1000, + fees_paid: (parseInt(p.fee) || 0) * 1000, + created_at: p.created, + settled_at: p.created, + }; + + const content = JSON.stringify({ + notification_type: p.amount > 0 ? "payment_received" : "payment_sent", + notification, + }); + + const unsigned = { + content, + tags: [["p", pubkey]], + kind: 23196, + created_at: Math.floor(Date.now() / 1000), + }; + + const event = finalizeEvent(unsigned, serverSecret); + + publish(event).catch(nada); + } + } + } catch (e) { + warn("nwc notification failed", e.message); + } +}; |
