BlueGit

ict rusty-teams

Files

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

profile.rs

profile.rs 5.3 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 = match graph.user_profile(user_id).await {
74 Ok(details) => details,
75 // People from other organizations are not in this directory. Like Teams, their card
76 // still shows what presence knows: availability, status note and out of office.
77 Err(graph::Error::Session(session::Error::Api { status: 404, .. })) => ProfileDetails {
78 user: graph::UserProfile {
79 id: user_id.to_owned(),
80 ..Default::default()
81 },
82 manager: None,
83 direct_reports: Vec::new(),
84 },
85 Err(error) => return Err(error.into()),
86 };
87 let mail = details
88 .user
89 .mail
90 .clone()
91 .or_else(|| details.user.user_principal_name.clone());
92 let zone = async {
93 match mail {
94 Some(mail) => graph.time_zone_name(&mail).await.ok().flatten(),
95 None => None,
96 }
97 };
98 let presence_service = PresenceService::new(graph.session());
99 let (time_zone, presence) = tokio::join!(zone, presence_service.status(user_id));
100 Ok(PersonProfile::from_remote(
101 details,
102 time_zone,
103 presence.ok(),
104 ))
105}
106
107impl<R: Remote> SyncEngine<R> {
108 pub async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
109 self.remote.person_profile(user_id).await
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use chatsvc::StatusNote;
116 use graph::UserProfile;
117
118 use super::*;
119
120 fn details(user: UserProfile) -> ProfileDetails {
121 ProfileDetails {
122 user,
123 manager: None,
124 direct_reports: Vec::new(),
125 }
126 }
127
128 #[test]
129 fn the_user_principal_name_stands_in_for_a_missing_mail() {
130 let user = UserProfile {
131 id: "u".into(),
132 user_principal_name: Some("ada@example.com".into()),
133 ..Default::default()
134 };
135 let profile = PersonProfile::from_remote(details(user), None, None);
136 assert_eq!(profile.email.as_deref(), Some("ada@example.com"));
137 assert_eq!(profile.availability, None);
138 }
139
140 #[test]
141 fn presence_fills_availability_note_and_out_of_office() {
142 let presence = PresenceStatus {
143 availability: "DoNotDisturb".into(),
144 note: Some(StatusNote {
145 text: "Focus".into(),
146 show_when_messaged: false,
147 expires_at: None,
148 }),
149 out_of_office: true,
150 out_of_office_note: Some("Away until Monday".into()),
151 ..PresenceStatus::default()
152 };
153 let profile = PersonProfile::from_remote(
154 details(UserProfile::default()),
155 Some("UTC".into()),
156 Some(presence),
157 );
158 assert_eq!(profile.availability, Some(Availability::DoNotDisturb));
159 assert_eq!(profile.status_message.as_deref(), Some("Focus"));
160 assert_eq!(profile.out_of_office.as_deref(), Some("Away until Monday"));
161 assert_eq!(profile.time_zone.as_deref(), Some("UTC"));
162 }
163}