From 74b7d2a0d614f170ba5aea3a1a172a7a68356d80 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 13 Jun 2020 21:39:22 -0700 Subject: push notifications --- lib/notifications.js | 23 +++++++++++++ lib/sockets.js | 93 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 lib/notifications.js (limited to 'lib') diff --git a/lib/notifications.js b/lib/notifications.js new file mode 100644 index 00000000..710a5bfe --- /dev/null +++ b/lib/notifications.js @@ -0,0 +1,23 @@ +const webpush = require("web-push"); + +if (config.vapid) { + webpush.setVapidDetails( + config.vapid.url, + config.vapid.publicKey, + config.vapid.privateKey + ); +} + +notify = (user, payload) => { + let { subscriptions } = user; + + if (subscriptions) { + subscriptions.map(async subscription => { + try { + await webpush.sendNotification(subscription, payload); + } catch (e) { + l.error("problem sending notification", e); + } + }); + } +}; diff --git a/lib/sockets.js b/lib/sockets.js index 89beffb3..07af88d8 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -37,47 +37,66 @@ const names = {}; wss.on("connection", function(ws, req) { const id = uuidv4(); - setTimeout(() => ws.ok || ws.close(), 2000); + setTimeout(() => ws.user || ws.close(), 2000); - ws.on("message", async token => { - if (!token || token === "null") return; + ws.on("message", async (message) => { + let type, data; try { - const username = jwt.decode(token).username; - if (!sockets[username]) sockets[username] = {}; - sockets[username][id] = ws; - names[id] = username; + ({ type, data } = JSON.parse(message)); + } catch (e) { + l.error("coudn't parse socket message"); + } - let user = await getUser(username); - let payments = await user.getPayments({ - where: { - account_id: user.account_id, - }, - order: [["id", "DESC"]], - limit: 12, - include: { - model: db.Account, - as: "account" - } - }); - let accounts = await user.getAccounts(); - user = user.get({ plain: true }); - user.accounts = accounts; - user.payments = payments; + let token; + switch (type) { + case "login": + token = data; + if (!token || token === "null") return; + try { + const username = jwt.decode(token).username; + if (!sockets[username]) sockets[username] = {}; + sockets[username][id] = ws; + names[id] = username; - ws.ok = true; - ws.send(JSON.stringify({ type: "nodes", data: networks })); - ws.send(JSON.stringify({ type: "rates", data: app.get("rates") })); - ws.send(JSON.stringify({ type: "login", data: user })); - l.info("socket open", username, id); - } catch (e) { - ws.close(); - l.error( - "problem opening socket", - token, - e.message, - id, - req.connection.remoteAddress - ); + let user = await getUser(username); + let payments = await user.getPayments({ + where: { + account_id: user.account_id + }, + order: [["id", "DESC"]], + limit: 12, + include: { + model: db.Account, + as: "account" + } + }); + let accounts = await user.getAccounts(); + user = user.get({ plain: true }); + user.accounts = accounts; + user.payments = payments; + + ws.user = user; + ws.send(JSON.stringify({ type: "login", data: user })); + ws.send(JSON.stringify({ type: "version", data: clientVersion })); + l.info("socket open", username, id); + } catch (e) { + ws.close(); + l.error( + "problem opening socket", + token, + e.message, + id, + req.connection.remoteAddress + ); + } + break; + case "token": + token = data; + if (!ws.user) return; + ws.token = token; + break; + default: + l.warn("received socket message of unknown type", type, data); } }); -- cgit v1.2.3