BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / profile_model.rs

profile_model.rs

profile_model.rs 6.9 KiB Download Raw
1use std::time::{Duration, Instant};
2
3use chrono::{DateTime, Utc};
4use teams_core::{PersonProfile, WorkLocation, local_time};
5
6pub const PROFILE_MAX_AGE: Duration = Duration::from_secs(10 * 60);
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum ContactKind {
10 Email,
11 WorkPhone,
12 MobilePhone,
13 Office,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct ContactRow {
18 pub kind: ContactKind,
19 pub value: String,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct StatusBox {
24 pub text: String,
25 pub out_of_office: bool,
26}
27
28pub fn status_line(availability: &str, local_time: Option<&str>) -> String {
29 match (availability.is_empty(), local_time) {
30 (false, Some(time)) => format!("{availability} - {time} local time"),
31 (false, None) => availability.to_owned(),
32 (true, Some(time)) => format!("{time} local time"),
33 (true, None) => String::new(),
34 }
35}
36
37pub fn local_time_label(profile: &PersonProfile, now: DateTime<Utc>) -> Option<String> {
38 let zone = profile.time_zone.as_deref()?;
39 Some(local_time(zone, now)?.format("%H:%M").to_string())
40}
41
42pub fn work_location_label(location: WorkLocation) -> &'static str {
43 crate::own_status::work_location_label(Some(location))
44}
45
46pub fn status_box(profile: &PersonProfile) -> Option<StatusBox> {
47 if let Some(text) = &profile.out_of_office {
48 let text = match text.trim() {
49 "" => "Out of office".to_owned(),
50 text => text.to_owned(),
51 };
52 return Some(StatusBox {
53 text,
54 out_of_office: true,
55 });
56 }
57 profile.status_message.clone().map(|text| StatusBox {
58 text,
59 out_of_office: false,
60 })
61}
62
63pub fn contact_rows(profile: &PersonProfile) -> Vec<ContactRow> {
64 let present = |value: &Option<String>| value.clone().filter(|value| !value.trim().is_empty());
65 let mut rows = Vec::new();
66 let mut push = |kind: ContactKind, value: Option<String>| {
67 if let Some(value) = value {
68 rows.push(ContactRow { kind, value });
69 }
70 };
71 push(ContactKind::Email, present(&profile.email));
72 for phone in &profile.work_phones {
73 push(ContactKind::WorkPhone, present(&Some(phone.clone())));
74 }
75 push(ContactKind::MobilePhone, present(&profile.mobile_phone));
76 push(ContactKind::Office, present(&profile.office_location));
77 rows
78}
79
80pub fn has_organization(profile: &PersonProfile) -> bool {
81 profile.manager.is_some() || !profile.direct_reports.is_empty()
82}
83
84pub fn mailto_url(email: &str) -> String {
85 format!("mailto:{email}")
86}
87
88pub fn is_stale(fetched_at: Instant, now: Instant) -> bool {
89 now.duration_since(fetched_at) >= PROFILE_MAX_AGE
90}
91
92#[cfg(test)]
93mod tests {
94 use chrono::TimeZone;
95 use teams_core::{OrgPerson, WorkLocationKind, WorkLocationSource};
96
97 use super::*;
98
99 fn profile() -> PersonProfile {
100 PersonProfile {
101 user_id: "u".into(),
102 display_name: Some("Ada".into()),
103 job_title: None,
104 department: None,
105 company: None,
106 office_location: None,
107 email: None,
108 work_phones: Vec::new(),
109 mobile_phone: None,
110 manager: None,
111 direct_reports: Vec::new(),
112 time_zone: None,
113 availability: None,
114 status_message: None,
115 out_of_office: None,
116 work_location: None,
117 }
118 }
119
120 #[test]
121 fn status_line_joins_availability_and_local_time() {
122 assert_eq!(
123 status_line("Available", Some("14:35")),
124 "Available - 14:35 local time"
125 );
126 assert_eq!(status_line("Busy", None), "Busy");
127 assert_eq!(status_line("", Some("09:05")), "09:05 local time");
128 assert_eq!(status_line("", None), "");
129 }
130
131 #[test]
132 fn local_time_needs_a_known_zone() {
133 let now = Utc.with_ymd_and_hms(2026, 10, 9, 12, 0, 0).unwrap();
134 let mut person = profile();
135 assert_eq!(local_time_label(&person, now), None);
136 person.time_zone = Some("W. Europe Standard Time".into());
137 assert_eq!(local_time_label(&person, now).as_deref(), Some("14:00"));
138 person.time_zone = Some("Unknown Zone".into());
139 assert_eq!(local_time_label(&person, now), None);
140 }
141
142 #[test]
143 fn work_location_labels_follow_the_source() {
144 let at = |kind, source| work_location_label(WorkLocation { kind, source });
145 assert_eq!(
146 at(WorkLocationKind::Office, WorkLocationSource::Set),
147 "In the office"
148 );
149 assert_eq!(
150 at(WorkLocationKind::Office, WorkLocationSource::Scheduled),
151 "Planned in the office"
152 );
153 assert_eq!(
154 at(WorkLocationKind::Office, WorkLocationSource::Verified),
155 "Checked in to the office"
156 );
157 assert_eq!(
158 at(WorkLocationKind::Remote, WorkLocationSource::Set),
159 "Working remotely"
160 );
161 assert_eq!(
162 at(WorkLocationKind::Remote, WorkLocationSource::Scheduled),
163 "Planned to work remotely"
164 );
165 }
166
167 #[test]
168 fn out_of_office_replaces_the_status_message() {
169 let mut person = profile();
170 person.status_message = Some("Focus".into());
171 assert_eq!(
172 status_box(&person),
173 Some(StatusBox {
174 text: "Focus".into(),
175 out_of_office: false
176 })
177 );
178 person.out_of_office = Some("Back Monday".into());
179 assert_eq!(
180 status_box(&person),
181 Some(StatusBox {
182 text: "Back Monday".into(),
183 out_of_office: true
184 })
185 );
186 person.out_of_office = Some(" ".into());
187 assert_eq!(status_box(&person).unwrap().text, "Out of office");
188 }
189
190 #[test]
191 fn contact_rows_hide_empty_fields() {
192 let mut person = profile();
193 person.email = Some("ada@example.com".into());
194 person.work_phones = vec!["+49 1".into(), " ".into()];
195 person.mobile_phone = Some(String::new());
196 person.office_location = Some("Berlin".into());
197 let kinds: Vec<ContactKind> = contact_rows(&person)
198 .into_iter()
199 .map(|row| row.kind)
200 .collect();
201 assert_eq!(
202 kinds,
203 vec![
204 ContactKind::Email,
205 ContactKind::WorkPhone,
206 ContactKind::Office
207 ]
208 );
209 assert!(contact_rows(&profile()).is_empty());
210 }
211
212 #[test]
213 fn organization_needs_a_manager_or_reports() {
214 let mut person = profile();
215 assert!(!has_organization(&person));
216 person.direct_reports.push(OrgPerson::default());
217 assert!(has_organization(&person));
218 }
219
220 #[test]
221 fn profiles_go_stale_after_ten_minutes() {
222 let start = Instant::now();
223 assert!(!is_stale(start, start + Duration::from_secs(599)));
224 assert!(is_stale(start, start + Duration::from_secs(600)));
225 }
226}