From eb3c2aad4cef9b86294bc9a13a36da4918529209 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 11 Feb 2025 20:07:21 -0800 Subject: nwc notifications --- lib/notifications.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'lib/notifications.ts') 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); + } +}; -- cgit v1.2.3