BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / switcher_server.rs

switcher_server.rs

switcher_server.rs 7.0 KiB Download Raw
1use std::collections::{HashMap, HashSet};
2
3use chrono::NaiveDate;
4use teams_core::substrate_search::{SearchMessage, SearchPerson, split_highlights};
5
6use super::switcher::MessageResult;
7use crate::app_state::Selection;
8use crate::format;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct PersonResult {
12 pub user_id: String,
13 pub name: String,
14 pub subtitle: String,
15}
16
17pub fn people_results(
18 people: &[SearchPerson],
19 shown_titles: &HashSet<String>,
20 my_user_id: Option<&str>,
21 limit: usize,
22) -> Vec<PersonResult> {
23 people
24 .iter()
25 .filter_map(|person| {
26 let user_id = person.user_id.clone()?;
27 let subtitle = person_subtitle(person);
28 Some(PersonResult {
29 user_id,
30 name: person.display_name.clone(),
31 subtitle,
32 })
33 })
34 .filter(|person| Some(person.user_id.as_str()) != my_user_id)
35 .filter(|person| !shown_titles.contains(&person.name))
36 .take(limit)
37 .collect()
38}
39
40fn person_subtitle(person: &SearchPerson) -> String {
41 let primary = person
42 .job_title
43 .as_deref()
44 .filter(|title| !title.is_empty())
45 .or_else(|| person.email_addresses.first().map(String::as_str));
46 let extra = person
47 .company_name
48 .as_deref()
49 .filter(|name| !name.is_empty())
50 .or_else(|| {
51 person
52 .job_title
53 .as_deref()
54 .filter(|title| !title.is_empty())
55 .and_then(|_| {
56 person
57 .department
58 .as_deref()
59 .filter(|department| !department.is_empty())
60 })
61 });
62 [primary, extra]
63 .into_iter()
64 .flatten()
65 .collect::<Vec<_>>()
66 .join(", ")
67}
68
69pub fn server_message_results(
70 messages: &[SearchMessage],
71 conversations: &HashMap<String, (String, Selection)>,
72 today: NaiveDate,
73 offset: app_base::format::Zone,
74) -> Vec<MessageResult> {
75 messages
76 .iter()
77 .filter_map(|message| {
78 let (conversation, selection) = conversations.get(&message.conversation_id)?.clone();
79 let highlighted = split_highlights(
80 message
81 .highlighted_summary
82 .as_deref()
83 .unwrap_or(&message.preview),
84 );
85 Some(MessageResult {
86 selection,
87 message_id: message.message_id.clone(),
88 author: message
89 .sender_name
90 .clone()
91 .unwrap_or_else(|| "Unknown".to_owned()),
92 conversation,
93 date: message
94 .sent_at
95 .map(|sent_at| format::list_time_label(sent_at, today, offset))
96 .unwrap_or_default(),
97 snippet: highlighted.text.replace(['\n', '\r'], " "),
98 matches: highlighted.hits,
99 })
100 })
101 .collect()
102}
103
104pub fn merge_messages(
105 local: &[MessageResult],
106 server: Vec<MessageResult>,
107 limit: usize,
108) -> Vec<MessageResult> {
109 let mut seen: HashSet<String> = HashSet::new();
110 local
111 .iter()
112 .cloned()
113 .chain(server)
114 .filter(|message| seen.insert(message.message_id.clone()))
115 .take(limit)
116 .collect()
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 fn person(user_id: &str, name: &str) -> SearchPerson {
124 SearchPerson {
125 reference_id: None,
126 user_id: Some(user_id.to_owned()),
127 mri: None,
128 display_name: name.to_owned(),
129 given_name: None,
130 surname: None,
131 email_addresses: Vec::new(),
132 user_principal_name: None,
133 job_title: Some("Engineer".to_owned()),
134 department: Some("AI".to_owned()),
135 company_name: None,
136 office_location: None,
137 people_subtype: None,
138 }
139 }
140
141 fn message(id: &str, conversation_id: &str) -> MessageResult {
142 MessageResult {
143 selection: Selection::Chat(conversation_id.to_owned()),
144 message_id: id.to_owned(),
145 author: "Ada".to_owned(),
146 conversation: "Chat".to_owned(),
147 date: String::new(),
148 snippet: String::new(),
149 matches: Vec::new(),
150 }
151 }
152
153 #[test]
154 fn people_skip_me_shown_chats_and_entries_without_an_id() {
155 let mut nameless = person("x", "No Id");
156 nameless.user_id = None;
157 let people = [
158 person("me", "Me Myself"),
159 person("a", "Ada Lovelace"),
160 person("b", "Grace Hopper"),
161 nameless,
162 ];
163 let shown = HashSet::from(["Ada Lovelace".to_owned()]);
164 let results = people_results(&people, &shown, Some("me"), 5);
165 assert_eq!(
166 results,
167 [PersonResult {
168 user_id: "b".to_owned(),
169 name: "Grace Hopper".to_owned(),
170 subtitle: "Engineer, AI".to_owned(),
171 }]
172 );
173 }
174
175 #[test]
176 fn an_external_person_shows_email_and_organization() {
177 let mut person = person("ada", "Ada Lovelace");
178 person.job_title = None;
179 person.department = None;
180 person.email_addresses = vec!["ada@other.example".into()];
181 person.company_name = Some("Other AG".into());
182 let results = people_results(&[person], &HashSet::new(), None, 5);
183 assert_eq!(results[0].subtitle, "ada@other.example, Other AG");
184 }
185
186 #[test]
187 fn server_messages_need_a_known_conversation_and_keep_highlights() {
188 let conversations = HashMap::from([(
189 "19:a".to_owned(),
190 ("Ada".to_owned(), Selection::Chat("19:a".to_owned())),
191 )]);
192 let hit = |conversation_id: &str| SearchMessage {
193 conversation_id: conversation_id.to_owned(),
194 message_id: "1".to_owned(),
195 highlighted_summary: Some("the <c0>build</c0>\nis green".to_owned()),
196 ..SearchMessage::default()
197 };
198 let today = NaiveDate::from_ymd_opt(2026, 11, 8).unwrap();
199 let offset = chrono::FixedOffset::east_opt(0).unwrap().into();
200 let results = server_message_results(
201 &[hit("19:a"), hit("19:unknown")],
202 &conversations,
203 today,
204 offset,
205 );
206 assert_eq!(results.len(), 1);
207 assert_eq!(results[0].snippet, "the build is green");
208 assert_eq!(results[0].matches, [4..9]);
209 }
210
211 #[test]
212 fn merged_messages_keep_local_first_without_duplicates() {
213 let merged = merge_messages(
214 &[message("1", "19:a")],
215 vec![
216 message("1", "19:a"),
217 message("2", "19:a"),
218 message("3", "19:a"),
219 ],
220 2,
221 );
222 let ids: Vec<&str> = merged
223 .iter()
224 .map(|message| message.message_id.as_str())
225 .collect();
226 assert_eq!(ids, ["1", "2"]);
227 }
228}