Reverse registry sync: LinkSpin-first members arrive on InstantAdPay as waiting accounts

POST /api/partner/registry (same HMAC LS_SYNC_SECRET scheme LinkSpin accepts from us, 10-minute window) feeds
lsinbound.js: an email already on InstantAdPay keeps its sponsor and only gets empty fields filled (username if free
and allowed, wallet if no other account or linked position holds it, profile); a new email becomes a waiting shell
(joinedVia linkspin) under its LinkSpin sponsor, building the sponsor's shell upward from known rows (25 levels),
remembering a sponsor whose row has not arrived and binding it later, refusing sponsor loops. No mail, notices or chain.

Unclaimed shells are left out of accounts.listAll by default (tank, coach nudges, Friday/Wednesday broadcasts, member
update audiences, leaderboard, 1,000-member milestone count); the admin members table and lssync ask for them. The first
sign-in claims the shell: the sign-up anti-fraud checks run then, a sponsorless shell takes the link used, lastSeen set.

Loop guard: lssync mutes an email while its LinkSpin row is applied, then records the result as already pushed when it
equals what LinkSpin sent; touch() now skips rows whose hash is unchanged. Status: GET /api/admin/lsinbound (also under
/api/admin/lssync as inbound).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-15 10:44:53 -05:00
parent ae72a06a85
commit d86f2fbcf8
4 changed files with 220 additions and 13 deletions
+22 -4
View File
@@ -11,6 +11,7 @@ const fs = require('fs');
const path = require('path');
let R = null, state = null, pending = new Map(), timer = null, ticking = false;
const muted = new Set(); // emails lsinbound.js is writing right now: their rows came FROM LinkSpin, hold them until it decides
const URL_ = () => String(process.env.LS_URL || 'https://linkspin.app').replace(/\/+$/, '');
const SECRET = () => String(process.env.LS_SYNC_SECRET || '').trim();
const on = () => SECRET().length >= 32;
@@ -55,20 +56,37 @@ async function push(rows) {
// an account just changed: send its row shortly (debounced so a join + username + wallet in a row go as one)
function touch(email) {
if (!on() || !email) return; pending.set(String(email).toLowerCase(), Date.now());
if (!timer) timer = setTimeout(async () => { timer = null; const emails = [...pending.keys()]; pending.clear(); try { const rows = []; for (const e of emails) { const a = await R.accounts.byEmail(e); if (a) rows.push(await rowFor(a)); } if (rows.length) await push(rows); } catch (e) { load().errors++; save(); console.error('lssync touch', e.message); } }, 3000);
if (!timer) timer = setTimeout(async () => { timer = null; const st = load(); const emails = [...pending.keys()].filter(e => !muted.has(e)); for (const e of emails) pending.delete(e); try { const rows = []; for (const e of emails) { const a = await R.accounts.byEmail(e); if (a) { const r = await rowFor(a); if (st.hashes[r.email] !== hashOf(r)) rows.push(r); } } if (rows.length) await push(rows); } catch (e) { load().errors++; save(); console.error('lssync touch', e.message); } }, 3000);
}
// the reconcile pass: only rows whose hash changed since the last push
async function tick(force) {
if (!on() || ticking) return { skipped: true };
ticking = true;
try {
const all = await R.accounts.listAll(50000); const st = load();
const all = await R.accounts.listAll(50000, { shells: true }); const st = load();
const byRef = new Map(); for (const a of all) { if (a.username) byRef.set(String(a.username).toLowerCase(), a); if (a.code) byRef.set(String(a.code).toLowerCase(), a); }
const rows = []; for (const a of all) { if (!a.email) continue; const r = await rowFor(a, byRef); if (force || st.hashes[r.email] !== hashOf(r)) rows.push(r); }
const rows = []; for (const a of all) { if (!a.email || muted.has(a.email)) continue; const r = await rowFor(a, byRef); if (force || st.hashes[r.email] !== hashOf(r)) rows.push(r); }
const sent = rows.length ? await push(rows) : 0;
return { accounts: all.length, changed: rows.length, sent };
} catch (e) { load().errors++; save(); console.error('lssync tick', e.message); return { error: e.message }; }
finally { ticking = false; }
}
// Reverse-sync loop guard (2026-10-15, see lsinbound.js): while a LinkSpin row is being applied its email is muted; afterwards,
// if the account now says exactly what LinkSpin sent (username, wallet, sponsor), its hash is recorded as already pushed, so
// it is never sent back. When InstantAdPay's facts differ (its sponsor binding stood, a wallet was skipped) the row goes
// back once; LinkSpin only fills empty fields, so that changes nothing there and its own hash stops a resend.
function mute(email) { muted.add(String(email || '').toLowerCase()); }
async function release(email, sent) {
const e = String(email || '').toLowerCase(); muted.delete(e); let same = false;
try {
const a = await R.accounts.byEmail(e);
if (a && sent) {
const r = await rowFor(a); const n = v => String(v || '').toLowerCase();
if (n(r.username) === n(sent.username) && n(r.wallet) === n(sent.wallet) && n(r.sponsorEmail) === n(sent.sponsorEmail)) { load().hashes[e] = hashOf(r); save(); same = true; }
}
} catch (err) {}
if (pending.has(e) && !timer) { pending.delete(e); touch(e); } // a touch that queued while muted goes now (the hash check drops it when same)
return same;
}
function adminView() { const st = load(); return { on: on(), url: URL_(), tracked: Object.keys(st.hashes).length, pushed: st.pushed, last: st.last, errors: st.errors, pendingTouches: pending.size }; }
module.exports = { init, touch, tick, adminView, on };
module.exports = { init, touch, tick, adminView, on, mute, release };