From 14868753c0fc0ac7ed51f663b740243e36a9d0a3 Mon Sep 17 00:00:00 2001 From: martbost Date: Mon, 14 Sep 2026 14:11:51 -0500 Subject: [PATCH] Member updates: saved draft (loads into the admin card, Save draft button) Co-Authored-By: Claude Fable 5.1 --- public/admin.html | 4 ++-- public/assets/admin.js | 3 +++ server.js | 6 +++++- updates.js | 6 +++++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/public/admin.html b/public/admin.html index 7ecd2ef..762e4aa 100644 --- a/public/admin.html +++ b/public/admin.html @@ -402,7 +402,7 @@ -
+
@@ -480,6 +480,6 @@
- + diff --git a/public/assets/admin.js b/public/assets/admin.js index fe9fff1..f5965de 100644 --- a/public/assets/admin.js +++ b/public/assets/admin.js @@ -446,11 +446,14 @@ $('updAudience').innerHTML = Object.entries(d.audiences).map(([k, v]) => '').join(''); $('updNotes').innerHTML = d.notes.length ? d.notes.map(n => '').join('') : 'No release notes yet.'; $('updLog').innerHTML = 'WhenSubjectAudienceSent' + (d.sends.length ? d.sends.map(x => '' + new Date(x.ts).toLocaleString() + '' + esc(x.subject) + '' + esc(d.audiences[x.audience] || x.audience) + '' + x.sent + ' of ' + x.total + (x.skipped ? ' · ' + x.skipped + ' opted out' : '') + (x.failed ? ' · ' + x.failed + ' failed' : '') + (x.status === 'running' ? ' · running' : '') + '').join('') : 'None yet.'); + if (d.draft && !updDraftLoaded) { updDraftLoaded = true; $('updSubject').value = d.draft.subject || ''; $('updIntro').value = d.draft.intro || ''; if (d.draft.audience) $('updAudience').value = d.draft.audience; $('updNotes').querySelectorAll('input').forEach(i => { i.checked = (d.draft.noteIds || []).includes(i.value); }); $('updSub').textContent += ' · draft loaded (saved ' + new Date(d.draft.savedAt).toLocaleString() + ')'; } if (d.running) setTimeout(loadUpdates, 4000); } catch (e) { $('updSub').textContent = e.message; } } + let updDraftLoaded = false; const updInput = () => ({ subject: $('updSubject').value, intro: $('updIntro').value, noteIds: [...$('updNotes').querySelectorAll('input:checked')].map(i => i.value), audience: $('updAudience').value }); if ($('updCard')) { + $('updSaveDraft').addEventListener('click', busy($('updSaveDraft'), async () => { await api('/api/admin/updates/draft', updInput()); IAP.status('Draft saved.', 'ok'); })); $('updPreview').addEventListener('click', busy($('updPreview'), async () => { const r = await api('/api/admin/updates/preview', updInput()); $('updPre').hidden = false; $('updPre').textContent = 'Subject: ' + r.subject + '\n\n' + r.text; })); $('updTest').addEventListener('click', busy($('updTest'), async () => { const r = await api('/api/admin/updates/send', Object.assign(updInput(), { test: true })); IAP.status('Test sent to ' + r.to + '.', 'ok'); })); $('updSend').addEventListener('click', busy($('updSend'), async () => { diff --git a/server.js b/server.js index 0b14955..730bc3e 100644 --- a/server.js +++ b/server.js @@ -1333,7 +1333,11 @@ 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 }, 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: await updates.counts(), 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' }); + return json(res, 200, { ok: true, draft: updates.saveDraft(await readBody(req)) }); } if (p === '/api/admin/updates/preview' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); diff --git a/updates.js b/updates.js index 3145150..1608e52 100644 --- a/updates.js +++ b/updates.js @@ -79,6 +79,10 @@ async function send(input, opts) { })(); return { ok: true, id: rec.id, total: rec.total }; } +// one saved draft (subject, intro, noteIds, audience): the admin card loads it, Save draft writes it +const DRAFT = () => path.join(R.dataDir, 'updates-draft.json'); +function draft() { try { return JSON.parse(fs.readFileSync(DRAFT(), 'utf8')); } catch (e) { return null; } } +function saveDraft(d) { const v = { subject: String(d.subject || '').slice(0, 150), intro: String(d.intro || '').slice(0, 8000), 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 }; +module.exports = { init, AUDIENCES, counts, compose, send, status, lastSentAt, draft, saveDraft };