/* ═══════════════════════════════════════════════════════════════ NEW-REQUEST NOTICES An email to the people in Panel → Site settings → "Email new requests to" when a content request (/request) or a data request (the Privacy page) comes in. Sent after the row is saved and never waited on: a mail problem is logged, the submission stands. Off until Gmail is set up (googleService.js) and someone is listed. ═══════════════════════════════════════════════════════════════ */ import { mailReady, sendMail } from "./googleService.js"; import { siteFor } from "./pageMeta.js"; const EMAIL = /^[^\s@,;<>]+@[^\s@,;<>]+\.[^\s@,;<>]+$/; /* The usable addresses in a list separated by commas, semicolons or spaces; anything else is left out. */ export const addresses = (text) => String(text ?? "") .split(/[\s,;]+/) .filter((address) => EMAIL.test(address)); export function recipients(db) { return addresses(db.prepare(`SELECT notify_emails FROM site_settings WHERE id = 'site'`).get()?.notify_emails); } /* queue: the admin path of the queue it landed in, eg. /admin/requests */ export function notifyNew(db, { id, subject, lines, replyTo, queue }) { if (!mailReady()) return; const to = recipients(db); if (to.length === 0) return; let origin = ""; try { origin = (siteFor(db).url ?? "").replace(/\/$/, ""); } catch { // No site.json: the notice goes without a link. } const text = [...lines, "", origin ? `Open it: ${origin}${queue}` : `In the admin: Forms, ${queue}`, ""].join("\n"); sendMail({ to, subject, text, replyTo: EMAIL.test(replyTo ?? "") ? replyTo : undefined }) .then(() => console.log(`feedback #${id}: notice sent to ${to.length}`)) .catch((err) => console.warn(`feedback #${id}: notice failed: ${err.message}`)); }