diff --git a/lsinbound.js b/lsinbound.js index 2c02ce4..7b12fbb 100644 --- a/lsinbound.js +++ b/lsinbound.js @@ -16,7 +16,15 @@ // then accounts.listAll leaves unclaimed shells out of every sweep that mails, counts or adopts members. // - Loop guard: each email is muted in lssync while its row is applied, then lssync records the result as already // pushed when it equals what LinkSpin sent, so it is not bounced back (see lssync.release). No-op rows change nothing. +// - SPONSOR FOLLOW (2026-10-22, sponsorfollow.js): a row that carries a DELIBERATE change made on LinkSpin (sponsorSetBy +// adopt / rescue / release / expire / admin) is the one exception to the first-platform rule: it is applied, a clear +// back to the tank included, when the member is not bound on InstantAdPay's contract (no memberId, not on chain), the +// change is newer than our own deliberate change, and the two sides agreed before it (or we have no sponsor). Bound, +// diverged and race cases change nothing here and are logged for Admin > Members > Sponsor check. Applied quietly. +// - NETWORK TANK: a row's `tank` (LinkSpin's own adoptions of the person) is kept as remote state, so our waiting list +// hides someone adopted on LinkSpin and the two-adoptions limit counts both sites. 'use strict'; +const SF = require('./sponsorfollow'); const fs = require('fs'); const path = require('path'); @@ -35,7 +43,7 @@ function load() { } function save() { try { const tmp = FILE() + '.tmp'; fs.writeFileSync(tmp, JSON.stringify(load())); fs.renameSync(tmp, FILE()); } catch (e) { console.error('lsinbound save', e.message); } } function note(ev) { const s = load(); s.log.push(Object.assign({ ts: Date.now() }, ev)); if (s.log.length > 300) s.log = s.log.slice(-300); } -function init(deps) { R = deps; load(); } // { dataDir, accounts, lssync } +function init(deps) { R = deps; load(); } // { dataDir, accounts, lssync, follow (sponsorfollow glue), store (sponsorfollow store) } function clean(x) { if (!x || typeof x !== 'object') return null; @@ -44,9 +52,18 @@ function clean(x) { const wallet = /^0x[0-9a-f]{40}$/.test(norm(x.wallet)) ? norm(x.wallet) : null; let sponsorEmail = norm(x.sponsorEmail); if (!EMAIL_RE.test(sponsorEmail) || sponsorEmail === email) sponsorEmail = null; const profile = x.profile && typeof x.profile === 'object' ? x.profile : null; - return { email, username, wallet, sponsorEmail, joinedAt: Number(x.joinedAt) || null, profile }; + const out = { email, username, wallet, sponsorEmail, joinedAt: Number(x.joinedAt) || null, profile }; + if (SF.isDeliberate(x.sponsorSetBy) && Number(x.sponsorSetAt) > 0) { // a deliberate change on LinkSpin (sponsorfollow.js) + out.sponsorSetAt = Number(x.sponsorSetAt); out.sponsorSetBy = String(x.sponsorSetBy); + const pv = norm(x.sponsorPrevEmail); out.sponsorPrevEmail = EMAIL_RE.test(pv) ? pv : null; + out.sponsorSetOrigin = norm(x.sponsorSetOrigin).replace(/[^a-z]/g, '').slice(0, 20) || 'linkspin'; + } + if (Object.prototype.hasOwnProperty.call(x, 'tank')) out.tank = SF.cleanTank(x.tank); // null = LinkSpin has no adoption of them now + return out; } const tokenOf = a => a.username || a.code; +// released / rescued / expired back to the tank on purpose here: an ordinary row must not refill the sponsor (sponsorfollow.js) +const emptiedOnPurpose = email => { const pv = R.store && R.store.get(email); return !!(pv && SF.isDeliberate(pv.by) && !pv.sponsorEmail); }; function conflict(sum, email, kind, detail) { sum.conflicts++; note({ type: 'conflict', email, kind, detail: String(detail || '').slice(0, 120) }); } // would binding `email` under `sp` close a loop (sp already sits below email)? @@ -94,7 +111,14 @@ async function place(email, depth, ctx) { ctx.done.add(email); R.lssync.mute(email); let a = await A.byEmail(email); let wrote = false; if (!r) return a; - if (!a) { + const inc = SF.incomingOf(r); + if (a && inc && R.follow) { + // a deliberate sponsor change made on LinkSpin: the follow rule decides (never on a member bound here) + const d = await R.follow.applyIncoming(email, r, { sponsorAccount: em => sponsorAccount(em, depth, ctx) }); + if (d.apply) { ctx.sum.followed++; wrote = true; delete s.pending[email]; note({ type: 'follow', email, by: r.sponsorSetBy, sponsor: r.sponsorEmail || null, action: d.action }); } + else if (d.action !== 'same' && d.action !== 'ordinary') { ctx.sum.followKept++; note({ type: 'follow-kept', email, by: r.sponsorSetBy, action: d.action, detail: String(d.reason || '').slice(0, 120) }); } + a = await A.byEmail(email); + } else if (!a) { let tok = ''; if (r.sponsorEmail) { const sp = await sponsorAccount(r.sponsorEmail, depth, ctx); @@ -104,7 +128,9 @@ async function place(email, depth, ctx) { if (!x || x.error) { conflict(ctx.sum, email, 'create', x && x.error); return null; } a = x.account; if (x.created) { ctx.sum.created++; wrote = true; note({ type: 'shell', email, sponsor: tok || null, pending: tok ? null : (r.sponsorEmail || null) }); } - } else if (A.isUnclaimedShell(a) && !a.sponsorRef && r.sponsorEmail) { + // a new waiting account built from a deliberate change carries the same record, so its row does not echo back + if (x.created && inc && R.store && (!r.sponsorEmail || tok)) R.store.record(email, { at: inc.at, by: inc.by, sponsorEmail: inc.sponsorEmail, prevEmail: inc.prevEmail, origin: inc.origin || 'linkspin' }); + } else if (A.isUnclaimedShell(a) && !a.sponsorRef && r.sponsorEmail && !emptiedOnPurpose(email)) { // our own waiting account, still without a sponsor: this row (or a later one) names one const sp = await sponsorAccount(r.sponsorEmail, depth, ctx); if (sp && sp.email !== email && !(await wouldCycle(email, sp))) { await A.setSponsorRef(email, tokenOf(sp)); delete s.pending[email]; ctx.sum.sponsored++; wrote = true; } @@ -119,7 +145,7 @@ async function resolvePending(ctx) { const s = load(); const A = R.accounts; for (const [email, spEmail] of Object.entries(s.pending)) { const a = await A.byEmail(email); - if (!a || !A.isUnclaimedShell(a) || a.sponsorRef) { delete s.pending[email]; continue; } // claimed or bound meanwhile: leave it + if (!a || !A.isUnclaimedShell(a) || a.sponsorRef || emptiedOnPurpose(email)) { delete s.pending[email]; continue; } // claimed, bound or emptied on purpose meanwhile: leave it const sp = await sponsorAccount(spEmail, 0, ctx); if (!sp) continue; if (sp.email === email || await wouldCycle(email, sp)) { delete s.pending[email]; conflict(ctx.sum, email, 'sponsor-cycle', spEmail); continue; } R.lssync.mute(email); ctx.done.add(email); @@ -129,9 +155,10 @@ async function resolvePending(ctx) { } async function handle(rows) { const s = load(); s.stats.batches++; - const sum = { rows: 0, noop: 0, created: 0, filled: 0, sponsored: 0, conflicts: 0, pendingSponsor: 0, errors: 0 }; + const sum = { rows: 0, noop: 0, created: 0, filled: 0, sponsored: 0, conflicts: 0, pendingSponsor: 0, errors: 0, followed: 0, followKept: 0, tank: 0 }; const ctx = { sum, done: new Set() }; const batch = []; for (const x of rows || []) { const r = clean(x); if (!r) continue; sum.rows++; s.rows[r.email] = r; batch.push(r); } + if (R.store) for (const r of batch) if (Object.prototype.hasOwnProperty.call(r, 'tank')) { R.store.setRemote(r.email, r.tank); sum.tank++; } // network tank try { for (const r of batch) { try { await place(r.email, 0, ctx); } @@ -141,9 +168,9 @@ async function handle(rows) { } finally { for (const email of ctx.done) await R.lssync.release(email, s.rows[email]); // loop guard: equal results are not echoed } - s.stats.received += sum.rows; for (const k of ['noop', 'created', 'filled', 'sponsored', 'conflicts', 'errors']) s.stats[k] += sum[k]; + s.stats.received += sum.rows; for (const k of ['noop', 'created', 'filled', 'sponsored', 'conflicts', 'errors', 'followed', 'followKept']) s.stats[k] = (s.stats[k] || 0) + sum[k]; s.last = Object.assign({ at: Date.now() }, sum); save(); - return sum; + return Object.assign({ proto: SF.PROTO }, sum); // proto 2: this side understands sponsor follow + network tank facts } // batches are applied one at a time (two overlapping posts must not both create the same sponsor shell) function receive(rows) { const run = chain_.then(() => handle(rows)); chain_ = run.catch(() => {}); return run; } diff --git a/lssync.js b/lssync.js index b722681..4253e13 100644 --- a/lssync.js +++ b/lssync.js @@ -5,8 +5,15 @@ // hash in DATA_DIR/lssync.json and only changed rows go, 100 per request. Catches anything touch() missed. // LinkSpin upserts the rows and creates shell accounts with their sponsor line, so a new InstantAdPay member is already // "waiting" on LinkSpin before they ever sign in there. Nothing here moves money; the registry is identity + line only. +// +// Sponsor follow (2026-10-22, sponsorfollow.js): a row also carries sponsorSetAt / sponsorSetBy / sponsorPrevEmail / +// sponsorSetOrigin while the account's sponsor is the result of a deliberate change (tank adoption, rescue, release, +// window expiry, admin edit), and `tank` (this site's own adoptions of the person: count + the open one, or null). +// Rows without those facts hash exactly as before, so nothing is resent on deploy. Until LinkSpin answers proto 2 the +// hash of a row carrying the new facts is not recorded, so it is sent again once LinkSpin runs the new code. 'use strict'; const crypto = require('crypto'); +const SF = require('./sponsorfollow'); const fs = require('fs'); const path = require('path'); @@ -20,11 +27,16 @@ function load() { if (state) return state; try { state = JSON.parse(fs.readFileS function save() { try { fs.writeFileSync(FILE(), JSON.stringify(load())); } catch (e) {} } function init(deps) { R = deps; load(); } -async function rowFor(acct, byRef) { +async function rowFor(acct, byRef, tankMap) { const ref = String(acct.sponsorRef || '').toLowerCase(); let sponsorEmail = null; if (ref) { const sp = byRef ? byRef.get(ref) : (await R.accounts.byUsername(ref) || await R.accounts.byCode(ref)); if (sp && sp.email !== acct.email) sponsorEmail = sp.email; } - return { email: acct.email, username: acct.username || null, wallet: acct.address || null, sponsorEmail, joinedAt: Number(acct.created) || null, memberId: acct.memberId || null, profile: profileOf(acct) }; + const row = { email: acct.email, username: acct.username || null, wallet: acct.address || null, sponsorEmail, joinedAt: Number(acct.created) || null, memberId: acct.memberId || null, profile: profileOf(acct) }; + if (R.follow) Object.assign(row, SF.rowFields(R.follow.get(acct.email), sponsorEmail) || {}); + const T = R.tank && R.tank(); + if (T) { try { row.tank = (tankMap ? tankMap.get(String(acct.email).toLowerCase()) : await T.summaryFor(acct.email)) || null; } catch (e) {} } + return row; } +const carriesNew = r => !!(r.sponsorSetBy || r.tank); // the member's profile travels too (Marty 2026-10-11: no re-typing on LinkSpin; they can change it there later). Avatar as an // absolute https URL so the other property can show it; socials as the parsed {platform: url} object. const PUBLIC = () => String(process.env.PUBLIC_URL || 'https://instantadpay.com').replace(/\/+$/, ''); @@ -36,7 +48,7 @@ function profileOf(a) { if (!avatarUrl && !bio && !(socials && Object.keys(socials).length)) return null; return { avatarUrl, bio, socials: socials && Object.keys(socials).length ? socials : null }; } -const hashOf = r => crypto.createHash('sha1').update([r.username || '', r.wallet || '', r.sponsorEmail || '', r.joinedAt || '', r.memberId || '', r.profile ? JSON.stringify(r.profile) : ''].join('|')).digest('hex').slice(0, 16); +const hashOf = r => crypto.createHash('sha1').update([r.username || '', r.wallet || '', r.sponsorEmail || '', r.joinedAt || '', r.memberId || '', r.profile ? JSON.stringify(r.profile) : ''].join('|') + SF.hashExtra(r)).digest('hex').slice(0, 16); async function post(rows) { const raw = JSON.stringify({ rows }); const ts = String(Date.now()); @@ -48,8 +60,9 @@ async function post(rows) { async function push(rows) { const st = load(); let sent = 0; for (let i = 0; i < rows.length; i += 100) { - const chunk = rows.slice(i, i + 100); await post(chunk); sent += chunk.length; - for (const r of chunk) st.hashes[r.email] = hashOf(r); + const chunk = rows.slice(i, i + 100); const res = await post(chunk); sent += chunk.length; + const peerNew = Number(res && res.proto) >= SF.PROTO; if (R.follow) R.follow.setPeer(peerNew ? SF.PROTO : 1); + for (const r of chunk) { if (peerNew || !carriesNew(r)) st.hashes[r.email] = hashOf(r); } // an old LinkSpin ignored the new facts: send again later } st.pushed += sent; st.last = { at: Date.now(), rows: sent }; save(); return sent; } @@ -65,7 +78,8 @@ async function tick(force) { try { 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 || muted.has(a.email)) continue; const r = await rowFor(a, byRef); if (force || st.hashes[r.email] !== hashOf(r)) rows.push(r); } + const T = R.tank && R.tank(); let tankMap = null; if (T) { try { tankMap = await T.summaryMap(); } catch (e) { tankMap = null; } } + const rows = []; for (const a of all) { if (!a.email || muted.has(a.email)) continue; const r = await rowFor(a, byRef, tankMap || (T ? new Map() : null)); 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 }; } @@ -82,7 +96,9 @@ async function release(email, sent) { 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; } + // a deliberate change from LinkSpin counts as the same only when we now carry that same record (applied, not refused) + const sameFollow = !sent.sponsorSetBy || (Number(r.sponsorSetAt) === Number(sent.sponsorSetAt) && String(r.sponsorSetBy) === String(sent.sponsorSetBy)); + if (n(r.username) === n(sent.username) && n(r.wallet) === n(sent.wallet) && n(r.sponsorEmail) === n(sent.sponsorEmail) && sameFollow) { 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) diff --git a/public/admin.html b/public/admin.html index 25b86a8..4633ad3 100644 --- a/public/admin.html +++ b/public/admin.html @@ -265,6 +265,13 @@
Adoptions (newest first)
Deliberate changes (a tank adoption, a rescue, a release, the end of an adoption window, an admin edit) follow to the other site until the member is bound on that site's contract. Everything else keeps the first sponsor. To settle a difference, pick the side whose sponsor is right: it is applied on the other site, unless the member is already bound there.
+ +Loading duplicate signals…