| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | use crate::captions::{BotAction, recorder_features}; |
| 4 | use crate::trouter_events::find_key; |
| 5 | |
| 6 | const CLOUD_RECORDING: &str = "cloudRecording"; |
| 7 | const CONSENT_DETAILS: &str = "recordingConsentDetails"; |
| 8 | const ACTIVE: &str = "Active"; |
| 9 | const PROCESSING_MODES: [&str; 2] = ["recording", "realTimeTranscript"]; |
| 10 | const STORAGE_TYPE: &str = "OnedriveForBusiness"; |
| 11 | const STORAGE_LOCATION: &str = "Recordings"; |
| 12 | const RECORDING_MODE: &str = "Normal"; |
| 13 | const FILE_SUFFIX: &str = "Meeting Recording"; |
| 14 | const CONSENT_STATE: &str = "consentToRecording"; |
| 15 | const DENY_STATE: &str = "denyConsentToRecording"; |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 18 | pub struct StateChange { |
| 19 | pub recording: Option<bool>, |
| 20 | pub consent_required: Option<bool>, |
| 21 | } |
| 22 | |
| 23 | impl StateChange { |
| 24 | pub fn is_empty(&self) -> bool { |
| 25 | self.recording.is_none() && self.consent_required.is_none() |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, Copy, Default)] |
| 30 | pub struct RecordingStatus { |
| 31 | active: bool, |
| 32 | consent_required: bool, |
| 33 | sequence: u64, |
| 34 | } |
| 35 | |
| 36 | impl RecordingStatus { |
| 37 | pub fn active(&self) -> bool { |
| 38 | self.active |
| 39 | } |
| 40 | |
| 41 | pub fn consent_required(&self) -> bool { |
| 42 | self.consent_required |
| 43 | } |
| 44 | |
| 45 | pub fn apply(&mut self, body: &Value) -> StateChange { |
| 46 | let mut change = StateChange::default(); |
| 47 | if let Some(state) = find_key(body, CLOUD_RECORDING) { |
| 48 | let sequence = state["sequenceNumber"].as_u64().unwrap_or_default(); |
| 49 | let current = state["value"]["State"].as_str().map(|name| name == ACTIVE); |
| 50 | if let Some(current) = current |
| 51 | && sequence >= self.sequence |
| 52 | { |
| 53 | self.sequence = sequence; |
| 54 | if current != self.active { |
| 55 | self.active = current; |
| 56 | change.recording = Some(current); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | if let Some(required) = find_key(body, CONSENT_DETAILS).and_then(|details| details["consentActivelyRequired"].as_bool()) |
| 61 | && required != self.consent_required |
| 62 | { |
| 63 | self.consent_required = required; |
| 64 | change.consent_required = Some(required); |
| 65 | } |
| 66 | change |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[derive(Debug, Clone, Copy)] |
| 71 | pub struct StartDetails<'a> { |
| 72 | pub own_mri: &'a str, |
| 73 | pub participant_leg_id: &'a str, |
| 74 | pub timestamp: &'a str, |
| 75 | pub file_name: &'a str, |
| 76 | pub meeting_title: &'a str, |
| 77 | pub organizer_name: &'a str, |
| 78 | pub correlation_id: &'a str, |
| 79 | } |
| 80 | |
| 81 | /// The skype token is added inside the page, so the body travels without it. |
| 82 | pub fn start_body(details: StartDetails<'_>) -> Value { |
| 83 | json!({ |
| 84 | "timestamp": details.timestamp, |
| 85 | "participantMri": details.own_mri, |
| 86 | "participantLegId": details.participant_leg_id, |
| 87 | "action": "start", |
| 88 | "processingModes": PROCESSING_MODES, |
| 89 | "actionParameters": { |
| 90 | "recordingFeatures": recorder_features(), |
| 91 | "recordingStorageSettings": [{ |
| 92 | "StorageType": STORAGE_TYPE, |
| 93 | "StorageLocation": STORAGE_LOCATION, |
| 94 | "FileName": details.file_name, |
| 95 | }], |
| 96 | "correlationId": details.correlation_id, |
| 97 | "meetingTitle": details.meeting_title, |
| 98 | "exchangeId": null, |
| 99 | "meetingOrganizer": details.organizer_name, |
| 100 | "recordingMode": RECORDING_MODE, |
| 101 | "spokenLanguage": "", |
| 102 | "type": "start", |
| 103 | }, |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | pub fn stop_body(own_mri: &str, participant_leg_id: &str, timestamp: &str) -> Value { |
| 108 | json!({ |
| 109 | "timestamp": timestamp, |
| 110 | "participantMri": own_mri, |
| 111 | "participantLegId": participant_leg_id, |
| 112 | "action": "stop", |
| 113 | "processingModes": PROCESSING_MODES, |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | pub fn command_body(action: BotAction, details: StartDetails<'_>) -> Value { |
| 118 | match action { |
| 119 | BotAction::Start => start_body(details), |
| 120 | BotAction::Stop => stop_body(details.own_mri, details.participant_leg_id, details.timestamp), |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | pub fn file_name(meeting_title: &str, now: chrono::DateTime<chrono::Utc>) -> String { |
| 125 | let title = if meeting_title.trim().is_empty() { "Meeting" } else { meeting_title.trim() }; |
| 126 | let safe: String = title.chars().map(|letter| if letter.is_alphanumeric() || letter == ' ' || letter == '-' { letter } else { '_' }).collect(); |
| 127 | format!("{safe}-{}-{FILE_SUFFIX}", now.format("%Y%m%d_%H%M%S")) |
| 128 | } |
| 129 | |
| 130 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 131 | pub enum Consent { |
| 132 | Give, |
| 133 | Deny, |
| 134 | } |
| 135 | |
| 136 | pub fn consent_body(from: Value, sequence_number: u32, consent: Consent) -> Value { |
| 137 | let state_type = match consent { |
| 138 | Consent::Give => CONSENT_STATE, |
| 139 | Consent::Deny => DENY_STATE, |
| 140 | }; |
| 141 | json!({ |
| 142 | "from": from, |
| 143 | "publishedState": { |
| 144 | "stateType": state_type, |
| 145 | "level": "user", |
| 146 | "content": "{}", |
| 147 | "sequenceNumber": sequence_number, |
| 148 | }, |
| 149 | }) |
| 150 | } |
| 151 | |
| 152 | #[cfg(test)] |
| 153 | mod tests { |
| 154 | use super::*; |
| 155 | |
| 156 | fn update(state: &str, sequence: u64) -> Value { |
| 157 | json!({"meetingStates": {"cloudRecording": {"value": {"State": state}, "sequenceNumber": sequence}, "transcription": {"value": {"State": state}, "sequenceNumber": sequence}}}) |
| 158 | } |
| 159 | |
| 160 | fn consent(required: bool) -> Value { |
| 161 | json!({"meetingDetails": {"recordingConsentDetails": {"explicitRecordingConsentEnabled": true, "consentActivelyRequired": required}}}) |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn the_recording_state_follows_the_meeting_state_and_ignores_stale_updates() { |
| 166 | let mut status = RecordingStatus::default(); |
| 167 | assert_eq!(status.apply(&update("Active", 3)), StateChange { recording: Some(true), consent_required: None }); |
| 168 | assert!(status.active()); |
| 169 | assert!(status.apply(&update("Active", 3)).is_empty()); |
| 170 | assert!(status.apply(&update("Inactive", 2)).is_empty()); |
| 171 | assert!(status.active()); |
| 172 | assert_eq!(status.apply(&update("Inactive", 4)).recording, Some(false)); |
| 173 | assert!(!status.active()); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn unrelated_updates_change_nothing() { |
| 178 | let mut status = RecordingStatus::default(); |
| 179 | assert!(status.apply(&json!({"activeModalities": {"groupChat": {"threadId": "19:x"}}})).is_empty()); |
| 180 | assert!(status.apply(&Value::Null).is_empty()); |
| 181 | } |
| 182 | |
| 183 | #[test] |
| 184 | fn consent_is_required_when_the_meeting_says_so() { |
| 185 | let mut status = RecordingStatus::default(); |
| 186 | assert!(status.apply(&consent(false)).is_empty()); |
| 187 | assert_eq!(status.apply(&consent(true)), StateChange { recording: None, consent_required: Some(true) }); |
| 188 | assert!(status.consent_required()); |
| 189 | assert!(status.apply(&consent(true)).is_empty()); |
| 190 | assert_eq!(status.apply(&consent(false)).consent_required, Some(false)); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn one_update_can_carry_both_states() { |
| 195 | let mut both = update("Active", 1); |
| 196 | both["meetingDetails"] = consent(true)["meetingDetails"].clone(); |
| 197 | let change = RecordingStatus::default().apply(&both); |
| 198 | assert_eq!(change, StateChange { recording: Some(true), consent_required: Some(true) }); |
| 199 | } |
| 200 | |
| 201 | fn details() -> StartDetails<'static> { |
| 202 | StartDetails { |
| 203 | own_mri: "8:orgid:me", |
| 204 | participant_leg_id: "leg-1", |
| 205 | timestamp: "2026-10-10T08:44:12.705Z", |
| 206 | file_name: "Standup-20261010_084412-Meeting Recording", |
| 207 | meeting_title: "Standup", |
| 208 | organizer_name: "Me", |
| 209 | correlation_id: "corr-1", |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn the_start_command_records_to_onedrive_without_a_mode_key() { |
| 215 | let body = command_body(BotAction::Start, details()); |
| 216 | assert_eq!(body["action"], "start"); |
| 217 | assert_eq!(body["processingModes"], json!(["recording", "realTimeTranscript"])); |
| 218 | assert!(body.get("mode").is_none()); |
| 219 | assert!(body.get("participantSkypeToken").is_none()); |
| 220 | let parameters = &body["actionParameters"]; |
| 221 | assert_eq!(parameters["type"], "start"); |
| 222 | assert_eq!(parameters["recordingMode"], "Normal"); |
| 223 | assert_eq!(parameters["meetingTitle"], "Standup"); |
| 224 | assert_eq!(parameters["recordingStorageSettings"][0]["StorageType"], "OnedriveForBusiness"); |
| 225 | assert_eq!(parameters["recordingStorageSettings"][0]["StorageLocation"], "Recordings"); |
| 226 | assert_eq!(parameters["recordingStorageSettings"][0]["FileName"], "Standup-20261010_084412-Meeting Recording"); |
| 227 | assert_eq!(body["participantLegId"], "leg-1"); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn the_stop_command_is_short() { |
| 232 | let body = command_body(BotAction::Stop, details()); |
| 233 | assert_eq!(body["action"], "stop"); |
| 234 | assert_eq!(body["processingModes"], json!(["recording", "realTimeTranscript"])); |
| 235 | assert!(body.get("actionParameters").is_none()); |
| 236 | assert_eq!(body["timestamp"], "2026-10-10T08:44:12.705Z"); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn file_names_are_safe_and_stamped() { |
| 241 | let at = chrono::DateTime::parse_from_rfc3339("2026-10-10T08:44:12Z").unwrap().with_timezone(&chrono::Utc); |
| 242 | assert_eq!(file_name("Q3 / Plan: v2", at), "Q3 _ Plan_ v2-20261010_084412-Meeting Recording"); |
| 243 | assert_eq!(file_name(" ", at), "Meeting-20261010_084412-Meeting Recording"); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn consent_and_denial_publish_a_user_state() { |
| 248 | let given = consent_body(json!({"id": "8:orgid:me"}), 4, Consent::Give); |
| 249 | assert_eq!(given["publishedState"]["stateType"], "consentToRecording"); |
| 250 | assert_eq!(given["publishedState"]["level"], "user"); |
| 251 | assert_eq!(given["publishedState"]["content"], "{}"); |
| 252 | assert_eq!(given["publishedState"]["sequenceNumber"], 4); |
| 253 | let denied = consent_body(json!({"id": "8:orgid:me"}), 5, Consent::Deny); |
| 254 | assert_eq!(denied["publishedState"]["stateType"], "denyConsentToRecording"); |
| 255 | } |
| 256 | } |