aboutsummaryrefslogtreecommitdiffstats
path: root/routes
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 /routes
parentdcb21adb40a44c8108f1d652cc6ddd59ae7c4024 (diff)
downloadcoinos-server-74b7d2a0d614f170ba5aea3a1a172a7a68356d80.tar.gz
coinos-server-74b7d2a0d614f170ba5aea3a1a172a7a68356d80.zip
push notifications
Diffstat (limited to 'routes')
-rw-r--r--routes/bitcoin/receive.js10
-rw-r--r--routes/info.js (renamed from routes/balances.js)11
-rw-r--r--routes/lightning/receive.js6
-rw-r--r--routes/liquid/receive.js8
-rw-r--r--routes/send.js3
-rw-r--r--routes/users.js41
6 files changed, 69 insertions, 10 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index 6928dd5c..0593584e 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -106,7 +106,8 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
emit(user.username, "account", account);
emit(user.username, "payment", payment);
- l.info("bitcoin detected", user.username, o.value);
+ l.info("bitcoin detected", user.username, value);
+ notify(user, `${value} SAT payment detected`);
}
})
);
@@ -149,9 +150,11 @@ setInterval(async () => {
const { user } = p;
if (p) {
+ let total = p.amount + p.tip;
+
p.confirmed = 1;
- p.account.balance += p.amount + p.tip;
- p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
+ p.account.balance += total;
+ p.account.pending -= Math.min(p.account.pending, total);
await p.account.save();
await p.save();
@@ -159,6 +162,7 @@ setInterval(async () => {
emit(user.username, "account", p.account);
emit(user.username, "payment", p);
l.info("bitcoin confirmed", user.username, p.amount, p.tip);
+ notify(user, `${total} SAT payment confirmed`);
} else {
l.warn("couldn't find payment", hash);
}
diff --git a/routes/balances.js b/routes/info.js
index ba834306..81cb2095 100644
--- a/routes/balances.js
+++ b/routes/info.js
@@ -1,6 +1,14 @@
const sequelize = require("sequelize");
const { Op } = sequelize;
+const fs = require("fs");
+try {
+ clientVersion = fs.readFileSync('config/clientversion').toString();
+ l.info("client version", clientVersion);
+} catch(e) {
+ l.warn("no client version found");
+}
+
app.get("/info", async (req, res) => {
const accounts = await db.Account.findAll({
attributes: [
@@ -49,6 +57,9 @@ app.get("/info", async (req, res) => {
liquid,
lnchannel,
lnwallet,
+ rates: app.get("rates"),
+ nodes: networks,
+ clientVersion,
};
info.total =
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index 6a9c95c8..292f19e2 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -48,8 +48,9 @@ const handlePayment = async msg => {
},
{ transaction });
- invoice.received += amount + tip;
- account.balance += amount + tip;
+ let total = amount + tip;
+ invoice.received += total;
+ account.balance += total;
await account.save({ transaction });
await invoice.save({ transaction });
@@ -60,6 +61,7 @@ const handlePayment = async msg => {
payment.account = account.get({ plain: true });
emit(user.username, "payment", payment);
emit(user.username, "account", payment.account);
+ notify(user, `Received ${total} SAT`);
l.info(
"lightning payment received",
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index b3f82a2d..9a97aa47 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -121,6 +121,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
emit(user.username, "payment", payment);
l.info("liquid detected", user.username, asset, value);
+ notify(user, `${value} SAT payment detected`);
}
})
);
@@ -231,9 +232,10 @@ setInterval(async () => {
const { user } = p;
if (p) {
+ let total = p.amount + p.tip;
p.confirmed = 1;
- p.account.balance += p.amount + p.tip;
- p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
+ p.account.balance += total;
+ p.account.pending -= Math.min(p.account.pending, total);
await p.account.save();
await p.save();
@@ -247,6 +249,8 @@ setInterval(async () => {
p.amount,
p.tip
);
+
+ notify(user, `${total} SAT payment confirmed`);
} else {
l.warn("couldn't find payment", hash);
}
diff --git a/routes/send.js b/routes/send.js
index 837a9e45..8b4137d8 100644
--- a/routes/send.js
+++ b/routes/send.js
@@ -122,7 +122,8 @@ module.exports = async (req, res) => {
emit(user.username, "account", account);
emit(user.username, "payment", payment);
- l.info("received internal", user.username, payment.amount);
+ l.info("received internal", user.username, amount);
+ notify(user, `Received ${amount} SAT`);
});
} catch (e) {
l.error(
diff --git a/routes/users.js b/routes/users.js
index fc2f2975..9de84b83 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -101,7 +101,10 @@ app.post("/register", async (req, res) => {
let ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
- if (!challenge[ip] || user.response.toLowerCase() !== challenge[ip].toLowerCase()) {
+ if (
+ !challenge[ip] ||
+ user.response.toLowerCase() !== challenge[ip].toLowerCase()
+ ) {
l.info("failed challenge", ip, user.response, challenge[ip]);
return res.status(500).send("Failed challenge");
}
@@ -203,7 +206,8 @@ app.post("/user", auth, async (req, res) => {
twofa,
pin,
password,
- passconfirm
+ passconfirm,
+ tokens
} = req.body;
let exists = await db.User.findOne({
@@ -231,6 +235,7 @@ app.post("/user", auth, async (req, res) => {
user.currency = currency;
user.currencies = currencies;
+ user.tokens = tokens;
user.twofa = twofa;
user.pin = pin;
@@ -290,6 +295,19 @@ app.post("/login", async (req, res) => {
}
});
+
+app.post("/logout", auth, async (req, res) => {
+ let { subscription } = req.body;
+ l.info("logging out", req.user.username);
+ if (!subscription) return res.end();
+ let i = req.user.subscriptions.findIndex(s => JSON.stringify(s) === subscription);
+ if (i > -1) {
+ req.user.subscriptions.splice(i, 1);
+ }
+ await req.user.save();
+ res.end();
+});
+
app.post("/address", auth, async (req, res) => {
if (!config.bitcoin) return res.status(500).send("Bitcoin not configured");
@@ -363,3 +381,22 @@ app.post("/shiftAccount", auth, async (req, res) => {
});
setInterval(() => (faucet = 2000), DAY);
+
+app.get("/vapidPublicKey", function(req, res) {
+ res.send(config.vapid.publicKey);
+});
+
+app.post("/subscribe", auth, async function(req, res) {
+ let { subscriptions } = req.user;
+ let { subscription } = req.body;
+ if (!subscriptions) subscriptions = [];
+ if (
+ !subscriptions.find(s => JSON.stringify(s) === JSON.stringify(subscription))
+ )
+ subscriptions.push(subscription);
+ req.user.subscriptions = subscriptions;
+ l.info("subscribing", req.user.username);
+ await req.user.save();
+ res.sendStatus(201);
+});
+