Merge pages track: link-domain pool with safety checks, page image uploads, form provider labels, Reports pages + zones

This commit is contained in:
martbost
2026-10-10 11:30:23 -05:00
11 changed files with 441 additions and 19 deletions
+49 -4
View File
@@ -42,6 +42,8 @@ const rotator = require('./rotator');
const health = require('./health'); // destination health checks + auto-pause (2026-10-10)
const postbacks = require('./postbacks'); // outbound + inbound conversion postbacks on rotator clicks (2026-09-29)
const pages = require('./pages'); // member pages: bridge / review / squeeze, served at /p/<slug> (2026-09-29)
const domains = require('./domains'); // link-domain pool by channel + six-hourly safety checks; burned domains leave the pool (2026-10-10)
const zonereport = require('./zonereport'); // read-only zone numbers for member Reports (2026-10-10)
const zones = require('./zones'); // ad zones on members' own sites (2026-09-29)
const nas = require('./nas');
const audiences = require('./audiences'); // first-party retargeting (2026-10-08) // only asked whether syndication is on (the status strip)
@@ -421,8 +423,12 @@ async function boot() {
setInterval(() => { try { rotator.sweepDemos(); } catch (e) {} }, 60 * 60 * 1000); // unclaimed home-page demo spins expire after 24 h
postbacks.init({ dataDir: DATA_DIR, ownHosts, rotator });
zones.init({ dataDir: DATA_DIR, ads, siteHost: SITE_HOST });
pages.init({ dataDir: DATA_DIR, ownHosts, openrouterKey: () => { if (process.env.OPENROUTER_API_KEY) return process.env.OPENROUTER_API_KEY.trim(); try { return fs.readFileSync(path.join(DATA_DIR, 'openrouter.key'), 'utf8').trim(); } catch (e) { return ''; } } });
pages.init({ dataDir: DATA_DIR, ownHosts, imageBase: () => (IS_PROD ? 'https' : 'http') + '://' + SITE_HOST, openrouterKey: () => { if (process.env.OPENROUTER_API_KEY) return process.env.OPENROUTER_API_KEY.trim(); try { return fs.readFileSync(path.join(DATA_DIR, 'openrouter.key'), 'utf8').trim(); } catch (e) { return ''; } } });
audiences.init({ dataDir: DATA_DIR });
// link-domain pool: channels live in site.json (linkDomainChannels), check state in domains.json; a burn alerts the admin once
domains.init({ dataDir: DATA_DIR, siteConfig, saveSite: patch => fs.writeFileSync(SITE_FILE, JSON.stringify(Object.assign(siteConfig(), patch), null, 2)),
notify: async text => { const sc = siteConfig(); if (sc.telegramBotToken && sc.telegramAdminChatId) await telegramSend(sc.telegramAdminChatId, text); else if (ADMIN_EMAIL && mailer.hasKey()) await mailer.send(ADMIN_EMAIL, 'LinkSpin: link domain burned', text.replace(/<[^>]+>/g, '')); } });
zonereport.init({ dataDir: DATA_DIR });
snapshot.init({ dataDir: DATA_DIR, db, chain, siteConfig, send: async (c, t, th) => { await telegramSend(c, t, th); return true; } });
setInterval(() => snapshot.tick().catch(e => console.error('snapshot', e.message)), 10 * 60 * 1000);
tank.init({ dataDir: DATA_DIR, chain, accounts, messages, mailer, site: SITE_URL });
@@ -1993,11 +1999,48 @@ const server = http.createServer(async (req, res) => {
if (!isAdmin(req)) return json(res, 401, { error: 'Admin only.' });
const r = await zones.adminSet(m[1], await readBody(req)); return json(res, r.error ? 400 : 200, r);
}
// -- hosted page images: /u/pages/<owner-hash>/<id>.<ext>, only files the pages index knows, a year of cache (2026-10-10)
m = /^\/u\/pages\/([a-f0-9]{16})\/([a-f0-9]{12})\.(png|jpg|webp)$/.exec(p);
if (m && (req.method === 'GET' || req.method === 'HEAD')) {
const f = pages.imageFile(m[1], m[2], m[3]); if (!f) { res.writeHead(404, { 'Content-Type': 'text/plain' }); return res.end('Not found'); }
return fs.readFile(f.file, (err, data) => { if (err) { res.writeHead(404, { 'Content-Type': 'text/plain' }); return res.end('Not found'); } res.writeHead(200, { 'Content-Type': f.mime, 'Content-Length': String(data.length), 'Cache-Control': 'public, max-age=31536000, immutable', 'X-Content-Type-Options': 'nosniff', 'Content-Disposition': 'inline', 'Access-Control-Allow-Origin': '*' }); res.end(req.method === 'HEAD' ? '' : data); });
}
// -- the link-domain pool for the member Links screen: live (not burned, not reserve) hosts with their channel
if (p === '/api/my/link-hosts' && req.method === 'GET') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const hs = domains.hosts(); return json(res, 200, { hosts: hs.length ? hs : [{ host: SITE_HOST, channel: 'email', label: 'Email', note: 'The app host (no link domains are live)' }], channels: domains.CHANNELS }, { 'Cache-Control': 'no-store' });
}
if (p === '/api/my/pages' && req.method === 'GET') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const hosts = String(siteConfig().linkDomains || '').split(',').map(x => x.trim()).filter(x => x && !x.startsWith('www.'));
const hosts = domains.live(); // burned or reserve domains are never handed out for new pages
const ph = pagesHost(); const pageBase = ph ? 'https://' + ph + '/' : 'https://' + (hosts[0] || SITE_HOST) + '/p/';
return json(res, 200, { pages: await pages.list(s.email), hosts: ph ? [ph] : (hosts.length ? hosts : [SITE_HOST]), pageBase, types: pages.TYPES, styles: require('./pagestyle').catalog() }, { 'Cache-Control': 'no-store' });
return json(res, 200, { pages: await pages.list(s.email), hosts: ph ? [ph] : (hosts.length ? hosts : [SITE_HOST]), pageBase, types: pages.TYPES, styles: require('./pagestyle').catalog(), providers: pages.providers(), images: await pages.images(s.email), imageMax: pages.IMG_MAX, imageBytes: pages.IMG_BYTES }, { 'Cache-Control': 'no-store' });
}
if (p === '/api/my/pages/upload' && req.method === 'POST') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const up = await pages.readUpload(req); if (up.error) return json(res, 400, up);
const r = await pages.uploadImage(s.email, up.buf, up.ct); return json(res, r.error ? 400 : 200, r);
}
m = /^\/api\/my\/pages\/images\/([a-f0-9]{12})$/.exec(p);
if (m && req.method === 'POST') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const b = await readBody(req); if (!b.remove) return json(res, 400, { error: 'Nothing to do.' });
const r = await pages.removeImage(s.email, m[1]); return json(res, r.error ? 400 : 200, r);
}
// -- admin: the link-domain pool (channel per domain, last safety check, burned flag, check now / clear / pull)
if (p === '/api/admin/domains' && req.method === 'GET') {
if (!isAdmin(req)) return json(res, 401, { error: 'Admin only.' });
return json(res, 200, { domains: domains.all(), channels: domains.CHANNELS, labels: domains.LABEL, notes: domains.NOTE, gsb: !!String(process.env.GSB_API_KEY || '').trim() }, { 'Cache-Control': 'no-store' });
}
if (p === '/api/admin/domains' && req.method === 'POST') {
if (!isAdmin(req)) return json(res, 401, { error: 'Admin only.' });
const b = await readBody(req); let r;
if (b.action === 'check') r = b.domain ? await domains.check(b.domain) : { ok: true, results: await domains.checkAll() };
else if (b.action === 'channel') r = domains.setChannel(b.domain, b.channel);
else if (b.action === 'clear') r = domains.clear(b.domain);
else if (b.action === 'burn') r = domains.burn(b.domain, b.reason);
else r = { error: 'Unknown action.' };
return json(res, r.error ? 400 : 200, Object.assign(r, { domains: domains.all() }));
}
if (p === '/api/my/pages' && req.method === 'POST') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
@@ -2024,7 +2067,9 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/my/reports' && req.method === 'GET') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const days = Math.max(1, Math.min(365, Number(u.searchParams.get('days')) || 30));
return json(res, 200, await rotator.summary(s.email, days), { 'Cache-Control': 'no-store' });
// spins (the rotator summary, spread at the top level so older report screens keep working) + pages + zones (2026-10-10)
const [spins, pg, zn] = await Promise.all([rotator.summary(s.email, days), pages.summary(s.email, days), zonereport.summary(s.email, days)]);
return json(res, 200, Object.assign({}, spins, { spins, pages: pg, zones: zn, days: spins.days, range: days }), { 'Cache-Control': 'no-store' });
}
if (p === '/api/my/postbacks' && req.method === 'GET') {
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });