Admin > Email an update loads at once; audience counts fill in from the background

The panel waited on a Sendy pass over every member (minutes, and the cache
empties on each deploy), so it showed blank. Counts are now a snapshot with
a background refresh, and the form no longer depends on them.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-28 04:43:42 -05:00
parent 53f0970209
commit 927b13a7cf
4 changed files with 1534 additions and 1525 deletions
+516 -516
View File
File diff suppressed because it is too large Load Diff
+1010 -1007
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1934,7 +1934,7 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/admin/updates' && req.method === 'GET') { // member update emails: notes, audiences, log
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
const since = updates.lastSentAt();
return json(res, 200, Object.assign({ notes: releases.notes().map(n => ({ id: n.id, title: n.title, date: n.date, fresh: !since || (n.created || 0) > since || (n.date && new Date(n.date + 'T12:00:00Z').getTime() > since) })), audiences: updates.AUDIENCES, counts: await updates.counts(), mailer: mailer.hasKey(), lastSentAt: since, draft: updates.draft() }, updates.status()));
return json(res, 200, Object.assign({ notes: releases.notes().map(n => ({ id: n.id, title: n.title, date: n.date, fresh: !since || (n.created || 0) > since || (n.date && new Date(n.date + 'T12:00:00Z').getTime() > since) })), audiences: updates.AUDIENCES, counts: updates.countsSnapshot(), mailer: mailer.hasKey(), lastSentAt: since, draft: updates.draft() }, updates.status()));
}
if (p === '/api/admin/updates/draft' && req.method === 'POST') {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
+7 -1
View File
@@ -36,6 +36,12 @@ async function recipients(kind) {
return out;
}
async function counts() { const o = {}; for (const k of Object.keys(AUDIENCES)) o[k] = (await recipients(k)).length; return o; }
// The admin panel must never wait on a count: one pass asks Sendy about every member with a pause between
// each (minutes), and the in-memory cache starts empty after every deploy. The panel gets the last known
// counts at once (or none) and a refresh runs in the background (Marty, 2026-09-28: the panel showed empty).
let countsSnap = null, counting = false;
async function refreshCounts() { if (counting) return; counting = true; try { countsSnap = await counts(); } catch (e) {} finally { counting = false; } }
function countsSnapshot() { if (!countsSnap && !counting) refreshCounts().catch(() => {}); return countsSnap; }
function compose({ subject, intro, noteIds, closing }, acct) {
const notes = R.releases.notes().filter(n => noteIds.includes(n.id));
@@ -93,4 +99,4 @@ function draft() { try { return JSON.parse(fs.readFileSync(DRAFT(), 'utf8')); }
function saveDraft(d) { const v = { subject: String(d.subject || '').slice(0, 150), intro: String(d.intro || '').slice(0, 8000), closing: String(d.closing || '').slice(0, 2000), noteIds: (d.noteIds || []).map(String).slice(0, 40), audience: AUDIENCES[d.audience] ? d.audience : 'optin', savedAt: Date.now() }; try { fs.writeFileSync(DRAFT(), JSON.stringify(v)); } catch (e) {} return v; }
function status() { const l = log(); if (running) { const k = l.sends.find(x => x.id === running.id); if (k) Object.assign(k, running); } return { sends: l.sends.slice(0, 12), running: !!running }; }
function lastSentAt() { const l = log(); const d = l.sends.find(x => x.status === 'done'); return d ? d.ts : 0; }
module.exports = { init, AUDIENCES, counts, compose, send, status, lastSentAt, draft, saveDraft };
module.exports = { init, AUDIENCES, counts, countsSnapshot, compose, send, status, lastSentAt, draft, saveDraft };