use crate::i18n::{Key, Lang, mail_footer, t}; pub const COMMENT_LIMIT: usize = 8000; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Verb { Opened, Commented, Closed, Reopened, Merged, Reviewed, AssignedYou, ReviewRequested, Pushed, MentionedYou, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Why { Mention, Assign, ReviewRequested, Author, Comment, Subscribed, Watching, } pub struct Notification<'a> { pub actor: Option<&'a str>, pub verb: Verb, pub repo: &'a str, pub number: i64, pub title: &'a str, pub comment: Option<&'a str>, pub url: &'a str, pub why: Why, pub settings_url: &'a str, } pub fn notification(lang: Lang, ctx: &Notification<'_>) -> (String, String) { let title = ctx.title.replace(['\r', '\n'], " "); let subject = format!("[{}] {title} (#{})", ctx.repo, ctx.number); let who = ctx.actor.unwrap_or(t(lang, Key::MailSomeone)); let mut body = format!("{who} {}.\n", t(lang, verb_key(ctx.verb))); if let Some(text) = ctx.comment.filter(|text| !text.trim().is_empty()) { body.push('\n'); body.push_str(&truncate(text, COMMENT_LIMIT)); body.push('\n'); } body.push('\n'); body.push_str(ctx.url); body.push_str("\n\n"); body.push_str(&mail_footer( lang, t(lang, why_key(ctx.why)), ctx.settings_url, )); body.push('\n'); (subject, body) } fn verb_key(verb: Verb) -> Key { match verb { Verb::Opened => Key::MailVerbOpened, Verb::Commented => Key::MailVerbCommented, Verb::Closed => Key::MailVerbClosed, Verb::Reopened => Key::MailVerbReopened, Verb::Merged => Key::MailVerbMerged, Verb::Reviewed => Key::MailVerbReviewed, Verb::AssignedYou => Key::MailVerbAssigned, Verb::ReviewRequested => Key::MailVerbReviewRequested, Verb::Pushed => Key::MailVerbPushed, Verb::MentionedYou => Key::MailVerbMentioned, } } fn why_key(why: Why) -> Key { match why { Why::Mention => Key::MailReasonMention, Why::Assign => Key::MailReasonAssign, Why::ReviewRequested => Key::MailReasonReviewRequested, Why::Author => Key::MailReasonAuthor, Why::Comment => Key::MailReasonComment, Why::Subscribed => Key::MailReasonSubscribed, Why::Watching => Key::MailReasonWatching, } } fn truncate(text: &str, max: usize) -> String { if text.chars().count() <= max { return text.to_owned(); } let mut out: String = text.chars().take(max).collect(); out.push('…'); out } pub fn test_mail() -> (String, String) { ( "Test message from forge".to_owned(), "This is a test message from forge.\n\nIf you can read it, outgoing mail works.\n" .to_owned(), ) } pub fn password_reset(username: &str, link: &str) -> (String, String) { ( "Reset your forge password".to_owned(), format!( "Hi {username},\n\nSomeone asked to reset the password for your forge account. \ If that was you, open this link within an hour. It works once:\n\n{link}\n\n\ If it was not you, ignore this message. Your password stays the same.\n" ), ) } pub fn password_changed(username: &str) -> (String, String) { ( "Your forge password was changed".to_owned(), format!( "Hi {username},\n\nThe password of your forge account was just changed and your \ other sessions were signed out. If that was you, nothing more to do. If it was \ not you, reset your password now and contact the administrator.\n" ), ) } pub fn passkey_added(username: &str, name: &str) -> (String, String) { ( "A passkey was added to your forge account".to_owned(), format!( "Hi {username},\n\nA passkey named \"{name}\" was just added to your forge \ account. It can now be used to sign in. If that was you, nothing more to do. If it \ was not you, remove the passkey from your security settings, change your password \ and contact the administrator.\n" ), ) } pub fn passkey_removed(username: &str, name: &str) -> (String, String) { ( "A passkey was removed from your forge account".to_owned(), format!( "Hi {username},\n\nThe passkey named \"{name}\" was just removed from your forge \ account. If that was you, nothing more to do. If it was not you, change your \ password now and contact the administrator.\n" ), ) } pub fn email_verify(username: &str, link: &str) -> (String, String) { ( "Confirm your email address for forge".to_owned(), format!( "Hi {username},\n\nPlease confirm that this address belongs to your forge account. \ Open this link within 24 hours. It works once:\n\n{link}\n\n\ If you did not ask for this, ignore this message.\n" ), ) } pub fn invite(link: &str) -> (String, String) { ( "You are invited to forge".to_owned(), format!( "Hello,\n\nSomeone invited you to join forge. Open this link to create your account. \ It works once and expires in seven days:\n\n{link}\n\n\ If you were not expecting this, ignore this message.\n" ), ) } #[cfg(test)] mod tests { use super::*; fn sample<'a>(comment: Option<&'a str>, title: &'a str) -> Notification<'a> { Notification { actor: Some("tahinli"), verb: Verb::Commented, repo: "bob/widgets", number: 7, title, comment, url: "https://forge.test/bob/widgets/issues/7", why: Why::Subscribed, settings_url: "https://forge.test/settings/notifications", } } #[test] fn subject_names_the_repo_title_and_number_and_stays_on_one_line() { let cases = [ ( "plain", "Crash on start", "[bob/widgets] Crash on start (#7)", ), ("newline", "two\r\nlines", "[bob/widgets] two lines (#7)"), ]; for (name, title, want) in cases { let (subject, _) = notification(Lang::En, &sample(None, title)); assert_eq!(subject, want, "case {name}"); } } #[test] fn long_comments_are_cut_at_the_limit_with_an_ellipsis() { let long = "x".repeat(COMMENT_LIMIT + 500); let (_, body) = notification(Lang::En, &sample(Some(long.as_str()), "T")); let kept = format!("{}…", "x".repeat(COMMENT_LIMIT)); assert!(body.contains(&kept)); assert!(!body.contains(&"x".repeat(COMMENT_LIMIT + 1))); } #[test] fn body_has_the_actor_line_comment_url_and_footer_in_order() { let (_, body) = notification(Lang::En, &sample(Some("Looks good"), "T")); let line = body.find("tahinli commented on this.").expect("actor line"); let text = body.find("Looks good").expect("comment"); let url = body .find("https://forge.test/bob/widgets/issues/7") .expect("url"); let footer = body.find("You are receiving this because").expect("footer"); assert!(line < text && text < url && url < footer, "{body}"); assert!( body.contains("Manage email notifications: https://forge.test/settings/notifications") ); } #[test] fn a_system_change_reads_as_someone() { let mut ctx = sample(None, "T"); ctx.actor = None; let (_, body) = notification(Lang::En, &ctx); assert!(body.starts_with("Someone commented on this."), "{body}"); } #[test] fn test_mail_is_plain_text_with_a_subject() { let (subject, body) = test_mail(); assert!(!subject.is_empty()); assert!(!body.contains('<'), "plain text, no markup"); assert!(body.ends_with('\n')); } }