Files
banner-line/dlbnas.js
T
martbost a428075330 Spot edits: unlimited, full link check on every one, every one logged; admin takedown and restore (Marty 2026-11-16)
Edits (POST /api/my/spot/<id>, /api/my/default, uploads): still any number of times. Every change now gets the full check:
checkLink, then (LINK_PROBE on) the link must answer (safety.probeLink, up to 3 redirects each checked) and an image address must
download as a real 468x60 image of 150 KB or less (safety.probeImage; the address is kept, a Banner Spin image rotates). The
Banner Spin image + click link LinkSpin just handed the member are not fetched (that would count a LinkSpin click). safety.js gains
privateIp + safeLookup with the Node 20 fix VBB/ViralAdBlitz needed (honors opts.all, answers an array). Every real change is
logged on the spot (or the default): old and new link and image, time, via edit/upload; the owner sees the newest 10. 120 changes
an hour per connection (each one fetches the link).

Admin takedown (the Viral Banner Blitz way, 8e6d2f9): /admin (new page) finds a spot by #number, wallet or network username;
POST /api/admin/spots/<id>/takedown {reason (required, the owner sees it), credit}. Hidden everywhere at once: the board shows a
house tile in its place (nobody else moves), the gate gives its step to a house site, /go/<id> goes home, its Network Ad Space
banner is paused (approved = 0) and no new one is placed. No views or clicks count while down. My Spots shows "Taken down" with
the reason and no edit controls. Restore (POST .../restore) reverses it (NAS resumed). Admin ledger: GET /api/admin/actions (who,
when, reason, credit decision). The contract is never touched.
CREDIT BACK IS NOT BUILT: a spot is a $5 on-chain purchase and this site has no free replacement spot or post credit, so
credit: "back" is refused with a note until Marty decides what it means here; takedowns are "no credit".
Chatbot: CANNED takedown + banner answer (unlimited checked, logged edits) + FACTS EDITS/TAKEDOWN lines, KNOWLEDGE_DATE 2026-11-16.
line.css?v=9, my.js?v=z3.
Tests: smoke section 10 (the lookup shape, edit log, unlimited edits, refusals: unreachable, 404, private address, http redirect,
shortener, bad image size/404/unreachable; redirect followed; same link logs nothing; upload logged; default checked + logged;
takedown: token, reason, credit choice, credit back refused, held house tile, image nowhere, /go home, gate, no views, owner view,
no edits while down, ledger, restore, /admin) against a stand-in internet preloaded into the app (qa/out/probe-stub.cjs, nothing
leaves the machine), plus phone renders of My Spots taken down and /admin: 193/193 with UI. chatbot.test 181/181.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 05:23:54 -06:00

81 lines
6.4 KiB
JavaScript

