// LinkSpin tracking on member text ads (Marty 2026-11-15, the DownlineHydra module generalized: LinkSpin /api/partner//links). // When a member saves their text ad, LinkSpin makes (or updates) a tracked link for it on that member's own LinkSpin account (every // hub member has one through join-all), tagged with the account's "ViralAdBlitz" source and the ad id as a sub-id (?s1=&placement=), // so LinkSpin Reports show the ad's clicks. The ad's public link (/go/) then sends to the tracked link, which lands on the member's // own link. Per ad the member can switch tracking off ("Track with LinkSpin", default on): then /go/ sends straight to their link. // Fail soft: if LinkSpin does not answer (or does not know the endpoint yet), the ad works exactly as before with its plain link, // and the sweep tries again later. A tracked link is used only while it points at the ad's current link (an edit never sends // traffic to an old destination). Links with {clickid} / {placement} macros are not tracked (they are filled here on every click). // Account: by network username (ViralAdBlitz never stores emails). The call: POST LS_URL/api/partner/vab/links, X-Net-Ts + X-Net-Sig = // hex HMAC-SHA256('net.v1.' + ts + newline + raw body) with NETWORK_SSO_SECRET (the Downline Banners Banner Spins pattern). // ensureAd(ad) -> updates ad.ls { code, link, url, at } or ad.lsErr / ad.lsTry; saves through the store // clicks(username) -> { : { hits, hits30, unique30 } } | null (cached 5 minutes per member) // dest(ad) -> the tracked link when it is current, else null 'use strict'; const crypto = require('crypto'); let C = null, store = null; const st = { ensured: 0, failed: 0, noAccount: 0, lastError: null, lastOkAt: 0, sweeps: 0 }; const cache = new Map(); const MACRO = /\{(clickid|placement)\}|%7B(clickid|placement)%7D/i; const SITE = 'vab'; function init(cfg, st0) { C = cfg; store = st0; } const base = () => String(process.env.LS_TRACK_URL || (C && C.lsBase) || '').replace(/\/+$/, ''); const on = () => !!(C && process.env.LS_TRACK !== '0' && String(process.env.NETWORK_SSO_SECRET || '').trim().length >= 32 && base()); async function post(body, ms) { const sec = String(process.env.NETWORK_SSO_SECRET || '').trim(); const raw = JSON.stringify(body); const ts = String(Date.now()); const sig = crypto.createHmac('sha256', sec).update('net.v1.' + ts + '\n' + raw).digest('hex'); const ac = new AbortController(); const t = setTimeout(() => ac.abort(), ms || 8000); try { const r = await fetch(base() + '/api/partner/' + SITE + '/links', { method: 'POST', body: raw, signal: ac.signal, headers: { 'Content-Type': 'application/json', 'X-Net-Ts': ts, 'X-Net-Sig': sig } }); const j = await r.json().catch(() => ({})); return r.ok ? j : { error: j.error || 'LinkSpin answered ' + r.status }; } catch (e) { return { error: 'LinkSpin did not answer.' }; } finally { clearTimeout(t); } } const ownerName = a => { const m = store.member(a.owner); return m && m.username ? String(m.username) : null; }; // what the member sees about tracking for their ad function state(a) { if (!a) return null; if (a.track === false) return 'off'; if (MACRO.test(String(a.link || ''))) return 'macros'; if (a.ls && a.ls.url === a.link && a.ls.link) return 'tracked'; if (!ownerName(a)) return 'no-username'; if (a.lsNoAccount && a.lsTry && Date.now() - a.lsTry < 86400000) return 'no-account'; return 'pending'; } const wants = a => a && a.status !== 'removed' && a.track !== false && !MACRO.test(String(a.link || '')) && !(a.ls && a.ls.url === a.link && a.ls.link) && !!ownerName(a); function dest(a) { return a && a.track !== false && a.ls && a.ls.link && a.ls.url === a.link && !MACRO.test(String(a.link || '')) ? a.ls.link : null; } async function ensureAd(a) { if (!on() || !wants(a)) return { skipped: true }; const username = ownerName(a); const r = await post({ username, op: 'ensure', links: [{ id: String(a.id), name: a.headline, url: a.link }] }); a.lsTry = Date.now(); if (r.error) { st.failed++; st.lastError = String(r.error).slice(0, 160); a.lsErr = st.lastError; store.saveAd(a); return r; } if (r.account === false) { st.noAccount++; a.lsNoAccount = true; delete a.lsErr; store.saveAd(a); return r; } const l = r.links && r.links[String(a.id)]; if (!l) { st.failed++; a.lsErr = String((r.errors && r.errors[String(a.id)]) || 'no link').slice(0, 160); store.saveAd(a); return r; } a.ls = { code: l.code, link: l.link, url: a.link, at: Date.now() }; delete a.lsErr; delete a.lsNoAccount; st.ensured++; st.lastOkAt = Date.now(); cache.delete(username.toLowerCase()); store.saveAd(a); return { ok: true, link: l.link }; } // the retry sweep: ads that want a tracked link and have not been tried lately (failures back off 10 minutes, no account a day) async function sweep(max) { if (!on()) return 0; st.sweeps++; let n = 0; const backoff = Number(process.env.LS_RETRY_MS || 600000); for (const a of store.liveAds()) { if (n >= (max || 25)) break; if (!wants(a)) continue; if (a.lsTry && Date.now() - a.lsTry < (a.lsNoAccount ? 86400000 : backoff)) continue; n++; const r = await ensureAd(a).catch(() => ({ error: 'x' })); if (r && r.error === 'LinkSpin did not answer.') break; // LinkSpin is down: stop this round } return n; } async function clicks(username) { if (!on() || !username) return null; const k = String(username).toLowerCase(); const hit = cache.get(k); if (hit && Date.now() - hit.at < Number(process.env.LS_CACHE_MS || 300000)) return hit.v; const r = await post({ username, op: 'stats' }, 3000); const v = r.error || r.account === false ? null : (r.stats || {}); cache.set(k, { at: Date.now(), v }); if (cache.size > 20000) cache.clear(); return v; } function view() { return Object.assign({ on: on(), base: base() }, st); } module.exports = { init, ensureAd, sweep, clicks, dest, state, view, on, _reset: () => cache.clear() };