diff options
Diffstat (limited to 'routes/users.js')
| -rw-r--r-- | routes/users.js | 331 |
1 files changed, 21 insertions, 310 deletions
diff --git a/routes/users.js b/routes/users.js index 4e6b8a63..4a01cc1e 100644 --- a/routes/users.js +++ b/routes/users.js @@ -7,19 +7,11 @@ import { getUser, nada, uniq, wait } from "$lib/utils"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; import { authenticator } from "otplib"; -import Sequelize from "@sequelize/core"; -import { fromBase58, fromPrivateKey } from "bip32"; -import { Mutex } from "async-mutex"; -import bip32 from "bip32"; import whitelist from "$lib/whitelist"; import { l, err, warn } from "$lib/logging"; -import db from "$db"; -import bc from "$lib/bitcoin"; -import lq from "$lib/liquid"; import { emit } from "$lib/sockets"; import register from "$lib/register"; import { requirePin } from "$lib/utils"; -import { coinos, q } from "$lib/nostr"; import got from "got"; import { bech32 } from "bech32"; @@ -30,28 +22,7 @@ const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); app.get("/me", auth, async (req, res) => { try { let user = req.user.get({ plain: true }); - let payments = await req.user.getPayments({ - where: { - account_id: user.account_id - }, - order: [["id", "DESC"]], - limit: 12, - include: [ - { - model: db.Account, - as: "account" - }, - { - model: db.Payment, - as: "fee_payment" - } - ] - }); - - user.accounts = await req.user.getAccounts(); - user.payments = payments; user.haspin = !!user.pin; - res.send(pick(user, ...whitelist)); } catch (e) { console.log("problem fetching user", e); @@ -59,53 +30,37 @@ app.get("/me", auth, async (req, res) => { } }); -app.get("/users/:username", async (req, res) => { +app.get("/users/:key", async (req, res) => { try { - let { username } = req.params; - - if (username.startsWith("npub")) { - username = Buffer.from(fromWords(decode(username).words)).toString("hex"); + if (key.startsWith("npub")) { + key = Buffer.from(fromWords(decode(key).words)).toString("hex"); } - let user = await db.User.findOne({ - attributes: [ - "username", - "banner", - "profile", - "address", - "currency", - "pubkey", - "display", - "prompt", - "uuid" - ], - where: { - [Sequelize.Op.or]: [ - Sequelize.where( - Sequelize.fn("lower", Sequelize.col("username")), - username.toLowerCase() - ), - { pubkey: username } - ] - } - }); - - if (user) user = user.get({ plain: true }); - else user = JSON.parse(await redis.get(`user:${username}`)); + user = JSON.parse(await redis.get(`user:${key}`)); - if (!user && username.length === 64) { - let newuser = { - username: username.substr(0, 6), - pubkey: username, + if (!user && key.length === 64) { + user = { + username: key.substr(0, 6), + pubkey: key, anon: true }; - - user = JSON.parse(await redis.get(`user:${newuser.pubkey}`)) || newuser; } if (!user) return res.code(500).send("User not found"); - res.send(user); + let whitelist = [ + "username", + "banner", + "profile", + "address", + "currency", + "pubkey", + "display", + "prompt", + "uuid" + ]; + + res.send(pick(user, ...whitelist)); } catch (e) { console.log(e); res.code(500).send(e.message); @@ -402,8 +357,6 @@ let login = async (req, res) => { }; app.post("/login", login); -app.post("/taboggan", login); -app.post("/doggin", login); app.post("/logout", optionalAuth, async (req, res) => { let { subscription } = req.body; @@ -428,100 +381,6 @@ app.post("/logout", optionalAuth, async (req, res) => { res.send({}); }); -app.post("/account", auth, async (req, res) => { - const { user } = req; - const { - id, - name, - ticker, - precision, - domain, - seed, - pubkey, - privkey, - path, - hide, - index - } = req.body; - - try { - await db.transaction(async transaction => { - let account = await db.Account.findOne({ - where: { id, user_id: user.id }, - lock: transaction.LOCK.UPDATE, - transaction - }); - - let params = { - name, - ticker, - precision, - domain, - seed, - pubkey, - path, - hide, - index, - privkey - }; - - if (!account || (account.pubkey && !pubkey)) delete params.pubkey; - - await db.Account.update(params, { - where: { id, user_id: user.id }, - transaction - }); - - await account.reload({ transaction }); - emit(user.username, "account", account); - res.send({}); - }); - } catch (e) { - err("problem updating account", e.message); - return res.code(500).send("There was a problem updating the account"); - } -}); - -app.post("/shiftAccount", auth, async (req, res) => { - let { user } = req; - let { id } = req.body; - - try { - const account = await db.Account.findOne({ - where: { id } - }); - - if (account.user_id !== user.id) - return res.code(500).send("Failed to open wallet"); - - user.account_id = account.id; - await user.save(); - let payments = await db.Payment.findAll({ - where: { - account_id: id - }, - order: [["id", "DESC"]], - limit: 12, - include: { - model: db.Account, - as: "account" - } - }); - - user = user.get({ plain: true }); - user.payments = payments; - user.account = account.get({ plain: true }); - - emit(user.username, "account", user.account); - emit(user.username, "user", user); - - res.send(user); - } catch (e) { - err("problem switching account", e.message); - return res.code(500).send("There was a problem switching accounts"); - } -}); - app.get("/vapidPublicKey", function(req, res) { res.send(config.vapid.publicKey); }); @@ -548,36 +407,6 @@ app.post("/password", auth, async function(req, res) { res.send(await bcrypt.compare(password, user.password)); }); -app.get("/isInternal", auth, async function(req, res) { - let { user } = req; - let { address } = req.query; - if (!address) throw new Error("Address not provided"); - - let invoice = await db.Invoice.findOne({ - where: { address }, - include: { - attributes: ["username"], - model: db.User, - as: "user" - } - }); - - if (invoice) { - let info; - try { - info = await bc.getAddressInfo(address); - } catch (e) { - info = await lq.getAddressInfo(address); - } - if (info.ismine) { - emit(user.username, "to", invoice.user); - return res.send(true); - } - } - - res.send(false); -}); - app.get("/invoices", auth, async function(req, res) { let invoices = await db.Invoice.findAll({ where: { user_id: req.user.id } @@ -585,22 +414,6 @@ app.get("/invoices", auth, async function(req, res) { res.send(invoices); }); -app.post("/signMessage", auth, async function(req, res) { - let { address, message } = req.body; - - let invoices = await db.Invoice.findAll({ - where: { user_id: req.user.id } - }); - - if (invoices.find(i => i.address === address)) { - if (config.bitcoin.walletpass) - await bc.walletPassphrase(config.bitcoin.walletpass, 300); - return res.send(await bc.signMessage(address, message)); - } - - res.code(500).send("Address not found for user"); -}); - app.post("/otpsecret", auth, async function(req, res) { try { await requirePin(req); @@ -652,105 +465,3 @@ app.post("/checkPassword", auth, async function(req, res) { res.code(500).send("Invalid password"); } }); - -app.get("/:pubkey/follows", async (req, res) => { - try { - let { pubkey } = req.params; - let { tagsonly } = req.query; - - let sub = `${pubkey}:follows`; - q( - sub, - { - limit: 1, - kinds: [3], - authors: [pubkey] - }, - { timeout: 60000, eager: 60000 } - ).catch(nada); - - let tags = JSON.parse(await redis.get(`${pubkey}:follows`)) || []; - if (tagsonly) return res.send(tags); - - let follows = []; - for (let f of tags) { - let [_, pubkey] = f; - - q(`${pubkey}:profile:f1`, { - limit: 1, - kinds: [0], - authors: [pubkey] - }).catch(nada); - - let user = JSON.parse(await redis.get(`user:${pubkey}`)); - - if (!user) - user = { - username: pubkey.substr(0, 6), - pubkey, - anon: true - }; - - follows.push(user); - } - - follows = uniq(follows, e => e.pubkey); - follows.sort((a, b) => a.username.localeCompare(b.username)); - - res.send(follows); - } catch (e) { - console.log(e); - res.code(500).send(e && e.message); - } -}); - -app.get("/:pubkey/followers", async (req, res) => { - try { - let { pubkey } = req.params; - - let pubkeys = [ - ...new Set([ - ...(await got(`${config.nostr}/followers?pubkey=${pubkey}`).json()), - ...(await redis.sMembers(`${pubkey}:followers`)) - ]) - ]; - - let followers = []; - - q( - `${pubkey}:followers`, - { kinds: [3], "#p": [pubkey] }, - { timeout: 60000, eager: 60000 } - ).catch(nada); - - for (let pubkey of pubkeys) { - let user = JSON.parse(await redis.get(`user:${pubkey}`)); - if (!user || !user.updated) { - q(`${pubkey}:profile:f2`, { - limit: 1, - kinds: [0], - authors: [pubkey] - }).catch(nada); - - user = JSON.parse(await redis.get(`user:${pubkey}`)); - } - - if (!user) - user = { - username: pubkey.substr(0, 6), - pubkey, - anon: true - }; - - followers.push(user); - } - - followers = uniq(followers, e => e.pubkey); - followers.sort((a, b) => a.username.localeCompare(b.username)); - - res.send(followers); - } catch (e) { - console.log(e); - res.code(500).send(e && e.message); - } -}); |
