325d2273de
weekend.js (WEEKEND_BOOST_DAYS=0 off, WEEKEND_NOW test clock); wire.js stacks SKU days + the bonus by the purchase's chain time, so it needs no contract change (the SKU holds 7 board days; a Boost is days, not views). /api/config, quote, order show the weekend days. netdeals.js + /assets/netweekend.js ribbon via common.js (not admin, never framed), common.js?v=3. Chatbot FACTS line and chatcore finish hook while live. qa/weekend.test.cjs; e2e 63/63. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
3.8 KiB
JavaScript
55 lines
3.8 KiB
JavaScript
// Network deals for this site's "Network Weekend" ribbon (Marty 2026-11-17). One source: the hub's public GET /v1/network-deals
|
|
// (HUB_URL). Cached 2 minutes, refreshed in the background, never waits on a slow hub (2.5 s timeout); hub down = no ribbon.
|
|
// The same file lives in every network site. Never RM Circle, never the games (the hub list never names them).
|
|
// The page (/assets/netweekend.js) switches the ribbon on and off by its own clock at startsAt / endsAt, so a cached copy never
|
|
// shows early or late. setLocal(fn) lets a site rewrite its own deal line from live data (for example a price read from chain),
|
|
// so the ribbon never shows a number the contract does not charge: fn(deal) returns the deal, a changed copy, or null to drop it.
|
|
const TTL = 2 * 60 * 1000;
|
|
let cache = { at: 0, body: { events: [] } };
|
|
let inflight = null;
|
|
let local = null;
|
|
|
|
const hubUrl = () => String(process.env.HUB_URL || 'https://hub.saasy.top').trim().replace(/\/+$/, '');
|
|
const safeUrl = u => (/^https:\/\/[a-z0-9.-]+(\/[^\s"'<>]*)?$/i.test(String(u || '')) ? String(u) : '');
|
|
const str = (v, n) => String(v == null ? '' : v).slice(0, n || 300);
|
|
function clean(j) {
|
|
if (!j || !Array.isArray(j.events)) return null;
|
|
return { events: j.events.slice(0, 5).filter(e => e && e.id && !Number.isNaN(Date.parse(e.startsAt)) && !Number.isNaN(Date.parse(e.endsAt))).map(e => ({
|
|
id: str(e.id, 60), name: str(e.name, 60), tagline: str(e.tagline, 160), url: safeUrl(e.url), startsAt: e.startsAt, endsAt: e.endsAt,
|
|
days: e.days && typeof e.days === 'object' ? Object.fromEntries(Object.entries(e.days).slice(0, 20).map(([k, v]) => [str(k, 10), str(v, 40)])) : {},
|
|
deals: (Array.isArray(e.deals) ? e.deals : []).slice(0, 40).map(d => {
|
|
const x = { site: str(d.site, 30), name: str(d.name, 80), url: safeUrl(d.url), text: str(d.text), endsAt: Number.isNaN(Date.parse(d.endsAt)) ? e.endsAt : d.endsAt };
|
|
if (d.note) x.note = str(d.note, 120);
|
|
if (d.byDay && typeof d.byDay === 'object') x.byDay = Object.fromEntries(Object.entries(d.byDay).slice(0, 20).map(([k, v]) => [str(k, 10), str(v)]));
|
|
return x;
|
|
})
|
|
})) };
|
|
}
|
|
function refresh() {
|
|
if (inflight || process.env.NETWORK_DEALS_HUB === '0') return inflight;
|
|
const ac = new AbortController(); const t = setTimeout(() => ac.abort(), 2500);
|
|
inflight = fetch(hubUrl() + '/v1/network-deals', { signal: ac.signal })
|
|
.then(r => (r.ok ? r.json() : null))
|
|
.then(j => { const b = clean(j); if (b) cache = { at: Date.now(), body: b }; else cache.at = Date.now(); })
|
|
.catch(() => { cache.at = Date.now() - TTL + 30 * 1000; }) // hub down: try again in 30 s, keep the last good copy
|
|
.finally(() => { clearTimeout(t); inflight = null; });
|
|
return inflight;
|
|
}
|
|
function current() { if (Date.now() - cache.at > TTL) refresh(); return cache.body; }
|
|
function setLocal(fn) { local = typeof fn === 'function' ? fn : null; }
|
|
function withLocal(body, self) {
|
|
if (!local) return body;
|
|
return { events: body.events.map(e => Object.assign({}, e, { deals: e.deals.map(d => {
|
|
if (d.site !== self) return d;
|
|
try { const r = local(Object.assign({}, d)); return r === undefined ? d : r; } catch (err) { return d; }
|
|
}).filter(Boolean) })) };
|
|
}
|
|
// GET /api/network-deals: public, short cache, the same answer on every page. self = this site's network id (NETWORK_SITE_ID).
|
|
function handle(req, res, self) {
|
|
const me = String(self || process.env.NETWORK_SITE_ID || '').trim().toLowerCase();
|
|
const body = Object.assign({ self: me }, withLocal(current(), me));
|
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'public, max-age=60', 'X-Content-Type-Options': 'nosniff' });
|
|
res.end(JSON.stringify(body));
|
|
}
|
|
module.exports = { current, handle, refresh, setLocal, clean, _set: b => { cache = { at: Date.now(), body: clean(b) || { events: [] } }; } };
|