BlueGit

ict rusty-teams

Files

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

switcher_server.rs

switcher_server.rs 5.8 KiB Download Raw
1use std::collections::{HashMap, HashSet};
2
3use chrono::{FixedOffset, 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.job_title.as_deref(), person.department.as_deref()]
28 .into_iter()
29 .flatten()
30 .filter(|part| !part.is_empty())
31 .collect::<Vec<_>>()
32 .join(", ");
33 Some(PersonResult {
34 user_id,
35 name: person.display_name.clone(),
36 subtitle,
37 })
38 })
39 .filter(|person| Some(person.user_id.as_str()) != my_user_id)
40 .filter(|person| !shown_titles.contains(&person.name))
41 .take(limit)
42 .collect()
43}
44
45pub fn server_message_results(
46 messages: &[SearchMessage],
47 conversations: &HashMap<String, (String, Selection)>,
48 today: NaiveDate,
49 offset: FixedOffset,
50) -> Vec<MessageResult> {
51 messages
52 .iter()
53 .filter_map(|message| {
54 let (conversation, selection) = conversations.get(&message.conversation_id)?.clone();
55 let highlighted = split_highlights(
56 message
57 .highlighted_summary
58 .as_deref()
59 .unwrap_or(&message.preview),
60 );
61 Some(MessageResult {
62 selection,
63 message_id: message.message_id.clone(),
64 author: message
65 .sender_name
66 .clone()
67 .unwrap_or_else(|| "Unknown".to_owned()),
68 conversation,
69 date: message
70 .sent_at
71 .map(|sent_at| format::list_time_label(sent_at, today, offset))
72 .unwrap_or_default(),
73 snippet: highlighted.text.replace(['\n', '\r'], " "),
74 matches: highlighted.hits,
75 })
76 })
77 .collect()
78}
79
80pub fn merge_messages(
81 local: &[MessageResult],
82 server: Vec<MessageResult>,
83 limit: usize,
84) -> Vec<MessageResult> {
85 let mut seen: HashSet<String> = HashSet::new();
86 local
87 .iter()
88 .cloned()
89 .chain(server)
90 .filter(|message| seen.insert(message.message_id.clone()))
91 .take(limit)
92 .collect()
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 fn person(user_id: &str, name: &str) -> SearchPerson {
100 SearchPerson {
101 reference_id: None,
102 user_id: Some(user_id.to_owned()),
103 mri: None,
104 display_name: name.to_owned(),
105 given_name: None,
106 surname: None,
107 email_addresses: Vec::new(),
108 user_principal_name: None,
109 job_title: Some("Engineer".to_owned()),
110 department: Some("AI".to_owned()),
111 company_name: None,
112 office_location: None,
113 people_subtype: None,
114 }
115 }
116
117 fn message(id: &str, conversation_id: &str) -> MessageResult {
118 MessageResult {
119 selection: Selection::Chat(conversation_id.to_owned()),
120 message_id: id.to_owned(),
121 author: "Ada".to_owned(),
122 conversation: "Chat".to_owned(),
123 date: String::new(),
124 snippet: String::new(),
125 matches: Vec::new(),
126 }
127 }
128
129 #[test]
130 fn people_skip_me_shown_chats_and_entries_without_an_id() {
131 let mut nameless = person("x", "No Id");
132 nameless.user_id = None;
133 let people = [
134 person("me", "Me Myself"),
135 person("a", "Ada Lovelace"),
136 person("b", "Grace Hopper"),
137 nameless,
138 ];
139 let shown = HashSet::from(["Ada Lovelace".to_owned()]);
140 let results = people_results(&people, &shown, Some("me"), 5);
141 assert_eq!(
142 results,
143 [PersonResult {
144 user_id: "b".to_owned(),
145 name: "Grace Hopper".to_owned(),
146 subtitle: "Engineer, AI".to_owned(),
147 }]
148 );
149 }
150
151 #[test]
152 fn server_messages_need_a_known_conversation_and_keep_highlights() {
153 let conversations = HashMap::from([(
154 "19:a".to_owned(),
155 ("Ada".to_owned(), Selection::Chat("19:a".to_owned())),
156 )]);
157 let hit = |conversation_id: &str| SearchMessage {
158 conversation_id: conversation_id.to_owned(),
159 message_id: "1".to_owned(),
160 highlighted_summary: Some("the <c0>build</c0>\nis green".to_owned()),
161 ..SearchMessage::default()
162 };
163 let today = NaiveDate::from_ymd_opt(2026, 11, 8).unwrap();
164 let offset = FixedOffset::east_opt(0).unwrap();
165 let results =
166 server_message_results(&[hit("19:a"), hit("19:unknown")], &conversations, today, offset);
167 assert_eq!(results.len(), 1);
168 assert_eq!(results[0].snippet, "the build is green");
169 assert_eq!(results[0].matches, [4..9]);
170 }
171
172 #[test]
173 fn merged_messages_keep_local_first_without_duplicates() {
174 let merged = merge_messages(
175 &[message("1", "19:a")],
176 vec![message("1", "19:a"), message("2", "19:a"), message("3", "19:a")],
177 2,
178 );
179 let ids: Vec<&str> = merged.iter().map(|message| message.message_id.as_str()).collect();
180 assert_eq!(ids, ["1", "2"]);
181 }
182}