forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·8.8 MiB·7e521ed·Tahinli·2 hours ago·329 commits
| 1 | use crate::i18n::{Key, Lang, mail_footer, t}; |
| 2 | |
| 3 | pub const COMMENT_LIMIT: usize = 8000; |
| 4 | |
| 5 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 6 | pub enum Verb { |
| 7 | Opened, |
| 8 | Commented, |
| 9 | Closed, |
| 10 | Reopened, |
| 11 | Merged, |
| 12 | Reviewed, |
| 13 | AssignedYou, |
| 14 | ReviewRequested, |
| 15 | Pushed, |
| 16 | MentionedYou, |
| 17 | } |
| 18 | |
| 19 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 20 | pub enum Why { |
| 21 | Mention, |
| 22 | Assign, |
| 23 | ReviewRequested, |
| 24 | Author, |
| 25 | Comment, |
| 26 | Subscribed, |
| 27 | Watching, |
| 28 | } |
| 29 | |
| 30 | pub struct Notification<'a> { |
| 31 | pub actor: Option<&'a str>, |
| 32 | pub verb: Verb, |
| 33 | pub repo: &'a str, |
| 34 | pub number: i64, |
| 35 | pub title: &'a str, |
| 36 | pub comment: Option<&'a str>, |
| 37 | pub url: &'a str, |
| 38 | pub why: Why, |
| 39 | pub settings_url: &'a str, |
| 40 | } |
| 41 | |
| 42 | pub fn notification(lang: Lang, ctx: &Notification<'_>) -> (String, String) { |
| 43 | let title = ctx.title.replace(['\r', '\n'], " "); |
| 44 | let subject = format!("[{}] {title} (#{})", ctx.repo, ctx.number); |
| 45 | let who = ctx.actor.unwrap_or(t(lang, Key::MailSomeone)); |
| 46 | let mut body = format!("{who} {}.\n", t(lang, verb_key(ctx.verb))); |
| 47 | if let Some(text) = ctx.comment.filter(|text| !text.trim().is_empty()) { |
| 48 | body.push('\n'); |
| 49 | body.push_str(&truncate(text, COMMENT_LIMIT)); |
| 50 | body.push('\n'); |
| 51 | } |
| 52 | body.push('\n'); |
| 53 | body.push_str(ctx.url); |
| 54 | body.push_str("\n\n"); |
| 55 | body.push_str(&mail_footer( |
| 56 | lang, |
| 57 | t(lang, why_key(ctx.why)), |
| 58 | ctx.settings_url, |
| 59 | )); |
| 60 | body.push('\n'); |
| 61 | (subject, body) |
| 62 | } |
| 63 | |
| 64 | fn verb_key(verb: Verb) -> Key { |
| 65 | match verb { |
| 66 | Verb::Opened => Key::MailVerbOpened, |
| 67 | Verb::Commented => Key::MailVerbCommented, |
| 68 | Verb::Closed => Key::MailVerbClosed, |
| 69 | Verb::Reopened => Key::MailVerbReopened, |
| 70 | Verb::Merged => Key::MailVerbMerged, |
| 71 | Verb::Reviewed => Key::MailVerbReviewed, |
| 72 | Verb::AssignedYou => Key::MailVerbAssigned, |
| 73 | Verb::ReviewRequested => Key::MailVerbReviewRequested, |
| 74 | Verb::Pushed => Key::MailVerbPushed, |
| 75 | Verb::MentionedYou => Key::MailVerbMentioned, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn why_key(why: Why) -> Key { |
| 80 | match why { |
| 81 | Why::Mention => Key::MailReasonMention, |
| 82 | Why::Assign => Key::MailReasonAssign, |
| 83 | Why::ReviewRequested => Key::MailReasonReviewRequested, |
| 84 | Why::Author => Key::MailReasonAuthor, |
| 85 | Why::Comment => Key::MailReasonComment, |
| 86 | Why::Subscribed => Key::MailReasonSubscribed, |
| 87 | Why::Watching => Key::MailReasonWatching, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn truncate(text: &str, max: usize) -> String { |
| 92 | if text.chars().count() <= max { |
| 93 | return text.to_owned(); |
| 94 | } |
| 95 | let mut out: String = text.chars().take(max).collect(); |
| 96 | out.push('…'); |
| 97 | out |
| 98 | } |
| 99 | |
| 100 | pub fn test_mail() -> (String, String) { |
| 101 | ( |
| 102 | "Test message from forge".to_owned(), |
| 103 | "This is a test message from forge.\n\nIf you can read it, outgoing mail works.\n" |
| 104 | .to_owned(), |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | pub fn password_reset(username: &str, link: &str) -> (String, String) { |
| 109 | ( |
| 110 | "Reset your forge password".to_owned(), |
| 111 | format!( |
| 112 | "Hi {username},\n\nSomeone asked to reset the password for your forge account. \ |
| 113 | If that was you, open this link within an hour. It works once:\n\n{link}\n\n\ |
| 114 | If it was not you, ignore this message. Your password stays the same.\n" |
| 115 | ), |
| 116 | ) |
| 117 | } |
| 118 | |
| 119 | pub fn password_changed(username: &str) -> (String, String) { |
| 120 | ( |
| 121 | "Your forge password was changed".to_owned(), |
| 122 | format!( |
| 123 | "Hi {username},\n\nThe password of your forge account was just changed and your \ |
| 124 | other sessions were signed out. If that was you, nothing more to do. If it was \ |
| 125 | not you, reset your password now and contact the administrator.\n" |
| 126 | ), |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | pub fn passkey_added(username: &str, name: &str) -> (String, String) { |
| 131 | ( |
| 132 | "A passkey was added to your forge account".to_owned(), |
| 133 | format!( |
| 134 | "Hi {username},\n\nA passkey named \"{name}\" was just added to your forge \ |
| 135 | account. It can now be used to sign in. If that was you, nothing more to do. If it \ |
| 136 | was not you, remove the passkey from your security settings, change your password \ |
| 137 | and contact the administrator.\n" |
| 138 | ), |
| 139 | ) |
| 140 | } |
| 141 | |
| 142 | pub fn passkey_removed(username: &str, name: &str) -> (String, String) { |
| 143 | ( |
| 144 | "A passkey was removed from your forge account".to_owned(), |
| 145 | format!( |
| 146 | "Hi {username},\n\nThe passkey named \"{name}\" was just removed from your forge \ |
| 147 | account. If that was you, nothing more to do. If it was not you, change your \ |
| 148 | password now and contact the administrator.\n" |
| 149 | ), |
| 150 | ) |
| 151 | } |
| 152 | |
| 153 | pub fn email_verify(username: &str, link: &str) -> (String, String) { |
| 154 | ( |
| 155 | "Confirm your email address for forge".to_owned(), |
| 156 | format!( |
| 157 | "Hi {username},\n\nPlease confirm that this address belongs to your forge account. \ |
| 158 | Open this link within 24 hours. It works once:\n\n{link}\n\n\ |
| 159 | If you did not ask for this, ignore this message.\n" |
| 160 | ), |
| 161 | ) |
| 162 | } |
| 163 | |
| 164 | pub fn invite(link: &str) -> (String, String) { |
| 165 | ( |
| 166 | "You are invited to forge".to_owned(), |
| 167 | format!( |
| 168 | "Hello,\n\nSomeone invited you to join forge. Open this link to create your account. \ |
| 169 | It works once and expires in seven days:\n\n{link}\n\n\ |
| 170 | If you were not expecting this, ignore this message.\n" |
| 171 | ), |
| 172 | ) |
| 173 | } |
| 174 | |
| 175 | #[cfg(test)] |
| 176 | mod tests { |
| 177 | use super::*; |
| 178 | |
| 179 | fn sample<'a>(comment: Option<&'a str>, title: &'a str) -> Notification<'a> { |
| 180 | Notification { |
| 181 | actor: Some("tahinli"), |
| 182 | verb: Verb::Commented, |
| 183 | repo: "bob/widgets", |
| 184 | number: 7, |
| 185 | title, |
| 186 | comment, |
| 187 | url: "https://forge.test/bob/widgets/issues/7", |
| 188 | why: Why::Subscribed, |
| 189 | settings_url: "https://forge.test/settings/notifications", |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn subject_names_the_repo_title_and_number_and_stays_on_one_line() { |
| 195 | let cases = [ |
| 196 | ( |
| 197 | "plain", |
| 198 | "Crash on start", |
| 199 | "[bob/widgets] Crash on start (#7)", |
| 200 | ), |
| 201 | ("newline", "two\r\nlines", "[bob/widgets] two lines (#7)"), |
| 202 | ]; |
| 203 | for (name, title, want) in cases { |
| 204 | let (subject, _) = notification(Lang::En, &sample(None, title)); |
| 205 | assert_eq!(subject, want, "case {name}"); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn long_comments_are_cut_at_the_limit_with_an_ellipsis() { |
| 211 | let long = "x".repeat(COMMENT_LIMIT + 500); |
| 212 | let (_, body) = notification(Lang::En, &sample(Some(long.as_str()), "T")); |
| 213 | let kept = format!("{}…", "x".repeat(COMMENT_LIMIT)); |
| 214 | assert!(body.contains(&kept)); |
| 215 | assert!(!body.contains(&"x".repeat(COMMENT_LIMIT + 1))); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn short_comments_are_kept_whole() { |
| 220 | assert_eq!(truncate("héllo", 10), "héllo"); |
| 221 | assert_eq!(truncate("héllo", 2), "hé…"); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn body_has_the_actor_line_comment_url_and_footer_in_order() { |
| 226 | let (_, body) = notification(Lang::En, &sample(Some("Looks good"), "T")); |
| 227 | let line = body.find("tahinli commented on this.").expect("actor line"); |
| 228 | let text = body.find("Looks good").expect("comment"); |
| 229 | let url = body |
| 230 | .find("https://forge.test/bob/widgets/issues/7") |
| 231 | .expect("url"); |
| 232 | let footer = body.find("You are receiving this because").expect("footer"); |
| 233 | assert!(line < text && text < url && url < footer, "{body}"); |
| 234 | assert!( |
| 235 | body.contains("Manage email notifications: https://forge.test/settings/notifications") |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn turkish_mail_uses_the_turkish_phrases() { |
| 241 | let (_, body) = notification(Lang::Tr, &sample(None, "T")); |
| 242 | assert!(body.starts_with("tahinli yorum yaptı."), "{body}"); |
| 243 | assert!( |
| 244 | body.contains("Bu e-postayı şu nedenle alıyorsunuz:"), |
| 245 | "{body}" |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn a_system_change_reads_as_someone() { |
| 251 | let mut ctx = sample(None, "T"); |
| 252 | ctx.actor = None; |
| 253 | let (_, body) = notification(Lang::En, &ctx); |
| 254 | assert!(body.starts_with("Someone commented on this."), "{body}"); |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn test_mail_is_plain_text_with_a_subject() { |
| 259 | let (subject, body) = test_mail(); |
| 260 | assert!(!subject.is_empty()); |
| 261 | assert!(!body.contains('<'), "plain text, no markup"); |
| 262 | assert!(body.ends_with('\n')); |
| 263 | } |
| 264 | } |