| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use serde_json::Value; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | const STARTED_TYPE: &str = "#microsoft.graph.callStartedEventMessageDetail"; |
| 6 | const ENDED_TYPE: &str = "#microsoft.graph.callEndedEventMessageDetail"; |
| 7 | const SECONDS_PER_MINUTE: u64 = 60; |
| 8 | const SECONDS_PER_HOUR: u64 = 3600; |
| 9 | const SECONDS_PER_DAY: u64 = 86_400; |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 12 | pub enum CallPhase { |
| 13 | Started, |
| 14 | Ended, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 18 | pub enum CallType { |
| 19 | Call, |
| 20 | Meeting, |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 24 | pub struct CallEvent { |
| 25 | pub phase: CallPhase, |
| 26 | pub call_type: CallType, |
| 27 | pub call_id: String, |
| 28 | pub duration_seconds: Option<u64>, |
| 29 | pub initiator_id: Option<String>, |
| 30 | pub initiator_name: Option<String>, |
| 31 | pub participant_ids: Vec<String>, |
| 32 | } |
| 33 | |
| 34 | impl CallEvent { |
| 35 | pub fn from_graph(detail: &Value) -> Option<CallEvent> { |
| 36 | let phase = match detail.get("@odata.type")?.as_str()? { |
| 37 | STARTED_TYPE => CallPhase::Started, |
| 38 | ENDED_TYPE => CallPhase::Ended, |
| 39 | _ => return None, |
| 40 | }; |
| 41 | let call_type = match detail.get("callEventType").and_then(Value::as_str) { |
| 42 | Some("meeting") => CallType::Meeting, |
| 43 | _ => CallType::Call, |
| 44 | }; |
| 45 | Some(CallEvent { |
| 46 | phase, |
| 47 | call_type, |
| 48 | call_id: detail.get("callId")?.as_str()?.to_owned(), |
| 49 | duration_seconds: detail |
| 50 | .get("callDuration") |
| 51 | .and_then(Value::as_str) |
| 52 | .and_then(parse_duration_seconds), |
| 53 | initiator_id: text_at(detail, "/initiator/user/id"), |
| 54 | initiator_name: text_at(detail, "/initiator/user/displayName"), |
| 55 | participant_ids: detail |
| 56 | .get("callParticipants") |
| 57 | .and_then(Value::as_array) |
| 58 | .map(|participants| { |
| 59 | participants |
| 60 | .iter() |
| 61 | .filter_map(|participant| identity_id(participant.get("participant")?)) |
| 62 | .collect() |
| 63 | }) |
| 64 | .unwrap_or_default(), |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | pub fn from_json(json: &str) -> Option<CallEvent> { |
| 69 | serde_json::from_str(json).ok() |
| 70 | } |
| 71 | |
| 72 | pub fn to_json(&self) -> String { |
| 73 | serde_json::to_string(self).unwrap_or_default() |
| 74 | } |
| 75 | |
| 76 | pub fn initiated_by(&self, user_id: Option<&str>) -> bool { |
| 77 | user_id.is_some() && self.initiator_id.as_deref() == user_id |
| 78 | } |
| 79 | |
| 80 | pub fn missed(&self, my_user_id: Option<&str>) -> bool { |
| 81 | my_user_id.is_some() |
| 82 | && self.phase == CallPhase::Ended |
| 83 | && self.call_type == CallType::Call |
| 84 | && self.participant_ids.len() < 2 |
| 85 | && !self.initiated_by(my_user_id) |
| 86 | } |
| 87 | |
| 88 | pub fn title(&self, missed: bool) -> &'static str { |
| 89 | match (self.phase, self.call_type) { |
| 90 | _ if missed => "Missed call", |
| 91 | (CallPhase::Started, CallType::Call) => "Call started", |
| 92 | (CallPhase::Ended, CallType::Call) => "Call ended", |
| 93 | (CallPhase::Started, CallType::Meeting) => "Meeting started", |
| 94 | (CallPhase::Ended, CallType::Meeting) => "Meeting ended", |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn plain_text(&self) -> String { |
| 99 | let title = self.title(false); |
| 100 | match self.duration_seconds { |
| 101 | Some(seconds) => format!("{title} {}", format_duration(seconds)), |
| 102 | None => title.to_owned(), |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pub fn call_event(record: &MessageRecord) -> Option<CallEvent> { |
| 108 | CallEvent::from_json(record.call_event_json.as_deref()?) |
| 109 | } |
| 110 | |
| 111 | fn identity_id(identity_set: &Value) -> Option<String> { |
| 112 | identity_set |
| 113 | .as_object()? |
| 114 | .values() |
| 115 | .find_map(|identity| text_at(identity, "/id")) |
| 116 | } |
| 117 | |
| 118 | fn text_at(value: &Value, pointer: &str) -> Option<String> { |
| 119 | value |
| 120 | .pointer(pointer)? |
| 121 | .as_str() |
| 122 | .filter(|text| !text.is_empty()) |
| 123 | .map(str::to_owned) |
| 124 | } |
| 125 | |
| 126 | pub fn parse_duration_seconds(text: &str) -> Option<u64> { |
| 127 | let rest = text.strip_prefix('P')?; |
| 128 | let (date_part, time_part) = match rest.split_once('T') { |
| 129 | Some((date_part, time_part)) => (date_part, time_part), |
| 130 | None => (rest, ""), |
| 131 | }; |
| 132 | if date_part.is_empty() && time_part.is_empty() { |
| 133 | return None; |
| 134 | } |
| 135 | let days = sum_components(date_part, &[('D', SECONDS_PER_DAY)])?; |
| 136 | let time = sum_components( |
| 137 | time_part, |
| 138 | &[('H', SECONDS_PER_HOUR), ('M', SECONDS_PER_MINUTE), ('S', 1)], |
| 139 | )?; |
| 140 | Some(days + time) |
| 141 | } |
| 142 | |
| 143 | fn sum_components(part: &str, units: &[(char, u64)]) -> Option<u64> { |
| 144 | let mut total = 0; |
| 145 | let mut number = String::new(); |
| 146 | for character in part.chars() { |
| 147 | if character.is_ascii_digit() || character == '.' { |
| 148 | number.push(character); |
| 149 | continue; |
| 150 | } |
| 151 | let (_, factor) = units.iter().find(|(unit, _)| *unit == character)?; |
| 152 | let value: f64 = number.parse().ok()?; |
| 153 | total += (value * *factor as f64) as u64; |
| 154 | number.clear(); |
| 155 | } |
| 156 | number.is_empty().then_some(total) |
| 157 | } |
| 158 | |
| 159 | pub fn format_duration(seconds: u64) -> String { |
| 160 | let hours = seconds / SECONDS_PER_HOUR; |
| 161 | let minutes = seconds % SECONDS_PER_HOUR / SECONDS_PER_MINUTE; |
| 162 | let remainder = seconds % SECONDS_PER_MINUTE; |
| 163 | let mut parts = Vec::new(); |
| 164 | if hours > 0 { |
| 165 | parts.push(format!("{hours}h")); |
| 166 | } |
| 167 | if minutes > 0 || (hours > 0 && remainder > 0) { |
| 168 | parts.push(format!("{minutes}m")); |
| 169 | } |
| 170 | if remainder > 0 || (hours == 0 && minutes == 0) { |
| 171 | parts.push(format!("{remainder}s")); |
| 172 | } |
| 173 | parts.join(" ") |
| 174 | } |