aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-06-13 21:39:22 -0700
committerAdam Soltys <[email protected]>2020-06-13 21:39:22 -0700
commit74b7d2a0d614f170ba5aea3a1a172a7a68356d80 (patch)
treebe2a73a9917616c0b49c24dc99642378e5a47549 /lib
parentdcb21adb40a44c8108f1d652cc6ddd59ae7c4024 (diff)
downloadcoinos-server-74b7d2a0d614f170ba5aea3a1a172a7a68356d80.tar.gz
coinos-server-74b7d2a0d614f170ba5aea3a1a172a7a68356d80.zip
push notifications
Diffstat (limited to 'lib')
-rw-r--r--lib/notifications.js23
-rw-r--r--lib/sockets.js93
2 files changed, 79 insertions, 37 deletions
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);
}
});