diff options
| author | Adam Soltys <[email protected]> | 2023-03-27 21:09:22 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-03-27 21:09:22 -0700 |
| commit | 0559a382066722b794e1aaaafbb24266c7302f9e (patch) | |
| tree | ac390cb5f44f1d878f72760c89d8ee01f583e939 | |
| parent | 5756d04533a69d93091dcbb469d83da6b55456bc (diff) | |
| download | coinos-server-0559a382066722b794e1aaaafbb24266c7302f9e.tar.gz coinos-server-0559a382066722b794e1aaaafbb24266c7302f9e.zip | |
strip spaces from usernames
| -rw-r--r-- | lib/invoices.js | 9 | ||||
| -rw-r--r-- | lib/utils.js | 18 | ||||
| -rw-r--r-- | routes/lnurl.js | 6 | ||||
| -rw-r--r-- | routes/nostr.js | 42 | ||||
| -rw-r--r-- | routes/users.js | 43 |
5 files changed, 67 insertions, 51 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}`); +}; diff --git a/routes/lnurl.js b/routes/lnurl.js index cad209c5..97e4e8f8 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -1,6 +1,6 @@ import { g, s } from "$lib/db"; import { l, warn } from "$lib/logging"; -import { bail, fail } from "$lib/utils"; +import { bail, getUser, fail } from "$lib/utils"; import { v4 } from "uuid"; import got from "got"; import { generate } from "$lib/invoices"; @@ -42,7 +42,7 @@ export default { }, async lnurlp({ params: { username } }, res) { - let uid = await g(`user:${username.toLowerCase()}`); + let uid = await getUser(username); if (!uid) { let u = await got(`${classic}/admin/migrate/${username}?zero=true`, { headers: { authorization: `Bearer ${admin}` }, @@ -56,7 +56,7 @@ export default { u = { id: uid, about: u.address, ...pick(u, fields) }; delete u.address; - await s(`user:${username.toLowerCase()}`, uid); + await s(`user:${username.replace(/\s/g, '').toLowerCase()}`, uid); await s(`user:${uid}`, u); await s(`balance:${uid}`, balance); diff --git a/routes/nostr.js b/routes/nostr.js index e274f61e..33233a7a 100644 --- a/routes/nostr.js +++ b/routes/nostr.js @@ -15,7 +15,7 @@ export default { pubkey, anon: true, follows: [], - followers: [], + followers: [] }; res.send(event); @@ -31,12 +31,12 @@ export default { pubkey, anon: true, follows: [], - followers: [], + followers: [] }; let params = { kinds: [1], - authors: [pubkey], + authors: [pubkey] }, opts = { since: 0 }; @@ -45,16 +45,16 @@ export default { let ids = await db.sMembers(pubkey); let events = ids.length - ? (await db.mGet(ids.map((k) => "ev:" + k))).map(JSON.parse) + ? (await db.mGet(ids.map(k => "ev:" + k))).map(JSON.parse) : []; - res.send(events.map((e) => ({ ...e, user }))); + res.send(events.map(e => ({ ...e, user }))); }, async messages({ params: { pubkey, since = 0 } }, res) { let params = { kinds: [4], - authors: [pubkey], + authors: [pubkey] }; let opts = { since }; @@ -63,14 +63,15 @@ export default { params = { kinds: [4], - "#p": [pubkey], + "#p": [pubkey] }; await q(`${pubkey}:messages`, params, opts).catch(nada); let messages = await db.sMembers(`${pubkey}:messages`); + messages = await Promise.all( - messages.map(async (id) => { + messages.map(async id => { let m = await g(`ev:${id}`); let aid = await g(`user:${m.pubkey}`); @@ -97,7 +98,7 @@ export default { params = { limit: 1, kinds: [3], - authors: [pubkey], + authors: [pubkey] }, opts = { timeout: 60000, eager: 60000 }; @@ -113,7 +114,7 @@ export default { q(`${pubkey}:profile:f1`, { limit: 1, kinds: [0], - authors: [pubkey], + authors: [pubkey] }).catch(nada); let uid = await g(`user:${pubkey}`); @@ -123,13 +124,13 @@ export default { user = { username: pubkey.substr(0, 6), pubkey, - anon: true, + anon: true }; follows.push(user); } - follows = uniq(follows, (e) => e.pubkey); + follows = uniq(follows, e => e.pubkey); follows.sort((a, b) => a.username.localeCompare(b.username)); res.send(follows); @@ -138,7 +139,7 @@ export default { async followers({ params: { pubkey } }, res) { try { let pubkeys = [ - ...new Set([...(await db.sMembers(`${pubkey}:followers`))]), + ...new Set([...(await db.sMembers(`${pubkey}:followers`))]) ]; let followers = []; @@ -156,7 +157,7 @@ export default { await q(`${pubkey}:profile:f2`, { limit: 1, kinds: [0], - authors: [pubkey], + authors: [pubkey] }).catch(nada); uid = await g(`user:${pubkey}`); @@ -167,13 +168,13 @@ export default { user = { username: pubkey.substr(0, 6), pubkey, - anon: true, + anon: true }; followers.push(user); } - followers = uniq(followers, (e) => e.pubkey); + followers = uniq(followers, e => e.pubkey); followers.sort((a, b) => a.username.localeCompare(b.username)); res.send(followers); @@ -186,10 +187,11 @@ export default { async identities(req, res) { res.send({ names: { - adam: "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8", - asoltys: + adam: "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8", - }, + asoltys: + "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8" + } }); - }, + } }; diff --git a/routes/users.js b/routes/users.js index 3c083d17..780d4003 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,7 +1,16 @@ import { g, s, db } from "$lib/db"; import config from "$config"; import store from "$lib/store"; -import { fields, nada, pick, uniq, wait, bail, fail } from "$lib/utils"; + import { + fields, + nada, + pick, + uniq, + wait, + bail, + fail, + getUser +} from "$lib/utils"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; import { authenticator } from "otplib"; @@ -83,7 +92,7 @@ export default { username: key, display: key.substr(0, 6), pubkey: key, - anon: true, + anon: true }; } @@ -100,7 +109,7 @@ export default { "pubkey", "display", "prompt", - "id", + "id" ]; if (user.pubkey) @@ -124,7 +133,7 @@ export default { pubkey, password, username, - salt, + salt }; user = await register(user, ip, false); @@ -175,7 +184,7 @@ export default { if (!user.pin || user.pin === "undefined") delete user.pin; let exists; - if (username) exists = await g(`user:${username.toLowerCase()}`); + if (username) exists = await getUser(username); let token; if (user.username.toLowerCase() !== username.toLowerCase() && exists) { @@ -201,7 +210,7 @@ export default { "salt", "seed", "tokens", - "twofa", + "twofa" ]; for (let a of attributes) { @@ -232,7 +241,7 @@ export default { username = username.toLowerCase().replace(/\s/g, ""); let uid = await g(`user:${username}`); - let user = await g(`user:${uid}`); + let user = await getUser(username); if (classic && !(user && user.migrated)) { uid = user ? user.id : v4(); @@ -245,7 +254,7 @@ export default { if (!token) fail(); user = await got(`${classic}/admin/migrate/${username}?zero=true`, { - headers: { authorization: `Bearer ${config.admin}` }, + headers: { authorization: `Bearer ${config.admin}` } }).json(); let { balance } = user; @@ -257,7 +266,7 @@ export default { ...pick(user, fields), id: uid, about: user.address, - migrated: true, + migrated: true }; await s(`user:${username}`, uid); @@ -265,7 +274,7 @@ export default { await s(`balance:${uid}`, balance); let payments = await got(`${classic}/payments`, { - headers: { authorization: `Bearer ${token}` }, + headers: { authorization: `Bearer ${token}` } }).json(); for (let p of payments) { @@ -275,7 +284,7 @@ export default { "confirmed", "rate", "currency", - "preimage", + "preimage" ]); n.id = v4(); n.created = parseISO(p.createdAt).getTime(); @@ -293,7 +302,7 @@ export default { u = await got( `${classic}/admin/migrate/${p.with.username.toLowerCase()}`, { - headers: { authorization: `Bearer ${config.admin}` }, + headers: { authorization: `Bearer ${config.admin}` } } ).json(); @@ -365,7 +374,7 @@ export default { if (!subscriptions) subscriptions = []; if ( !subscriptions.find( - (s) => JSON.stringify(s) === JSON.stringify(subscription) + s => JSON.stringify(s) === JSON.stringify(subscription) ) ) subscriptions.push(subscription); @@ -402,8 +411,8 @@ export default { let contacts = (await g(`${id}:contacts`)) || []; for (let { ref } of ( - await Promise.all(payments.map(async (id) => await g(`payment:${id}`))) - ).filter((p) => p.type === types.internal && p.ref)) { + await Promise.all(payments.map(async id => await g(`payment:${id}`))) + ).filter(p => p.type === types.internal && p.ref)) { !~contacts.findIndex(({ id }) => id === ref) && contacts.push(await g(`user:${ref}`)); } @@ -419,7 +428,7 @@ export default { return res.code(401).send("unauthorized"); let { id, pubkey } = await g( - `user:${await g(`user:${username.toLowerCase()}`)}` + `user:${await g(`user:${username.replace(/\s/g, "").toLowerCase()}`)}` ); let invoices = await db.lRange(`${id}:invoices`, 0, -1); let payments = await db.lRange(`${id}:payments`, 0, -1); @@ -461,5 +470,5 @@ export default { } catch (e) { bail(res, e.message); } - }, + } }; |
