a8a1a8eec7
Plant a program free (referral link with {id}, link check green / yellow / red, daily recheck, reports), members save their
referral IDs in its packet, a sponsor-first link (member page / ?r= / the hub sponsor line, then the planter), the sprout meter
to REVIEW_SAVES (DEFAULT 25) -> Marty's review queue -> approved packets exported for the CTB builder / DownlineHydra wild heads
with every saved ID. Dormant old programs confirmed by their owner, old-link redirects (oldrefs), Spotlight on YPBShop (NOT
deployed; DEFAULT $10 / 1,000 views, 20/5/5/5/5, company 60), verified views with Spotlight delivery, trust pack (ledger,
contract, toasts, Telegram card, terms / disclaimer / privacy, TrustClickz only), network footer row, chatbot, promo kit,
Guides, PolHunter slot, LinkSpin tracked-link and zone hooks, AI writer through the hub. Seed-catalog look from the approved
mockup. Tests: qa/unit.test.cjs 33/33, qa/e2e.mjs 121/121 on a local anvil chain.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
8.5 KiB
JavaScript
123 lines
8.5 KiB
JavaScript
// The engine: verified visitors, packet views, Spotlight delivery and verified clicks (the ViralAdBlitz wire.js rules).
|
|
// - A page load gets a ticket listing the packets it showed and where (server side, never what the browser claims). After the
|
|
// page has been visible visitSeconds the browser redeems it: the visitor is verified (bots, early sends, the per-IP cap
|
|
// filtered) and each packet shown gets one verified view, once per visitor per packet per 24 h. A packet's planter never
|
|
// counts their own views.
|
|
// - A Spotlight (the chain's Purchase event, shop.js) guarantees `views` verified views for one packet. Only views in the
|
|
// Spotlight places count toward it (the top row of the Seed Bed, the Spotlight strip on packet and member pages), and every
|
|
// one is kept as a row (ypb_sviews: the delivery audit trail). Several Spotlights on one packet fill oldest first.
|
|
// Not delivered on-site within deliverDays: status "finishing-in-network" (the Network Ad Space finish is NOT wired here yet).
|
|
// - Verified click: /go/<packet> with the visitor's verified token, once per visitor per packet per 24 h, never the planter's own.
|
|
'use strict';
|
|
const crypto = require('crypto');
|
|
const cfgMod = require('./config');
|
|
const store = require('./store');
|
|
|
|
const C = () => cfgMod.get();
|
|
const H24 = 24 * 3600000;
|
|
const lastVisitByVk = new Map(), ipVks = new Map(), viewSeen = new Map(), sviewSeen = new Map(), clickSeen = new Map();
|
|
function rebuild() {
|
|
lastVisitByVk.clear(); ipVks.clear(); sviewSeen.clear(); clickSeen.clear();
|
|
const cut = Date.now() - H24;
|
|
for (const v of store.visits()) if (v.at >= cut) { lastVisitByVk.set(v.vk, v.at); ipAdd(v.iph, v.vk, v.at); }
|
|
for (const b of store.sviews()) if (b.at >= cut) sviewSeen.set(b.vk + '|' + b.packetId, b.at);
|
|
for (const c of store.clicks()) if (c.at >= cut && c.verified) clickSeen.set(c.vk + '|' + c.packetId, c.at);
|
|
}
|
|
function init() { rebuild(); }
|
|
function ipAdd(iph, vk, at) { let m = ipVks.get(iph); if (!m) ipVks.set(iph, m = new Map()); m.set(vk, at); }
|
|
const fresh = (m, k) => { const t = m.get(k); return t && Date.now() - t < H24; };
|
|
|
|
// ---- Spotlights ----
|
|
function purchases() { return Object.entries(store.state().purchases || {}).map(([key, x]) => Object.assign({ key }, x)); }
|
|
const delivered = b => b.delivered || 0;
|
|
const remaining = b => Math.max(0, (b.views || 0) - delivered(b));
|
|
function spotsOf(packetId) { return purchases().filter(b => b.packetId === Number(packetId)).sort((a, b) => (a.at || 0) - (b.at || 0)); }
|
|
function activeSpot(packetId) { return spotsOf(packetId).find(b => remaining(b) > 0) || null; }
|
|
const showable = p => p && p.status === 'live';
|
|
function activeSpots() { const out = []; for (const b of purchases()) if (b.packetId && remaining(b) > 0 && showable(store.packet(b.packetId))) out.push(b); return out.sort((a, b) => (a.at || 0) - (b.at || 0)); }
|
|
// a newly credited Spotlight: the packet named in `ref` (any live packet: an owner or a promoter may spotlight it), else none yet
|
|
function attach(rec) {
|
|
const p = store.packet(Number(rec.ref));
|
|
rec.packetId = p && p.status !== 'removed' ? p.id : null; rec.delivered = rec.delivered || 0;
|
|
rec.dueAt = (rec.blockAt ? rec.blockAt * 1000 : rec.at) + C().spotlight.deliverDays * 86400000;
|
|
return rec.packetId;
|
|
}
|
|
function spotView(b) {
|
|
return { key: b.key, tx: String(b.key).split(':')[0], orderId: b.orderId, at: (b.blockAt ? b.blockAt * 1000 : b.at) || null, packetId: b.packetId || null, qty: b.qty, views: b.views,
|
|
delivered: delivered(b), remaining: remaining(b), dueAt: b.dueAt || null, finishedAt: b.finishedAt || null,
|
|
status: remaining(b) === 0 ? 'delivered' : !b.packetId ? 'waiting-for-packet' : (b.dueAt && Date.now() > b.dueAt ? 'finishing-in-network' : 'delivering') };
|
|
}
|
|
// k spotlit packets for the Spotlight places: furthest behind first, a little shuffle among the first 3k
|
|
function pickSpots(k, exclude, shownOk) {
|
|
if (k <= 0) return [];
|
|
const ex = exclude || new Set(); const seen = new Set(); const list = [];
|
|
for (const b of activeSpots()) { if (ex.has(b.packetId) || seen.has(b.packetId)) continue; const p = store.packet(b.packetId); if (shownOk && !shownOk(p)) continue; seen.add(b.packetId); list.push(b); }
|
|
list.sort((a, b) => delivered(a) / a.views - delivered(b) / b.views || (a.at || 0) - (b.at || 0));
|
|
const pool = list.slice(0, k * 3);
|
|
for (let i = pool.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [pool[i], pool[j]] = [pool[j], pool[i]]; }
|
|
return pool.slice(0, k).map(b => store.packet(b.packetId)).filter(Boolean);
|
|
}
|
|
function spotProgress(packetId) {
|
|
const l = spotsOf(packetId); if (!l.length) return null;
|
|
const views = l.reduce((t, b) => t + (b.views || 0), 0), done = l.reduce((t, b) => t + delivered(b), 0);
|
|
return { views, delivered: done, active: done < views };
|
|
}
|
|
function weekStats(week) {
|
|
const wk = week || store.ctWeek(); let vclicks = 0; for (const c of store.clicks()) if (c.verified && c.wk === wk) vclicks++;
|
|
let spots = 0; for (const b of purchases()) if (store.ctWeek(b.blockAt ? b.blockAt * 1000 : b.at) === wk) spots += Number(b.qty) || 1;
|
|
let saves = 0; for (const s of store.saves()) if (store.ctWeek(s.at) === wk) saves++;
|
|
return { week: wk, spotlights: spots, vclicks, saves };
|
|
}
|
|
|
|
// ---- verified visitors ----
|
|
function canVisit(vk, iph) {
|
|
if (fresh(lastVisitByVk, vk)) return { ok: true, again: true };
|
|
const m = ipVks.get(iph); let n = 0; if (m) for (const [k, t] of m) { if (Date.now() - t < H24) n++; else m.delete(k); }
|
|
return n >= C().perIpDay ? { ok: false, why: 'ip' } : { ok: true };
|
|
}
|
|
// t = { vk, iph, page, src, shown: [{ id, slot }], viewer }. slot 'spot' = a Spotlight place. -> { verified, why?, views, spotViews }
|
|
function verify(t) {
|
|
const chk = canVisit(t.vk, t.iph); if (!chk.ok) return { verified: false, why: chk.why };
|
|
const now = Date.now(); const day = store.ctDay(now), wk = store.ctWeek(now);
|
|
if (!chk.again) { lastVisitByVk.set(t.vk, now); ipAdd(t.iph, t.vk, now); store.addVisit({ at: now, vk: t.vk, iph: t.iph, page: t.page, src: t.src || '', wk, day }); store.state().totals.visits++; store.bump('visits'); }
|
|
let views = 0, spotViews = 0; const done = new Set();
|
|
for (const x of t.shown || []) {
|
|
const p = store.packet(x.id); if (!showable(p) || (t.viewer && p.owner === t.viewer)) continue;
|
|
const k = t.vk + '|' + p.id;
|
|
if (!done.has(p.id) && !fresh(viewSeen, k)) { viewSeen.set(k, now); if (viewSeen.size > 300000) viewSeen.clear(); p.views = (p.views || 0) + 1; views++; store.savePacket(p); }
|
|
done.add(p.id);
|
|
if (x.slot !== 'spot') continue;
|
|
const b = activeSpot(p.id);
|
|
if (b && !fresh(sviewSeen, k)) {
|
|
sviewSeen.set(k, now); const rec = store.state().purchases[b.key];
|
|
rec.delivered = (rec.delivered || 0) + 1; if (remaining(Object.assign({ key: b.key }, rec)) === 0 && !rec.finishedAt) rec.finishedAt = now;
|
|
store.addSview({ at: now, vk: t.vk, packetId: p.id, spot: b.key, page: t.page, day }); spotViews++;
|
|
}
|
|
}
|
|
if (views) { store.state().totals.views += views; store.bump('views', views); }
|
|
if (spotViews) { store.state().totals.sviews += spotViews; store.bump('sviews', spotViews); }
|
|
store.save();
|
|
return { verified: true, views, spotViews };
|
|
}
|
|
// a click on /go/<packet>. -> { verified }
|
|
function click(p, vk, verifiedVisitor, viewer, placement, saveId) {
|
|
p.clicks = (p.clicks || 0) + 1; store.state().totals.clicks++; store.bump('clicks');
|
|
let verified = false;
|
|
if (verifiedVisitor && fresh(lastVisitByVk, vk) && !(viewer && viewer === p.owner)) {
|
|
const k = vk + '|' + p.id;
|
|
if (!fresh(clickSeen, k)) {
|
|
clickSeen.set(k, Date.now()); verified = true; p.vclicks = (p.vclicks || 0) + 1; store.state().totals.vclicks++; store.bump('vclicks');
|
|
store.addClick({ vk, packetId: p.id, save: saveId || null, verified: true, p: String(placement || '').slice(0, 20), wk: store.ctWeek(), day: store.ctDay() });
|
|
if (saveId) { const s = store.state().saves.find(x => x.id === saveId); if (s) { s.clicks = (s.clicks || 0) + 1; store.putSave(s.packetId, s.owner, {}); } }
|
|
}
|
|
}
|
|
store.savePacket(p); return { verified };
|
|
}
|
|
let SECRET = crypto.randomBytes(16).toString('hex');
|
|
function setSecret(s) { SECRET = String(s); }
|
|
const vtoken = vk => crypto.createHmac('sha256', SECRET).update('vt|' + vk + '|' + store.ctDay()).digest('hex').slice(0, 24);
|
|
const vtokenOk = (vk, t) => typeof t === 'string' && t.length === 24 && crypto.timingSafeEqual(Buffer.from(t), Buffer.from(vtoken(vk)));
|
|
|
|
module.exports = { init, rebuild, purchases, spotsOf, activeSpot, activeSpots, attach, spotView, pickSpots, spotProgress, weekStats,
|
|
canVisit, verify, click, vtoken, vtokenOk, setSecret, remaining, delivered };
|