One swap per banner + admin takedown (Marty 2026-11-15: "allow the member to swap it only, and they can only do it one time. Admin takes down the option to credit back or none.")
Member swap: My Banners shows "Swap image or link" on a banner that is waiting or in the loop, usable once (then "Swap used"). POST /api/my/banners/<id>/swap changes the image and/or link (or a Banner Spin to another of the member's own 125x125/250x250 spins via lsspins), same checks as a new post (checkLink, probeLink, upload/https image checks); id, place, clicks and loop progress stay. Logged on the banner (swaps: old/new image + link, time). No withdraw, removal or refund for members.
Admin takedown: /admin finds banners (#id, wallet, username), POST /api/admin/banners/<id>/takedown {reason required, credit: back|none}. The banner is hidden at once (splash loop + waiting line show a house tile in its place, /go goes home, no look/teaser/swap); its place rides out on time so nobody moves, then it ends with no cycle, no network views, no prize count. My Banners shows "Taken down" with the reason. Restore reverses a given credit (when the owner still holds one) and, if its place rode out meanwhile, records the cycle then. Admin ledger: GET /api/admin/actions (who, when, reason, credit decision).
Chatbot: CANNED 'swap' + FACTS line (KNOWLEDGE_DATE already 2026-11-15), chat-qa entry. my.js?v=4, admin.js?v=3, vbb.css?v=8.
Tests: smoke section 15 (swap once, refused twice, keeps place, bad link/image refused, spin swap, takedown credit back vs none, others keep places, ride-out without a cycle, restore, admin ledger, phone UI): 308/308 with the committed networksso.js; chatbot.test 182/182; qa:chain 49/49.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -167,7 +167,8 @@ function grantDaily(m) {
|
||||
function myData(addr) {
|
||||
const m = store.member(addr);
|
||||
const banners = store.bannersOf(addr).slice().reverse().map(b => Object.assign({ id: b.id, img: b.img, link: b.link, createdAt: b.createdAt, cycledAt: b.cycledAt || null, clicks: b.clicks || 0,
|
||||
look: !!b.look, teaser: b.look ? (b.teaser || '') : '', teaserDefault: b.spin ? b.spin.name : loop.hostOf(b.link), spin: b.spin ? { name: b.spin.name, size: b.spin.size } : null }, loop.placeOf(b)));
|
||||
look: !!b.look, teaser: b.look ? (b.teaser || '') : '', teaserDefault: b.spin ? b.spin.name : loop.hostOf(b.link), spin: b.spin ? { name: b.spin.name, size: b.spin.size } : null,
|
||||
swapUsed: swapUsed(b), swappedAt: swapUsed(b) ? b.swaps[b.swaps.length - 1].at : null, canSwap: canSwap(b) }, loop.memberPlace(b)));
|
||||
const rewards = store.ledger().filter(e => e.owner === m.address);
|
||||
const shareName = m.username || m.address;
|
||||
return { address: m.address, username: m.username, credits: m.credits, freeCreditCap: C.freeCreditCap, freeToday: m.freeDay === store.ctDay(), netVerified: netVerified(m),
|
||||
@@ -175,6 +176,11 @@ function myData(addr) {
|
||||
week: loop.memberWeek(m.address), share: 'https://' + C.site + '/?ref=' + encodeURIComponent(shareName) };
|
||||
}
|
||||
|
||||
// one swap per banner (Marty 2026-11-15: "allow the member to swap it only, and they can only do it one time"): image and/or link,
|
||||
// while the banner is waiting or in the loop and not taken down. No withdraw, no removal, no refund for members.
|
||||
const swapUsed = b => Array.isArray(b.swaps) && b.swaps.length > 0;
|
||||
const canSwap = b => !swapUsed(b) && !b.down && (b.state === 'queued' || b.state === 'loop');
|
||||
|
||||
// ---- visit tickets: issued on page load, redeemed after the page has been visible for visitSeconds ----
|
||||
const tickets = new Map();
|
||||
const srcHost = raw => { try { const u = new URL(String(raw || '')); if (!/^https?:$/.test(u.protocol)) return ''; const h = u.hostname.toLowerCase().replace(/^www\./, ''); return h === C.site || h.endsWith('.' + C.site) || !h.includes('.') ? '' : h.slice(0, 80); } catch (e) { return ''; } };
|
||||
@@ -261,7 +267,7 @@ const server = http.createServer(async (req, res) => {
|
||||
// click relay: /go/<bannerId> counts one click per visitor per banner per hour, then sends the visitor to the banner's link
|
||||
const gm = /^\/go\/(\d{1,9})$/.exec(p);
|
||||
if (gm) {
|
||||
const b = store.banner(gm[1]); if (!b) { res.writeHead(302, { Location: '/' }); return res.end(); }
|
||||
const b = store.banner(gm[1]); if (!b || b.down) { res.writeHead(302, { Location: '/' }); return res.end(); } // taken down: never sends anyone to it
|
||||
if (!isBot(req)) {
|
||||
const k = visitorKey(req) + ':' + b.id; const t = clickSeen.get(k);
|
||||
if (!t || Date.now() - t > 3600000) { clickSeen.set(k, Date.now()); if (clickSeen.size > 100000) clickSeen.clear(); b.clicks = (b.clicks || 0) + 1; store.saveBanner(b); store.bump('clicks'); }
|
||||
@@ -390,6 +396,7 @@ const server = http.createServer(async (req, res) => {
|
||||
if (sku === C.lookSku) {
|
||||
const bn = store.banner(b.ref);
|
||||
if (!bn || bn.owner !== me) return json(res, 400, { error: 'That banner is not yours.' });
|
||||
if (bn.down) return json(res, 400, { error: 'That banner was taken down.' });
|
||||
if (bn.look) return json(res, 400, { error: 'That banner already has the look.' });
|
||||
if (bn.state === 'cycled') return json(res, 400, { error: 'That banner has already cycled. Add the look to a banner that is waiting or in the loop.' });
|
||||
if (qty !== 1) return json(res, 400, { error: 'One look per banner.' });
|
||||
@@ -409,6 +416,7 @@ const server = http.createServer(async (req, res) => {
|
||||
const tm = /^\/api\/my\/banners\/(\d{1,9})\/teaser$/.exec(p);
|
||||
if (tm && req.method === 'POST') {
|
||||
const bn = store.banner(tm[1]); if (!bn || bn.owner !== me) return json(res, 404, { error: 'Not found' });
|
||||
if (bn.down) return json(res, 400, { error: 'That banner was taken down.' });
|
||||
if (!bn.look) return json(res, 400, { error: 'Add the banner look first.' });
|
||||
const t = String((await readBody(req)).teaser || '').replace(/[\u0000-\u001f\u007f<>]/g, '').replace(/\s+/g, ' ').trim();
|
||||
if (t.length > 40) return json(res, 400, { error: 'Keep the teaser to 40 characters.' });
|
||||
@@ -420,6 +428,43 @@ const server = http.createServer(async (req, res) => {
|
||||
const r = await lsspins.list(me, lsName(me)); if (r.error) return json(res, 502, r);
|
||||
return json(res, 200, { account: !!r.account, username: r.username || null, spins: (r.spins || []).filter(x => ['125x125', '250x250'].includes(x.size)).map(x => ({ id: x.id, name: x.name, size: x.size || null, paused: !!x.paused, banners: x.banners || 0 })) });
|
||||
}
|
||||
// swap the image and/or link of a banner that is waiting or in the loop, once per banner. The banner keeps its id, place, clicks
|
||||
// and cycle progress. The new image and link get exactly the checks a new post gets. A Banner Spin swap ({ spinId }) takes the
|
||||
// image + link from LinkSpin's signed answer for another of the member's own 125x125/250x250 spins. Logged on the banner.
|
||||
const sw = /^\/api\/my\/banners\/(\d{1,9})\/swap$/.exec(p);
|
||||
if (sw && req.method === 'POST') {
|
||||
if (limited(req, 'swap', 20, 3600000)) return json(res, 429, { error: 'Too many tries this hour.' });
|
||||
const bn = store.banner(sw[1]); if (!bn || bn.owner !== me) return json(res, 404, { error: 'Not found' });
|
||||
if (swapUsed(bn)) return json(res, 400, { error: 'This banner already used its one swap.' });
|
||||
if (bn.down) return json(res, 400, { error: 'That banner was taken down.' });
|
||||
if (bn.state !== 'queued' && bn.state !== 'loop') return json(res, 400, { error: 'Only a banner that is waiting or in the loop can be swapped.' });
|
||||
const b = await readBody(req); let next;
|
||||
if (b.spinId != null) {
|
||||
const r = await lsspins.use(me, lsName(me), b.spinId);
|
||||
if (r.error || !r.use) return json(res, 400, { error: r.error || (r.account === false ? 'No LinkSpin account holds this wallet yet.' : 'Could not use that Banner Spin.') });
|
||||
if (!['125x125', '250x250'].includes(r.use.size)) return json(res, 400, { error: 'Use a 125x125 (or 250x250) Banner Spin.' });
|
||||
const ic = safety.checkLink(r.use.img, 'Banner Spin image'); const sl = safety.checkLink(r.use.link, 'Banner Spin link');
|
||||
if (ic.error || sl.error || new URL(ic.url).hostname !== new URL(sl.url).hostname) return json(res, 400, { error: 'LinkSpin sent a link we cannot use. Try again in a minute.' });
|
||||
next = { img: ic.url, link: sl.url, spin: { id: Number(r.use.spinId), name: String(r.use.name || '').slice(0, 60), size: r.use.size } };
|
||||
if (bn.spin && bn.spin.id === next.spin.id) return json(res, 400, { error: 'That is the Banner Spin this banner already shows. Pick another one.' });
|
||||
} else {
|
||||
const hasImg = String(b.img || '').trim() !== '', hasLink = String(b.link || '').trim() !== '';
|
||||
if (!hasImg && !hasLink) return json(res, 400, { error: 'Add a new image, a new link, or both.' });
|
||||
if (bn.spin && !(hasImg && hasLink)) return json(res, 400, { error: 'A Banner Spin swaps to another Banner Spin, or to your own image and link together.' });
|
||||
let link = bn.link, img = bn.img;
|
||||
if (hasLink) { const lk = safety.checkLink(b.link, 'Target link'); if (lk.error) return json(res, 400, lk); link = lk.url; }
|
||||
if (hasImg) { const im = await checkedImage(me, b.img); if (im.error) return json(res, 400, im); img = im.img; }
|
||||
if (hasLink && C.linkProbe) { const pr = await safety.probeLink(link); if (pr.error) return json(res, 400, pr); }
|
||||
if (link === bn.link && img === bn.img) return json(res, 400, { error: 'That is the image and link the banner already has.' });
|
||||
next = { img, link, spin: null };
|
||||
}
|
||||
const cur = store.banner(sw[1]); // re-checked after the awaits (another tab, or the admin, may have acted meanwhile)
|
||||
if (swapUsed(cur) || !canSwap(cur)) return json(res, 400, { error: swapUsed(cur) ? 'This banner already used its one swap.' : 'This banner can no longer be swapped.' });
|
||||
cur.swaps = [{ at: Date.now(), old: { img: cur.img, link: cur.link, spin: cur.spin || null }, new: { img: next.img, link: next.link, spin: next.spin } }];
|
||||
cur.img = next.img; cur.link = next.link; if (next.spin) cur.spin = next.spin; else delete cur.spin;
|
||||
store.saveBanner(cur);
|
||||
return json(res, 200, { ok: true, banner: Object.assign({ id: cur.id, img: cur.img, link: cur.link, spin: cur.spin ? { name: cur.spin.name, size: cur.spin.size } : null, swapUsed: true }, loop.placeOf(cur)) });
|
||||
}
|
||||
if (p === '/api/my/upload' && req.method === 'POST') {
|
||||
if (limited(req, 'upload', 40, 3600000)) return json(res, 429, { error: 'Too many uploads this hour.' });
|
||||
let buf; try { buf = await readRaw(req, safety.MAX_UPLOAD + 1); } catch (e) { return json(res, 400, { error: 'The file is over 100 KB.' }); }
|
||||
@@ -430,7 +475,7 @@ const server = http.createServer(async (req, res) => {
|
||||
if (limited(req, 'post', 30, 3600000)) return json(res, 429, { error: 'Too many posts this hour.' });
|
||||
const b = await readBody(req); const m = store.member(me);
|
||||
if (m.credits < 1) return json(res, 400, { error: 'You have no post credits. A free credit arrives each day you sign in.' });
|
||||
const waiting = store.bannersOf(me).filter(x => x.state === 'queued').length;
|
||||
const waiting = store.bannersOf(me).filter(x => x.state === 'queued' && !x.down).length;
|
||||
if (waiting >= C.maxQueuedPerMember) return json(res, 400, { error: 'You already have ' + waiting + ' banners waiting. Post again when one enters the loop.' });
|
||||
// a LinkSpin Banner Spin (2026-11-14): image + link come from LinkSpin's signed answer, never from the browser. The image is the
|
||||
// spin's rotating image on LinkSpin (not copied: it changes on every view) and the link its click link (clicks count in
|
||||
@@ -447,14 +492,8 @@ const server = http.createServer(async (req, res) => {
|
||||
return json(res, 200, { ok: true, banner: Object.assign({ id: nb.id }, loop.placeOf(nb)), credits: m.credits, otherSource: !!r.use.otherSource });
|
||||
}
|
||||
const lk = safety.checkLink(b.link, 'Target link'); if (lk.error) return json(res, 400, lk);
|
||||
let img = String(b.img || '').trim();
|
||||
if (/^\/u\/[a-z0-9-]+\.(png|jpg|gif)$/.test(img)) {
|
||||
if (!img.slice(3).startsWith(me.slice(2, 10) + '-') || !fs.existsSync(path.join(UPL, img.slice(3)))) return json(res, 400, { error: 'Upload the image again.' });
|
||||
} else {
|
||||
const ic = safety.checkLink(img, 'Image address'); if (ic.error) return json(res, 400, ic);
|
||||
const f = await safety.fetchImage(ic.url); if (f.error) return json(res, 400, f);
|
||||
img = saveUpload(me, f.buf, f.info.ext);
|
||||
}
|
||||
const im = await checkedImage(me, b.img); if (im.error) return json(res, 400, im);
|
||||
const img = im.img;
|
||||
if (C.linkProbe) { const pr = await safety.probeLink(lk.url); if (pr.error) return json(res, 400, pr); }
|
||||
if (store.member(me).credits < 1) return json(res, 400, { error: 'You have no post credits.' }); // spent meanwhile by another tab
|
||||
m.credits -= 1;
|
||||
@@ -502,6 +541,51 @@ const server = http.createServer(async (req, res) => {
|
||||
const per = {}; for (const e of L) { const k = e.owner; const r = per[k] || (per[k] = { address: k, username: e.username, cycles: 0, views: 0 }); r.cycles++; r.views += e.views; if (e.username) r.username = e.username; }
|
||||
return json(res, 200, { entries: L.slice(-n).reverse(), members: Object.values(per).sort((a, b) => b.views - a.views), note: 'v1 records rewards only; nothing has been credited to InstantAdPay or LinkSpin.' });
|
||||
}
|
||||
// ---- banner takedown (Marty 2026-11-15: "Admin takes down the option to credit back or none") ----
|
||||
// GET ?q=#<id> | <id> | <wallet> | <username>: the banners with their swap + takedown history
|
||||
if (p === '/api/admin/banners' && req.method === 'GET') {
|
||||
const q = String(u.searchParams.get('q') || '').trim(); let list = [];
|
||||
if (/^#?\d{1,9}$/.test(q)) { const b = store.banner(Number(q.replace('#', ''))); if (b) list = [b]; }
|
||||
else if (ADDR_RE.test(q)) list = store.bannersOf(q);
|
||||
else if (NAME_RE.test(q.replace(/^@/, ''))) { const m = store.memberByName(q.replace(/^@/, '')); if (m) list = store.bannersOf(m.address); }
|
||||
if (!list.length) return json(res, 404, { error: 'No banner found for that.' });
|
||||
return json(res, 200, { banners: list.slice().reverse().slice(0, 100).map(adminBanner) });
|
||||
}
|
||||
const tk = /^\/api\/admin\/banners\/(\d{1,9})\/(takedown|restore)$/.exec(p);
|
||||
if (tk && req.method === 'POST') {
|
||||
const bn = store.banner(tk[1]); if (!bn) return json(res, 404, { error: 'No such banner.' });
|
||||
const b = await readBody(req); const by = String(b.by || 'admin').replace(/[^\w .@-]/g, '').slice(0, 40) || 'admin';
|
||||
const reason = String(b.reason || '').replace(/[\u0000-\u001f\u007f<>]/g, '').replace(/\s+/g, ' ').trim();
|
||||
const m = store.member(bn.owner);
|
||||
if (tk[2] === 'takedown') {
|
||||
if (bn.down) return json(res, 400, { error: 'That banner is already taken down.' });
|
||||
if (bn.state !== 'queued' && bn.state !== 'loop') return json(res, 400, { error: 'Only a banner that is waiting or in the loop can be taken down (this one is ' + bn.state + ').' });
|
||||
if (reason.length < 3 || reason.length > 300) return json(res, 400, { error: 'A reason is required (3 to 300 characters). The owner sees it.' });
|
||||
if (b.credit !== 'back' && b.credit !== 'none') return json(res, 400, { error: 'Choose credit: "back" (the owner gets the post credit back) or "none".' });
|
||||
const back = b.credit === 'back' && !!m;
|
||||
bn.down = { at: Date.now(), by, reason, creditBack: back, slot: bn.state };
|
||||
if (back) { m.credits += 1; store.save(); }
|
||||
store.saveBanner(bn);
|
||||
const e = adminLog({ action: 'takedown', bannerId: bn.id, owner: bn.owner, username: (m && m.username) || null, by, reason, credit: back ? 'back' : 'none', slot: bn.state });
|
||||
return json(res, 200, { ok: true, banner: adminBanner(bn), log: e, ownerCredits: m ? m.credits : null });
|
||||
}
|
||||
// restore a mistaken takedown: the banner shows again; a credit that was given back is taken back
|
||||
if (!bn.down) return json(res, 400, { error: 'That banner is not taken down.' });
|
||||
const was = bn.down; let reversed = false, short = false, cycle = null;
|
||||
if (was.creditBack && m) { if (m.credits >= 1) { m.credits -= 1; reversed = true; store.save(); } else short = true; }
|
||||
(bn.downs || (bn.downs = [])).push(Object.assign({}, was, { restoredAt: Date.now(), restoredBy: by, restoreReason: reason || null, creditReversed: reversed }));
|
||||
delete bn.down;
|
||||
// its place rode out while it was down: it gets the cycle it would have had
|
||||
if (bn.state === 'ended') { bn.state = 'cycled'; bn.cycledAt = Date.now(); delete bn.endedAt; cycle = loop.recordCycle(bn, { note: 'restored' }); }
|
||||
store.saveBanner(bn);
|
||||
const e = adminLog({ action: 'restore', bannerId: bn.id, owner: bn.owner, username: (m && m.username) || null, by, reason: reason || null,
|
||||
credit: was.creditBack ? (reversed ? 'taken back' : 'already used, not taken back') : 'none given', cycleRecorded: !!cycle });
|
||||
return json(res, 200, { ok: true, banner: adminBanner(bn), log: e, creditReversed: reversed, creditAlreadyUsed: short, cycle, ownerCredits: m ? m.credits : null });
|
||||
}
|
||||
if (p === '/api/admin/actions' && req.method === 'GET') {
|
||||
const n = Math.min(500, Math.max(1, Number(u.searchParams.get('n')) || 100));
|
||||
return json(res, 200, { actions: (store.state().adminLog || []).slice(-n).reverse() });
|
||||
}
|
||||
// the network hub's member lookup (Marty 2026-11-14, read-only). GET ?q=<wallet> | #<banner id> | <username>. No email lives here.
|
||||
if (p === '/api/admin/member-lookup' && req.method === 'GET') {
|
||||
const q = String(u.searchParams.get('q') || '').trim(); let a = null, via = null;
|
||||
@@ -529,6 +613,23 @@ const server = http.createServer(async (req, res) => {
|
||||
});
|
||||
// the network username LinkSpin may look the member up by when no LinkSpin account holds this wallet: network-verified members only
|
||||
const lsName = addr => { const m = store.member(addr); return m && netVerified(m) && m.username ? m.username : null; };
|
||||
// admin views of a banner + the admin action ledger (takedowns and restores: who, when, reason, credit decision)
|
||||
function adminBanner(b) { const m = store.member(b.owner);
|
||||
return Object.assign({ id: b.id, owner: b.owner, username: (m && m.username) || null, img: b.img, link: b.link, spin: b.spin || null, createdAt: b.createdAt, clicks: b.clicks || 0, look: !!b.look,
|
||||
swaps: b.swaps || [], down: b.down || null, downs: b.downs || [], cycledAt: b.cycledAt || null, endedAt: b.endedAt || null }, loop.placeOf(b)); }
|
||||
function adminLog(e) { const S = store.state(); const L = S.adminLog || (S.adminLog = []);
|
||||
e.id = 'a' + (S.seq.admin = (S.seq.admin || 0) + 1); e.at = Date.now(); L.push(e); if (L.length > 5000) L.splice(0, L.length - 5000); store.save(); return e; }
|
||||
// a banner image from the browser: one of this member's uploads (/u/...), or an https image address fetched once and checked like an upload
|
||||
async function checkedImage(me, raw) {
|
||||
const img = String(raw || '').trim();
|
||||
if (/^\/u\/[a-z0-9-]+\.(png|jpg|gif)$/.test(img)) {
|
||||
if (!img.slice(3).startsWith(me.slice(2, 10) + '-') || !fs.existsSync(path.join(UPL, img.slice(3)))) return { error: 'Upload the image again.' };
|
||||
return { img };
|
||||
}
|
||||
const ic = safety.checkLink(img, 'Image address'); if (ic.error) return ic;
|
||||
const f = await safety.fetchImage(ic.url); if (f.error) return f;
|
||||
return { img: saveUpload(me, f.buf, f.info.ext) };
|
||||
}
|
||||
// tracking macros in a banner link, filled on every click: {clickid} a fresh id, {spot} the banner number, {type} loop or waiting
|
||||
const MACRO = /\{(clickid|spot|type)\}|%7B(clickid|spot|type)%7D/gi;
|
||||
function clickLink(b) {
|
||||
@@ -540,7 +641,7 @@ function clickLink(b) {
|
||||
// waiting line. Never in the loop list: no shift, no cycle, no reward. Same picks for 2 minutes, so the page does not reshuffle.
|
||||
let fillCache = { key: '', at: 0, out: { loop: [], queue: [] } };
|
||||
function splashFill() {
|
||||
const S = store.state(); const ids = S.loop.concat(S.queue.slice(0, 24)); const bs = ids.map(id => store.banner(id)).filter(Boolean);
|
||||
const S = store.state(); const ids = S.loop.concat(S.queue.slice(0, 24)); const bs = ids.map(id => store.banner(id)).filter(b => b && !b.down);
|
||||
const open = Math.max(0, C.loopSize - S.loop.length - 1), queueSlots = S.queue.length ? 0 : 8;
|
||||
const key = ids.join(',') + '|' + open + '|' + queueSlots + '|' + lsspins.view().fill + '|' + lsspins.view().at;
|
||||
if (fillCache.key === key && Date.now() - fillCache.at < 120000) return fillCache.out;
|
||||
|
||||
Reference in New Issue
Block a user