Admin personal notices (2026-11-13, first use: Downline Banners wallet nudge, Marty's go): POST /api/admin/notice-batch {batch, subject, html, text, items:[{email, vars}]} fills {{key}} per member, sends to the InstantAdPay inbox + email through the site mailer (opt-outs skipped, unsubscribe link added), once per member per batch (marks.claim), 400 ms apart, in the background; GET ?batch= for the result

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-13 11:04:27 -06:00
parent e98a6ebcd8
commit d626469980
+40
View File
@@ -3688,6 +3688,46 @@ const server = http.createServer(async (req, res) => {
return json(res, 200, { ok: true, refuel: rv, facts: rv.on ? await engine.facts({ site: 'iap', adminMember, ads, coach }, email) : null });
}
// -- network hub hooks (hubsync.js, 2026-11-12): signed with HUB_SECRET (X-Hub-Ts + X-Hub-Sig), 5-minute window. Only with HUB_ENABLED.
// -- personal notices (2026-11-13, first use: Marty's go on the Downline Banners wallet nudge to sponsors). Admin only. One
// message per member, each with its own text: InstantAdPay inbox (messages.deliver, kind notice) + email through the site
// mailer, which skips anyone who unsubscribed and adds the unsubscribe link. Once per member per batch (marks.claim
// 'nb:<batch>:<email>'), so a repeated call never mails anyone twice. Runs in the background, 400 ms apart;
// GET ?batch= reads the result (notice-batches.json).
if (p === '/api/admin/notice-batch' && (req.method === 'POST' || req.method === 'GET')) {
if (!isAdmin(req)) return json(res, 401, { error: 'Admin only.' });
const NB = path.join(DATA_DIR, 'notice-batches.json');
const readNB = () => { try { return JSON.parse(fs.readFileSync(NB, 'utf8')); } catch (e) { return {}; } };
if (req.method === 'GET') return json(res, 200, readNB()[String(u.searchParams.get('batch') || '')] || { error: 'No such batch.' });
const b = await readBody(req); // { batch, subject, html, text, items: [{ email, vars: { key: value } }] }, {{key}} filled per member
const batch = String(b.batch || '').replace(/[^a-z0-9-]/gi, '').slice(0, 60); const subjectT = String(b.subject || '').slice(0, 160);
const items = Array.isArray(b.items) ? b.items.slice(0, 5000) : [];
const fill = (t, v, html) => String(t || '').replace(/\{\{(\w+)\}\}/g, (m, k) => { const x = v && v[k] != null ? String(v[k]) : ''; return html ? x.replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])) : x; });
const subject = subjectT;
if (!batch || !subject || !items.length) return json(res, 400, { error: 'batch, subject and items required' });
const all = readNB(); if (all[batch] && all[batch].running) return json(res, 409, { error: 'That batch is already running.' });
all[batch] = { subject, total: items.length, sent: 0, inbox: 0, optedOut: 0, noAccount: 0, repeat: 0, failed: 0, running: true, started: Date.now() };
fs.writeFileSync(NB, JSON.stringify(all));
json(res, 200, { ok: true, batch, total: items.length });
(async () => {
const st = all[batch];
for (const it of items) {
const e = String(it.email || '').trim().toLowerCase();
try {
const acct = await accounts.byEmail(e); if (!acct) { st.noAccount++; continue; }
if (!(await marks.claim('nb:' + batch + ':' + e))) { st.repeat++; continue; }
const subj = fill(subjectT, it.vars, false), html = fill(b.html, it.vars, true), text = fill(b.text, it.vars, false);
await messages.deliver(1, ADMIN_EMAIL || 'house@instantadpay.com', [e], subj, html, 'notice'); st.inbox++;
const ok = await mailer.send(e, subj, text, html);
if (ok === false) st.optedOut++; else st.sent++;
} catch (err) { st.failed++; console.error('notice-batch', batch, err.message); }
const cur = readNB(); cur[batch] = st; fs.writeFileSync(NB, JSON.stringify(cur));
await new Promise(r => setTimeout(r, 400));
}
st.running = false; st.finished = Date.now(); const cur = readNB(); cur[batch] = st; fs.writeFileSync(NB, JSON.stringify(cur));
console.log('notice-batch', batch, JSON.stringify(st));
})().catch(err => console.error('notice-batch', err.message));
return;
}
if (p === '/api/hub/events' && req.method === 'POST' && hubsync.enabled()) {
let raw; try { raw = (await readRaw(req, 2e6)).toString('utf8'); } catch (e) { return json(res, 413, { error: 'too big' }); }
const r = await hubsync.receive(req.headers, raw); return json(res, r.status, r.body);