BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / kvt / message.rs

message.rs

message.rs 5.8 KiB Download Raw
1//! Berichtmenu: «Ticket maken in Asclepius» en «Taak maken in Ponos» (beide een
2//! dialoog in de app, vooraf ingevuld met het bericht).
3//!
4//! Upstream-haak: `views/message_actions.rs` (veld `kvt` in `MessageMenu`) en
5//! `views/conversation.rs` (`message_menu`).
6
7use std::rc::Rc;
8
9use chrono::Local;
10use gpui_kit::assets::IconName;
11use gpui_kit::*;
12use kvt_sleutels::asclepius::MessageSource;
13use kvt_sleutels::format::date_time_nl;
14
15use super::installed;
16use crate::app_state::AppHandle;
17use crate::i18n::tr;
18use crate::runtime;
19
20pub type MenuAction = Rc<dyn Fn(&mut Window, &mut App)>;
21
22#[derive(Clone)]
23pub struct MenuEntry {
24 pub label: String,
25 pub icon: IconName,
26 pub action: MenuAction,
27}
28
29/// De KVT-regels voor het menu van één bericht; leeg buiten een gesprek.
30pub fn menu_entries(conversation_id: Option<String>, message_id: &str, cx: &App) -> Vec<MenuEntry> {
31 let Some(conversation_id) = conversation_id.filter(|_| installed(cx)) else {
32 return Vec::new();
33 };
34 let entry = |label: &str, icon: IconName, run: fn(MessageSource, &mut Window, &mut App)| {
35 let (conversation_id, message_id) = (conversation_id.clone(), message_id.to_owned());
36 MenuEntry {
37 label: tr(label),
38 icon,
39 action: Rc::new(move |window, cx| {
40 let handle = window.window_handle();
41 // Uitgesteld: het venster is tijdens de menuklik nog in gebruik.
42 with_source(&conversation_id, &message_id, cx, move |source, cx| {
43 cx.defer(move |cx| {
44 handle
45 .update(cx, |_, window, cx| run(source, window, cx))
46 .ok();
47 })
48 })
49 }),
50 }
51 };
52 vec![
53 entry(
54 "kvt.message.asclepius",
55 IconName::Ticket,
56 |source, window, cx| super::ticket_form::open_new(Some(source), window, cx),
57 ),
58 entry(
59 "kvt.message.ponos",
60 IconName::ClipboardCheck,
61 |source, window, cx| super::tasks::open_create(source, window, cx),
62 ),
63 ]
64}
65
66/// Tekst, afzender en tijd uit de opslag; de Teams-link erbij als die er is.
67fn with_source(
68 conversation_id: &str,
69 message_id: &str,
70 cx: &mut App,
71 then: impl FnOnce(MessageSource, &mut App) + 'static,
72) {
73 let Some(handle) = cx.try_global::<AppHandle>() else {
74 return;
75 };
76 let state = handle.0.read(cx);
77 let Some(record) = state.message_record(conversation_id, message_id) else {
78 log::warn!("kvt: message not in the store");
79 return;
80 };
81 let source = MessageSource {
82 text: crate::rows::message_text(&record),
83 sender: record.sender_name.clone().unwrap_or_default(),
84 sent_at: date_time_nl(record.created_at.with_timezone(&Local).naive_local()),
85 link: None,
86 mentions: Vec::new(),
87 };
88 let mentioned = mentioned_users(&record.mentions_json);
89 let Some(engine) = state.engine.clone() else {
90 let link = state
91 .mode
92 .demo
93 .then(|| teams_core::chat_message_link(conversation_id, message_id, None));
94 then(MessageSource { link, ..source }, cx);
95 return;
96 };
97 let (conversation_id, message_id) = (conversation_id.to_owned(), message_id.to_owned());
98 let linking = runtime::spawn(async move {
99 let link = engine
100 .message_link(&conversation_id, &message_id)
101 .await
102 .ok();
103 // Vermeldingen: het adres via het profiel (best effort), voor `@naam:domein` in Asclepius.
104 let mut mentions = Vec::new();
105 for (name, user_id) in mentioned {
106 if let Ok(profile) = engine.person_profile(&user_id).await
107 && let Some(email) = profile.email.filter(|email| email.contains('@'))
108 {
109 mentions.push((name, email));
110 }
111 }
112 (link, mentions)
113 });
114 cx.spawn(async move |cx| {
115 let (link, mentions) = linking.await.unwrap_or_default();
116 cx.update(|cx| {
117 then(
118 MessageSource {
119 link,
120 mentions,
121 ..source
122 },
123 cx,
124 )
125 });
126 })
127 .detach();
128}
129
130/// Vermelde personen (naam, Teams-id) uit het bericht; groepen en dubbelen niet.
131fn mentioned_users(mentions_json: &str) -> Vec<(String, String)> {
132 let infos: Vec<teams_core::MentionInfo> =
133 serde_json::from_str(mentions_json).unwrap_or_default();
134 let mut users: Vec<(String, String)> = Vec::new();
135 for info in infos.into_iter().filter(|info| !info.group) {
136 let Some(user_id) = info.user_id.filter(|id| !id.is_empty()) else {
137 continue;
138 };
139 let name = info.name.trim().to_owned();
140 if name.is_empty() || users.iter().any(|(_, known)| *known == user_id) {
141 continue;
142 }
143 users.push((name, user_id));
144 }
145 users.truncate(MENTIONS_MAX);
146 users
147}
148
149/// Zoveel profielen halen we hooguit op voor één bericht.
150const MENTIONS_MAX: usize = 10;
151
152#[cfg(test)]
153mod tests {
154 use super::mentioned_users;
155
156 #[test]
157 fn mentioned_users_skips_groups_and_duplicates() {
158 let json = r#"[
159 {"user_id":"8:orgid:a","name":"Jonas Ortega","id":0},
160 {"user_id":"8:orgid:a","name":"Jonas","id":1},
161 {"user_id":null,"name":"Iedereen","id":2,"group":true},
162 {"user_id":"8:orgid:b","name":"Mara","id":3}
163 ]"#;
164 assert_eq!(
165 mentioned_users(json),
166 [
167 ("Jonas Ortega".to_owned(), "8:orgid:a".to_owned()),
168 ("Mara".to_owned(), "8:orgid:b".to_owned())
169 ]
170 );
171 assert!(mentioned_users("geen json").is_empty());
172 }
173}