aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/invoices.js9
-rw-r--r--lib/utils.js18
2 files changed, 16 insertions, 11 deletions
diff --git a/lib/invoices.js b/lib/invoices.js
index 95a81104..74870f82 100644
--- a/lib/invoices.js
+++ b/lib/invoices.js
@@ -2,7 +2,7 @@ import store from "$lib/store";
import { l } from "$lib/logging";
import { emit } from "$lib/sockets";
import { db, g, s } from "$lib/db";
-import { bip21, fail } from "$lib/utils";
+import { getUser, bip21, fail } from "$lib/utils";
import { types } from "$lib/payments";
import { v4 } from "uuid";
@@ -18,8 +18,7 @@ export let generate = async ({ invoice, user, sender, memo }) => {
if (amount < 0) fail("amount out of range");
if (!user) user = sender;
- let uid = await g(`user:${user.username.toLowerCase()}`);
- user = await g(`user:${uid}`);
+ user = await getUser(user.username);
if (!user) fail("user not provided");
@@ -64,13 +63,13 @@ export let generate = async ({ invoice, user, sender, memo }) => {
text,
tip,
type,
- uid,
+ uid: user.id
};
l("creating invoice", user.username, amount, tip, currency, invoice.hash);
await s(`invoice:${hash}`, invoice);
- await db.lPush(`${uid}:invoices`, hash);
+ await db.lPush(`${user.id}:invoices`, hash);
if (request_id) {
let request = await g(`request:${request_id}`);
diff --git a/lib/utils.js b/lib/utils.js
index ae794a5e..05819ecb 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -4,7 +4,7 @@ import config from "$config";
export const nada = () => {};
-export const sleep = (n) => new Promise((r) => setTimeout(r, n));
+export const sleep = n => new Promise(r => setTimeout(r, n));
export const wait = async (f, s = 300, n = 50) => {
let i = 0;
while (!(await f()) && i < s) (await sleep(n)) && i++;
@@ -13,17 +13,17 @@ export const wait = async (f, s = 300, n = 50) => {
};
export const prod = process.env.NODE_ENV === "production";
-export const fail = (msg) => {
+export const fail = msg => {
throw new Error(msg);
};
export const bail = (res, msg) => res.code(500).send(msg);
export const SATS = 100000000;
-export const sats = (n) => Math.round(n * SATS);
-export const btc = (n) => parseFloat((n / SATS).toFixed(8));
+export const sats = n => Math.round(n * SATS);
+export const btc = n => parseFloat((n / SATS).toFixed(8));
-export const uniq = (a, k) => [...new Map(a.map((x) => [k(x), x])).values()];
+export const uniq = (a, k) => [...new Map(a.map(x => [k(x), x])).values()];
export const pick = (O, K) =>
K.reduce((o, k) => (typeof O[k] !== "undefined" && (o[k] = O[k]), o), {});
@@ -53,5 +53,11 @@ export const fields = [
"haspin",
"profile",
"prompt",
- "banner",
+ "banner"
];
+
+export const getUser = async username => {
+ username = username.replace(/\s/g, "").toLowerCase()
+ let uid = await g(`user:${username}`);
+ return g(`user:${uid}`);
+};