- worker: sends email_outbox mail over SMTP (lettre), woken by LISTEN/NOTIFY, SKIP LOCKED claiming, retries with backoff, hu/en templates (text + HTML), hourly cleanup of expired data - migration 0002: next_attempt_at, failed_at, notify trigger - docker-compose.override.yml: Mailpit for development only - Dockerfiles: lock shared cargo cache mounts for parallel builds Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
198 lines
8.3 KiB
Rust
198 lines
8.3 KiB
Rust
//! E-mail texts (Hungarian and English), as plain text and HTML.
|
|
//!
|
|
//! The payloads are written by the backend (`queue_email`); the validity
|
|
//! periods in the texts match the token lifetimes there.
|
|
|
|
use serde_json::Value;
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Rendered {
|
|
pub subject: String,
|
|
pub text: String,
|
|
pub html: String,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum TemplateError {
|
|
UnknownTemplate(String),
|
|
MissingField(&'static str),
|
|
}
|
|
|
|
impl std::fmt::Display for TemplateError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
TemplateError::UnknownTemplate(t) => write!(f, "unknown e-mail template '{t}'"),
|
|
TemplateError::MissingField(field) => write!(f, "e-mail payload has no '{field}'"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
enum Lang {
|
|
Hu,
|
|
En,
|
|
}
|
|
|
|
/// One e-mail: greeting, paragraphs, a button and a closing note.
|
|
struct Content {
|
|
subject: String,
|
|
greeting: String,
|
|
intro: String,
|
|
button: &'static str,
|
|
url: String,
|
|
note: &'static str,
|
|
}
|
|
|
|
pub fn render(template: &str, locale: &str, payload: &Value) -> Result<Rendered, TemplateError> {
|
|
let lang = if locale.eq_ignore_ascii_case("hu") { Lang::Hu } else { Lang::En };
|
|
let url = payload["url"].as_str().filter(|u| !u.is_empty()).ok_or(TemplateError::MissingField("url"))?.to_owned();
|
|
let greeting = |name: Option<String>| match (lang, name) {
|
|
(Lang::Hu, Some(n)) => format!("Szia {n}!"),
|
|
(Lang::Hu, None) => "Szia!".to_owned(),
|
|
(Lang::En, Some(n)) => format!("Hi {n},"),
|
|
(Lang::En, None) => "Hi,".to_owned(),
|
|
};
|
|
let content = match (template, lang) {
|
|
("verify_email", Lang::Hu) => Content {
|
|
subject: "Erősítsd meg az e-mail-címed".into(),
|
|
greeting: greeting(name(payload, "display_name")),
|
|
intro: "Köszönjük, hogy regisztráltál a Sorsfonalakon! Az alábbi gombbal erősítsd meg az e-mail-címed."
|
|
.into(),
|
|
button: "E-mail-cím megerősítése",
|
|
url,
|
|
note: "A link 3 napig érvényes. Ha nem te regisztráltál, nyugodtan hagyd figyelmen kívül ezt a levelet.",
|
|
},
|
|
("verify_email", Lang::En) => Content {
|
|
subject: "Confirm your e-mail address".into(),
|
|
greeting: greeting(name(payload, "display_name")),
|
|
intro: "Thanks for signing up for Sorsfonalak! Please confirm your e-mail address with the button below."
|
|
.into(),
|
|
button: "Confirm e-mail address",
|
|
url,
|
|
note: "The link is valid for 3 days. If you did not sign up, you can safely ignore this e-mail.",
|
|
},
|
|
("reset_password", Lang::Hu) => Content {
|
|
subject: "Új jelszó megadása".into(),
|
|
greeting: greeting(name(payload, "display_name")),
|
|
intro: "Jelszó-visszaállítást kértél a Sorsfonalak-fiókodhoz. Az alábbi gombbal adhatsz meg új jelszót."
|
|
.into(),
|
|
button: "Új jelszó megadása",
|
|
url,
|
|
note: "A link 1 óráig érvényes, és csak egyszer használható. Ha nem te kérted, hagyd figyelmen kívül ezt a levelet: a jelszavad nem változik.",
|
|
},
|
|
("reset_password", Lang::En) => Content {
|
|
subject: "Set a new password".into(),
|
|
greeting: greeting(name(payload, "display_name")),
|
|
intro:
|
|
"You asked to reset the password of your Sorsfonalak account. Use the button below to set a new one."
|
|
.into(),
|
|
button: "Set a new password",
|
|
url,
|
|
note: "The link is valid for 1 hour and can be used once. If you did not ask for this, ignore this e-mail: your password stays the same.",
|
|
},
|
|
("invite_accepted", Lang::Hu) => {
|
|
let who = name(payload, "invitee_name");
|
|
Content {
|
|
subject: match &who {
|
|
Some(n) => format!("{n} elfogadta a meghívásod"),
|
|
None => "Elfogadták a meghívásod".into(),
|
|
},
|
|
greeting: greeting(None),
|
|
intro: format!(
|
|
"{} elfogadta a „Mennyire illünk össze?” meghívásod, és elkészült a szinasztriátok.",
|
|
who.as_deref().unwrap_or("A meghívott")
|
|
),
|
|
button: "Szinasztria megnyitása",
|
|
url,
|
|
note: "A szinasztriát mindketten látjátok, de egymás születési adatait nem.",
|
|
}
|
|
}
|
|
("invite_accepted", Lang::En) => {
|
|
let who = name(payload, "invitee_name");
|
|
Content {
|
|
subject: match &who {
|
|
Some(n) => format!("{n} accepted your invitation"),
|
|
None => "Your invitation was accepted".into(),
|
|
},
|
|
greeting: greeting(None),
|
|
intro: format!(
|
|
"{} accepted your “How compatible are we?” invitation, and your synastry is ready.",
|
|
who.as_deref().unwrap_or("Your invitee")
|
|
),
|
|
button: "Open synastry",
|
|
url,
|
|
note: "You both see the synastry, but not each other's birth data.",
|
|
}
|
|
}
|
|
_ => return Err(TemplateError::UnknownTemplate(template.to_owned())),
|
|
};
|
|
Ok(content.render(lang))
|
|
}
|
|
|
|
/// A user-supplied name, safe for a subject line.
|
|
fn name(payload: &Value, field: &str) -> Option<String> {
|
|
let name: String = payload[field].as_str()?.chars().filter(|c| !c.is_control()).take(60).collect();
|
|
let name = name.trim();
|
|
(!name.is_empty()).then(|| name.to_owned())
|
|
}
|
|
|
|
fn escape(s: &str) -> String {
|
|
let mut out = String::with_capacity(s.len());
|
|
for c in s.chars() {
|
|
match c {
|
|
'&' => out.push_str("&"),
|
|
'<' => out.push_str("<"),
|
|
'>' => out.push_str(">"),
|
|
'"' => out.push_str("""),
|
|
'\'' => out.push_str("'"),
|
|
_ => out.push(c),
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
impl Content {
|
|
fn render(self, lang: Lang) -> Rendered {
|
|
let footer = match lang {
|
|
Lang::Hu => "Ezt a levelet a Sorsfonalak küldte.",
|
|
Lang::En => "This e-mail was sent by Sorsfonalak.",
|
|
};
|
|
let text = format!(
|
|
"{}\n\n{}\n\n{}:\n{}\n\n{}\n\n-- \nSorsfonalak\n",
|
|
self.greeting, self.intro, self.button, self.url, self.note
|
|
);
|
|
let html = format!(
|
|
r#"<!doctype html>
|
|
<html lang="{lang}">
|
|
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>{subject}</title></head>
|
|
<body style="margin:0;padding:0;background:#f4f1ea;font-family:Georgia,'Times New Roman',serif;color:#2a2620">
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f4f1ea;padding:32px 16px">
|
|
<tr><td align="center">
|
|
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="max-width:520px;background:#ffffff;border-radius:8px;padding:32px">
|
|
<tr><td style="font-size:20px;letter-spacing:2px;color:#9a7b3c;padding-bottom:24px">SORSFONALAK</td></tr>
|
|
<tr><td style="font-size:16px;line-height:1.6;padding-bottom:12px">{greeting}</td></tr>
|
|
<tr><td style="font-size:16px;line-height:1.6;padding-bottom:24px">{intro}</td></tr>
|
|
<tr><td style="padding-bottom:24px"><a href="{url}" style="display:inline-block;background:#9a7b3c;color:#ffffff;text-decoration:none;padding:12px 24px;border-radius:6px;font-size:16px">{button}</a></td></tr>
|
|
<tr><td style="font-size:14px;line-height:1.6;color:#6b645a;padding-bottom:16px">{note}</td></tr>
|
|
<tr><td style="font-size:12px;line-height:1.5;color:#9b948a;word-break:break-all">{url_text}</td></tr>
|
|
</table>
|
|
<p style="font-size:12px;color:#9b948a;max-width:520px">{footer}</p>
|
|
</td></tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
"#,
|
|
lang = if lang == Lang::Hu { "hu" } else { "en" },
|
|
subject = escape(&self.subject),
|
|
greeting = escape(&self.greeting),
|
|
intro = escape(&self.intro),
|
|
url = escape(&self.url),
|
|
button = escape(self.button),
|
|
note = escape(self.note),
|
|
url_text = escape(&self.url),
|
|
footer = escape(footer),
|
|
);
|
|
Rendered { subject: self.subject, text, html }
|
|
}
|
|
}
|