BlueGit

ict rusty-teams

Files

rusty-teams / crates / kvt-sleutels / src / asclepius.rs

asclepius.rs

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