From 1962a6dcf1bb37bb8beb3fc4c0231d5a504a75fe Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 17 Jul 2020 20:55:47 -0700 Subject: memos --- lib/sockets.js | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'lib/sockets.js') diff --git a/lib/sockets.js b/lib/sockets.js index d0f00771..2dae9e93 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -26,19 +26,19 @@ broadcast = (type, data) => { } }; -server.on("upgrade", function(req, socket, head) { - wss.handleUpgrade(req, socket, head, function(ws) { +server.on("upgrade", function (req, socket, head) { + wss.handleUpgrade(req, socket, head, function (ws) { wss.emit("connection", ws, req); }); }); const names = {}; -wss.on("connection", function(ws, req) { +wss.on("connection", function (ws, req) { const id = uuidv4(); ws.send(JSON.stringify({ type: "connected" })); - ws.on("message", async message => { + ws.on("message", async (message) => { let type, data; try { ({ type, data } = JSON.parse(message)); @@ -57,22 +57,24 @@ wss.on("connection", function(ws, req) { const existing = Object.keys(sockets[username]); if (existing.length > 5) { l.warn("too many sockets, logging out", existing[0]); - sockets[username][existing[0]].send(JSON.stringify({ type: "login", data: false })); - } + sockets[username][existing[0]].send( + JSON.stringify({ type: "login", data: false }) + ); + } sockets[username][id] = ws; names[id] = username; let user = await getUser(username); let payments = await user.getPayments({ where: { - account_id: user.account_id + account_id: user.account_id, }, order: [["id", "DESC"]], limit: 12, include: { model: db.Account, - as: "account" - } + as: "account", + }, }); let accounts = await user.getAccounts(); let keys = await user.getKeys(); @@ -106,12 +108,29 @@ wss.on("connection", function(ws, req) { case "lnurl": sessions[data] = ws; break; + case "updateMemo": + const payment = await db.Payment.findOne({ + where: { + user_id: ws.user.id, + id: data.id, + }, + include: { + model: db.Account, + as: "account", + }, + }); + if (payment) { + payment.memo = data.memo; + await payment.save(); + emit(ws.user.username, "payment", payment); + } + break; default: l.warn("received socket message of unknown type", type, data); } }); - ws.on("close", function() { + ws.on("close", function () { try { const username = names[id]; if (sockets[username] && sockets[username][id]) -- cgit v1.2.3