// Network Ad Space for Downline Banners (SPEC "delivery": 300 guaranteed views per spot within 30 days, then Network Ad Space
// finishes it; Marty 2026-11-13 "proceed"). Same direct-write pattern as InstantAdPay's nas.js (NAS is Marty's own EvolutionScript
// site; its MySQL is the API): INSERT payments, then sponsorads with approved=1, and read `remaining` back.
// - house: one standing 468x60 banner for downlinebanners.com, a modest allowance (HOUSE_IMPS), topped up when it runs low,
// so the board gets traffic without crowding InstantAdPay members' paid ads out of NAS.
// - spots: a spot past its 30 days with fewer than 300 views gets its own banner placed in NAS for exactly the views still owed;
// what NAS serves counts toward the spot's 300 (served(spotId)).
// - a spot the admin took down (2026-11-16) gets no new NAS banner, and its running one is paused (approved = 0, so NAS serves
// none of it and no views count) until it is restored (approved = 1).
// State in dlb-nas.json (a MySQL document through docstore). Inert unless NAS_DB_* are set.
'use strict';
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const cfgMod = require('./config');
const HOUSE_IMPS = 25000, HOUSE_LOW = 3000, CATID_CRYPTO = 5;
let pool = null, S = null, R = null;
const on = () => !!(process.env.NAS_DB_HOST && process.env.NAS_DB_USER && process.env.NAS_DB_PASSWORD && process.env.NAS_DB_NAME);
const FILE = () => path.join(cfgMod.get().dataDir, 'dlb-nas.json');
function load() { if (S) return S; try { S = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { S = {}; } S.spots = S.spots || {}; return S; }
function save() { try { fs.writeFileSync(FILE(), JSON.stringify(load())); } catch (e) { console.error('dlbnas save', e.message); } }
function q(sql, args) {
if (!pool) pool = require('mysql2/promise').createPool({ host: process.env.NAS_DB_HOST, user: process.env.NAS_DB_USER, password: process.env.NAS_DB_PASSWORD,
database: process.env.NAS_DB_NAME, port: Number(process.env.NAS_DB_PORT || 3306), connectionLimit: 2, connectTimeout: 10000 });
return pool.query(sql, args).then(r => r[0]);
}
async function push({ img, link, assigned, title, ref }) {
const token = 'dlb_' + crypto.randomBytes(8).toString('hex'); const now = new Date(); const edate = new Date(now.getTime() + 60 * 86400000);
const pay = await q('INSERT INTO payments (Username, Amount, Currency_code, status, Date, pay_address) VALUES (?,?,?,1,?,?)', [token, 0, 'DLB', now, ref]);
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 (?,?,NULL,?,?,?,0,1,?,1,?,?,?,?,'',?,?,?,2,0,?)`,
[token, title, link, assigned, assigned, now, 'Downline Banners', 'ads@downlinebanners.com', ref, edate, '468', '60', img, CATID_CRYPTO]);
return { id: ad.insertId, assigned };
} catch (e) { try { await q('DELETE FROM payments WHERE ID=?', [pay.insertId]); } catch (x) {} throw e; }
}
async function served(id) { const r = await q('SELECT assigned, remaining FROM sponsorads WHERE ID=?', [Number(id)]); if (!r.length) return null; return Math.max(0, Math.min(r[0].assigned, r[0].assigned - r[0].remaining)); }
async function tick() {
if (!on() || !R) return;
const c = cfgMod.get(); const s = load(); const site = 'https://' + c.site;
try {
// house banner: keep one running, refill when low
if (s.house) { const sv = await served(s.house.id); if (sv != null) s.house.served = sv; }
if (!s.house || (s.house.assigned - (s.house.served || 0)) < HOUSE_LOW) {
const ad = await push({ img: site + '/art/house-strip-nas.png', link: site + '/?src=nas', assigned: HOUSE_IMPS, title: 'Downline Banners: $5 banner spots, paid on-chain', ref: 'dlb_house' });
s.houseHistory = (s.houseHistory || []).concat(s.house ? [s.house] : []); s.house = Object.assign(ad, { served: 0, at: Date.now() });
console.log('dlbnas: house banner placed', ad.id, ad.assigned);
}
// spots past 30 days that still owe views
for (const sp of R.spots()) {
const id = String(sp.id); const owed = c.viewsOwed - R.views(sp.id);
const rec = s.spots[id];
if (rec) { const sv = await served(rec.id); if (sv != null) rec.served = sv; if (R.isDown && R.isDown(sp.id) && !rec.paused) await pause(sp.id); continue; }
if (R.isDown && R.isDown(sp.id)) continue; // taken down: no NAS banner while it is down
if (sp.kind === 3 || owed <= 0 || Date.now() - sp.boughtAt < c.spotDays * 86400000) continue;
const cr = R.creative(sp); let img = cr.img || ''; if (img.startsWith('/')) img = site + img;
if (!/^https:\/\/.+\.(png|jpe?g|gif)(\?.*)?$/i.test(img) && !/\/i\/[a-z0-9]+\.gif/i.test(img)) img = site + '/art/house-strip-nas.png';
const ad = await push({ img, link: site + '/go/' + id, assigned: owed, title: 'Spot #' + id, ref: 'dlb_spot:' + id });
s.spots[id] = Object.assign(ad, { served: 0, at: Date.now() }); console.log('dlbnas: spot', id, 'finishing', owed, 'views in NAS, ad', ad.id);
}
s.lastOk = Date.now(); delete s.lastErr;
} catch (e) { s.lastErr = String(e.message).slice(0, 200); console.error('dlbnas', e.message); }
save();
}
// takedown / restore (2026-11-16): pause or resume a spot's running NAS banner. -> 'paused' | 'resumed' | 'none' | 'off'
async function pause(id) {
const rec = load().spots[String(id)]; if (!rec) return 'none'; if (!on()) return 'off';
await q('UPDATE sponsorads SET approved = 0 WHERE ID = ?', [Number(rec.id)]); rec.paused = Date.now(); save(); return 'paused';
}
async function resume(id) {
const rec = load().spots[String(id)]; if (!rec || !rec.paused) return 'none'; if (!on()) return 'off';
await q('UPDATE sponsorads SET approved = 1 WHERE ID = ?', [Number(rec.id)]); delete rec.paused; save(); return 'resumed';
}
// views NAS delivered for a spot (counted toward its 300)
function spotServed(id) { const r = load().spots[String(id)]; return r ? (r.served || 0) : 0; }
function init(deps) { R = deps; if (!on()) { console.log('dlbnas: off (no NAS_DB_*)'); return; } load(); setTimeout(() => tick().catch(() => {}), 30000); const t = setInterval(() => tick().catch(() => {}), 6 * 3600000); t.unref && t.unref(); }
function view() { const s = load(); return { on: on(), house: s.house || null, spots: s.spots, lastOk: s.lastOk || null, lastErr: s.lastErr || null }; }
module.exports = { init, tick, spotServed, view, on, pause, resume };