From a40fe3364af0242fd6584b06528a00866918f437 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 6 Nov 2022 05:20:15 -0800 Subject: emit requests --- routes/requests.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/routes/requests.js b/routes/requests.js index 823e92c1..56acb823 100644 --- a/routes/requests.js +++ b/routes/requests.js @@ -1,15 +1,21 @@ import app from "$app"; import db from "$db"; import { auth } from "$lib/passport"; +import { emit } from "$lib/sockets"; app.post("/requests", auth, async (req, res) => { try { let { recipient: username, ...params } = req.body; - let { id: recipient_id } = await db.User.findOne({ where: { username }}); + let { id: recipient_id } = await db.User.findOne({ where: { username } }); let request = await db.Request.create({ recipient_id, ...params }); + request = request.get({ plain: true }); + request.requester = { username: req.user.username }; + emit(username, "request", request); + res.send(request); } catch (e) { + console.log(e) res.code(500).send(e.message); } }); -- cgit v1.2.3 From bee5e350576baf674594a101795b23318b654c9a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 6 Nov 2022 06:19:17 -0800 Subject: send invoice to recipient --- routes/invoices.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/routes/invoices.js b/routes/invoices.js index 6e0fe358..c492b475 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -60,7 +60,7 @@ app.get("/invoice/:text", async (req, res, next) => { app.post("/invoice", optionalAuth, async (req, res, next) => { try { let { type = "bech32", liquidAddress, id, invoice, user, tx } = req.body; - let { blindkey, currency, tip, amount, rate, network } = invoice; + let { blindkey, currency, tip, amount, rate, network, recipient } = invoice; let address, unconfidential, text; if (amount < 0) throw new Error("amount out of range"); @@ -151,6 +151,10 @@ app.post("/invoice", optionalAuth, async (req, res, next) => { if (blindkey) await lq.importBlindingKey(invoice.address, blindkey); } + if (recipient) { + emit(recipient, "invoice", invoice.get({ plain: true {)); + } + res.send(invoice); } catch (e) { err(e.message, e.stack); -- cgit v1.2.3 From 0290034a9ecd7fe3b5b93d8395e8a86cd0e3635e Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 6 Nov 2022 06:20:02 -0800 Subject: return requester profile --- routes/requests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/requests.js b/routes/requests.js index 56acb823..a4aad29f 100644 --- a/routes/requests.js +++ b/routes/requests.js @@ -10,7 +10,7 @@ app.post("/requests", auth, async (req, res) => { let request = await db.Request.create({ recipient_id, ...params }); request = request.get({ plain: true }); - request.requester = { username: req.user.username }; + request.requester = { username: req.user.username, profile: req.user.profile }; emit(username, "request", request); res.send(request); -- cgit v1.2.3 From 07a709c80da8a303b5cea23b5c193dd36302d6b4 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 6 Nov 2022 11:23:32 -0800 Subject: invoice requests --- routes/invoices.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/routes/invoices.js b/routes/invoices.js index c492b475..074aed61 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -8,18 +8,19 @@ import { err, l } from "$lib/logging"; import bc from "$lib/bitcoin"; import lq from "$lib/liquid"; import { SATS, bip21, deriveAddress, derivePayRequest } from "$lib/utils"; +import { emit } from "$lib/sockets"; app.get("/invoice", async (req, res, next) => { try { const invoice = await db.Invoice.findOne({ where: { - uuid: req.query.uuid, + uuid: req.query.uuid }, include: { model: db.User, as: "user", - attributes: ["username", "currency"], - }, + attributes: ["username", "currency"] + } }); res.send(invoice); @@ -38,7 +39,7 @@ app.get("/invoice/:text", async (req, res, next) => { : text.startsWith("ln") ? { text } : { - [Op.or]: [{ unconfidential: text }, { address: text }], + [Op.or]: [{ unconfidential: text }, { address: text }] }; const invoice = await db.Invoice.findOne({ @@ -46,8 +47,8 @@ app.get("/invoice/:text", async (req, res, next) => { include: { model: db.User, as: "user", - attributes: ["username", "currency"], - }, + attributes: ["username", "currency"] + } }); res.send(invoice); @@ -60,23 +61,22 @@ app.get("/invoice/:text", async (req, res, next) => { app.post("/invoice", optionalAuth, async (req, res, next) => { try { let { type = "bech32", liquidAddress, id, invoice, user, tx } = req.body; - let { blindkey, currency, tip, amount, rate, network, recipient } = invoice; + let { blindkey, currency, tip, amount, rate, network, requester } = invoice; let address, unconfidential, text; if (amount < 0) throw new Error("amount out of range"); - if (tip > 5 * amount || tip < 0) - throw new Error("tip amount out of range"); + if (tip > 5 * amount || tip < 0) throw new Error("tip amount out of range"); if (!user) ({ user } = req); else { user = await db.User.findOne({ where: { - username: user.username, + username: user.username }, include: { model: db.Account, - as: "account", - }, + as: "account" + } }); } if (!user) throw new Error("user not provided"); @@ -110,7 +110,7 @@ app.post("/invoice", optionalAuth, async (req, res, next) => { rate, tip, unconfidential, - user_id: user.id, + user_id: user.id }; if (network === "lightning") { @@ -151,9 +151,12 @@ app.post("/invoice", optionalAuth, async (req, res, next) => { if (blindkey) await lq.importBlindingKey(invoice.address, blindkey); } - if (recipient) { - emit(recipient, "invoice", invoice.get({ plain: true {)); - } + if (requester) { + emit(requester, "invoice", { + uuid: invoice.uuid, + user: { username: user.username, profile: user.profile } + }); + } res.send(invoice); } catch (e) { -- cgit v1.2.3