Files
instantadpay/nas.js
T
martbost 8c9d72a768 Spamhaus DBL cleanup (2026-11-17): close the ad-click redirector, mail links on the share domain
- /api/ads/click/<id> only redirects off-site for an active campaign (or its owner). Ended, paused,
  archived and flagged campaigns (488 of them, e.g. #164 -> shrt1.xyz) kept forwarding forever, a
  standing instantadpay.com redirector. They now land on the home page, uncounted.
- mailer.js rewrites every instantadpay.com URL in subject/text/html and List-Unsubscribe to
  MAIL_BASE (default https://instantadpay.link; '' = off). Email addresses and subdomains untouched.
- Share hosts answer /unsubscribe (GET + RFC 8058 POST) and /banners/* in place; leaderboard, blog,
  presentation, partners, launch, plays, bixpay, wall are pages, not usernames (none taken).
- NAS syndicated ad click URLs go out on instantadpay.link (NAS_CLICK_BASE) for new pushes.
- toolkit.js member links (AI writer, handout QR, split-test, partner promo) use shareDomain.
- my.js: banner "copy URL" on the share domain; bridge link (Wednesday/Friday) now builds from the
  share-form invite line (it silently fell back to the plain invite). my.js?v=20261117dbl.
iap-qa member: 0 bugs; networksso-partner e2e all passed on rerun.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 09:29:11 -06:00

148 lines
7.8 KiB
JavaScript

// NAS syndication — pushes IAP campaigns out to Network Ad Space (Marty's own
// EvolutionScript platform; no source, so its MySQL DB IS the API). Mirrors the
// CTB Rewards direct-write pattern: INSERT payments then sponsorads with
// approved:1 (bypasses NAS moderation), and reads `remaining` back to reconcile
// spend into IAP's unified credit pool.
//
// KNOWN NAS TRUTHS (measured across RM Circle + CTB, 1600+ ads):
// - sponsorads.remaining counts DOWN: served = assigned - remaining.
// - pid 1 = text, pid 2 = banner ONLY; banner size lives in width/height.
// - hits = clicks (not impressions). catid 5 = Cryptocurrencies.
// - NAS drifts its own counters upward post-insert → clamp served to assigned.
//
// FEATURE-FLAGGED: inert unless NAS_DB_HOST/USER/PASSWORD/NAME are all set.
// Nothing here runs (no connection, no writes) when the flag is off.
const crypto = require('crypto');
let pool = null;
function enabled() {
return !!(process.env.NAS_DB_HOST && process.env.NAS_DB_USER
&& process.env.NAS_DB_PASSWORD && process.env.NAS_DB_NAME);
}
function nasPool() {
if (pool) return pool;
const mysql = require('mysql2/promise');
pool = mysql.createPool({
host: process.env.NAS_DB_HOST,
port: Number(process.env.NAS_DB_PORT || 3306),
user: process.env.NAS_DB_USER,
password: process.env.NAS_DB_PASSWORD,
database: process.env.NAS_DB_NAME,
charset: 'latin1', // EvolutionScript is latin1
connectionLimit: 3,
connectTimeout: 10000
});
return pool;
}
async function q(sql, args) { const [r] = await nasPool().query(sql, args); return r; }
const CATID_CRYPTO = Number(process.env.NAS_CATID || 5);
// how many NAS impressions one IAP credit buys, per format. IAP charges credits
// on its own surfaces at these same rates, so NAS delivery draws the same pool.
function impressionsPerCredit(type) {
return type === 'banner' ? 5 : type === 'text' ? 10 : type === 'video' ? 0 : 0;
}
function nasKind(type) {
// pid encodes text(1)/banner(2); adtype is a separate small int — 1 is the
// value the overwhelming majority of live NAS rows use for both formats.
const adtype = Number(process.env.NAS_ADTYPE || 1);
if (type === 'banner') return { pid: 2, adtype };
if (type === 'text') return { pid: 1, adtype };
return null; // only banner/text syndicate to NAS in v1 (login/solo/video are IAP-native)
}
// push one IAP campaign into NAS. `c` is a pubC-shaped campaign. Returns
// { nasAdId, assigned } or { skipped } / throws on a real DB error.
async function pushCampaign(c, opts = {}) {
if (!enabled()) return { skipped: 'flag-off' };
const kind = nasKind(c.type);
if (!kind) return { skipped: 'type' };
const budgetLeft = c.budget - (c.spent || 0);
const window = c.dailyCap ? Math.min(c.dailyCap, budgetLeft) : budgetLeft; // capped campaigns start with one day's allowance
const assigned = Math.max(1, Math.floor(window * impressionsPerCredit(c.type)));
const token = 'iap_' + crypto.randomBytes(8).toString('hex'); // manage token = Username
const now = new Date();
const days = Number(opts.days || 30);
const edate = new Date(now.getTime() + days * 86400000);
const payref = 'iap_campaign:' + c.id;
// payments first (MyISAM, no txn) — hand-rollback the row if sponsorads fails
const pay = await q(
'INSERT INTO payments (Username, Amount, Currency_code, status, Date, pay_address) VALUES (?,?,?,1,?,?)',
[token, 0, 'IAP_CREDIT', now, payref]);
try {
const ad = await q(
`INSERT INTO sponsorads
(Username, Subject, Body, WebsiteURL, assigned, remaining, hits, approved, Date, adtype,
Name, Email, PaymentDetails, EDate, sp, width, height, BannerURL, pid, ref_by, catid)
VALUES (?,?,?,?,?,?,0,1,?,?,?,?,?,?,'',?,?,?,?,0,?)`,
// clicks route through our redirect so they count and carry the network site as the referrer
[token, c.title || null, c.type === 'text' ? (c.body || null) : null, (process.env.NAS_CLICK_BASE || 'https://instantadpay.link') + '/api/ads/click/' + c.id,
assigned, assigned, now, kind.adtype,
opts.name || 'InstantAdPay member', opts.email || 'ads@instantadpay.com',
'InstantAdPay campaign #' + c.id, edate,
c.width || '', c.height || '', c.type === 'banner' ? c.imageUrl : null, kind.pid, CATID_CRYPTO]);
return { nasAdId: ad.insertId, manageToken: token, assigned };
} catch (e) {
try { await q('DELETE FROM payments WHERE ID=?', [pay.insertId]); } catch (e2) {}
throw e;
}
}
// read served count for a syndicated ad (assigned - remaining, clamped ≥0 and
// ≤ assigned because NAS drifts counters upward post-insert).
async function readServed(nasAdId) {
if (!enabled()) return null;
const rows = await q('SELECT assigned, remaining, hits FROM sponsorads WHERE ID=?', [Number(nasAdId)]);
if (!rows.length) return null;
const assigned = Number(rows[0].assigned) || 0;
const remaining = Number(rows[0].remaining) || 0;
const served = Math.max(0, Math.min(assigned, assigned - remaining));
return { assigned, remaining, served, clicks: Number(rows[0].hits) || 0 };
}
// Stop a syndicated ad (budget spent, paused, or expired). NAS has no stop flag: its
// serving query only picks rows with remaining>0, so zeroing that counter is the only
// way to halt an ad.
//
// THE TRAP (found 2026-09-17): served is derived as assigned-remaining, so zeroing also
// makes a stopped ad read back as 100% DELIVERED. That inflated every stopped campaign's
// reported delivery, and worse, reconcileNas charges members for the difference — so
// pausing a campaign could bill for impressions that were never served. A naturally
// exhausted ad also ends at remaining=0, so the two cases are indistinguishable
// afterwards. The true figure therefore has to be captured BEFORE the stop.
async function deactivate(nasAdId) {
if (!enabled()) return { skipped: 'flag-off' };
let served = null;
try { const s = await readServed(nasAdId); if (s) served = s.served; } catch (e) {}
await q('UPDATE sponsorads SET remaining=0, EDate=NOW() WHERE ID=?', [Number(nasAdId)]);
return { ok: true, served }; // callers MUST persist this as the final served count
}
// top up a syndicated ad with more impressions (buy-more-views / reactivate).
// point an existing NAS ad at our click redirect (migration for rows pushed before 2026-09-10)
async function setClickUrl(nasAdId, campaignId) {
if (!enabled()) return { skipped: 'flag-off' };
await q('UPDATE sponsorads SET WebsiteURL=? WHERE ID=?', [(process.env.NAS_CLICK_BASE || 'https://instantadpay.link') + '/api/ads/click/' + Number(campaignId), Number(nasAdId)]);
return { ok: true };
}
async function topUp(nasAdId, addImpressions, days) {
if (!enabled()) return { skipped: 'flag-off' };
await q(`UPDATE sponsorads SET assigned=assigned+?, remaining=remaining+?, approved=1,
EDate=DATE_ADD(NOW(), INTERVAL ? DAY) WHERE ID=?`,
[Number(addImpressions), Number(addImpressions), Number(days || 30), Number(nasAdId)]);
return { ok: true };
}
// restart a STOPPED ad (deactivate zeroed `remaining`) with `addImpressions` more, re-basing `assigned` on the TRUE served
// count we recorded at the stop. A plain topUp would leave assigned - remaining = the whole old allocation, so the
// undelivered part would read back as served (the same trap as deactivate, one step later).
async function restart(nasAdId, trueServed, addImpressions, days) {
if (!enabled()) return { skipped: 'flag-off' };
const add = Math.max(0, Math.floor(Number(addImpressions) || 0)), base = Math.max(0, Math.floor(Number(trueServed) || 0));
await q(`UPDATE sponsorads SET assigned=?, remaining=?, approved=1, EDate=DATE_ADD(NOW(), INTERVAL ? DAY) WHERE ID=?`,
[base + add, add, Number(days || 30), Number(nasAdId)]);
return { ok: true };
}
module.exports = { setClickUrl, enabled, pushCampaign, readServed, deactivate, topUp, restart, impressionsPerCredit, nasKind };