BlueGit

ict rusty-teams

Files

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

profile_model.rs

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