diff options
| author | Dedan Kibere <[email protected]> | 2024-10-31 18:26:18 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-10-31 18:26:18 +0300 |
| commit | c9316a7f967168aad3760ebe219262089a84f986 (patch) | |
| tree | c74493e4ec760dee2322042db8c2bf7beb11a718 /lib | |
| parent | 3485563edc522ecd66a6b9d2c8c21b786b4d0716 (diff) | |
| parent | b3203b848a25b5aadebfe8fff3e0f01dda1919fa (diff) | |
| download | coinos-server-c9316a7f967168aad3760ebe219262089a84f986.tar.gz coinos-server-c9316a7f967168aad3760ebe219262089a84f986.zip | |
Merge branch 'coinos:master' into master
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/invoices.ts | 30 | ||||
| -rw-r--r-- | lib/locations.ts | 53 |
2 files changed, 47 insertions, 36 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts index f2653d74..749994b4 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -1,16 +1,16 @@ import config from "$config"; -import { emit } from "$lib/sockets"; import { db, g, s } from "$lib/db"; -import { getUser, bip21, fail, SATS } from "$lib/utils"; +import ln from "$lib/ln"; import { types } from "$lib/payments"; -import { v4 } from "uuid"; +import { emit } from "$lib/sockets"; +import { SATS, bip21, fail, getUser } from "$lib/utils"; import rpc from "@coinos/rpc"; -import ln from "$lib/ln"; +import { v4 } from "uuid"; -let bc = rpc(config.bitcoin); -let lq = rpc(config.liquid); +const bc = rpc(config.bitcoin); +const lq = rpc(config.liquid); -export let generate = async ({ invoice, user }) => { +export const generate = async ({ invoice, user }) => { let { bolt11, aid, @@ -40,7 +40,7 @@ export let generate = async ({ invoice, user }) => { if (!account) account = await g(`account:${user.id}`); if (!account) fail("account not found"); - let rates = await g("rates"); + const rates = await g("rates"); if (!currency) currency = user.currency; if (!rate) rate = rates[currency]; if (fiat) amount = Math.round((SATS * fiat) / rate); @@ -49,13 +49,13 @@ export let generate = async ({ invoice, user }) => { if (rate < 0) fail("invalid rate"); if (memo && memo.length > 5000) fail("memo too long"); - let id = v4(); + const id = v4(); let hash, text, path; if (account.seed) { type = "bitcoin"; - let node = rpc({ ...config[type], wallet: aid }); + const node = rpc({ ...config[type], wallet: aid }); hash = await node.getNewAddress(); text = bip21(hash, invoice); @@ -63,7 +63,7 @@ export let generate = async ({ invoice, user }) => { } else if (type === types.lightning) { let r; if (bolt11) { - let { id: nodeid } = await ln.getinfo(); + const { id: nodeid } = await ln.getinfo(); r = await ln.decode(bolt11); if (r.payee !== nodeid) fail("invalid invoice"); amount = Math.round(r.amount_msat / 1000); @@ -91,7 +91,7 @@ export let generate = async ({ invoice, user }) => { } else if (type === types.internal) { hash = id; } else { - fail("unrecognized type", type); + fail(`unrecognized type ${type}`); } invoice = { @@ -120,7 +120,7 @@ export let generate = async ({ invoice, user }) => { }; if (type === "liquid") { - let { unconfidential } = await lq.getAddressInfo(hash); + const { unconfidential } = await lq.getAddressInfo(hash); await s(`invoice:${unconfidential}`, id); } @@ -129,9 +129,9 @@ export let generate = async ({ invoice, user }) => { await db.lPush(`${aid}:invoices`, id); if (request_id) { - let request = await g(`request:${request_id}`); + const request = await g(`request:${request_id}`); if (request) { - let { invoice_id: prev } = request; + const { invoice_id: prev } = request; request.invoice_id = id; await s(`request:${request_id}`, request); diff --git a/lib/locations.ts b/lib/locations.ts index 11981c19..bb8b08cf 100644 --- a/lib/locations.ts +++ b/lib/locations.ts @@ -1,11 +1,24 @@ +import { g, s } from "$lib/db"; import { err } from "$lib/logging"; +import { fields, pick } from "$lib/utils"; import got from "got"; -import { g, s } from "$lib/db"; -import { fields, pick, uniq } from "$lib/utils"; -export let getLocations = async () => { +const dedup = (array) => + Object.values( + array.reduce((acc, obj) => { + if ( + !acc[obj.id] || + new Date(obj.updated_at) > new Date(acc[obj.id].updated_at) + ) { + acc[obj.id] = obj; + } + return acc; + }, {}), + ); + +export const getLocations = async () => { try { - let previous = await g("locations"); + const previous = await g("locations") || []; let since = await g("locations:since"); if (!since) since = "2022-09-19T00:00:00Z"; if (Date.now() - new Date(since).getTime() < 60000) return; @@ -16,36 +29,34 @@ export let getLocations = async () => { locations = locations.filter( (l) => - l["osm_json"].tags && - l["osm_json"].tags["payment:coinos"] === "yes" && - l["osm_json"].tags.name && - !l["deleted_at"], + l.osm_json.tags && + l.osm_json.tags["payment:coinos"] === "yes" && + l.osm_json.tags.name && + !l.deleted_at, ); locations.map((l) => { - let { bounds, lat, lon } = l["osm_json"]; + const { bounds, lat, lon } = l.osm_json; - l["osm_json"].lat = lat || (bounds.minlat + bounds.maxlat) / 2; - l["osm_json"].lon = lon || (bounds.minlon + bounds.maxlon) / 2; + l.osm_json.lat = lat || (bounds.minlat + bounds.maxlat) / 2; + l.osm_json.lon = lon || (bounds.minlon + bounds.maxlon) / 2; }); - for await (let l of locations) { - let username = l.tags["payment:coinos"]; + for await (const l of locations) { + const username = l.tags["payment:coinos"]; if (username) { - let uid = await g(`user:${username}`); - let user = await g(`user:${uid}`); - if (user) l["osm_json"].tags.user = pick(user, fields); + const uid = await g(`user:${username}`); + const user = await g(`user:${uid}`); + if (user) l.osm_json.tags.user = pick(user, fields); } } locations.push(...previous); - await s( - "locations", - uniq(locations, (l) => l.id), - ); - await s("locations:since", new Date().toISOString().split(".")[0] + "Z"); + await s("locations", dedup(locations)); + await s("locations:since", `${new Date().toISOString().split(".")[0]}Z`); } catch (e) { + console.log(e); err("problem fetching locations", e); } |
