MC/website/node/automated-mail-templates.mjs
2026-11-27 23:43:05 +00:00

21 lines
5.1 KiB
JavaScript

const escape=value=>String(value??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
const line=value=>String(value??'').replace(/[\r\n]+/g,' ').slice(0,240);
const variants={
'jarvis-application':{title:'Recruitment Register',intro:'An update about your DuneRealms recruitment application.',action:'View the recruitment record'},
'political-event':{title:'Election Register',intro:'An update from the official DuneRealms election register.',action:'Open the election register'},
'mail-report':{title:'Reported mail',intro:'A user has reported an internal message. Review the report and original message before creating a Legal ticket.',action:'Review reported mail'},
'ticket-submitted':{title:'Ticket submitted',intro:'Your ticket has been submitted. You can follow its progress and reply in the Support Centre.',action:'View your ticket'},
'new-ticket-alert':{title:'New ticket alert',intro:'A new ticket is waiting for your team. Open the staff ticket desk to review it.',action:'Open staff ticket'},
'mailbox-ready':{title:'Your internal inbox is ready',intro:'Your DuneRealms internal inbox is ready. Open your account centre to read messages and manage your mail settings.',action:'Open Internal Mail'},
'forwarding-updated':{title:'Mail forwarding updated',intro:'Your internal mail forwarding settings have changed. A copy of forwarded messages stays in your internal inbox.',action:'Review mail settings'}
};
export function automatedMailTemplate(kind,{ticketId='',subject='',department='',recipientName='',message='',url}={}){
const variant=variants[kind];if(!variant)throw Error('Unknown automatic email template');
const target=new URL(url);if(target.protocol!=='https:'||!['cp.dunerealms.com','support.dunerealms.com'].includes(target.hostname)||target.username||target.password)throw Error('Unsupported email action link');
const title=variant.title,reference=line(ticketId),summary=line(subject),dept=line(department),greeting=recipientName?'Hello '+line(recipientName)+',':'Hello,';
const details=[reference&&['Ticket',reference],summary&&['Subject',summary],dept&&['Department',dept]].filter(Boolean);
const text=[greeting,'',title,'',variant.intro,...details.map(([k,v])=>k+': '+v),'',String(message||'').slice(0,4000),'',variant.action+': '+target.href,'','For support, visit https://support.dunerealms.com/ and submit a ticket.','DuneRealms · The Caravan Dispatch'].join('\n');
const rows=details.map(([k,v])=>`<tr><td style="padding:8px 12px;color:#b7c5b6;font-size:13px;width:95px">${escape(k)}</td><td style="padding:8px 12px;color:#eee6d2;word-break:break-word">${escape(v)}</td></tr>`).join('');
const html=`<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escape(title)}</title></head><body bgcolor="#0d211b" style="margin:0;background:#0d211b;color:#eee6d2"><table role="presentation" width="100%" bgcolor="#0d211b" cellspacing="0" cellpadding="0"><tr><td align="center" style="padding:24px 12px"><table role="presentation" width="640" bgcolor="#142e26" cellspacing="0" cellpadding="0" style="width:100%;max-width:640px;border:1px solid #b79a50;border-radius:12px;font:15px/1.65 Arial,sans-serif;color:#eee6d2"><tr><td style="padding:28px 28px 20px;border-bottom:1px solid #786d45"><img src="https://www.dunerealms.com/assets/dunerealms-season-logo-v2.png" width="220" alt="DuneRealms" style="display:block;width:220px;max-width:100%;height:auto"><p style="font-size:10px;letter-spacing:2px;color:#c3a159;margin:16px 0 0">THE CARAVAN DISPATCH</p></td></tr><tr><td style="padding:28px"><h1 style="margin:0 0 20px;color:#e2c379;font-size:26px">${escape(title)}</h1><p>${escape(greeting)}</p><p>${escape(variant.intro)}</p>${message?'<p style="white-space:pre-line">'+escape(String(message).slice(0,4000))+'</p>':''}${rows?`<table role="presentation" width="100%" bgcolor="#10271f" cellspacing="0" cellpadding="0" style="margin:22px 0;border:1px solid #52664e;border-radius:7px">${rows}</table>`:''}<table role="presentation" cellspacing="0" cellpadding="0" style="margin:24px 0"><tr><td bgcolor="#254b37" style="border:1px solid #c3a159;border-radius:7px"><a href="${escape(target.href)}" style="display:inline-block;padding:12px 20px;color:#f0d58c;text-decoration:none;font-weight:bold">${escape(variant.action)}</a></td></tr></table><p style="font-size:12px;color:#b7c5b6">If the button does not open, use:<br><a href="${escape(target.href)}" style="color:#e2c379;word-break:break-all">${escape(target.href)}</a></p></td></tr><tr><td style="padding:20px 28px;border-top:1px solid #786d45"><table role="presentation"><tr><td style="padding-right:16px"><img src="https://www.dunerealms.com/assets/seals/department-support-150.png" width="58" height="58" alt="DuneRealms Support" style="display:block;border:0"></td><td style="font-size:12px;color:#b7c5b6"><strong style="color:#e2c379">DuneRealms</strong><br>For support, <a href="https://support.dunerealms.com/" style="color:#e2c379">visit our support site and submit a ticket</a>.</td></tr></table></td></tr></table></td></tr></table></body></html>`;
return {subject:title+(reference?' · '+reference:''),text,html};
}