| 1 | use serde_json::Value; |
| 2 | |
| 3 | use crate::signaling::MeetingTarget; |
| 4 | use crate::trouter_events::find_key; |
| 5 | |
| 6 | const MEETUP_PATH: &str = "/l/meetup-join/"; |
| 7 | |
| 8 | #[derive(Debug, Clone, PartialEq)] |
| 9 | pub struct BreakoutMove { |
| 10 | pub room_name: String, |
| 11 | pub target: MeetingTarget, |
| 12 | pub returning: bool, |
| 13 | } |
| 14 | |
| 15 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | pub struct RoomAssignment { |
| 17 | pub name: String, |
| 18 | pub open: bool, |
| 19 | pub join_url: String, |
| 20 | pub version: u64, |
| 21 | } |
| 22 | |
| 23 | fn percent_decoded(text: &str) -> String { |
| 24 | let bytes = text.as_bytes(); |
| 25 | let mut decoded = Vec::with_capacity(bytes.len()); |
| 26 | let mut index = 0; |
| 27 | while index < bytes.len() { |
| 28 | let hex = bytes |
| 29 | .get(index + 1..index + 3) |
| 30 | .and_then(|pair| std::str::from_utf8(pair).ok()) |
| 31 | .and_then(|pair| u8::from_str_radix(pair, 16).ok()); |
| 32 | match (bytes[index], hex) { |
| 33 | (b'%', Some(byte)) => { |
| 34 | decoded.push(byte); |
| 35 | index += 3; |
| 36 | } |
| 37 | (byte, _) => { |
| 38 | decoded.push(byte); |
| 39 | index += 1; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | String::from_utf8_lossy(&decoded).into_owned() |
| 44 | } |
| 45 | |
| 46 | fn context_of(decoded_url: &str) -> Option<Value> { |
| 47 | let start = decoded_url.find('{')?; |
| 48 | let mut depth = 0usize; |
| 49 | for (offset, letter) in decoded_url[start..].char_indices() { |
| 50 | match letter { |
| 51 | '{' => depth += 1, |
| 52 | '}' => { |
| 53 | depth -= 1; |
| 54 | if depth == 0 { |
| 55 | return serde_json::from_str(&decoded_url[start..=start + offset]).ok(); |
| 56 | } |
| 57 | } |
| 58 | _ => {} |
| 59 | } |
| 60 | } |
| 61 | None |
| 62 | } |
| 63 | |
| 64 | /// `https://<host>/l/meetup-join/<thread>/<messageId>?context={"Tid":..,"Oid":..}`; the host does not matter. |
| 65 | pub fn target_from_join_url(url: &str) -> Option<MeetingTarget> { |
| 66 | let decoded = percent_decoded(url); |
| 67 | let after = &decoded[decoded.find(MEETUP_PATH)? + MEETUP_PATH.len()..]; |
| 68 | let thread_id = after |
| 69 | .split(['/', '?']) |
| 70 | .next() |
| 71 | .filter(|thread| thread.starts_with("19:"))? |
| 72 | .to_owned(); |
| 73 | let context = context_of(after)?; |
| 74 | Some(MeetingTarget { |
| 75 | thread_id, |
| 76 | tenant_id: context["Tid"].as_str()?.to_owned(), |
| 77 | organizer_id: context["Oid"].as_str()?.to_owned(), |
| 78 | meeting_data: None, |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | /// `<tenantId>/<organizerId>/<threadId with the first ":" written as "_">/<messageId>`. |
| 83 | pub fn target_from_coordinates(coordinates: &str) -> Option<MeetingTarget> { |
| 84 | let mut parts = coordinates.split('/'); |
| 85 | let (tenant_id, organizer_id, thread) = (parts.next()?, parts.next()?, parts.next()?); |
| 86 | if tenant_id.is_empty() || organizer_id.is_empty() { |
| 87 | return None; |
| 88 | } |
| 89 | let thread_id = thread.replacen('_', ":", 1); |
| 90 | thread_id.starts_with("19:").then(|| MeetingTarget { |
| 91 | thread_id, |
| 92 | tenant_id: tenant_id.to_owned(), |
| 93 | organizer_id: organizer_id.to_owned(), |
| 94 | meeting_data: None, |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | pub fn room_assignment(body: &Value) -> Option<RoomAssignment> { |
| 99 | let properties = find_key(body, "mainMeetingProperties")?; |
| 100 | let room = properties["assignedBreakoutRooms"].as_array()?.first()?; |
| 101 | Some(RoomAssignment { |
| 102 | name: room["name"] |
| 103 | .as_str() |
| 104 | .unwrap_or("a breakout room") |
| 105 | .to_owned(), |
| 106 | open: room["isOpen"].as_bool().unwrap_or(false), |
| 107 | join_url: room["meetingJoinUrl"].as_str()?.to_owned(), |
| 108 | version: properties["breakoutRoomVersion"] |
| 109 | .as_u64() |
| 110 | .unwrap_or_default(), |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | pub fn main_meeting_target(conversation_update: &Value) -> Option<MeetingTarget> { |
| 115 | let properties = find_key(conversation_update, "breakoutRoomProperties")?; |
| 116 | target_from_join_url(properties["mainMeetingJoinUrl"].as_str()?) |
| 117 | } |
| 118 | |
| 119 | /// Mode 1: the server rings the client with a "replaces" call that names the room or the main meeting. |
| 120 | pub fn invite_move(body: &Value) -> Option<BreakoutMove> { |
| 121 | let data = find_key(body, "breakoutRoomData")?; |
| 122 | let returning = data["isMainMeetingInvite"].as_bool().unwrap_or(false); |
| 123 | if !returning && !data["isRoomInvite"].as_bool().unwrap_or(false) { |
| 124 | return None; |
| 125 | } |
| 126 | let target = target_from_coordinates(data["relatedMeetingCoords"].as_str()?)?; |
| 127 | let room_name = data["meetingName"] |
| 128 | .as_str() |
| 129 | .filter(|name| !name.is_empty()) |
| 130 | .map_or_else( |
| 131 | || { |
| 132 | if returning { |
| 133 | "the main meeting".to_owned() |
| 134 | } else { |
| 135 | "a breakout room".to_owned() |
| 136 | } |
| 137 | }, |
| 138 | str::to_owned, |
| 139 | ); |
| 140 | Some(BreakoutMove { |
| 141 | room_name, |
| 142 | target, |
| 143 | returning, |
| 144 | }) |
| 145 | } |
| 146 | |
| 147 | /// Mode 2: no invite, the room simply opens; moves once per room opening. |
| 148 | #[derive(Debug, Default)] |
| 149 | pub struct RoomWatcher { |
| 150 | version: u64, |
| 151 | joined: Option<String>, |
| 152 | } |
| 153 | |
| 154 | impl RoomWatcher { |
| 155 | pub fn apply(&mut self, assignment: Option<RoomAssignment>) -> Option<BreakoutMove> { |
| 156 | let assignment = assignment?; |
| 157 | if assignment.version < self.version { |
| 158 | return None; |
| 159 | } |
| 160 | self.version = assignment.version; |
| 161 | if !assignment.open { |
| 162 | self.joined = None; |
| 163 | return None; |
| 164 | } |
| 165 | if self.joined.as_deref() == Some(assignment.join_url.as_str()) { |
| 166 | return None; |
| 167 | } |
| 168 | let target = target_from_join_url(&assignment.join_url)?; |
| 169 | self.joined = Some(assignment.join_url); |
| 170 | Some(BreakoutMove { |
| 171 | room_name: assignment.name, |
| 172 | target, |
| 173 | returning: false, |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | pub fn entered(&mut self, join_url: &str) { |
| 178 | self.joined = Some(join_url.to_owned()); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[cfg(test)] |
| 183 | mod tests { |
| 184 | use serde_json::json; |
| 185 | |
| 186 | use super::*; |
| 187 | |
| 188 | const ROOM_URL: &str = "https://teams.microsoft.com/l/meetup-join/19%3ameeting_Room1abc%40thread.v2/0?context=%7b%22Tid%22%3a%22tenant-1%22%2c%22Oid%22%3a%22org-1%22%7d"; |
| 189 | |
| 190 | fn room_update(open: bool, version: u64) -> Value { |
| 191 | json!({"breakoutDetails": {"mainMeetingProperties": { |
| 192 | "breakoutRoomVersion": version, |
| 193 | "breakoutRoomManagers": [], |
| 194 | "allBreakoutRooms": [{"name": "Room 1", "isOpen": open, "meetingJoinUrl": ROOM_URL, "invitees": []}], |
| 195 | "assignedBreakoutRooms": [{"name": "Room 1", "isOpen": open, "meetingJoinUrl": ROOM_URL, "invitees": [{"id": "8:orgid:me"}], "eTag": "1", "moveJitter": 3}], |
| 196 | }}}) |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn a_meetup_join_link_becomes_a_meeting_target() { |
| 201 | let target = target_from_join_url(ROOM_URL).unwrap(); |
| 202 | assert_eq!(target.thread_id, "19:meeting_Room1abc@thread.v2"); |
| 203 | assert_eq!(target.tenant_id, "tenant-1"); |
| 204 | assert_eq!(target.organizer_id, "org-1"); |
| 205 | let plain = "https://teams.cloud.microsoft/l/meetup-join/19:meeting_x@thread.v2/0?context={\"Tid\":\"t\",\"Oid\":\"o\"}"; |
| 206 | assert_eq!(target_from_join_url(plain).unwrap().organizer_id, "o"); |
| 207 | assert!(target_from_join_url("https://example.com/other").is_none()); |
| 208 | assert!( |
| 209 | target_from_join_url("https://teams.microsoft.com/l/meetup-join/19:x@thread.v2/0") |
| 210 | .is_none() |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn coordinates_name_the_meeting_with_the_first_colon_replaced() { |
| 216 | let target = |
| 217 | target_from_coordinates("tenant-1/org-1/19_meeting_Room1abc@thread.v2/0").unwrap(); |
| 218 | assert_eq!(target.thread_id, "19:meeting_Room1abc@thread.v2"); |
| 219 | assert_eq!( |
| 220 | (target.tenant_id.as_str(), target.organizer_id.as_str()), |
| 221 | ("tenant-1", "org-1") |
| 222 | ); |
| 223 | assert!(target_from_coordinates("tenant-1/org-1").is_none()); |
| 224 | assert!(target_from_coordinates("t/o/nonsense/0").is_none()); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn the_assigned_room_is_read_from_the_participant_update() { |
| 229 | let assignment = room_assignment(&room_update(true, 5)).unwrap(); |
| 230 | assert_eq!( |
| 231 | assignment, |
| 232 | RoomAssignment { |
| 233 | name: "Room 1".into(), |
| 234 | open: true, |
| 235 | join_url: ROOM_URL.into(), |
| 236 | version: 5 |
| 237 | } |
| 238 | ); |
| 239 | assert!(room_assignment(&json!({"breakoutDetails": {"mainMeetingProperties": {"assignedBreakoutRooms": []}}})).is_none()); |
| 240 | assert!(room_assignment(&json!({"other": 1})).is_none()); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn an_opening_room_moves_you_once() { |
| 245 | let mut watcher = RoomWatcher::default(); |
| 246 | assert!( |
| 247 | watcher |
| 248 | .apply(room_assignment(&room_update(false, 1))) |
| 249 | .is_none() |
| 250 | ); |
| 251 | let moved = watcher |
| 252 | .apply(room_assignment(&room_update(true, 2))) |
| 253 | .unwrap(); |
| 254 | assert_eq!(moved.room_name, "Room 1"); |
| 255 | assert_eq!(moved.target.thread_id, "19:meeting_Room1abc@thread.v2"); |
| 256 | assert!(!moved.returning); |
| 257 | assert!( |
| 258 | watcher |
| 259 | .apply(room_assignment(&room_update(true, 3))) |
| 260 | .is_none() |
| 261 | ); |
| 262 | assert!( |
| 263 | watcher |
| 264 | .apply(room_assignment(&room_update(false, 4))) |
| 265 | .is_none() |
| 266 | ); |
| 267 | assert!( |
| 268 | watcher |
| 269 | .apply(room_assignment(&room_update(true, 5))) |
| 270 | .is_some() |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn old_versions_and_the_room_you_are_already_in_do_not_move_you() { |
| 276 | let mut watcher = RoomWatcher::default(); |
| 277 | assert!( |
| 278 | watcher |
| 279 | .apply(room_assignment(&room_update(true, 9))) |
| 280 | .is_some() |
| 281 | ); |
| 282 | assert!( |
| 283 | watcher |
| 284 | .apply(room_assignment(&room_update(false, 4))) |
| 285 | .is_none() |
| 286 | ); |
| 287 | let mut inside = RoomWatcher::default(); |
| 288 | inside.entered(ROOM_URL); |
| 289 | assert!( |
| 290 | inside |
| 291 | .apply(room_assignment(&room_update(true, 1))) |
| 292 | .is_none() |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | fn invite(room: bool, main: bool) -> Value { |
| 297 | json!({"callNotification": {"callType": "replaces", "invitationData": {"breakoutRoomData": { |
| 298 | "isRoomInvite": room, "isMainMeetingInvite": main, "meetingName": "Room 2", |
| 299 | "relatedMeetingCoords": "tenant-1/org-1/19_meeting_Room2@thread.v2/0", "shouldAutoAcceptInvite": true, "correlationId": "c"}}, |
| 300 | "meetingInfo": {"tenantId": "tenant-1", "organizerId": "org-1"}}}) |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn a_room_invite_names_the_room_and_a_main_invite_sends_you_back() { |
| 305 | let moved = invite_move(&invite(true, false)).unwrap(); |
| 306 | assert_eq!(moved.room_name, "Room 2"); |
| 307 | assert_eq!(moved.target.thread_id, "19:meeting_Room2@thread.v2"); |
| 308 | assert!(!moved.returning); |
| 309 | let back = invite_move(&invite(false, true)).unwrap(); |
| 310 | assert!(back.returning); |
| 311 | assert!(invite_move(&invite(false, false)).is_none()); |
| 312 | assert!(invite_move(&json!({"callNotification": {}})).is_none()); |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn the_room_properties_carry_the_way_back() { |
| 317 | let update = json!({"meetingDetails": {"breakoutDetails": {"breakoutRoomProperties": {"mainMeetingJoinUrl": ROOM_URL, "remainingRoomDuration": 600}}}}); |
| 318 | assert_eq!(main_meeting_target(&update).unwrap().tenant_id, "tenant-1"); |
| 319 | assert!(main_meeting_target(&json!({"meetingDetails": {}})).is_none()); |
| 320 | } |
| 321 | } |