Files
instantadpay/tools/patch_update_image.py

36 lines
3.0 KiB
Python

# Member update emails can carry ONE header image (Marty, 2026-10-03: the Spooky Doors announcement with its share image).
# An optional https image (+ link) adds a simple HTML part next to the plain text, never instead of it: the image on
# top (linked), the same paragraphs, the same footer and unsubscribe link. No template, so deliverability stays plain.
ROOT = 'D:/Projects/HighRisk/onchain-ad-membership/site/'
def edit(path, pairs):
b = open(ROOT + path, 'rb').read(); crlf = b.count(b'\r\n') > b.count(b'\n') // 2
s = b.decode('utf-8').replace('\r\n', '\n')
for a, r in pairs:
if s.count(a) != 1: raise SystemExit('%s anchor %d: %s' % (path, s.count(a), a[:70]))
s = s.replace(a, r)
if crlf: s = s.replace('\n', '\r\n')
open(ROOT + path, 'wb').write(s.encode('utf-8')); print('patched', path)
HTML = r"""// the optional HTML twin of the plain text: one linked header image, then the same paragraphs (URLs clickable)
const escH = s => String(s == null ? '' : s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
const okUrl = u => /^https:\/\/[^\s"'<>]+$/.test(String(u || ''));
function toHtml(parts, image, link) {
const para = t => '<p style="margin:0 0 16px">' + escH(t).replace(/https:\/\/[^\s<]+/g, u => '<a href="' + u + '" style="color:#6d28d9">' + u + '</a>').replace(/\n/g, '<br>') + '</p>';
const img = '<img src="' + escH(image) + '" alt="" width="560" style="display:block;width:100%;max-width:560px;height:auto;border:0;border-radius:10px;margin:0 0 20px">';
return '<!doctype html><html><body style="margin:0;padding:0;background:#ffffff"><div style="max-width:560px;margin:0 auto;padding:20px 16px;font:15px/1.55 Arial,Helvetica,sans-serif;color:#1f2430">'
+ (okUrl(link) ? '<a href="' + escH(link) + '">' + img + '</a>' : img) + parts.map(para).join('') + '</div></body></html>';
}
"""
edit('updates.js', [
("function compose({ subject, intro, noteIds, closing }, acct) {", HTML + "function compose({ subject, intro, noteIds, closing, image, imageLink }, acct) {"),
(" return { subject: String(subject || '').trim().slice(0, 150) || 'What is new on InstantAdPay', text: parts.join('\\n\\n') };",
" return { subject: String(subject || '').trim().slice(0, 150) || 'What is new on InstantAdPay', text: parts.join('\\n\\n'), html: okUrl(image) ? toHtml(parts, image, imageLink) : null };"),
(" await R.mailer.send(to, '[TEST] ' + m.subject, m.text);", " await R.mailer.send(to, '[TEST] ' + m.subject, m.text, m.html);"),
(" await R.mailer.send(acct.email, m.subject, m.text); rec.sent += 1;", " await R.mailer.send(acct.email, m.subject, m.text, m.html); rec.sent += 1;"),
])
edit('server.js', [
("const r = await updates.send({ subject: b.subject, intro: b.intro, closing: b.closing, noteIds: (b.noteIds || []).map(String), audience: b.audience,",
"const r = await updates.send({ subject: b.subject, intro: b.intro, closing: b.closing, image: b.image, imageLink: b.imageLink, noteIds: (b.noteIds || []).map(String), audience: b.audience,"),
])
print('ok')