diff --git a/hubsync.js b/hubsync.js index 8645202..6f0e519 100644 --- a/hubsync.js +++ b/hubsync.js @@ -20,6 +20,12 @@ // Outbound (/v1/join, /v1/wallet, signed as HUB_SITE): new accounts made here since the flag first went on and every main // wallet linked here go into a persisted outbox (DATA_DIR/hubsync.json) and are sent in order, never in the way of a // sign-up. Retried with backoff on 5xx / network / 404-before-join; a 400/409 refusal is final and logged. +// Gaps closed 2026-11-13 (Marty: "let's get the gaps report built"), all still one way: +// - any later change to a member's username or sponsor goes to /v1/join again (names them; a sponsor only lands where the +// hub has none), old members included, not only accounts made since the flag went on; +// - a DELIBERATE sponsor change (sponsorfollow: adopt, rescue, release, expiry, admin) goes to /v1/sponsor-change; +// - every MemberActivated on the InstantAdPay contract goes to /v1/contract-bind (contract 'iap', the sponsor by wallet), +// by email when the wallet belongs to an account here, else by wallet alone. // Where the hub and InstantAdPay name different sponsors, nothing is forced on either side: the case goes to `disagree` // (Admin > /api/admin/hub) for Marty, since an InstantAdPay member bound on our contract cannot move anyway. 'use strict'; @@ -29,7 +35,8 @@ const path = require('path'); let R = null, st = null, chain_ = Promise.resolve(), timer = null, sending = false, interval = null; const MAX_UP = 25, EVENT_TRIES = 5, OUT_TRIES = 20, KEEP_DONE = 5000; -const SHELL_ORIGINS = new Set(['linkspin', 'btc4hits', 'hub']); // came here from another site: that site tells the hub itself +const SHELL_ORIGINS = new Set(['linkspin', 'btc4hits', 'hub']); +const DELIBERATE = new Set(['adopt', 'rescue', 'release', 'expiry', 'admin']); // sponsorfollow.js kinds the hub accepts // came here from another site: that site tells the hub itself const enabled = () => /^(1|true|yes|on)$/i.test(String(process.env.HUB_ENABLED || '').trim()); const URL_ = () => String(process.env.HUB_URL || '').trim().replace(/\/+$/, ''); const SECRET = () => String(process.env.HUB_SECRET || '').trim(); @@ -159,17 +166,34 @@ function enqueue(kind, email) { s.outbox.push({ kind, email, at: Date.now(), tries: 0, next: 0 }); save(); kick(); } function kick() { if (!timer) timer = setTimeout(() => { timer = null; flush().catch(e => console.error('hubsync flush', e.message)); }, 2000); } -// accounts.onChange: a new account made here (or its first username) goes to /v1/join +// accounts.onChange: a new account made here, or a later username / sponsor change on any account, goes to /v1/join; +// a deliberate sponsor change also goes to /v1/sponsor-change function touch(email) { if (!R || !on() || !email) return; (async () => { const e = norm(email); const a = await R.accounts.byEmail(e); const s = load(); - if (!a || SHELL_ORIGINS.has(a.joinedVia) || Number(a.created) < s.since) return; + if (!a || SHELL_ORIGINS.has(a.joinedVia)) return; + const sp = await R.accounts.sponsorOf(e); const spE = sp ? norm(sp.email) : ''; + const key = (a.username || '') + '|' + spE; const rep = s.reported[e]; - if (!rep) { s.reported[e] = { u: a.username || null }; enqueue('join', e); } - else if (a.username && rep.u !== a.username) { rep.u = a.username; enqueue('join', e); } // join again names them on the hub + if (!rep) { s.reported[e] = { u: a.username || null, k: key }; enqueue('join', e); } // new here, or an older member's first change since the flag + else if (rep.k !== key) { rep.u = a.username || null; rep.k = key; enqueue('join', e); } + const rec = R.follow && R.follow.get ? R.follow.get(e) : null; + if (rec && DELIBERATE.has(rec.by) && norm(rec.sponsorEmail) === spE && Number(rec.at) > 0 && s.reported[e].sc !== Number(rec.at)) { + s.reported[e].sc = Number(rec.at); enqueue('sponsor', e); + } + save(); })().catch(err => console.error('hubsync touch', err.message)); } +// chain.onEvent (once across copies): a payout switch-on on the InstantAdPay contract goes to /v1/contract-bind +function onChain(ev) { + if (!R || !on() || !ev || ev.type !== 'MemberActivated' || !ev.account) return; + (async () => { + let spW = null; try { const m = ev.sponsorId ? await R.chain.member(ev.sponsorId) : null; spW = m && m.account ? norm(m.account) : null; } catch (e) {} + const s = load(); if (s.outbox.length >= 20000) { s.stats.sendErrors++; return; } + s.outbox.push({ kind: 'bind', email: norm(ev.account), wallet: norm(ev.account), sponsorWallet: spW, at: Date.now(), tries: 0, next: 0 }); save(); kick(); + })().catch(err => console.error('hubsync onChain', err.message)); +} // accounts.onWalletLinked: a main wallet linked here goes to /v1/wallet function wallet(email) { if (!R || !on() || !email) return; @@ -178,8 +202,18 @@ function wallet(email) { if (a && a.address && !R.accounts.isUnclaimedShell(a)) enqueue('wallet', norm(email)); // a waiting account's wallet came from a sync })().catch(err => console.error('hubsync wallet', err.message)); } +const ROUTE = { join: '/v1/join', wallet: '/v1/wallet', sponsor: '/v1/sponsor-change', bind: '/v1/contract-bind' }; async function bodyFor(item) { + if (item.kind === 'bind') { + let owner = null; try { owner = await R.accounts.byAddress(item.wallet); } catch (e) {} + return { contract: 'iap', email: owner && owner.email ? owner.email : undefined, wallet: item.wallet, sponsorRef: item.sponsorWallet || '' }; + } const a = await R.accounts.byEmail(item.email); if (!a) return null; + if (item.kind === 'sponsor') { + const sp = await R.accounts.sponsorOf(a.email); const rec = R.follow && R.follow.get ? R.follow.get(a.email) : null; + if (!rec || !DELIBERATE.has(rec.by) || norm(rec.sponsorEmail) !== (sp ? norm(sp.email) : '')) return null; // superseded since + return { email: a.email, sponsorEmail: sp ? sp.email : null, by: rec.by }; + } if (item.kind === 'wallet') return a.address ? { email: a.email, wallet: a.address } : null; const sp = await R.accounts.sponsorOf(a.email); return { email: a.email, ref: sp ? sp.email : (a.sponsorRef || undefined), username: a.username || undefined }; @@ -195,13 +229,13 @@ async function flush(force) { // force (tests, admin): ignore the backoff wait let r = null; try { const body = await bodyFor(item); if (!body) { drop('gone'); save(); continue; } - r = await hubPost(item.kind === 'join' ? '/v1/join' : '/v1/wallet', body); + r = await hubPost(ROUTE[item.kind] || '/v1/wallet', body); } catch (e) { r = { status: 0, body: { error: e.message } }; } const code = (r.body && r.body.error) || null; if (r.status >= 200 && r.status < 300) { drop(null); s.stats.sent++; sent++; if (item.kind === 'join' && r.body && r.body.person) await compare(item.email, r.body.person.sponsor && r.body.person.sponsor.email, 'iap-join'); - } else if ((r.status === 400 || r.status === 409) || (r.status === 403 && code === 'blocked')) { + } else if ((r.status === 400 || r.status === 409) || (r.status === 403 && code === 'blocked') || (item.kind === 'sponsor' && r.status === 404)) { drop('refused', code); s.stats.refused++; } else { item.tries++; s.stats.sendErrors++; @@ -237,4 +271,4 @@ function adminView() { return Object.assign({ enabled: enabled(), on: on(), site: SITE(), url: URL_(), since: s.since, outbox: s.outbox.length, retrying: Object.keys(s.tries).length, disagree: Object.keys(s.disagree).length }, s.stats, { disagreements: Object.entries(s.disagree).slice(-25).map(([email, d]) => Object.assign({ email }, d)), recent: s.log.slice(-25).reverse() }); } -module.exports = { init, enabled, on, receive, touch, wallet, flush, reconcile, start, adminView, _sign: sign }; +module.exports = { init, enabled, on, receive, touch, wallet, onChain, flush, reconcile, start, adminView, _sign: sign }; diff --git a/server.js b/server.js index c4ff7fb..2db58ff 100644 --- a/server.js +++ b/server.js @@ -564,6 +564,7 @@ async function boot() { wednesday.onEvent(ev).catch(e => console.error('wednesday bonus', e.message)); refuel.onIapPurchase(ev).catch(e => console.error('refuel streak', e.message)); // its own bonus, stacked on Friday/Wednesday; no-op while refuelStreak is off sponsorSyncOnEvent(ev).catch(e => console.error('sponsor sync', e.message)); + hubsync.onChain(ev); // network hub: payout switch-on -> contract bind (no-op unless HUB_ENABLED) lsReportPurchase(ev).catch(e => console.error('lsreport purchase', e.message)); // reporting only: one local row, delivered in the background }).catch(e => console.error('marks claim', e.message)); } }); @@ -704,7 +705,7 @@ async function boot() { lssync.init({ dataDir: DATA_DIR, accounts, follow: followStore, tank: () => tank }); accounts.onChange(email => lssync.touch(email)); lsinbound.init({ dataDir: DATA_DIR, accounts, lssync, follow, store: followStore }); // network hub (hubsync.js): nothing at all happens here unless HUB_ENABLED is on; the LinkSpin sync above runs either way - if (hubsync.enabled()) { hubsync.init({ dataDir: DATA_DIR, accounts, lsinbound }); accounts.onChange(email => { lssync.touch(email); hubsync.touch(email); }); hubsync.start(); } + if (hubsync.enabled()) { hubsync.init({ dataDir: DATA_DIR, accounts, lsinbound, follow: followStore, chain }); accounts.onChange(email => { lssync.touch(email); hubsync.touch(email); }); hubsync.start(); } setTimeout(() => lssync.tick().catch(e => console.error('lssync', e.message)), 2 * 60 * 1000); setInterval(() => lssync.tick().catch(e => console.error('lssync', e.message)), 2 * 60 * 1000); setInterval(() => follow.retryPending().catch(e => console.error('sponsorfollow retry', e.message)), 2 * 60 * 1000); // follows waiting on a chain read or a sponsor account