| 1 | //! «Ticket maken van bericht»: het nieuw-ticketformulier van Asclepius in de |
| 2 | //! browser, vooraf ingevuld via `new_title` en `new_description` (Markdown). |
| 3 | //! Geen token of API: de gebruiker kiest zelf de categorie en slaat op. |
| 4 | |
| 5 | use url::Url; |
| 6 | |
| 7 | /// Asclepius kort de titel in op 150 tekens. |
| 8 | pub const TITLE_MAX: usize = 150; |
| 9 | /// Ruim onder de URL-grens van browsers; Asclepius zelf staat 20 000 toe. |
| 10 | pub const DESCRIPTION_MAX: usize = 4000; |
| 11 | |
| 12 | /// Het bericht waar een ticket of taak van gemaakt wordt. |
| 13 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 14 | pub struct MessageSource { |
| 15 | /// Platte tekst (Markdown) van het bericht. |
| 16 | pub text: String, |
| 17 | pub sender: String, |
| 18 | /// Al opgemaakt als `15 jan 2026 13:39`. |
| 19 | pub sent_at: String, |
| 20 | /// Link naar het bericht in Teams, als die er is. |
| 21 | pub link: Option<String>, |
| 22 | /// Vermelde personen in het bericht (naam zoals in de tekst, e-mailadres), als bekend. |
| 23 | pub mentions: Vec<(String, String)>, |
| 24 | } |
| 25 | |
| 26 | impl MessageSource { |
| 27 | /// Eerste niet-lege regel, zonder Markdown-tekens vooraan, ingekort. |
| 28 | pub fn title(&self, max: usize) -> String { |
| 29 | let line = self |
| 30 | .text |
| 31 | .lines() |
| 32 | .map(|line| { |
| 33 | line.trim() |
| 34 | .trim_start_matches(['#', '>', '-', '*', ' ']) |
| 35 | .trim() |
| 36 | }) |
| 37 | .find(|line| !line.is_empty()) |
| 38 | .unwrap_or_default(); |
| 39 | shorten(line, max) |
| 40 | } |
| 41 | |
| 42 | /// Het bericht als citaat, met afzender, tijd en link eronder. |
| 43 | pub fn description(&self, max: usize) -> String { |
| 44 | let mut footer = format!("Uit een Teams-bericht van {}", self.sender); |
| 45 | if !self.sent_at.is_empty() { |
| 46 | footer.push_str(&format!(" ({})", self.sent_at)); |
| 47 | } |
| 48 | if let Some(link) = self |
| 49 | .link |
| 50 | .as_deref() |
| 51 | .filter(|link| link.starts_with("https://")) |
| 52 | { |
| 53 | footer.push_str(&format!(": {link}")); |
| 54 | } |
| 55 | let room = max.saturating_sub(footer.chars().count() + 4); |
| 56 | let quoted = shorten(self.text.trim(), room) |
| 57 | .lines() |
| 58 | .map(|line| format!("> {line}")) |
| 59 | .collect::<Vec<_>>() |
| 60 | .join("\n"); |
| 61 | format!("{quoted}\n\n{footer}") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn shorten(text: &str, max: usize) -> String { |
| 66 | if text.chars().count() <= max { |
| 67 | return text.to_owned(); |
| 68 | } |
| 69 | let mut short: String = text.chars().take(max.saturating_sub(1)).collect(); |
| 70 | short = short.trim_end().to_owned(); |
| 71 | short.push('…'); |
| 72 | short |
| 73 | } |
| 74 | |
| 75 | /// `https://sleutels.kvt.nl/asclepius/?new_title=…&new_description=…`. |
| 76 | pub fn new_ticket_url(base_url: &str, source: &MessageSource) -> String { |
| 77 | let mut url = Url::parse(&format!("{}/asclepius/", base_url.trim_end_matches('/'))) |
| 78 | .unwrap_or_else(|_| Url::parse("https://sleutels.kvt.nl/asclepius/").expect("vaste url")); |
| 79 | url.query_pairs_mut() |
| 80 | .append_pair("new_title", &source.title(TITLE_MAX)) |
| 81 | .append_pair("new_description", &source.description(DESCRIPTION_MAX)); |
| 82 | url.to_string() |
| 83 | } |
| 84 | |
| 85 | #[cfg(test)] |
| 86 | mod tests { |
| 87 | use super::*; |
| 88 | |
| 89 | fn source() -> MessageSource { |
| 90 | MessageSource { |
| 91 | text: "## Printer op kantoor\nDe printer bij de receptie geeft & weer een fout.".into(), |
| 92 | sender: "Mara Lindqvist".into(), |
| 93 | sent_at: "15 jan 2026 13:39".into(), |
| 94 | link: Some("https://teams.microsoft.com/l/message/19:x/1".into()), |
| 95 | mentions: Vec::new(), |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn title_is_the_first_line_without_markdown() { |
| 101 | assert_eq!(source().title(150), "Printer op kantoor"); |
| 102 | assert_eq!(source().title(8), "Printer…"); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn description_quotes_the_message_and_names_the_source() { |
| 107 | let description = source().description(4000); |
| 108 | assert!(description.starts_with("> ## Printer op kantoor\n> De printer")); |
| 109 | assert!(description.ends_with("Uit een Teams-bericht van Mara Lindqvist (15 jan 2026 13:39): https://teams.microsoft.com/l/message/19:x/1")); |
| 110 | let long = MessageSource { |
| 111 | text: "x".repeat(10_000), |
| 112 | ..source() |
| 113 | }; |
| 114 | assert!(long.description(DESCRIPTION_MAX).chars().count() <= DESCRIPTION_MAX); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn url_prefills_title_and_description_only() { |
| 119 | let url = Url::parse(&new_ticket_url("https://sleutels.kvt.nl", &source())).unwrap(); |
| 120 | assert_eq!(url.path(), "/asclepius/"); |
| 121 | let pairs: Vec<(String, String)> = url.query_pairs().into_owned().collect(); |
| 122 | assert_eq!(pairs[0], ("new_title".into(), "Printer op kantoor".into())); |
| 123 | assert_eq!(pairs[1].0, "new_description"); |
| 124 | assert!(pairs[1].1.contains("geeft & weer")); |
| 125 | assert!(!url.as_str().contains("new_category")); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn a_non_https_link_is_left_out() { |
| 130 | let source = MessageSource { |
| 131 | link: Some("javascript:alert(1)".into()), |
| 132 | ..source() |
| 133 | }; |
| 134 | assert!(!source.description(4000).contains("javascript")); |
| 135 | } |
| 136 | } |