forge
Explore
Log in

Don't you dare go Hollow

iz

Clone
★ 0

0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits

mail_text.rs312 lines, 11.4 KiBHistoryRawDownload
1//! The words of every mail iz sends: rule mails, reminders, admin notices and the test mail, in English
2//! and Turkish. Pure text: no I/O, no HTML (the web layer escapes the plain text when it builds the HTML part).
3//!
4//! The strings are keyed by [`K`] and matched exhaustively on `(key, language)`, so a missing translation
5//! does not compile. Values (names, titles, times) are cleaned with [`clean_text`] before they are placed,
6//! so no value can add a line to a subject. The brand in every text is the single word `iz`.
7
8use jiff::Timestamp;
9
10use crate::rules::clean_text;
11use crate::zones::{parse_zone, utc_to_local};
12
13/// The language a mail is written in: the recipient's own choice, else the directory's, else English.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum MailLang {
16 /// English.
17 En,
18 /// Turkish.
19 Tr,
20}
21
22impl MailLang {
23 /// The language for a member: `locale_override` first, then the directory `locale`, else English.
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/// A subject and a plain-text body, ready to be stored in the outbox.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct Mail {
35 /// The subject, plain text, already cleaned and short.
36 pub subject: String,
37 /// The body, plain text with `\n` line breaks.
38 pub body: String,
39}
40
41/// Every fixed string of a mail. Matched exhaustively in [`text`].
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum K {
44 /// Heading label of the key and title line.
45 Key,
46 /// Label of the column.
47 Column,
48 /// Label of the deadline day.
49 Deadline,
50 /// Label of the assignees.
51 Assignees,
52 /// Label of the meeting time.
53 When,
54 /// Label of the link back to the board.
55 Link,
56 /// Sentence: the task moved to a column.
57 SentStatus,
58 /// Sentence: the task was created.
59 SentCreated,
60 /// Sentence: someone was assigned.
61 SentAssigned,
62 /// Sentence: someone was removed from the assignees.
63 SentUnassigned,
64 /// Sentence: a comment was posted.
65 SentCommented,
66 /// Sentence: a deadline or meeting time was set.
67 SentDeadlineSet,
68 /// Sentence: a deadline or meeting time was cleared.
69 SentDeadlineCleared,
70 /// Sentence: the title changed.
71 SentRetitled,
72 /// Sentence: a dependency was added.
73 SentLinked,
74 /// Sentence: a dependency was removed.
75 SentUnlinked,
76 /// Sentence: the task was deleted.
77 SentDeleted,
78 /// Sentence: the task stopped being blocked.
79 SentUnblocked,
80 /// Subject prefix of a reminder, before the title.
81 ReminderSubject,
82 /// The reminder body's first line, before the meeting time.
83 ReminderLead,
84 /// Subject of the test mail.
85 TestSubject,
86 /// Body of the test mail.
87 TestBody,
88}
89
90/// The text of `key` in `lang`. Exhaustive over both languages.
91pub 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/// The meeting time as text in a member's zone: `2026-10-28 14:30 Europe/Istanbul`. Unknown zones are
144/// shown in UTC with the zone name `UTC`, so the text is never empty for a stored instant.
145pub 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/// Everything a rule mail says about one task and one event.
158pub struct RuleMail<'a> {
159 /// The recipient's language.
160 pub lang: MailLang,
161 /// The trigger word of the event.
162 pub kind: &'a str,
163 /// Who did it, by name.
164 pub actor: &'a str,
165 /// The person the event names, by name (assigned and unassigned events).
166 pub subject_name: &'a str,
167 /// The task key, for example `IZ-42`.
168 pub key: &'a str,
169 /// The task title.
170 pub title: &'a str,
171 /// The column name.
172 pub column: &'a str,
173 /// The deadline day, if any.
174 pub deadline: Option<&'a str>,
175 /// The assignee names, sorted as the store lists them.
176 pub assignees: &'a [String],
177 /// The meeting time in the recipient's zone, if any.
178 pub when: Option<&'a str>,
179 /// The link back to the task.
180 pub link: &'a str,
181 /// Whether the details block is included (the rule's `include_details`).
182 pub details: bool,
183 /// The subject, already rendered from the rule's template.
184 pub subject: &'a str,
185}
186
187/// The body of a rule mail: the event sentence, then the details the rule opts into, then the link.
188pub 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/// A reminder for a meeting: subject with the title and key, body with the meeting time in the
245/// recipient's zone and the link.
246pub 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/// The test mail sent to the admin who asks for one.
273pub 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)]
281mod 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}