Reverse registry sync: push LinkSpin-first members to InstantAdPay (iapsync.js)

Mirror of InstantAdPay's lssync: touch() on account create / username / wallet (incl. admin set) / sponsor / profile
via a new accounts.onChange hook, plus a 2-minute hashed tick, 100 rows per signed POST to IAP_URL/api/partner/registry
with the same LS_SYNC_SECRET. Inert while the secret is missing or short.

Loop guard (documented in the module header): only rows changed since our last push, and never rows the registry
already holds (= what InstantAdPay told us), so the accounts built from the InstantAdPay import are recorded and not
sent. A pushed LinkSpin-first email gets a registry row with firstProperty 'linkspin', and registry.upsert no longer
overwrites an existing firstProperty, so InstantAdPay's echo cannot relabel it.

Also: the inbound InstantAdPay handler binds sponsors in a second pass, so a row that precedes its sponsor's row in the
same batch (newest-first order) is no longer left without a sponsor. Admin: GET /api/admin/iapsync, POST .../run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-15 10:44:54 -05:00
parent baa7db61ff
commit 2e026e1a42
4 changed files with 156 additions and 9 deletions
+14 -1
View File
@@ -37,6 +37,7 @@ const audit = require('./audit'); // counter audit: views vs delivery logs,
const leaderboard = require('./leaderboard');
const toolkit = require('./toolkit');
const registry = require('./registry'); // network registry: who is who across properties (LinkSpin, 2026-09-15)
const iapsync = require('./iapsync'); // reverse registry sync: LinkSpin-first members reach InstantAdPay (2026-10-15)
const carry = require('./carry'); // sponsor carry-over: engine activation, claim window, notices
const rotator = require('./rotator');
const health = require('./health'); // destination health checks + auto-pause (2026-10-10)
@@ -416,6 +417,10 @@ async function boot() {
pipeline.init({ dataDir: DATA_DIR, accounts, coach, chain });
registry.init({ dataDir: DATA_DIR });
carry.init({ dataDir: DATA_DIR, accounts, registry, chain, mailer, messages, siteConfig, adminEmail: ADMIN_EMAIL, host: SITE_HOST });
// reverse registry sync (2026-10-15): account changes reach InstantAdPay within seconds; a 2-minute reconcile catches the rest
iapsync.init({ dataDir: DATA_DIR, accounts, registry, host: SITE_HOST }); accounts.onChange(email => iapsync.touch(email));
setTimeout(() => iapsync.tick().catch(e => console.error('iapsync', e.message)), 2 * 60 * 1000);
setInterval(() => iapsync.tick().catch(e => console.error('iapsync', e.message)), 2 * 60 * 1000);
const ownHosts = () => [SITE_HOST].concat(String(siteConfig().linkDomains || '').split(',').map(x => x.trim().toLowerCase()).filter(Boolean), pagesHost() ? [pagesHost()] : []);
rotator.init({ dataDir: DATA_DIR, geo, ownHosts, sources });
// destination health: every 30 min, first pass 2 min after boot; a pause lands in the owner's on-site inbox (same path as the payment notices)
@@ -2149,7 +2154,6 @@ const server = http.createServer(async (req, res) => {
if (!a) continue;
if (r.username && !a.username) { try { await accounts.setUsername(e, r.username); updated++; } catch (err) {} }
if (r.wallet && !a.address) { try { await accounts.linkWallet(e, r.wallet); updated++; } catch (err) {} }
if (r.sponsorEmail && !a.sponsorRef) { const sp = await accounts.byEmail(r.sponsorEmail); if (sp && (sp.username || sp.code)) { try { await accounts.setSponsorRef(e, sp.username || sp.code); updated++; } catch (err) {} } }
if (r.profile && typeof r.profile === 'object') { // the InstantAdPay profile fills only what is still empty here; the member can change it on this side any time (Marty 2026-10-11)
const pf = r.profile, ok = v => /^https:[/][/][^ ]+$/i.test(v) && v.length <= 300;
const av = String(pf.avatarUrl || '').trim(); const bio = String(pf.bio || '').trim().slice(0, 600); let soc = null;
@@ -2158,8 +2162,17 @@ const server = http.createServer(async (req, res) => {
if (upd.some(x => x !== undefined)) { try { await accounts.setProfile(e, upd[0], upd[1], upd[2]); updated++; } catch (err) {} }
}
}
// sponsors in a second pass: a batch is newest-first, so a member's row usually comes BEFORE their sponsor's, whose
// shell did not exist yet in the first pass (the sponsor line was left empty until the next change, 2026-10-15)
for (const r of rows) {
const e = String(r.email || '').trim().toLowerCase(); if (!e || !r.sponsorEmail) continue;
const a = await accounts.byEmail(e); if (!a || a.sponsorRef) continue;
const sp = await accounts.byEmail(r.sponsorEmail); if (sp && sp.email !== a.email && (sp.username || sp.code)) { try { await accounts.setSponsorRef(e, sp.username || sp.code); updated++; } catch (err) {} }
}
return json(res, 200, { ok: true, rows: rows.length, imported: n, shells, updated, registry: await registry.count() });
}
if (p === '/api/admin/iapsync' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, iapsync.adminView()); }
if (p === '/api/admin/iapsync/run' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); const b = await readBody(req); return json(res, 200, await iapsync.tick(!!(b && b.force))); }
if (p === '/api/admin/registry/import' && req.method === 'POST') {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
const b = await readBody(req); const n = await registry.importRows(b.rows || [], b.property || 'instantadpay');