| 1 | use serde_json::{Value, json}; |
| 2 | use session::{Method, Request, SPACES, Scope, Session}; |
| 3 | |
| 4 | use crate::error::{Error, Result}; |
| 5 | use crate::organizer::restrictions_value; |
| 6 | use crate::signaling::{CHATSVC_REGION, MeetingTarget}; |
| 7 | use crate::whiteboard::encoded; |
| 8 | |
| 9 | const OPTIONS_SCOPE: &str = "user_impersonation"; |
| 10 | const READ_HOST: &str = "https://teams.microsoft.com"; |
| 11 | const LIVE_HOST: &str = "https://teams.cloud.microsoft"; |
| 12 | const AUTO_ADMITTED: &str = "AutoAdmittedUsers"; |
| 13 | const PRESENTER: &str = "PresenterOption"; |
| 14 | const RESTRICTIONS: &str = "AttendeeRestrictions"; |
| 15 | const MEETING_CHAT: &str = "AllowMeetingChat"; |
| 16 | const REACTIONS: &str = "AllowTeamsMeetingReactions"; |
| 17 | const AUTO_RECORD: &str = "AutoRecordAndTranscribeMode"; |
| 18 | const RECORD_ON: &str = "AutoRecordingAndTranscription"; |
| 19 | const RECORD_OFF: &str = "Off"; |
| 20 | const LIST: &str = "List"; |
| 21 | const BOOLEAN: &str = "Boolean"; |
| 22 | const BIT_MASK: &str = "BitMask"; |
| 23 | |
| 24 | pub const BYPASS_CHOICES: [(&str, &str); 6] = [ |
| 25 | ("Everyone", "Everyone"), |
| 26 | ("EveryoneInSameAndFederatedCompany", "People in my org and trusted orgs"), |
| 27 | ("EveryoneInCompany", "People in my org"), |
| 28 | ("EveryoneInCompanyExcludingGuests", "People in my org, no guests"), |
| 29 | ("InvitedUsers", "People I invite"), |
| 30 | ("OrganizerOnly", "Only me"), |
| 31 | ]; |
| 32 | pub const PRESENTER_CHOICES: [(&str, &str); 3] = [ |
| 33 | ("Everyone", "Everyone"), |
| 34 | ("EveryoneInCompany", "People in my org"), |
| 35 | ("OrganizerOnly", "Only me"), |
| 36 | ]; |
| 37 | pub const CHAT_CHOICES: [(&str, &str); 3] = [ |
| 38 | ("Enabled", "On"), |
| 39 | ("InMeeting", "In-meeting only"), |
| 40 | ("Disabled", "Off"), |
| 41 | ]; |
| 42 | |
| 43 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 44 | pub struct MeetingOptions { |
| 45 | pub bypass_lobby: String, |
| 46 | pub presenters: String, |
| 47 | pub attendee_mic: bool, |
| 48 | pub attendee_camera: bool, |
| 49 | pub meeting_chat: String, |
| 50 | pub reactions: bool, |
| 51 | pub record_automatically: bool, |
| 52 | record_mode: String, |
| 53 | } |
| 54 | |
| 55 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 56 | pub struct OptionWrite { |
| 57 | pub name: &'static str, |
| 58 | pub kind: &'static str, |
| 59 | pub value: Value, |
| 60 | } |
| 61 | |
| 62 | impl OptionWrite { |
| 63 | pub fn restrictions(mic_off: bool, camera_off: bool) -> OptionWrite { |
| 64 | OptionWrite { name: RESTRICTIONS, kind: BIT_MASK, value: json!(restrictions_value(mic_off, camera_off)) } |
| 65 | } |
| 66 | |
| 67 | fn body(&self) -> Value { |
| 68 | json!({"options": [{"name": self.name, "type": self.kind, "currentValue": self.value, "isRecommended": null}]}) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl MeetingOptions { |
| 73 | pub fn from_response(body: &Value) -> Option<MeetingOptions> { |
| 74 | let options = body["options"].as_array()?; |
| 75 | let current = |name: &str| options.iter().find(|option| option["name"].as_str() == Some(name)).map(|option| &option["currentValue"]); |
| 76 | let text = |name: &str| current(name).and_then(Value::as_str).map(str::to_owned); |
| 77 | let flag = |name: &str| current(name).and_then(|value| value.as_bool().or_else(|| value.as_str().map(|text| text.eq_ignore_ascii_case("true")))); |
| 78 | let restrictions = text(RESTRICTIONS).unwrap_or_default(); |
| 79 | let record_mode = text(AUTO_RECORD).unwrap_or_else(|| RECORD_OFF.to_owned()); |
| 80 | Some(MeetingOptions { |
| 81 | bypass_lobby: text(AUTO_ADMITTED)?, |
| 82 | presenters: text(PRESENTER)?, |
| 83 | attendee_mic: !restrictions.contains("DisableAudio"), |
| 84 | attendee_camera: !restrictions.contains("DisableVideo"), |
| 85 | meeting_chat: text(MEETING_CHAT)?, |
| 86 | reactions: flag(REACTIONS).unwrap_or(true), |
| 87 | record_automatically: record_mode == RECORD_ON, |
| 88 | record_mode, |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | pub fn changes(&self, edited: &MeetingOptions) -> Vec<OptionWrite> { |
| 93 | let mut writes = Vec::new(); |
| 94 | let mut list = |name: &'static str, before: &str, after: &str| { |
| 95 | if before != after { |
| 96 | writes.push(OptionWrite { name, kind: LIST, value: json!(after) }); |
| 97 | } |
| 98 | }; |
| 99 | list(AUTO_ADMITTED, &self.bypass_lobby, &edited.bypass_lobby); |
| 100 | list(PRESENTER, &self.presenters, &edited.presenters); |
| 101 | list(MEETING_CHAT, &self.meeting_chat, &edited.meeting_chat); |
| 102 | if self.record_automatically != edited.record_automatically { |
| 103 | let mode = if edited.record_automatically { RECORD_ON } else { RECORD_OFF }; |
| 104 | writes.push(OptionWrite { name: AUTO_RECORD, kind: LIST, value: json!(mode) }); |
| 105 | } |
| 106 | if self.reactions != edited.reactions { |
| 107 | writes.push(OptionWrite { name: REACTIONS, kind: BOOLEAN, value: json!(edited.reactions) }); |
| 108 | } |
| 109 | if (self.attendee_mic, self.attendee_camera) != (edited.attendee_mic, edited.attendee_camera) { |
| 110 | writes.push(OptionWrite::restrictions(!edited.attendee_mic, !edited.attendee_camera)); |
| 111 | } |
| 112 | writes |
| 113 | } |
| 114 | |
| 115 | pub fn attendees_off(&self) -> (bool, bool) { |
| 116 | (!self.attendee_mic, !self.attendee_camera) |
| 117 | } |
| 118 | |
| 119 | pub fn record_mode(&self) -> &str { |
| 120 | &self.record_mode |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn coordinates(target: &MeetingTarget) -> String { |
| 125 | format!("{}/{}/{}", encoded(&target.tenant_id), encoded(&target.organizer_id), encoded(&target.thread_id)) |
| 126 | } |
| 127 | |
| 128 | pub fn read_url(target: &MeetingTarget) -> String { |
| 129 | format!("{READ_HOST}/api/mt/{CHATSVC_REGION}//beta/meetings/v1/options/{}/0?monarch=true", coordinates(target)) |
| 130 | } |
| 131 | |
| 132 | pub fn write_url(target: &MeetingTarget) -> String { |
| 133 | format!("{LIVE_HOST}/api/mt/{CHATSVC_REGION}/beta/meetings/v1/options/{}/0/0?monarch=true", coordinates(target)) |
| 134 | } |
| 135 | |
| 136 | fn with_headers(mut request: Request, caller: &str) -> Request { |
| 137 | request.headers.extend([ |
| 138 | ("X-Ms-Migration".to_owned(), "True".to_owned()), |
| 139 | ("x-ms-client-caller".to_owned(), caller.to_owned()), |
| 140 | ("X-MS-Request-Id".to_owned(), uuid::Uuid::new_v4().to_string()), |
| 141 | ]); |
| 142 | request |
| 143 | } |
| 144 | |
| 145 | fn scope() -> Scope { |
| 146 | Scope::new(SPACES, OPTIONS_SCOPE) |
| 147 | } |
| 148 | |
| 149 | pub async fn fetch(session: &Session, target: &MeetingTarget) -> Result<MeetingOptions> { |
| 150 | let request = with_headers(Request::get(read_url(target)), "meeting-options-dialog"); |
| 151 | let response = session.send(request, &scope()).await?; |
| 152 | MeetingOptions::from_response(&response.body).ok_or_else(|| Error::Signaling("meeting options answer without the expected options".into())) |
| 153 | } |
| 154 | |
| 155 | pub fn write_failed(body: &Value) -> bool { |
| 156 | body["success"].as_bool() == Some(false) |
| 157 | || body["options"].as_array().is_some_and(|options| options.iter().any(|option| option["success"].as_bool() == Some(false))) |
| 158 | } |
| 159 | |
| 160 | pub async fn write(session: &Session, target: &MeetingTarget, change: &OptionWrite) -> Result<()> { |
| 161 | let mut request = with_headers(Request::with_body(Method::Post, write_url(target), change.body()), "meeting-options-dialog"); |
| 162 | request.headers.push(("x-ms-test-user".to_owned(), "False".to_owned())); |
| 163 | let response = session.send(request, &scope()).await?; |
| 164 | if write_failed(&response.body) { |
| 165 | return Err(Error::Signaling(format!("the service refused {}", change.name))); |
| 166 | } |
| 167 | Ok(()) |
| 168 | } |
| 169 | |
| 170 | #[cfg(test)] |
| 171 | mod tests { |
| 172 | use super::*; |
| 173 | |
| 174 | fn target() -> MeetingTarget { |
| 175 | MeetingTarget { |
| 176 | thread_id: "19:meeting_abc@thread.v2".into(), |
| 177 | tenant_id: "00000000-0000-0000-0000-000000000001".into(), |
| 178 | organizer_id: "00000000-0000-0000-0000-000000000002".into(), |
| 179 | meeting_data: None, |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | fn option(name: &str, kind: &str, value: Value) -> Value { |
| 184 | json!({"name": name, "type": kind, "currentValue": value, "category": "x", "isDisabled": false}) |
| 185 | } |
| 186 | |
| 187 | fn response() -> Value { |
| 188 | json!({"options": [ |
| 189 | option("AutoAdmittedUsers", "List", json!("EveryoneInCompany")), |
| 190 | option("PresenterOption", "List", json!("Everyone")), |
| 191 | option("AttendeeRestrictions", "BitMask", json!("DisableAudio")), |
| 192 | option("AllowMeetingChat", "List", json!("InMeeting")), |
| 193 | option("AllowTeamsMeetingReactions", "Boolean", json!(true)), |
| 194 | option("AutoRecordAndTranscribeMode", "List", json!("AutoTranscriptionOnly")), |
| 195 | ], "meetingDetails": {"title": "Standup"}}) |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn the_read_url_keeps_the_double_slash_and_the_write_url_adds_a_zero() { |
| 200 | assert_eq!( |
| 201 | read_url(&target()), |
| 202 | "https://teams.microsoft.com/api/mt/emea//beta/meetings/v1/options/00000000-0000-0000-0000-000000000001/00000000-0000-0000-0000-000000000002/19%3Ameeting_abc%40thread.v2/0?monarch=true" |
| 203 | ); |
| 204 | assert_eq!( |
| 205 | write_url(&target()), |
| 206 | "https://teams.cloud.microsoft/api/mt/emea/beta/meetings/v1/options/00000000-0000-0000-0000-000000000001/00000000-0000-0000-0000-000000000002/19%3Ameeting_abc%40thread.v2/0/0?monarch=true" |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn the_read_answer_becomes_the_dialog_fields() { |
| 212 | let options = MeetingOptions::from_response(&response()).unwrap(); |
| 213 | assert_eq!(options.bypass_lobby, "EveryoneInCompany"); |
| 214 | assert_eq!(options.presenters, "Everyone"); |
| 215 | assert!(!options.attendee_mic); |
| 216 | assert!(options.attendee_camera); |
| 217 | assert_eq!(options.meeting_chat, "InMeeting"); |
| 218 | assert!(options.reactions); |
| 219 | assert!(!options.record_automatically); |
| 220 | assert_eq!(options.record_mode(), "AutoTranscriptionOnly"); |
| 221 | assert!(MeetingOptions::from_response(&json!({"options": []})).is_none()); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn only_changed_options_are_written() { |
| 226 | let before = MeetingOptions::from_response(&response()).unwrap(); |
| 227 | assert!(before.changes(&before.clone()).is_empty()); |
| 228 | let edited = MeetingOptions { |
| 229 | bypass_lobby: "OrganizerOnly".into(), |
| 230 | meeting_chat: "Disabled".into(), |
| 231 | record_automatically: true, |
| 232 | reactions: false, |
| 233 | attendee_mic: true, |
| 234 | attendee_camera: false, |
| 235 | ..before.clone() |
| 236 | }; |
| 237 | let writes = before.changes(&edited); |
| 238 | let names: Vec<&str> = writes.iter().map(|write| write.name).collect(); |
| 239 | assert_eq!(names, ["AutoAdmittedUsers", "AllowMeetingChat", "AutoRecordAndTranscribeMode", "AllowTeamsMeetingReactions", "AttendeeRestrictions"]); |
| 240 | assert_eq!(writes[2].value, json!("AutoRecordingAndTranscription")); |
| 241 | assert_eq!(writes[4].value, json!("DisableVideo")); |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn a_write_sends_one_option_in_the_captured_shape() { |
| 246 | let write = OptionWrite { name: "AllowMeetingChat", kind: "List", value: json!("Disabled") }; |
| 247 | assert_eq!( |
| 248 | write.body(), |
| 249 | json!({"options": [{"name": "AllowMeetingChat", "type": "List", "currentValue": "Disabled", "isRecommended": null}]}) |
| 250 | ); |
| 251 | assert_eq!(OptionWrite::restrictions(true, true).body()["options"][0]["currentValue"], "DisableAudio,DisableVideo"); |
| 252 | assert_eq!(OptionWrite::restrictions(false, false).value, json!("UnRestricted")); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn a_refused_write_is_told_apart_from_an_accepted_one() { |
| 257 | assert!(!write_failed(&json!({"success": true, "options": [{"success": true}]}))); |
| 258 | assert!(write_failed(&json!({"success": false}))); |
| 259 | assert!(write_failed(&json!({"success": true, "options": [{"success": false}]}))); |
| 260 | } |
| 261 | } |