diff --git a/proof.js b/proof.js index ed9c3b4..117646b 100644 --- a/proof.js +++ b/proof.js @@ -7,6 +7,7 @@ 'use strict'; const store = require('./store'); const docstore = require('./docstore'); +const hubpeople = require('./hubpeople'); const CARDS = ['/art/proof/ypb-proof-plate.png', '/art/proof/ypb-proof-bed.png']; const st = { posted: 0, skipped: 0, failed: 0, lastError: null }; @@ -17,7 +18,18 @@ function init(cfg, deps) { C = cfg; shop = deps.shop; wire = deps.wire; } const on = () => !!(process.env.TG_TOKEN && process.env.TG_CHAT); const lc = a => String(a || '').toLowerCase(); const short = a => a ? a.slice(0, 6) + '...' + a.slice(-4) : ''; -function nameLocal(addr) { const m = store.member(lc(addr)); return m && m.username ? m.username : null; } +// people paid in a Spotlight need not have signed in here (Marty 11-18: Biz Ventures showed as a wallet): fall back to the hub's username +const hubNames = new Map(); +function nameLocal(addr) { const m = store.member(lc(addr)); return m && m.username ? m.username : (hubNames.get(lc(addr)) || {}).name || null; } +async function warmNames(addrs) { + for (const a of new Set(addrs.map(lc).filter(Boolean))) { + const m = store.member(a); if (m && m.username) continue; + const h = hubNames.get(a); if (h && Date.now() - h.at < 3600000) continue; + const r = await hubpeople.byWallet(a).catch(() => ({ ok: false })); if (!r.ok) return; + if (hubNames.size > 5000) hubNames.clear(); + hubNames.set(a, { at: Date.now(), name: r.person && r.person.username ? String(r.person.username) : null }); + } +} const who = a => nameLocal(a) ? '@' + nameLocal(a) : short(lc(a)); const pol = wei => { const v = Number(BigInt(wei || 0) / 10n ** 12n) / 1e6; return v >= 100 ? v.toFixed(2) : v.toFixed(4); }; const usd = cents => '$' + (Number(cents || 0) / 100).toFixed(2); @@ -31,6 +43,7 @@ function split(x) { function rows() { return Object.entries((store.state() && store.state().purchases) || {}).map(([k, x]) => Object.assign({ key: k, tx: k.split(':')[0] }, x)); } function ledger(n) { const lim = Math.min(200, Math.max(1, Number(n) || 50)); + warmNames(rows().flatMap(x => [x.buyer].concat(split(x).map(q => q.to)))).catch(() => {}); const list = rows().map(x => { const p = x.packetId ? store.packet(x.packetId) : null; const dv = wire.spotView(x); return { kind: 'spotlight', tx: x.tx, at: (x.blockAt ? x.blockAt * 1000 : x.at) || null, orderId: x.orderId, buyer: x.buyer, buyerName: nameLocal(x.buyer), qty: x.qty, views: x.views, @@ -97,6 +110,7 @@ function onPurchase(rec, tx) { if (Date.now() - at > 2 * 3600000) { st.skipped++; return; } if (!once('p:' + lc(tx) + ':' + rec.orderId) || !(await docstore.claim('ypbtg:' + lc(tx) + ':' + rec.orderId))) return; const photo = (process.env.TG_PHOTO_BASE ? String(process.env.TG_PHOTO_BASE).replace(/\/+$/, '') : 'https://' + C.site) + CARDS[card++ % CARDS.length]; + await warmNames([x.buyer].concat(split(x).map(q => q.to))); const text = caption(x); let ok = await tg('sendPhoto', { photo, caption: text }); if (!ok) ok = await tg('sendMessage', { text, disable_web_page_preview: true }); if (ok) st.posted++;