BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / profile.rs

profile.rs

profile.rs 4.7 KiB Download Raw
1use chatsvc::{PresenceService, PresenceStatus, WorkLocation};
2use graph::{Graph, ProfileDetails};
3
4pub use graph::OrgPerson;
5
6use crate::engine::SyncEngine;
7use crate::error::Result;
8use crate::presence::Availability;
9use crate::remote::Remote;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct PersonProfile {
13 pub user_id: String,
14 pub display_name: Option<String>,
15 pub job_title: Option<String>,
16 pub department: Option<String>,
17 pub company: Option<String>,
18 pub office_location: Option<String>,
19 pub email: Option<String>,
20 pub work_phones: Vec<String>,
21 pub mobile_phone: Option<String>,
22 pub manager: Option<OrgPerson>,
23 pub direct_reports: Vec<OrgPerson>,
24 pub time_zone: Option<String>,
25 pub availability: Option<Availability>,
26 pub status_message: Option<String>,
27 pub out_of_office: Option<String>,
28 pub work_location: Option<WorkLocation>,
29}
30
31impl PersonProfile {
32 pub fn from_remote(
33 details: ProfileDetails,
34 time_zone: Option<String>,
35 presence: Option<PresenceStatus>,
36 ) -> Self {
37 let ProfileDetails {
38 user,
39 manager,
40 direct_reports,
41 } = details;
42 let email = user.mail.or(user.user_principal_name);
43 PersonProfile {
44 user_id: user.id,
45 display_name: user.display_name,
46 job_title: user.job_title,
47 department: user.department,
48 company: user.company_name,
49 office_location: user.office_location,
50 email,
51 work_phones: user.business_phones,
52 mobile_phone: user.mobile_phone,
53 manager,
54 direct_reports,
55 time_zone,
56 availability: presence
57 .as_ref()
58 .map(|detail| Availability::from_service(&detail.availability)),
59 status_message: presence
60 .as_ref()
61 .and_then(|detail| detail.note.as_ref().map(|note| note.text.clone())),
62 work_location: presence.as_ref().and_then(|detail| detail.work_location),
63 out_of_office: presence.and_then(|detail| {
64 detail
65 .out_of_office
66 .then(|| detail.out_of_office_note.unwrap_or_default())
67 }),
68 }
69 }
70}
71
72pub(crate) async fn load_profile(graph: &Graph, user_id: &str) -> Result<PersonProfile> {
73 let details = graph.user_profile(user_id).await?;
74 let mail = details
75 .user
76 .mail
77 .clone()
78 .or_else(|| details.user.user_principal_name.clone());
79 let zone = async {
80 match mail {
81 Some(mail) => graph.time_zone_name(&mail).await.ok().flatten(),
82 None => None,
83 }
84 };
85 let presence_service = PresenceService::new(graph.session());
86 let (time_zone, presence) = tokio::join!(zone, presence_service.status(user_id));
87 Ok(PersonProfile::from_remote(
88 details,
89 time_zone,
90 presence.ok(),
91 ))
92}
93
94impl<R: Remote> SyncEngine<R> {
95 pub async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
96 self.remote.person_profile(user_id).await
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use chatsvc::StatusNote;
103 use graph::UserProfile;
104
105 use super::*;
106
107 fn details(user: UserProfile) -> ProfileDetails {
108 ProfileDetails {
109 user,
110 manager: None,
111 direct_reports: Vec::new(),
112 }
113 }
114
115 #[test]
116 fn the_user_principal_name_stands_in_for_a_missing_mail() {
117 let user = UserProfile {
118 id: "u".into(),
119 user_principal_name: Some("ada@example.com".into()),
120 ..Default::default()
121 };
122 let profile = PersonProfile::from_remote(details(user), None, None);
123 assert_eq!(profile.email.as_deref(), Some("ada@example.com"));
124 assert_eq!(profile.availability, None);
125 }
126
127 #[test]
128 fn presence_fills_availability_note_and_out_of_office() {
129 let presence = PresenceStatus {
130 availability: "DoNotDisturb".into(),
131 note: Some(StatusNote {
132 text: "Focus".into(),
133 show_when_messaged: false,
134 expires_at: None,
135 }),
136 out_of_office: true,
137 out_of_office_note: Some("Away until Monday".into()),
138 ..PresenceStatus::default()
139 };
140 let profile = PersonProfile::from_remote(
141 details(UserProfile::default()),
142 Some("UTC".into()),
143 Some(presence),
144 );
145 assert_eq!(profile.availability, Some(Availability::DoNotDisturb));
146 assert_eq!(profile.status_message.as_deref(), Some("Focus"));
147 assert_eq!(profile.out_of_office.as_deref(), Some("Away until Monday"));
148 assert_eq!(profile.time_zone.as_deref(), Some("UTC"));
149 }
150}