//! Berichtmenu: «Ticket maken in Asclepius» en «Taak maken in Ponos» (beide een //! dialoog in de app, vooraf ingevuld met het bericht). //! //! Upstream-haak: `views/message_actions.rs` (veld `kvt` in `MessageMenu`) en //! `views/conversation.rs` (`message_menu`). use std::rc::Rc; use chrono::Local; use gpui_kit::assets::IconName; use gpui_kit::*; use kvt_sleutels::asclepius::MessageSource; use kvt_sleutels::format::date_time_nl; use super::installed; use crate::app_state::AppHandle; use crate::i18n::tr; use crate::runtime; pub type MenuAction = Rc; #[derive(Clone)] pub struct MenuEntry { pub label: String, pub icon: IconName, pub action: MenuAction, } /// De KVT-regels voor het menu van één bericht; leeg buiten een gesprek. pub fn menu_entries(conversation_id: Option, message_id: &str, cx: &App) -> Vec { let Some(conversation_id) = conversation_id.filter(|_| installed(cx)) else { return Vec::new(); }; let entry = |label: &str, icon: IconName, run: fn(MessageSource, &mut Window, &mut App)| { let (conversation_id, message_id) = (conversation_id.clone(), message_id.to_owned()); MenuEntry { label: tr(label), icon, action: Rc::new(move |window, cx| { let handle = window.window_handle(); // Uitgesteld: het venster is tijdens de menuklik nog in gebruik. with_source(&conversation_id, &message_id, cx, move |source, cx| { cx.defer(move |cx| { handle .update(cx, |_, window, cx| run(source, window, cx)) .ok(); }) }) }), } }; vec![ entry( "kvt.message.asclepius", IconName::Ticket, |source, window, cx| super::ticket_form::open_new(Some(source), window, cx), ), entry( "kvt.message.ponos", IconName::ClipboardCheck, |source, window, cx| super::tasks::open_create(source, window, cx), ), ] } /// Tekst, afzender en tijd uit de opslag; de Teams-link erbij als die er is. fn with_source( conversation_id: &str, message_id: &str, cx: &mut App, then: impl FnOnce(MessageSource, &mut App) + 'static, ) { let Some(handle) = cx.try_global::() else { return; }; let state = handle.0.read(cx); let Some(record) = state.message_record(conversation_id, message_id) else { log::warn!("kvt: message not in the store"); return; }; let source = MessageSource { text: crate::rows::message_text(&record), sender: record.sender_name.clone().unwrap_or_default(), sent_at: date_time_nl(record.created_at.with_timezone(&Local).naive_local()), link: None, mentions: Vec::new(), }; let mentioned = mentioned_users(&record.mentions_json); let Some(engine) = state.engine.clone() else { let link = state .mode .demo .then(|| teams_core::chat_message_link(conversation_id, message_id, None)); then(MessageSource { link, ..source }, cx); return; }; let (conversation_id, message_id) = (conversation_id.to_owned(), message_id.to_owned()); let linking = runtime::spawn(async move { let link = engine .message_link(&conversation_id, &message_id) .await .ok(); // Vermeldingen: het adres via het profiel (best effort), voor `@naam:domein` in Asclepius. let mut mentions = Vec::new(); for (name, user_id) in mentioned { if let Ok(profile) = engine.person_profile(&user_id).await && let Some(email) = profile.email.filter(|email| email.contains('@')) { mentions.push((name, email)); } } (link, mentions) }); cx.spawn(async move |cx| { let (link, mentions) = linking.await.unwrap_or_default(); cx.update(|cx| { then( MessageSource { link, mentions, ..source }, cx, ) }); }) .detach(); } /// Vermelde personen (naam, Teams-id) uit het bericht; groepen en dubbelen niet. fn mentioned_users(mentions_json: &str) -> Vec<(String, String)> { let infos: Vec = serde_json::from_str(mentions_json).unwrap_or_default(); let mut users: Vec<(String, String)> = Vec::new(); for info in infos.into_iter().filter(|info| !info.group) { let Some(user_id) = info.user_id.filter(|id| !id.is_empty()) else { continue; }; let name = info.name.trim().to_owned(); if name.is_empty() || users.iter().any(|(_, known)| *known == user_id) { continue; } users.push((name, user_id)); } users.truncate(MENTIONS_MAX); users } /// Zoveel profielen halen we hooguit op voor één bericht. const MENTIONS_MAX: usize = 10; #[cfg(test)] mod tests { use super::mentioned_users; #[test] fn mentioned_users_skips_groups_and_duplicates() { let json = r#"[ {"user_id":"8:orgid:a","name":"Jonas Ortega","id":0}, {"user_id":"8:orgid:a","name":"Jonas","id":1}, {"user_id":null,"name":"Iedereen","id":2,"group":true}, {"user_id":"8:orgid:b","name":"Mara","id":3} ]"#; assert_eq!( mentioned_users(json), [ ("Jonas Ortega".to_owned(), "8:orgid:a".to_owned()), ("Mara".to_owned(), "8:orgid:b".to_owned()) ] ); assert!(mentioned_users("geen json").is_empty()); } }