| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use jiff::Timestamp; |
| 9 | |
| 10 | use crate::rules::clean_text; |
| 11 | use crate::zones::{parse_zone, utc_to_local}; |
| 12 | |
| 13 | |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 15 | pub enum MailLang { |
| 16 | |
| 17 | En, |
| 18 | |
| 19 | Tr, |
| 20 | } |
| 21 | |
| 22 | impl MailLang { |
| 23 | |
| 24 | pub fn of(locale_override: Option<&str>, locale: Option<&str>) -> MailLang { |
| 25 | match locale_override.or(locale) { |
| 26 | Some("tr") => MailLang::Tr, |
| 27 | _ => MailLang::En, |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 34 | pub struct Mail { |
| 35 | |
| 36 | pub subject: String, |
| 37 | |
| 38 | pub body: String, |
| 39 | } |
| 40 | |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 43 | pub enum K { |
| 44 | |
| 45 | Key, |
| 46 | |
| 47 | Column, |
| 48 | |
| 49 | Deadline, |
| 50 | |
| 51 | Assignees, |
| 52 | |
| 53 | When, |
| 54 | |
| 55 | Link, |
| 56 | |
| 57 | SentStatus, |
| 58 | |
| 59 | SentCreated, |
| 60 | |
| 61 | SentAssigned, |
| 62 | |
| 63 | SentUnassigned, |
| 64 | |
| 65 | SentCommented, |
| 66 | |
| 67 | SentDeadlineSet, |
| 68 | |
| 69 | SentDeadlineCleared, |
| 70 | |
| 71 | SentRetitled, |
| 72 | |
| 73 | SentLinked, |
| 74 | |
| 75 | SentUnlinked, |
| 76 | |
| 77 | SentDeleted, |
| 78 | |
| 79 | SentUnblocked, |
| 80 | |
| 81 | ReminderSubject, |
| 82 | |
| 83 | ReminderLead, |
| 84 | |
| 85 | TestSubject, |
| 86 | |
| 87 | TestBody, |
| 88 | } |
| 89 | |
| 90 | |
| 91 | pub fn text(lang: MailLang, key: K) -> &'static str { |
| 92 | use MailLang::{En, Tr}; |
| 93 | match (key, lang) { |
| 94 | (K::Key, En) => "Task", |
| 95 | (K::Key, Tr) => "Kart", |
| 96 | (K::Column, En) => "Column", |
| 97 | (K::Column, Tr) => "Sütun", |
| 98 | (K::Deadline, En) => "Deadline", |
| 99 | (K::Deadline, Tr) => "Son tarih", |
| 100 | (K::Assignees, En) => "Assignees", |
| 101 | (K::Assignees, Tr) => "Atananlar", |
| 102 | (K::When, En) => "Meeting time", |
| 103 | (K::When, Tr) => "Toplantı saati", |
| 104 | (K::Link, En) => "Open the task", |
| 105 | (K::Link, Tr) => "Kartı aç", |
| 106 | (K::SentStatus, En) => "moved the task to the column", |
| 107 | (K::SentStatus, Tr) => "kartı sütuna taşıdı", |
| 108 | (K::SentCreated, En) => "created the task", |
| 109 | (K::SentCreated, Tr) => "kartı oluşturdu", |
| 110 | (K::SentAssigned, En) => "assigned", |
| 111 | (K::SentAssigned, Tr) => "kişisini atadı", |
| 112 | (K::SentUnassigned, En) => "removed the assignment of", |
| 113 | (K::SentUnassigned, Tr) => "kişisinin atamasını kaldırdı", |
| 114 | (K::SentCommented, En) => "commented on the task", |
| 115 | (K::SentCommented, Tr) => "karta yorum yaptı", |
| 116 | (K::SentDeadlineSet, En) => "set the deadline or meeting time", |
| 117 | (K::SentDeadlineSet, Tr) => "son tarihi veya toplantı saatini belirledi", |
| 118 | (K::SentDeadlineCleared, En) => "cleared the deadline or meeting time", |
| 119 | (K::SentDeadlineCleared, Tr) => "son tarihi veya toplantı saatini kaldırdı", |
| 120 | (K::SentRetitled, En) => "changed the title", |
| 121 | (K::SentRetitled, Tr) => "kartın başlığını değiştirdi", |
| 122 | (K::SentLinked, En) => "added a dependency", |
| 123 | (K::SentLinked, Tr) => "bir bağımlılık ekledi", |
| 124 | (K::SentUnlinked, En) => "removed a dependency", |
| 125 | (K::SentUnlinked, Tr) => "bir bağımlılığı kaldırdı", |
| 126 | (K::SentDeleted, En) => "deleted the task", |
| 127 | (K::SentDeleted, Tr) => "kartı sildi", |
| 128 | (K::SentUnblocked, En) => "The task is no longer blocked.", |
| 129 | (K::SentUnblocked, Tr) => "Kart artık engelli değil.", |
| 130 | (K::ReminderSubject, En) => "Reminder:", |
| 131 | (K::ReminderSubject, Tr) => "Hatırlatma:", |
| 132 | (K::ReminderLead, En) => "A meeting for this task is coming up.", |
| 133 | (K::ReminderLead, Tr) => "Bu kart için bir toplantı yaklaşıyor.", |
| 134 | (K::TestSubject, En) => "iz test mail", |
| 135 | (K::TestSubject, Tr) => "iz deneme e-postası", |
| 136 | (K::TestBody, En) => "This is a test mail from iz. No other action follows from it.", |
| 137 | (K::TestBody, Tr) => { |
| 138 | "Bu, iz uygulamasından bir deneme e-postasıdır. Başka bir işlem yapmaz." |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | pub fn when_text(stamp: &str, zoneinfo: &str) -> Option<String> { |
| 146 | let at: Timestamp = stamp.parse().ok()?; |
| 147 | let zone = parse_zone(zoneinfo).ok(); |
| 148 | let name = zone |
| 149 | .as_ref() |
| 150 | .and_then(|z| z.iana_name().map(str::to_owned)) |
| 151 | .unwrap_or_else(|| "UTC".to_owned()); |
| 152 | let zone = zone.unwrap_or(jiff::tz::TimeZone::UTC); |
| 153 | let local = utc_to_local(&zone, at); |
| 154 | Some(format!("{} {}", local.strftime("%Y-%m-%d %H:%M"), name)) |
| 155 | } |
| 156 | |
| 157 | |
| 158 | pub struct RuleMail<'a> { |
| 159 | |
| 160 | pub lang: MailLang, |
| 161 | |
| 162 | pub kind: &'a str, |
| 163 | |
| 164 | pub actor: &'a str, |
| 165 | |
| 166 | pub subject_name: &'a str, |
| 167 | |
| 168 | pub key: &'a str, |
| 169 | |
| 170 | pub title: &'a str, |
| 171 | |
| 172 | pub column: &'a str, |
| 173 | |
| 174 | pub deadline: Option<&'a str>, |
| 175 | |
| 176 | pub assignees: &'a [String], |
| 177 | |
| 178 | pub when: Option<&'a str>, |
| 179 | |
| 180 | pub link: &'a str, |
| 181 | |
| 182 | pub details: bool, |
| 183 | |
| 184 | pub subject: &'a str, |
| 185 | } |
| 186 | |
| 187 | |
| 188 | pub fn rule_body(m: &RuleMail<'_>) -> String { |
| 189 | let lang = m.lang; |
| 190 | let actor = clean_text(m.actor); |
| 191 | let subject_name = clean_text(m.subject_name); |
| 192 | let sentence = match m.kind { |
| 193 | "status" => format!( |
| 194 | "{actor} {} {}.", |
| 195 | text(lang, K::SentStatus), |
| 196 | clean_text(m.column) |
| 197 | ), |
| 198 | "created" => format!("{actor} {}.", text(lang, K::SentCreated)), |
| 199 | "assigned" => format!("{actor} {} {subject_name}.", text(lang, K::SentAssigned)), |
| 200 | "unassigned" => format!("{actor} {} {subject_name}.", text(lang, K::SentUnassigned)), |
| 201 | "commented" => format!("{actor} {}.", text(lang, K::SentCommented)), |
| 202 | "deadline_set" => format!("{actor} {}.", text(lang, K::SentDeadlineSet)), |
| 203 | "deadline_cleared" => format!("{actor} {}.", text(lang, K::SentDeadlineCleared)), |
| 204 | "retitled" => format!("{actor} {}.", text(lang, K::SentRetitled)), |
| 205 | "linked" => format!("{actor} {}.", text(lang, K::SentLinked)), |
| 206 | "unlinked" => format!("{actor} {}.", text(lang, K::SentUnlinked)), |
| 207 | "deleted" => format!("{actor} {}.", text(lang, K::SentDeleted)), |
| 208 | _ => text(lang, K::SentUnblocked).to_owned(), |
| 209 | }; |
| 210 | let mut lines = vec![ |
| 211 | format!( |
| 212 | "{}: {} — {}", |
| 213 | text(lang, K::Key), |
| 214 | clean_text(m.key), |
| 215 | clean_text(m.title) |
| 216 | ), |
| 217 | sentence, |
| 218 | ]; |
| 219 | if m.details { |
| 220 | lines.push(format!( |
| 221 | "{}: {}", |
| 222 | text(lang, K::Column), |
| 223 | clean_text(m.column) |
| 224 | )); |
| 225 | if let Some(day) = m.deadline { |
| 226 | lines.push(format!("{}: {}", text(lang, K::Deadline), clean_text(day))); |
| 227 | } |
| 228 | let names: Vec<String> = m.assignees.iter().map(|n| clean_text(n)).collect(); |
| 229 | if !names.is_empty() { |
| 230 | lines.push(format!( |
| 231 | "{}: {}", |
| 232 | text(lang, K::Assignees), |
| 233 | names.join(", ") |
| 234 | )); |
| 235 | } |
| 236 | if let Some(when) = m.when { |
| 237 | lines.push(format!("{}: {}", text(lang, K::When), clean_text(when))); |
| 238 | } |
| 239 | } |
| 240 | lines.push(format!("{}: {}", text(lang, K::Link), clean_text(m.link))); |
| 241 | lines.join("\n") |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | pub fn reminder(lang: MailLang, key: &str, title: &str, when: &str, link: &str) -> Mail { |
| 247 | Mail { |
| 248 | subject: clean_text(&format!( |
| 249 | "{} {} ({})", |
| 250 | text(lang, K::ReminderSubject), |
| 251 | title, |
| 252 | key |
| 253 | )) |
| 254 | .chars() |
| 255 | .take(crate::limits::MAIL_SUBJECT_MAX) |
| 256 | .collect(), |
| 257 | body: [ |
| 258 | text(lang, K::ReminderLead).to_owned(), |
| 259 | format!( |
| 260 | "{}: {} — {}", |
| 261 | text(lang, K::Key), |
| 262 | clean_text(key), |
| 263 | clean_text(title) |
| 264 | ), |
| 265 | format!("{}: {}", text(lang, K::When), clean_text(when)), |
| 266 | format!("{}: {}", text(lang, K::Link), clean_text(link)), |
| 267 | ] |
| 268 | .join("\n"), |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | |
| 273 | pub fn test_mail(lang: MailLang) -> Mail { |
| 274 | Mail { |
| 275 | subject: text(lang, K::TestSubject).to_owned(), |
| 276 | body: text(lang, K::TestBody).to_owned(), |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | #[cfg(test)] |
| 281 | mod tests { |
| 282 | use super::*; |
| 283 | |
| 284 | #[test] |
| 285 | fn every_key_has_text_in_both_languages() { |
| 286 | for key in [ |
| 287 | K::Key, |
| 288 | K::Column, |
| 289 | K::Deadline, |
| 290 | K::Assignees, |
| 291 | K::When, |
| 292 | K::Link, |
| 293 | K::TestSubject, |
| 294 | ] { |
| 295 | assert!(!text(MailLang::En, key).is_empty()); |
| 296 | assert!(!text(MailLang::Tr, key).is_empty()); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn the_locale_override_wins_over_the_directory() { |
| 302 | assert_eq!(MailLang::of(Some("en"), Some("tr")), MailLang::En); |
| 303 | assert_eq!(MailLang::of(None, Some("tr")), MailLang::Tr); |
| 304 | assert_eq!(MailLang::of(None, None), MailLang::En); |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn meeting_time_is_shown_in_the_members_zone() { |
| 309 | let text = when_text("2026-10-28T12:00:00.000000000Z", "Europe/Istanbul").unwrap(); |
| 310 | assert_eq!(text, "2026-10-28 15:00 Europe/Istanbul"); |
| 311 | } |
| 312 | } |