diff options
| author | Adam Soltys <[email protected]> | 2024-10-31 09:03:23 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-31 09:03:23 -0700 |
| commit | 2ff6a289f142636beee1846132049cae7cfd82ea (patch) | |
| tree | a23313129b1aa54c8e7925471fc3e9f873192a86 | |
| parent | b3203b848a25b5aadebfe8fff3e0f01dda1919fa (diff) | |
| download | coinos-server-2ff6a289f142636beee1846132049cae7cfd82ea.tar.gz coinos-server-2ff6a289f142636beee1846132049cae7cfd82ea.zip | |
detect internal payments
| -rw-r--r-- | lib/locations.ts | 3 | ||||
| -rw-r--r-- | lib/notifications.ts | 38 | ||||
| -rw-r--r-- | lib/payments.ts | 6 | ||||
| -rw-r--r-- | routes/payments.ts | 10 |
4 files changed, 34 insertions, 23 deletions
diff --git a/lib/locations.ts b/lib/locations.ts index bb8b08cf..abf578d8 100644 --- a/lib/locations.ts +++ b/lib/locations.ts @@ -18,7 +18,7 @@ const dedup = (array) => export const getLocations = async () => { try { - const previous = await g("locations") || []; + const previous = (await g("locations")) || []; let since = await g("locations:since"); if (!since) since = "2022-09-19T00:00:00Z"; if (Date.now() - new Date(since).getTime() < 60000) return; @@ -45,6 +45,7 @@ export const getLocations = async () => { for await (const l of locations) { const username = l.tags["payment:coinos"]; if (username) { + console.log(username); const uid = await g(`user:${username}`); const user = await g(`user:${uid}`); if (user) l.osm_json.tags.user = pick(user, fields); diff --git a/lib/notifications.ts b/lib/notifications.ts index 023fdeff..08d96507 100644 --- a/lib/notifications.ts +++ b/lib/notifications.ts @@ -1,10 +1,10 @@ -import { emit } from "$lib/sockets"; -import { err, warn } from "$lib/logging"; -import { db } from "$lib/db"; -import mqtt from "$lib/mqtt"; import config from "$config"; +import { db } from "$lib/db"; +import { err, warn } from "$lib/logging"; import { mail, templates } from "$lib/mail"; -import { f, fmt, link, fiat, t } from "$lib/utils"; +import mqtt from "$lib/mqtt"; +import { emit } from "$lib/sockets"; +import { f, fiat, fmt, link, t } from "$lib/utils"; import webpush from "web-push"; if (config.vapid) { @@ -17,8 +17,8 @@ if (config.vapid) { export const notify = async (p, user, withdrawal) => { emit(user.id, "payment", p); - let { username } = user; - let { paymentReceived } = t(user); + const { username } = user; + const { paymentReceived } = t(user); try { if (user.verified && user.notify) { @@ -32,16 +32,14 @@ export const notify = async (p, user, withdrawal) => { fiat: f(fiat(p.amount, p.rate), p.currency), fiatTip: p.tip ? f(fiat(p.tip, p.rate), p.currency) : undefined, memo: p.memo, - items: - p.items && - p.items.map((i) => { - return { - quantity: i.quantity, - name: i.name, - total: i.quantity * i.price, - totalFiat: f(i.quantity * i.price, p.currency), - }; - }), + items: p.items?.map((i) => { + return { + quantity: i.quantity, + name: i.name, + total: i.quantity * i.price, + totalFiat: f(i.quantity * i.price, p.currency), + }; + }), }, withdrawal, }); @@ -50,15 +48,15 @@ export const notify = async (p, user, withdrawal) => { err("problem emailing", e.message); } - let subscriptions = await db.sMembers(`${user.id}:subscriptions`); + const subscriptions = await db.sMembers(`${user.id}:subscriptions`); - let payload = { + const payload = { title: paymentReceived, body: `${fmt(p.amount)} ${f(fiat(p.amount, p.rate), p.currency)}`, url: `/payment/${p.id}`, }; - for (let s of subscriptions) { + for (const s of subscriptions) { webpush .sendNotification(JSON.parse(s), JSON.stringify(payload)) .catch((e) => { diff --git a/lib/payments.ts b/lib/payments.ts index 88b9230d..98c4bc50 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -65,6 +65,9 @@ export const debit = async ({ let iid; if (invoice) { + console.log("AMT", amount); + console.log("RCVD", invoice.received); + console.log(invoice.received >= amount); if (invoice.received >= amount) fail("Invoice already paid"); ({ id: iid } = invoice); @@ -436,7 +439,8 @@ export const sendLightning = async ({ if (typeof amount !== "undefined") { amount = parseInt(amount); - if (amount < 0 || amount > SATS || isNaN(amount)) fail("Invalid amount"); + if (amount < 0 || amount > SATS || Number.isNaN(amount)) + fail("Invalid amount"); } let total = amount; diff --git a/routes/payments.ts b/routes/payments.ts index 4573ac4f..618e9195 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -55,7 +55,15 @@ export default { let p; if (payreq) { - p = await sendLightning({ user, pr: payreq, amount, maxfee, memo }); + const iid = await g(`invoice:${payreq}`); + if (iid) { + const invoice = await g(`invoice:${iid}`); + if (invoice.aid === user.id) fail("Cannot send to self"); + hash = payreq; + if (!amount) ({ amount } = invoice); + } else { + p = await sendLightning({ user, pr: payreq, amount, maxfee, memo }); + } } if (!p) { |
