| 1 | use std::sync::Arc; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use calling::{ |
| 5 | CallCommand, CallState, CallUpdate, CameraDevice, CaptionEntry, CaptionState, ContentShare, DeviceChoice, DeviceLists, EndKind, EndReason, |
| 6 | HoldState, MeetingTarget, Progress, Reaction, RosterEntry, ShareKind, ShareSource, SpeakingDetector, VideoHub, |
| 7 | }; |
| 8 | use tokio::sync::mpsc::UnboundedSender; |
| 9 | |
| 10 | use super::background::BackgroundPick; |
| 11 | use super::pictures::CallPictures; |
| 12 | |
| 13 | pub const TEST_CALL_TITLE: &str = "Test call"; |
| 14 | pub const ECHO_NAME: &str = "Teams echo"; |
| 15 | pub const ECHO_MRI: &str = calling::signaling::ECHO_BOT_MRI; |
| 16 | pub const MAX_TILES: usize = 9; |
| 17 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 18 | const WIDE_TILES: usize = 2; |
| 19 | const MEDIUM_TILES: usize = 4; |
| 20 | pub const REACTION_SHOWN: Duration = Duration::from_secs(3); |
| 21 | const OWN_ECHO_WINDOW: Duration = Duration::from_secs(2); |
| 22 | pub const CHAT_OPEN_TILES: usize = 6; |
| 23 | pub const CAPTION_SHOWN: Duration = Duration::from_secs(4); |
| 24 | const CAPTION_LINES: usize = 2; |
| 25 | |
| 26 | pub struct ActiveCall { |
| 27 | pub id: u64, |
| 28 | pub model: CallModel, |
| 29 | pub commands: UnboundedSender<CallCommand>, |
| 30 | pub viewing: bool, |
| 31 | pub conversation_id: Option<String>, |
| 32 | pub video: Arc<VideoHub>, |
| 33 | pub pictures: CallPictures, |
| 34 | pub stage_fullscreen: bool, |
| 35 | pub chat_open: bool, |
| 36 | pub meeting_target: Option<MeetingTarget>, |
| 37 | pub pending_move: Option<PendingMove>, |
| 38 | pub consult: Option<ConsultCall>, |
| 39 | } |
| 40 | |
| 41 | #[derive(Debug, Clone, PartialEq)] |
| 42 | pub struct PendingMove { |
| 43 | pub target: MeetingTarget, |
| 44 | pub title: String, |
| 45 | pub breakout: Option<BreakoutState>, |
| 46 | } |
| 47 | |
| 48 | pub struct ConsultCall { |
| 49 | pub id: u64, |
| 50 | pub target: (String, String), |
| 51 | pub commands: UnboundedSender<CallCommand>, |
| 52 | pub state: CallState, |
| 53 | pub replacement: Option<String>, |
| 54 | pub transferring: bool, |
| 55 | } |
| 56 | |
| 57 | impl ConsultCall { |
| 58 | pub fn connected(&self) -> bool { |
| 59 | matches!(self.state, CallState::Connected { .. }) |
| 60 | } |
| 61 | |
| 62 | pub fn ready_to_transfer(&self) -> bool { |
| 63 | self.connected() && self.replacement.is_some() |
| 64 | } |
| 65 | |
| 66 | pub fn status_text(&self) -> String { |
| 67 | let name = &self.target.1; |
| 68 | match &self.state { |
| 69 | CallState::Connected { .. } => format!("Consulting {name}"), |
| 70 | _ => format!("Calling {name}..."), |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl ActiveCall { |
| 76 | pub fn new(id: u64, model: CallModel, commands: UnboundedSender<CallCommand>, conversation_id: Option<String>, video: Arc<VideoHub>) -> Self { |
| 77 | ActiveCall { |
| 78 | id, |
| 79 | model, |
| 80 | commands, |
| 81 | viewing: true, |
| 82 | conversation_id, |
| 83 | video, |
| 84 | pictures: CallPictures::default(), |
| 85 | stage_fullscreen: false, |
| 86 | chat_open: false, |
| 87 | meeting_target: None, |
| 88 | pending_move: None, |
| 89 | consult: None, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | pub fn chat_thread(&self) -> Option<&str> { |
| 94 | chat_thread(self.model.kind, self.model.meeting_chat.as_deref(), self.conversation_id.as_deref()) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn chat_thread<'a>(kind: CallKind, meeting_chat: Option<&'a str>, conversation_id: Option<&'a str>) -> Option<&'a str> { |
| 99 | match kind { |
| 100 | CallKind::Meeting => meeting_chat.or(conversation_id), |
| 101 | CallKind::Test | CallKind::Direct | CallKind::Group => conversation_id, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 106 | pub enum CallKind { |
| 107 | Test, |
| 108 | Direct, |
| 109 | Group, |
| 110 | Meeting, |
| 111 | } |
| 112 | |
| 113 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 114 | pub enum TileState { |
| 115 | Invited, |
| 116 | Present, |
| 117 | InLobby, |
| 118 | Left, |
| 119 | } |
| 120 | |
| 121 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 122 | pub struct Tile { |
| 123 | pub mri: String, |
| 124 | pub user_id: Option<String>, |
| 125 | pub name: String, |
| 126 | pub muted: bool, |
| 127 | pub state: TileState, |
| 128 | pub invited: bool, |
| 129 | pub has_video: bool, |
| 130 | pub hand: Option<u64>, |
| 131 | pub spotlight: Option<u64>, |
| 132 | } |
| 133 | |
| 134 | impl Tile { |
| 135 | pub fn invited(mri: &str, name: &str) -> Tile { |
| 136 | Tile { |
| 137 | mri: mri.to_owned(), |
| 138 | user_id: mri.strip_prefix(ORGID_PREFIX).map(str::to_owned), |
| 139 | name: name.to_owned(), |
| 140 | muted: false, |
| 141 | state: TileState::Invited, |
| 142 | invited: true, |
| 143 | has_video: false, |
| 144 | hand: None, |
| 145 | spotlight: None, |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[derive(Debug, Clone, PartialEq)] |
| 151 | pub struct BreakoutState { |
| 152 | pub room_name: String, |
| 153 | pub main: Option<MeetingTarget>, |
| 154 | pub main_title: String, |
| 155 | } |
| 156 | |
| 157 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 158 | pub struct Focus { |
| 159 | pub mri: String, |
| 160 | pub own: bool, |
| 161 | pub spotlight: bool, |
| 162 | pub pinned: bool, |
| 163 | } |
| 164 | |
| 165 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 166 | pub struct CaptionLine { |
| 167 | pub id: String, |
| 168 | pub speaker: String, |
| 169 | pub text: String, |
| 170 | pub at: Instant, |
| 171 | } |
| 172 | |
| 173 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 174 | pub struct GridLayout { |
| 175 | pub shown_others: usize, |
| 176 | pub hidden: usize, |
| 177 | } |
| 178 | |
| 179 | pub fn grid_layout(others: usize, max_tiles: usize) -> GridLayout { |
| 180 | let total = others + 1; |
| 181 | if total <= max_tiles { |
| 182 | return GridLayout { |
| 183 | shown_others: others, |
| 184 | hidden: 0, |
| 185 | }; |
| 186 | } |
| 187 | let shown_others = max_tiles - 2; |
| 188 | GridLayout { |
| 189 | shown_others, |
| 190 | hidden: others - shown_others, |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 195 | pub struct TileSize { |
| 196 | pub width: f32, |
| 197 | pub height: f32, |
| 198 | pub avatar: f32, |
| 199 | } |
| 200 | |
| 201 | pub fn tile_size(cells: usize) -> TileSize { |
| 202 | if cells <= WIDE_TILES { |
| 203 | TileSize { |
| 204 | width: 260., |
| 205 | height: 210., |
| 206 | avatar: 96., |
| 207 | } |
| 208 | } else if cells <= MEDIUM_TILES { |
| 209 | TileSize { |
| 210 | width: 220., |
| 211 | height: 180., |
| 212 | avatar: 72., |
| 213 | } |
| 214 | } else { |
| 215 | TileSize { |
| 216 | width: 190., |
| 217 | height: 150., |
| 218 | avatar: 56., |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 224 | pub struct ReactionChip { |
| 225 | pub mri: String, |
| 226 | pub reaction: Reaction, |
| 227 | pub since: Instant, |
| 228 | pub serial: u64, |
| 229 | } |
| 230 | |
| 231 | #[derive(Debug, Clone)] |
| 232 | pub struct CallModel { |
| 233 | pub title: String, |
| 234 | pub kind: CallKind, |
| 235 | pub peer_name: String, |
| 236 | pub state: CallState, |
| 237 | pub progress: Option<Progress>, |
| 238 | pub muted: bool, |
| 239 | pub listen_only: bool, |
| 240 | pub lobby: bool, |
| 241 | pub can_end_meeting: bool, |
| 242 | pub incoming: bool, |
| 243 | pub input: DeviceChoice, |
| 244 | pub output: DeviceChoice, |
| 245 | pub devices: DeviceLists, |
| 246 | pub local_speaking: bool, |
| 247 | pub remote_speaking: bool, |
| 248 | pub speaking_mris: Vec<String>, |
| 249 | pub connected_since: Option<Instant>, |
| 250 | pub own_mri: Option<String>, |
| 251 | pub tiles: Vec<Tile>, |
| 252 | pub screen_sharer: Option<String>, |
| 253 | pub camera_on: bool, |
| 254 | pub cameras: Vec<CameraDevice>, |
| 255 | pub camera: DeviceChoice, |
| 256 | pub local_share: Option<String>, |
| 257 | pub share_sources: Vec<ShareSource>, |
| 258 | pub share_sound: bool, |
| 259 | pub own_hand: Option<u64>, |
| 260 | pub own_spotlight: Option<u64>, |
| 261 | pub organizer: bool, |
| 262 | pub pinned: Option<String>, |
| 263 | pub captions: CaptionState, |
| 264 | pub caption_lines: Vec<CaptionLine>, |
| 265 | pub background: BackgroundPick, |
| 266 | pub recording: bool, |
| 267 | pub consent_required: bool, |
| 268 | pub hold: HoldState, |
| 269 | pub whiteboard: Option<ContentShare>, |
| 270 | pub breakout: Option<BreakoutState>, |
| 271 | pub main_meeting: Option<MeetingTarget>, |
| 272 | pub reactions: Vec<ReactionChip>, |
| 273 | reaction_serial: u64, |
| 274 | pub meeting_chat: Option<String>, |
| 275 | local_detector: SpeakingDetector, |
| 276 | remote_detector: SpeakingDetector, |
| 277 | } |
| 278 | |
| 279 | impl CallModel { |
| 280 | fn base(kind: CallKind, title: &str, peer_name: &str, tiles: Vec<Tile>) -> Self { |
| 281 | CallModel { |
| 282 | title: title.to_owned(), |
| 283 | kind, |
| 284 | peer_name: peer_name.to_owned(), |
| 285 | state: CallState::Connecting, |
| 286 | progress: None, |
| 287 | muted: false, |
| 288 | listen_only: false, |
| 289 | lobby: false, |
| 290 | can_end_meeting: false, |
| 291 | incoming: false, |
| 292 | input: DeviceChoice::SystemDefault, |
| 293 | output: DeviceChoice::SystemDefault, |
| 294 | devices: DeviceLists::default(), |
| 295 | local_speaking: false, |
| 296 | remote_speaking: false, |
| 297 | speaking_mris: Vec::new(), |
| 298 | connected_since: None, |
| 299 | own_mri: None, |
| 300 | tiles, |
| 301 | screen_sharer: None, |
| 302 | camera_on: false, |
| 303 | cameras: Vec::new(), |
| 304 | camera: DeviceChoice::SystemDefault, |
| 305 | local_share: None, |
| 306 | share_sources: Vec::new(), |
| 307 | share_sound: false, |
| 308 | own_hand: None, |
| 309 | own_spotlight: None, |
| 310 | organizer: false, |
| 311 | pinned: None, |
| 312 | captions: CaptionState::Off, |
| 313 | caption_lines: Vec::new(), |
| 314 | background: BackgroundPick::None, |
| 315 | recording: false, |
| 316 | consent_required: false, |
| 317 | hold: HoldState::Active, |
| 318 | whiteboard: None, |
| 319 | breakout: None, |
| 320 | main_meeting: None, |
| 321 | reactions: Vec::new(), |
| 322 | reaction_serial: 0, |
| 323 | meeting_chat: None, |
| 324 | local_detector: SpeakingDetector::default(), |
| 325 | remote_detector: SpeakingDetector::default(), |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | pub fn test() -> Self { |
| 330 | Self::base( |
| 331 | CallKind::Test, |
| 332 | TEST_CALL_TITLE, |
| 333 | ECHO_NAME, |
| 334 | vec![Tile::invited(ECHO_MRI, ECHO_NAME)], |
| 335 | ) |
| 336 | } |
| 337 | |
| 338 | pub fn people(title: &str, callees: &[(String, String)]) -> Self { |
| 339 | let kind = if callees.len() == 1 { CallKind::Direct } else { CallKind::Group }; |
| 340 | let peer_name = match callees { |
| 341 | [(_, name)] => name.clone(), |
| 342 | _ => title.to_owned(), |
| 343 | }; |
| 344 | let tiles = callees.iter().map(|(mri, name)| Tile::invited(mri, name)).collect(); |
| 345 | Self::base(kind, title, &peer_name, tiles) |
| 346 | } |
| 347 | |
| 348 | pub fn meeting(title: &str, can_end_meeting: bool) -> Self { |
| 349 | let mut model = Self::base(CallKind::Meeting, title, title, Vec::new()); |
| 350 | model.can_end_meeting = can_end_meeting; |
| 351 | model |
| 352 | } |
| 353 | |
| 354 | pub fn incoming(title: &str, caller_mri: &str, caller_name: &str) -> Self { |
| 355 | let mut model = Self::base( |
| 356 | CallKind::Direct, |
| 357 | title, |
| 358 | caller_name, |
| 359 | vec![Tile { |
| 360 | state: TileState::Present, |
| 361 | ..Tile::invited(caller_mri, caller_name) |
| 362 | }], |
| 363 | ); |
| 364 | model.incoming = true; |
| 365 | model |
| 366 | } |
| 367 | |
| 368 | pub fn apply(&mut self, update: CallUpdate) { |
| 369 | match update { |
| 370 | CallUpdate::State(state) => { |
| 371 | if let Some(since) = state.started_at() { |
| 372 | self.connected_since = Some(since); |
| 373 | } |
| 374 | if !matches!(state, CallState::Connected { .. }) { |
| 375 | self.silence(); |
| 376 | } |
| 377 | self.state = state; |
| 378 | } |
| 379 | CallUpdate::Muted(muted) => { |
| 380 | self.muted = muted; |
| 381 | if muted { |
| 382 | self.local_speaking = false; |
| 383 | self.local_detector.reset(); |
| 384 | } |
| 385 | } |
| 386 | CallUpdate::Levels { local, remote } => { |
| 387 | self.local_speaking = !self.muted && self.local_detector.update(local); |
| 388 | self.remote_speaking = self.remote_detector.update(remote); |
| 389 | } |
| 390 | CallUpdate::Devices(devices) => self.devices = devices, |
| 391 | CallUpdate::Selected { input, output } => { |
| 392 | self.input = input; |
| 393 | self.output = output; |
| 394 | } |
| 395 | CallUpdate::ListenOnly(listen_only) => self.listen_only = listen_only, |
| 396 | CallUpdate::Stats { .. } => {} |
| 397 | CallUpdate::Progress(progress) => self.progress = Some(progress), |
| 398 | CallUpdate::Roster(entries) => self.apply_roster(&entries), |
| 399 | CallUpdate::Speakers(mris) => self.speaking_mris = mris, |
| 400 | CallUpdate::Lobby(lobby) => self.lobby = lobby, |
| 401 | CallUpdate::OwnIdentity { mri } => self.own_mri = Some(mri), |
| 402 | CallUpdate::ScreenShare(sharer) => self.screen_sharer = sharer, |
| 403 | CallUpdate::VideoReady | CallUpdate::Notice(_) => {} |
| 404 | CallUpdate::Camera(on) => self.camera_on = on, |
| 405 | CallUpdate::Cameras(cameras) => self.cameras = cameras, |
| 406 | CallUpdate::LocalShare(label) => { |
| 407 | if label.is_none() { |
| 408 | self.share_sound = false; |
| 409 | } |
| 410 | self.local_share = label; |
| 411 | } |
| 412 | CallUpdate::ShareSources(sources) => self.share_sources = sources, |
| 413 | CallUpdate::ShareSound(active) => self.share_sound = active, |
| 414 | CallUpdate::OwnHand(rank) => self.own_hand = rank, |
| 415 | CallUpdate::Reaction { mri, reaction } => self.note_reaction(mri, reaction, Instant::now()), |
| 416 | CallUpdate::MeetingChat(thread) => self.meeting_chat = Some(thread), |
| 417 | CallUpdate::Captions(state) => { |
| 418 | if state != CaptionState::On { |
| 419 | self.caption_lines.clear(); |
| 420 | } |
| 421 | self.captions = state; |
| 422 | } |
| 423 | CallUpdate::Caption(entry) => self.note_caption(entry, Instant::now()), |
| 424 | CallUpdate::Recording(on) => self.recording = on, |
| 425 | CallUpdate::ConsentRequired(required) => self.consent_required = required, |
| 426 | CallUpdate::Hold(hold) => self.hold = hold, |
| 427 | CallUpdate::Whiteboard(share) => self.whiteboard = share, |
| 428 | CallUpdate::BreakoutRoom { main } => self.main_meeting = Some(main), |
| 429 | CallUpdate::BlurTiming(_) |
| 430 | | CallUpdate::Organizer { .. } |
| 431 | | CallUpdate::WhiteboardUrl(_) |
| 432 | | CallUpdate::BreakoutMove(_) |
| 433 | | CallUpdate::ReplacementLink(_) => {} |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | pub fn note_caption(&mut self, entry: CaptionEntry, at: Instant) { |
| 438 | if self.captions == CaptionState::Off { |
| 439 | return; |
| 440 | } |
| 441 | let speaker = if entry.display_name.is_empty() { "Someone".to_owned() } else { entry.display_name }; |
| 442 | let line = CaptionLine { id: entry.id, speaker, text: entry.text, at }; |
| 443 | match self.caption_lines.last_mut() { |
| 444 | Some(last) if last.id == line.id => *last = line, |
| 445 | _ => self.caption_lines.push(line), |
| 446 | } |
| 447 | let surplus = self.caption_lines.len().saturating_sub(CAPTION_LINES); |
| 448 | self.caption_lines.drain(..surplus); |
| 449 | } |
| 450 | |
| 451 | pub fn visible_captions(&self, now: Instant) -> Vec<&CaptionLine> { |
| 452 | self.caption_lines.iter().filter(|line| now.saturating_duration_since(line.at) < CAPTION_SHOWN).collect() |
| 453 | } |
| 454 | |
| 455 | pub fn captions_on(&self) -> bool { |
| 456 | matches!(self.captions, CaptionState::Starting | CaptionState::On) |
| 457 | } |
| 458 | |
| 459 | fn silence(&mut self) { |
| 460 | self.local_speaking = false; |
| 461 | self.remote_speaking = false; |
| 462 | self.speaking_mris.clear(); |
| 463 | self.local_detector.reset(); |
| 464 | self.remote_detector.reset(); |
| 465 | } |
| 466 | |
| 467 | pub fn note_reaction(&mut self, mri: String, reaction: Reaction, at: Instant) { |
| 468 | let own_echo = Some(&mri) == self.own_mri.as_ref() |
| 469 | && self.reactions.iter().any(|chip| { |
| 470 | chip.mri == mri && chip.reaction == reaction && at.saturating_duration_since(chip.since) < OWN_ECHO_WINDOW |
| 471 | }); |
| 472 | if own_echo { |
| 473 | return; |
| 474 | } |
| 475 | self.reactions.retain(|chip| chip.mri != mri && at.saturating_duration_since(chip.since) < REACTION_SHOWN); |
| 476 | self.reaction_serial += 1; |
| 477 | self.reactions.push(ReactionChip { mri, reaction, since: at, serial: self.reaction_serial }); |
| 478 | } |
| 479 | |
| 480 | pub fn chip_of(&self, mri: &str, now: Instant) -> Option<&ReactionChip> { |
| 481 | self.reactions |
| 482 | .iter() |
| 483 | .find(|chip| chip.mri == mri && now.saturating_duration_since(chip.since) < REACTION_SHOWN) |
| 484 | } |
| 485 | |
| 486 | pub fn own_chip(&self, now: Instant) -> Option<&ReactionChip> { |
| 487 | self.chip_of(self.own_mri.as_deref()?, now) |
| 488 | } |
| 489 | |
| 490 | pub fn hand_position(&self, rank: u64) -> usize { |
| 491 | let raised = self.tiles.iter().filter_map(|tile| tile.hand).chain(self.own_hand); |
| 492 | 1 + raised.filter(|other| *other < rank).count() |
| 493 | } |
| 494 | |
| 495 | pub fn can_manage(&self) -> bool { |
| 496 | self.can_end_meeting || self.organizer |
| 497 | } |
| 498 | |
| 499 | pub fn organizes_meeting(&self) -> bool { |
| 500 | self.kind == CallKind::Meeting && self.can_manage() |
| 501 | } |
| 502 | |
| 503 | pub fn can_lower_hands(&self) -> bool { |
| 504 | self.can_manage() |
| 505 | } |
| 506 | |
| 507 | pub fn lobby_guests(&self) -> Vec<&Tile> { |
| 508 | self.tiles.iter().filter(|tile| tile.state == TileState::InLobby).collect() |
| 509 | } |
| 510 | |
| 511 | pub fn can_admit(&self) -> bool { |
| 512 | self.can_manage() && self.is_live() && !self.lobby |
| 513 | } |
| 514 | |
| 515 | pub fn is_pinned(&self, mri: &str) -> bool { |
| 516 | self.pinned.as_deref() == Some(mri) |
| 517 | } |
| 518 | |
| 519 | pub fn toggle_pin(&mut self, mri: &str) { |
| 520 | self.pinned = if self.is_pinned(mri) { None } else { Some(mri.to_owned()) }; |
| 521 | } |
| 522 | |
| 523 | pub fn is_spotlighted(&self, mri: &str) -> bool { |
| 524 | self.own_mri.as_deref() == Some(mri) && self.own_spotlight.is_some() |
| 525 | || self.tiles.iter().any(|tile| tile.mri == mri && tile.spotlight.is_some()) |
| 526 | } |
| 527 | |
| 528 | pub fn focus(&self) -> Option<Focus> { |
| 529 | let own = |mri: &str| self.own_mri.as_deref() == Some(mri); |
| 530 | let on_stage = |mri: &str| own(mri) || self.visible_tiles().iter().any(|tile| tile.mri == mri && tile.state == TileState::Present); |
| 531 | if let Some(pinned) = self.pinned.as_deref().filter(|mri| on_stage(mri)) { |
| 532 | return Some(Focus { mri: pinned.to_owned(), own: own(pinned), spotlight: self.is_spotlighted(pinned), pinned: true }); |
| 533 | } |
| 534 | let remote = self.tiles.iter().filter(|tile| tile.state == TileState::Present).filter_map(|tile| Some((tile.spotlight?, tile.mri.as_str()))); |
| 535 | let (_, mri) = remote.chain(self.own_spotlight.zip(self.own_mri.as_deref())).min_by_key(|(rank, _)| *rank)?; |
| 536 | Some(Focus { mri: mri.to_owned(), own: own(mri), spotlight: true, pinned: false }) |
| 537 | } |
| 538 | |
| 539 | pub fn any_hand_raised(&self) -> bool { |
| 540 | self.own_hand.is_some() || self.tiles.iter().any(|tile| tile.hand.is_some()) |
| 541 | } |
| 542 | |
| 543 | pub fn own_cell_index(&self, remote_cells: usize) -> usize { |
| 544 | let Some(own) = self.own_hand else { return remote_cells }; |
| 545 | let ahead = self.visible_tiles().iter().filter(|tile| tile.hand.is_some_and(|rank| rank < own)).count(); |
| 546 | ahead.min(remote_cells) |
| 547 | } |
| 548 | |
| 549 | fn apply_roster(&mut self, entries: &[RosterEntry]) { |
| 550 | let own_entry = entries.iter().find(|entry| Some(&entry.mri) == self.own_mri.as_ref()); |
| 551 | if let Some(entry) = own_entry { |
| 552 | self.own_hand = entry.hand.as_ref().map(|hand| hand.rank); |
| 553 | } |
| 554 | self.own_spotlight = own_entry.and_then(|entry| entry.spotlight.as_ref().map(|spotlight| spotlight.rank)); |
| 555 | if let Some(entry) = own_entry { |
| 556 | self.organizer = entry.organizer; |
| 557 | } |
| 558 | for entry in entries.iter().filter(|entry| Some(&entry.mri) != self.own_mri.as_ref()) { |
| 559 | let state = if entry.in_lobby { TileState::InLobby } else { TileState::Present }; |
| 560 | match self.tiles.iter_mut().find(|tile| tile.mri == entry.mri) { |
| 561 | Some(tile) => { |
| 562 | tile.state = state; |
| 563 | tile.muted = entry.muted; |
| 564 | tile.has_video = entry.has_video; |
| 565 | tile.hand = entry.hand.as_ref().map(|hand| hand.rank); |
| 566 | tile.spotlight = entry.spotlight.as_ref().map(|spotlight| spotlight.rank); |
| 567 | if tile.name.is_empty() { |
| 568 | tile.name = entry.display_name.clone(); |
| 569 | } |
| 570 | } |
| 571 | None => self.tiles.push(Tile { |
| 572 | mri: entry.mri.clone(), |
| 573 | user_id: entry.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned), |
| 574 | name: entry.display_name.clone(), |
| 575 | muted: entry.muted, |
| 576 | state, |
| 577 | invited: false, |
| 578 | has_video: entry.has_video, |
| 579 | hand: entry.hand.as_ref().map(|hand| hand.rank), |
| 580 | spotlight: entry.spotlight.as_ref().map(|spotlight| spotlight.rank), |
| 581 | }), |
| 582 | } |
| 583 | } |
| 584 | let own_mri = self.own_mri.clone(); |
| 585 | let listed = |mri: &str| entries.iter().any(|entry| entry.mri == mri); |
| 586 | if self.pinned.as_deref().is_some_and(|mri| !listed(mri) && Some(mri) != own_mri.as_deref()) { |
| 587 | self.pinned = None; |
| 588 | } |
| 589 | self.tiles.retain_mut(|tile| { |
| 590 | if listed(&tile.mri) || Some(&tile.mri) == own_mri.as_ref() { |
| 591 | return true; |
| 592 | } |
| 593 | match (tile.invited, tile.state) { |
| 594 | (true, TileState::Invited) => true, |
| 595 | (true, _) => { |
| 596 | tile.state = TileState::Left; |
| 597 | true |
| 598 | } |
| 599 | (false, _) => false, |
| 600 | } |
| 601 | }); |
| 602 | } |
| 603 | |
| 604 | pub fn visible_tiles(&self) -> Vec<&Tile> { |
| 605 | let mut tiles: Vec<&Tile> = self.tiles.iter().filter(|tile| tile.state != TileState::InLobby).collect(); |
| 606 | tiles.sort_by_key(|tile| { |
| 607 | let state = match tile.state { |
| 608 | TileState::Present => 0, |
| 609 | TileState::Invited => 1, |
| 610 | _ => 2, |
| 611 | }; |
| 612 | (!self.is_pinned(&tile.mri), tile.hand.is_none(), tile.hand.unwrap_or(0), state) |
| 613 | }); |
| 614 | tiles |
| 615 | } |
| 616 | |
| 617 | pub fn strip_tiles(&self) -> Vec<&Tile> { |
| 618 | let mut tiles = self.visible_tiles(); |
| 619 | tiles.sort_by_key(|tile| (!self.is_pinned(&tile.mri), tile.hand.is_none(), tile.hand.unwrap_or(0), !self.tile_speaking(tile))); |
| 620 | tiles |
| 621 | } |
| 622 | |
| 623 | pub fn sharing_label(&self) -> Option<String> { |
| 624 | let sharer = self.screen_sharer.as_ref()?; |
| 625 | let name = self |
| 626 | .tiles |
| 627 | .iter() |
| 628 | .find(|tile| &tile.mri == sharer) |
| 629 | .map(|tile| tile.name.as_str()) |
| 630 | .filter(|name| !name.is_empty()) |
| 631 | .unwrap_or("Someone"); |
| 632 | Some(format!("{name} is sharing")) |
| 633 | } |
| 634 | |
| 635 | pub fn whiteboard_label(&self) -> Option<String> { |
| 636 | let share = self.whiteboard.as_ref()?; |
| 637 | let name = share |
| 638 | .presenter |
| 639 | .as_deref() |
| 640 | .and_then(|presenter| self.tiles.iter().find(|tile| tile.mri == presenter)) |
| 641 | .map(|tile| tile.name.as_str()) |
| 642 | .filter(|name| !name.is_empty()) |
| 643 | .unwrap_or("Someone"); |
| 644 | Some(format!("{name} is sharing a whiteboard")) |
| 645 | } |
| 646 | |
| 647 | pub fn whiteboard_url(&self) -> Option<&str> { |
| 648 | self.whiteboard.as_ref()?.url.as_deref() |
| 649 | } |
| 650 | |
| 651 | pub fn is_one_to_one(&self) -> bool { |
| 652 | self.kind == CallKind::Direct |
| 653 | } |
| 654 | |
| 655 | pub fn can_hold(&self) -> bool { |
| 656 | self.is_one_to_one() && self.is_live() && self.hold != HoldState::Remote |
| 657 | } |
| 658 | |
| 659 | pub fn can_transfer(&self) -> bool { |
| 660 | self.is_one_to_one() && self.is_live() && self.hold == HoldState::Active |
| 661 | } |
| 662 | |
| 663 | pub fn can_record(&self) -> bool { |
| 664 | self.organizes_meeting() && self.is_live() && !self.lobby |
| 665 | } |
| 666 | |
| 667 | pub fn can_open_whiteboard(&self) -> bool { |
| 668 | self.kind == CallKind::Meeting && self.is_live() && !self.lobby |
| 669 | } |
| 670 | |
| 671 | pub fn tile_speaking(&self, tile: &Tile) -> bool { |
| 672 | if self.speaking_mris.contains(&tile.mri) { |
| 673 | return true; |
| 674 | } |
| 675 | let only_remote = self.tiles.len() == 1; |
| 676 | only_remote && tile.state != TileState::Left && self.remote_speaking |
| 677 | } |
| 678 | |
| 679 | pub fn tile_caption(&self, tile: &Tile) -> Option<&'static str> { |
| 680 | match tile.state { |
| 681 | TileState::Invited => Some(match self.progress { |
| 682 | Some(Progress::Ringing) => "Ringing", |
| 683 | _ => "Calling...", |
| 684 | }), |
| 685 | TileState::Left => Some("Left"), |
| 686 | TileState::Present | TileState::InLobby => None, |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | pub fn elapsed(&self, now: Instant) -> Option<Duration> { |
| 691 | self.connected_since |
| 692 | .map(|since| now.saturating_duration_since(since)) |
| 693 | } |
| 694 | |
| 695 | pub fn connecting_text(&self) -> &'static str { |
| 696 | match (self.kind, self.progress) { |
| 697 | (CallKind::Meeting, _) => "Joining...", |
| 698 | (_, _) if self.incoming => "Connecting...", |
| 699 | (_, Some(Progress::Ringing)) => "Ringing", |
| 700 | _ => "Calling...", |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | pub fn is_active(&self) -> bool { |
| 705 | matches!( |
| 706 | self.state, |
| 707 | CallState::Connecting | CallState::Connected { .. } | CallState::Reconnecting { .. } |
| 708 | ) |
| 709 | } |
| 710 | |
| 711 | pub fn is_connecting(&self) -> bool { |
| 712 | matches!(self.state, CallState::Connecting) |
| 713 | } |
| 714 | |
| 715 | pub fn is_reconnecting(&self) -> bool { |
| 716 | matches!(self.state, CallState::Reconnecting { .. }) |
| 717 | } |
| 718 | |
| 719 | pub fn ended_reason(&self) -> Option<&EndReason> { |
| 720 | match &self.state { |
| 721 | CallState::Ended { reason } => Some(reason), |
| 722 | _ => None, |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | pub fn timer_text(&self, now: Instant) -> String { |
| 727 | self.elapsed(now).map(format_elapsed).unwrap_or_default() |
| 728 | } |
| 729 | |
| 730 | pub fn has_reaction_controls(&self) -> bool { |
| 731 | matches!(self.kind, CallKind::Group | CallKind::Meeting) |
| 732 | } |
| 733 | |
| 734 | pub fn can_react(&self) -> bool { |
| 735 | self.is_live() && !self.lobby |
| 736 | } |
| 737 | |
| 738 | pub fn can_use_camera(&self) -> bool { |
| 739 | !self.cameras.is_empty() |
| 740 | } |
| 741 | |
| 742 | pub fn can_share(&self) -> bool { |
| 743 | !self.share_sources.is_empty() && self.is_live() |
| 744 | } |
| 745 | |
| 746 | fn is_live(&self) -> bool { |
| 747 | matches!(self.state, CallState::Connected { .. } | CallState::Reconnecting { .. }) |
| 748 | } |
| 749 | |
| 750 | pub fn screens(&self) -> Vec<&ShareSource> { |
| 751 | self.share_sources.iter().filter(|source| source.kind == ShareKind::Screen).collect() |
| 752 | } |
| 753 | |
| 754 | pub fn windows(&self) -> Vec<&ShareSource> { |
| 755 | self.share_sources.iter().filter(|source| source.kind == ShareKind::Window).collect() |
| 756 | } |
| 757 | |
| 758 | pub fn can_unmute(&self) -> bool { |
| 759 | !self.listen_only |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | pub fn format_elapsed(elapsed: Duration) -> String { |
| 764 | let seconds = elapsed.as_secs(); |
| 765 | let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60); |
| 766 | if hours > 0 { |
| 767 | format!("{hours}:{minutes:02}:{seconds:02}") |
| 768 | } else { |
| 769 | format!("{minutes:02}:{seconds:02}") |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | pub fn ended_notice(reason: &EndReason, elapsed: Option<Duration>, peer_name: &str) -> String { |
| 774 | let ended = || match elapsed { |
| 775 | Some(elapsed) => format!("Call ended {}", format_elapsed(elapsed)), |
| 776 | None => "Call ended".to_owned(), |
| 777 | }; |
| 778 | match reason { |
| 779 | EndReason::Cancelled => "Call cancelled".to_owned(), |
| 780 | EndReason::Dropped => "Call dropped".to_owned(), |
| 781 | EndReason::Failed(message) => format!("Call failed: {message}"), |
| 782 | EndReason::LocalHangup => ended(), |
| 783 | EndReason::Remote(EndKind::Declined) => format!("{peer_name} declined"), |
| 784 | EndReason::Remote(EndKind::NoAnswer) => "No answer".to_owned(), |
| 785 | EndReason::Remote(EndKind::Unavailable) => format!("{peer_name} is not available"), |
| 786 | EndReason::Remote(_) => ended(), |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | #[cfg(test)] |
| 791 | mod tests { |
| 792 | use super::*; |
| 793 | |
| 794 | fn connected_model() -> CallModel { |
| 795 | let mut model = CallModel::test(); |
| 796 | model.apply(CallUpdate::State(CallState::Connected { |
| 797 | since: Instant::now(), |
| 798 | })); |
| 799 | model |
| 800 | } |
| 801 | |
| 802 | fn entry(mri: &str, name: &str, muted: bool, in_lobby: bool) -> RosterEntry { |
| 803 | RosterEntry { |
| 804 | mri: mri.into(), |
| 805 | display_name: name.into(), |
| 806 | muted, |
| 807 | in_lobby, |
| 808 | has_video: false, |
| 809 | sharing: false, |
| 810 | hand: None, |
| 811 | spotlight: None, |
| 812 | organizer: false, |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | fn spotlit(mri: &str, name: &str, rank: u64) -> RosterEntry { |
| 817 | RosterEntry { |
| 818 | spotlight: Some(calling::Spotlight { state_id: format!("sp-{rank}"), rank }), |
| 819 | ..entry(mri, name, false, false) |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | fn organizer_entry(mri: &str) -> RosterEntry { |
| 824 | RosterEntry { organizer: true, ..entry(mri, "Me", false, false) } |
| 825 | } |
| 826 | |
| 827 | fn raised(mri: &str, name: &str, rank: u64) -> RosterEntry { |
| 828 | RosterEntry { |
| 829 | hand: Some(calling::RaisedHand { state_id: format!("s-{rank}"), rank }), |
| 830 | ..entry(mri, name, false, false) |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | fn meeting_with_me() -> CallModel { |
| 835 | let mut model = CallModel::meeting("Standup", false); |
| 836 | model.apply(CallUpdate::OwnIdentity { mri: "8:orgid:me".into() }); |
| 837 | model |
| 838 | } |
| 839 | |
| 840 | #[test] |
| 841 | fn elapsed_text_is_minutes_and_seconds() { |
| 842 | assert_eq!(format_elapsed(Duration::from_secs(42)), "00:42"); |
| 843 | assert_eq!(format_elapsed(Duration::from_secs(61)), "01:01"); |
| 844 | assert_eq!(format_elapsed(Duration::from_secs(3725)), "1:02:05"); |
| 845 | } |
| 846 | |
| 847 | #[test] |
| 848 | fn levels_drive_the_speaking_rings() { |
| 849 | let mut model = connected_model(); |
| 850 | model.apply(CallUpdate::Levels { |
| 851 | local: 0.0, |
| 852 | remote: 0.4, |
| 853 | }); |
| 854 | assert!(model.remote_speaking); |
| 855 | assert!(!model.local_speaking); |
| 856 | model.apply(CallUpdate::Levels { |
| 857 | local: 0.4, |
| 858 | remote: 0.0, |
| 859 | }); |
| 860 | assert!(model.local_speaking); |
| 861 | } |
| 862 | |
| 863 | #[test] |
| 864 | fn muting_switches_the_local_ring_off_at_once() { |
| 865 | let mut model = connected_model(); |
| 866 | model.apply(CallUpdate::Levels { |
| 867 | local: 0.5, |
| 868 | remote: 0.0, |
| 869 | }); |
| 870 | assert!(model.local_speaking); |
| 871 | model.apply(CallUpdate::Muted(true)); |
| 872 | assert!(!model.local_speaking); |
| 873 | model.apply(CallUpdate::Levels { |
| 874 | local: 0.5, |
| 875 | remote: 0.0, |
| 876 | }); |
| 877 | assert!(!model.local_speaking); |
| 878 | } |
| 879 | |
| 880 | #[test] |
| 881 | fn reconnecting_keeps_the_timer_and_clears_the_rings() { |
| 882 | let mut model = connected_model(); |
| 883 | let since = model.connected_since.unwrap(); |
| 884 | model.apply(CallUpdate::Levels { |
| 885 | local: 0.5, |
| 886 | remote: 0.5, |
| 887 | }); |
| 888 | model.apply(CallUpdate::State(CallState::Reconnecting { since })); |
| 889 | assert!(model.is_reconnecting()); |
| 890 | assert_eq!(model.connected_since, Some(since)); |
| 891 | assert!(!model.remote_speaking && !model.local_speaking); |
| 892 | } |
| 893 | |
| 894 | #[test] |
| 895 | fn notices_for_every_end_reason() { |
| 896 | let elapsed = Some(Duration::from_secs(42)); |
| 897 | let notice = |reason: EndReason, elapsed| ended_notice(&reason, elapsed, "Bea"); |
| 898 | assert_eq!(notice(EndReason::LocalHangup, elapsed), "Call ended 00:42"); |
| 899 | assert_eq!(notice(EndReason::Remote(EndKind::Normal), elapsed), "Call ended 00:42"); |
| 900 | assert_eq!(notice(EndReason::Remote(EndKind::Normal), None), "Call ended"); |
| 901 | assert_eq!(notice(EndReason::Cancelled, None), "Call cancelled"); |
| 902 | assert_eq!(notice(EndReason::Dropped, elapsed), "Call dropped"); |
| 903 | assert_eq!(notice(EndReason::Failed("no route".into()), None), "Call failed: no route"); |
| 904 | assert_eq!(notice(EndReason::Remote(EndKind::Declined), None), "Bea declined"); |
| 905 | assert_eq!(notice(EndReason::Remote(EndKind::NoAnswer), None), "No answer"); |
| 906 | assert_eq!(notice(EndReason::Remote(EndKind::Unavailable), None), "Bea is not available"); |
| 907 | assert_eq!(notice(EndReason::Remote(EndKind::Cancelled), None), "Call ended"); |
| 908 | assert_eq!(notice(EndReason::Remote(EndKind::Other), None), "Call ended"); |
| 909 | assert_eq!(notice(EndReason::Remote(EndKind::AnsweredElsewhere), None), "Call ended"); |
| 910 | } |
| 911 | |
| 912 | #[test] |
| 913 | fn the_connecting_text_follows_the_kind_of_call() { |
| 914 | let mut direct = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 915 | assert_eq!(direct.connecting_text(), "Calling..."); |
| 916 | direct.apply(CallUpdate::Progress(Progress::Ringing)); |
| 917 | assert_eq!(direct.connecting_text(), "Ringing"); |
| 918 | assert_eq!(CallModel::meeting("Standup", false).connecting_text(), "Joining..."); |
| 919 | assert_eq!(CallModel::incoming("Bea", "8:orgid:b", "Bea").connecting_text(), "Connecting..."); |
| 920 | assert_eq!(CallModel::test().connecting_text(), "Calling..."); |
| 921 | } |
| 922 | |
| 923 | #[test] |
| 924 | fn listen_only_blocks_unmuting() { |
| 925 | let mut model = connected_model(); |
| 926 | assert!(model.can_unmute()); |
| 927 | model.apply(CallUpdate::ListenOnly(true)); |
| 928 | assert!(!model.can_unmute()); |
| 929 | } |
| 930 | |
| 931 | #[test] |
| 932 | fn callees_start_invited_and_follow_the_progress() { |
| 933 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 934 | assert_eq!(model.kind, CallKind::Direct); |
| 935 | assert_eq!(model.peer_name, "Bea"); |
| 936 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Calling...")); |
| 937 | model.apply(CallUpdate::Progress(Progress::Ringing)); |
| 938 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Ringing")); |
| 939 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:b", "Bea", false, false)])); |
| 940 | assert_eq!(model.tiles[0].state, TileState::Present); |
| 941 | assert_eq!(model.tile_caption(&model.tiles[0]), None); |
| 942 | } |
| 943 | |
| 944 | #[test] |
| 945 | fn several_callees_make_a_group_call_named_after_the_chat() { |
| 946 | let model = CallModel::people( |
| 947 | "Retro team", |
| 948 | &[("8:orgid:b".into(), "Bea".into()), ("8:orgid:c".into(), "Cy".into())], |
| 949 | ); |
| 950 | assert_eq!(model.kind, CallKind::Group); |
| 951 | assert_eq!(model.peer_name, "Retro team"); |
| 952 | assert_eq!(model.tiles.len(), 2); |
| 953 | } |
| 954 | |
| 955 | #[test] |
| 956 | fn roster_adds_guests_updates_mute_and_drops_leavers() { |
| 957 | let mut model = meeting_with_me(); |
| 958 | model.apply(CallUpdate::Roster(vec![ |
| 959 | entry("8:orgid:me", "Me", false, false), |
| 960 | entry("8:orgid:a", "Ana", true, false), |
| 961 | entry("8:orgid:b", "Bo", false, false), |
| 962 | ])); |
| 963 | assert_eq!(model.tiles.len(), 2); |
| 964 | assert!(model.tiles.iter().find(|tile| tile.name == "Ana").unwrap().muted); |
| 965 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:me", "Me", false, false), entry("8:orgid:b", "Bo", false, false)])); |
| 966 | assert_eq!(model.tiles.len(), 1); |
| 967 | assert_eq!(model.tiles[0].name, "Bo"); |
| 968 | } |
| 969 | |
| 970 | #[test] |
| 971 | fn an_invited_callee_who_leaves_stays_as_left() { |
| 972 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 973 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:b", "Bea", false, false)])); |
| 974 | model.apply(CallUpdate::Roster(Vec::new())); |
| 975 | assert_eq!(model.tiles[0].state, TileState::Left); |
| 976 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Left")); |
| 977 | } |
| 978 | |
| 979 | #[test] |
| 980 | fn lobby_guests_get_no_tile() { |
| 981 | let mut model = meeting_with_me(); |
| 982 | model.apply(CallUpdate::Roster(vec![ |
| 983 | entry("8:orgid:a", "Ana", false, false), |
| 984 | entry("8:orgid:g", "Guest", false, true), |
| 985 | ])); |
| 986 | let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 987 | assert_eq!(names, vec!["Ana"]); |
| 988 | } |
| 989 | |
| 990 | #[test] |
| 991 | fn the_lobby_flag_follows_the_call() { |
| 992 | let mut model = meeting_with_me(); |
| 993 | model.apply(CallUpdate::Lobby(true)); |
| 994 | assert!(model.lobby); |
| 995 | model.apply(CallUpdate::Lobby(false)); |
| 996 | assert!(!model.lobby); |
| 997 | } |
| 998 | |
| 999 | #[test] |
| 1000 | fn speakers_light_their_own_tile_and_a_lone_remote_follows_the_level() { |
| 1001 | let mut model = meeting_with_me(); |
| 1002 | model.apply(CallUpdate::Roster(vec![ |
| 1003 | entry("8:orgid:a", "Ana", false, false), |
| 1004 | entry("8:orgid:b", "Bo", false, false), |
| 1005 | ])); |
| 1006 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1007 | model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()])); |
| 1008 | let tiles: Vec<Tile> = model.tiles.clone(); |
| 1009 | assert!(!model.tile_speaking(&tiles[0])); |
| 1010 | assert!(model.tile_speaking(&tiles[1])); |
| 1011 | let mut lone = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 1012 | lone.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1013 | lone.apply(CallUpdate::Levels { local: 0.0, remote: 0.5 }); |
| 1014 | assert!(lone.tile_speaking(&lone.tiles[0].clone())); |
| 1015 | } |
| 1016 | |
| 1017 | #[test] |
| 1018 | fn more_than_nine_cells_collapse_into_a_plus_n_tile() { |
| 1019 | assert_eq!(grid_layout(0, MAX_TILES), GridLayout { shown_others: 0, hidden: 0 }); |
| 1020 | assert_eq!(grid_layout(8, MAX_TILES), GridLayout { shown_others: 8, hidden: 0 }); |
| 1021 | assert_eq!(grid_layout(9, MAX_TILES), GridLayout { shown_others: 7, hidden: 2 }); |
| 1022 | assert_eq!(grid_layout(30, MAX_TILES), GridLayout { shown_others: 7, hidden: 23 }); |
| 1023 | } |
| 1024 | |
| 1025 | #[test] |
| 1026 | fn an_open_chat_panel_leaves_room_for_fewer_tiles() { |
| 1027 | assert_eq!(grid_layout(5, CHAT_OPEN_TILES), GridLayout { shown_others: 5, hidden: 0 }); |
| 1028 | assert_eq!(grid_layout(6, CHAT_OPEN_TILES), GridLayout { shown_others: 4, hidden: 2 }); |
| 1029 | } |
| 1030 | |
| 1031 | #[test] |
| 1032 | fn crowded_calls_get_smaller_tiles() { |
| 1033 | assert_eq!(tile_size(2).avatar, 96.); |
| 1034 | assert_eq!(tile_size(4).avatar, 72.); |
| 1035 | assert_eq!(tile_size(9).avatar, 56.); |
| 1036 | } |
| 1037 | |
| 1038 | #[test] |
| 1039 | fn the_stage_label_names_the_sharer_and_the_strip_puts_the_speaker_first() { |
| 1040 | let mut model = meeting_with_me(); |
| 1041 | model.apply(CallUpdate::Roster(vec![ |
| 1042 | entry("8:orgid:a", "Ana", false, false), |
| 1043 | entry("8:orgid:b", "Bo", false, false), |
| 1044 | ])); |
| 1045 | assert_eq!(model.sharing_label(), None); |
| 1046 | model.apply(CallUpdate::ScreenShare(Some("8:orgid:b".into()))); |
| 1047 | assert_eq!(model.sharing_label().as_deref(), Some("Bo is sharing")); |
| 1048 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1049 | model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()])); |
| 1050 | let names: Vec<&str> = model.strip_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 1051 | assert_eq!(names, vec!["Bo", "Ana"]); |
| 1052 | model.apply(CallUpdate::ScreenShare(None)); |
| 1053 | assert_eq!(model.sharing_label(), None); |
| 1054 | } |
| 1055 | |
| 1056 | #[test] |
| 1057 | fn roster_entries_carry_the_camera_flag_to_the_tile() { |
| 1058 | let mut model = meeting_with_me(); |
| 1059 | let mut with_camera = entry("8:orgid:a", "Ana", false, false); |
| 1060 | with_camera.has_video = true; |
| 1061 | model.apply(CallUpdate::Roster(vec![with_camera])); |
| 1062 | assert!(model.tiles[0].has_video); |
| 1063 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false)])); |
| 1064 | assert!(!model.tiles[0].has_video); |
| 1065 | } |
| 1066 | |
| 1067 | #[test] |
| 1068 | fn present_guests_sort_before_waiting_ones() { |
| 1069 | let mut model = CallModel::people( |
| 1070 | "Group", |
| 1071 | &[("8:orgid:b".into(), "Bea".into()), ("8:orgid:c".into(), "Cy".into())], |
| 1072 | ); |
| 1073 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:c", "Cy", false, false)])); |
| 1074 | let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 1075 | assert_eq!(names, vec!["Cy", "Bea"]); |
| 1076 | } |
| 1077 | |
| 1078 | #[test] |
| 1079 | fn raised_hands_get_queue_positions_by_raise_time_and_sort_first() { |
| 1080 | let mut model = meeting_with_me(); |
| 1081 | model.apply(CallUpdate::Roster(vec![ |
| 1082 | entry("8:orgid:a", "Ana", false, false), |
| 1083 | raised("8:orgid:b", "Bo", 9), |
| 1084 | raised("8:orgid:c", "Cy", 4), |
| 1085 | raised("8:orgid:me", "Me", 6), |
| 1086 | ])); |
| 1087 | assert_eq!(model.own_hand, Some(6)); |
| 1088 | assert!(model.any_hand_raised()); |
| 1089 | let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 1090 | assert_eq!(names, vec!["Cy", "Bo", "Ana"]); |
| 1091 | let positions: Vec<usize> = [4, 6, 9].into_iter().map(|rank| model.hand_position(rank)).collect(); |
| 1092 | assert_eq!(positions, vec![1, 2, 3]); |
| 1093 | assert_eq!(model.own_cell_index(3), 1); |
| 1094 | let strip: Vec<&str> = model.strip_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 1095 | assert_eq!(strip, vec!["Cy", "Bo", "Ana"]); |
| 1096 | } |
| 1097 | |
| 1098 | #[test] |
| 1099 | fn a_roster_without_the_own_entry_keeps_the_own_hand() { |
| 1100 | let mut model = meeting_with_me(); |
| 1101 | model.apply(CallUpdate::OwnHand(Some(5))); |
| 1102 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false)])); |
| 1103 | assert_eq!(model.own_hand, Some(5)); |
| 1104 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:me", "Me", false, false)])); |
| 1105 | assert_eq!(model.own_hand, None); |
| 1106 | } |
| 1107 | |
| 1108 | #[test] |
| 1109 | fn reaction_controls_show_in_group_calls_and_meetings_only() { |
| 1110 | let mut model = CallModel::test(); |
| 1111 | for (kind, expected) in [(CallKind::Test, false), (CallKind::Direct, false), (CallKind::Group, true), (CallKind::Meeting, true)] { |
| 1112 | model.kind = kind; |
| 1113 | assert_eq!(model.has_reaction_controls(), expected); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | #[test] |
| 1118 | fn the_server_echo_of_an_own_reaction_does_not_restart_the_chip() { |
| 1119 | let mut model = meeting_with_me(); |
| 1120 | let start = Instant::now(); |
| 1121 | model.note_reaction("8:orgid:me".into(), Reaction::Like, start); |
| 1122 | let serial = model.reactions[0].serial; |
| 1123 | model.note_reaction("8:orgid:me".into(), Reaction::Like, start + Duration::from_millis(1500)); |
| 1124 | assert_eq!(model.reactions.len(), 1); |
| 1125 | assert_eq!(model.reactions[0].serial, serial); |
| 1126 | model.note_reaction("8:orgid:me".into(), Reaction::Heart, start + Duration::from_millis(1600)); |
| 1127 | assert_eq!(model.reactions[0].reaction, Reaction::Heart); |
| 1128 | model.note_reaction("8:orgid:me".into(), Reaction::Heart, start + Duration::from_secs(4)); |
| 1129 | assert_ne!(model.reactions[0].serial, serial); |
| 1130 | } |
| 1131 | |
| 1132 | #[test] |
| 1133 | fn another_persons_repeated_reaction_restarts_the_chip() { |
| 1134 | let mut model = meeting_with_me(); |
| 1135 | let start = Instant::now(); |
| 1136 | model.note_reaction("8:orgid:a".into(), Reaction::Like, start); |
| 1137 | let serial = model.reactions[0].serial; |
| 1138 | model.note_reaction("8:orgid:a".into(), Reaction::Like, start + Duration::from_secs(1)); |
| 1139 | assert_ne!(model.reactions[0].serial, serial); |
| 1140 | } |
| 1141 | |
| 1142 | #[test] |
| 1143 | fn lowering_clears_the_hand_everywhere() { |
| 1144 | let mut model = meeting_with_me(); |
| 1145 | model.apply(CallUpdate::Roster(vec![raised("8:orgid:a", "Ana", 1), raised("8:orgid:me", "Me", 2)])); |
| 1146 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false), entry("8:orgid:me", "Me", false, false)])); |
| 1147 | assert!(!model.any_hand_raised()); |
| 1148 | assert_eq!(model.own_cell_index(1), 1); |
| 1149 | } |
| 1150 | |
| 1151 | #[test] |
| 1152 | fn only_organizers_may_lower_hands() { |
| 1153 | assert!(!CallModel::meeting("Standup", false).can_lower_hands()); |
| 1154 | assert!(CallModel::meeting("Standup", true).can_lower_hands()); |
| 1155 | } |
| 1156 | |
| 1157 | #[test] |
| 1158 | fn a_reaction_shows_for_three_seconds_on_its_sender_and_the_latest_wins() { |
| 1159 | let mut model = meeting_with_me(); |
| 1160 | let start = Instant::now(); |
| 1161 | model.note_reaction("8:orgid:a".into(), Reaction::Heart, start); |
| 1162 | model.note_reaction("8:orgid:me".into(), Reaction::Like, start); |
| 1163 | let reaction_at = |model: &CallModel, mri: &str, seconds: u64| { |
| 1164 | model.chip_of(mri, start + Duration::from_secs(seconds)).map(|chip| chip.reaction) |
| 1165 | }; |
| 1166 | assert_eq!(reaction_at(&model, "8:orgid:a", 2), Some(Reaction::Heart)); |
| 1167 | assert_eq!(model.own_chip(start + Duration::from_secs(2)).map(|chip| chip.reaction), Some(Reaction::Like)); |
| 1168 | assert_eq!(reaction_at(&model, "8:orgid:a", 3), None); |
| 1169 | model.note_reaction("8:orgid:a".into(), Reaction::Laugh, start + Duration::from_secs(1)); |
| 1170 | assert_eq!(reaction_at(&model, "8:orgid:a", 2), Some(Reaction::Laugh)); |
| 1171 | assert_eq!(model.reactions.len(), 2); |
| 1172 | } |
| 1173 | |
| 1174 | #[test] |
| 1175 | fn the_chat_panel_picks_the_meeting_thread_or_the_call_chat() { |
| 1176 | assert_eq!(chat_thread(CallKind::Meeting, Some("19:meeting_x"), Some("19:list")), Some("19:meeting_x")); |
| 1177 | assert_eq!(chat_thread(CallKind::Meeting, None, Some("19:list")), Some("19:list")); |
| 1178 | assert_eq!(chat_thread(CallKind::Direct, Some("19:meeting_x"), Some("19:dm")), Some("19:dm")); |
| 1179 | assert_eq!(chat_thread(CallKind::Group, None, Some("19:group")), Some("19:group")); |
| 1180 | assert_eq!(chat_thread(CallKind::Test, None, None), None); |
| 1181 | } |
| 1182 | |
| 1183 | #[test] |
| 1184 | fn computer_sound_follows_the_share() { |
| 1185 | let mut model = connected_model(); |
| 1186 | model.apply(CallUpdate::LocalShare(Some("Screen".into()))); |
| 1187 | model.apply(CallUpdate::ShareSound(true)); |
| 1188 | assert!(model.share_sound); |
| 1189 | model.apply(CallUpdate::LocalShare(None)); |
| 1190 | assert!(!model.share_sound); |
| 1191 | } |
| 1192 | |
| 1193 | #[test] |
| 1194 | fn lobby_guests_are_listed_for_organizers_who_may_admit() { |
| 1195 | let mut model = meeting_with_me(); |
| 1196 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1197 | model.apply(CallUpdate::Roster(vec![ |
| 1198 | entry("8:orgid:a", "Ana", false, false), |
| 1199 | entry("8:orgid:g1", "Gast Eins", false, true), |
| 1200 | entry("8:orgid:g2", "Gast Zwei", false, true), |
| 1201 | ])); |
| 1202 | let waiting: Vec<&str> = model.lobby_guests().iter().map(|tile| tile.name.as_str()).collect(); |
| 1203 | assert_eq!(waiting, vec!["Gast Eins", "Gast Zwei"]); |
| 1204 | assert!(!model.can_admit()); |
| 1205 | model.apply(CallUpdate::Roster(vec![organizer_entry("8:orgid:me"), entry("8:orgid:g1", "Gast Eins", false, true)])); |
| 1206 | assert!(model.organizer && model.can_admit() && model.organizes_meeting()); |
| 1207 | let mut owner = CallModel::meeting("Standup", true); |
| 1208 | owner.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1209 | assert!(owner.can_admit()); |
| 1210 | owner.apply(CallUpdate::Lobby(true)); |
| 1211 | assert!(!owner.can_admit()); |
| 1212 | } |
| 1213 | |
| 1214 | #[test] |
| 1215 | fn a_spotlight_takes_the_stage_and_the_lowest_rank_wins() { |
| 1216 | let mut model = meeting_with_me(); |
| 1217 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false), spotlit("8:orgid:b", "Bo", 4), spotlit("8:orgid:c", "Cy", 2)])); |
| 1218 | assert_eq!(model.focus(), Some(Focus { mri: "8:orgid:c".into(), own: false, spotlight: true, pinned: false })); |
| 1219 | assert!(model.is_spotlighted("8:orgid:b") && !model.is_spotlighted("8:orgid:a")); |
| 1220 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false), spotlit("8:orgid:b", "Bo", 4), entry("8:orgid:c", "Cy", false, false)])); |
| 1221 | assert_eq!(model.focus().map(|focus| focus.mri), Some("8:orgid:b".to_owned())); |
| 1222 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false)])); |
| 1223 | assert_eq!(model.focus(), None); |
| 1224 | } |
| 1225 | |
| 1226 | #[test] |
| 1227 | fn spotlighting_yourself_puts_your_own_tile_on_the_stage() { |
| 1228 | let mut model = meeting_with_me(); |
| 1229 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false), spotlit("8:orgid:me", "Me", 1)])); |
| 1230 | assert_eq!(model.focus(), Some(Focus { mri: "8:orgid:me".into(), own: true, spotlight: true, pinned: false })); |
| 1231 | } |
| 1232 | |
| 1233 | #[test] |
| 1234 | fn a_pin_is_local_beats_the_spotlight_and_ends_when_the_person_leaves() { |
| 1235 | let mut model = meeting_with_me(); |
| 1236 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1237 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false), spotlit("8:orgid:b", "Bo", 1)])); |
| 1238 | model.toggle_pin("8:orgid:a"); |
| 1239 | assert_eq!(model.focus(), Some(Focus { mri: "8:orgid:a".into(), own: false, spotlight: false, pinned: true })); |
| 1240 | let names: Vec<&str> = model.strip_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 1241 | assert_eq!(names[0], "Ana"); |
| 1242 | model.toggle_pin("8:orgid:b"); |
| 1243 | assert_eq!(model.focus().map(|focus| (focus.mri, focus.spotlight, focus.pinned)), Some(("8:orgid:b".to_owned(), true, true))); |
| 1244 | model.toggle_pin("8:orgid:b"); |
| 1245 | assert_eq!(model.pinned, None); |
| 1246 | model.toggle_pin("8:orgid:a"); |
| 1247 | model.apply(CallUpdate::Roster(vec![spotlit("8:orgid:b", "Bo", 1)])); |
| 1248 | assert_eq!(model.pinned, None); |
| 1249 | } |
| 1250 | |
| 1251 | fn caption(id: &str, speaker: &str, text: &str, is_final: bool) -> CaptionEntry { |
| 1252 | CaptionEntry { id: id.into(), user_id: "u".into(), display_name: speaker.into(), text: text.into(), is_final } |
| 1253 | } |
| 1254 | |
| 1255 | #[test] |
| 1256 | fn captions_keep_two_lines_update_partials_and_fade_after_four_seconds() { |
| 1257 | let mut model = meeting_with_me(); |
| 1258 | let start = Instant::now(); |
| 1259 | model.note_caption(caption("1", "Ana", "ignored while off", false), start); |
| 1260 | assert!(model.caption_lines.is_empty()); |
| 1261 | model.apply(CallUpdate::Captions(CaptionState::On)); |
| 1262 | model.note_caption(caption("1", "Ana", "Hello", false), start); |
| 1263 | model.note_caption(caption("1", "Ana", "Hello there", true), start); |
| 1264 | assert_eq!(model.caption_lines.len(), 1); |
| 1265 | assert_eq!(model.caption_lines[0].text, "Hello there"); |
| 1266 | model.note_caption(caption("2", "", "Next", false), start + Duration::from_secs(1)); |
| 1267 | model.note_caption(caption("3", "Bo", "Third", false), start + Duration::from_secs(2)); |
| 1268 | let texts: Vec<&str> = model.caption_lines.iter().map(|line| line.text.as_str()).collect(); |
| 1269 | assert_eq!(texts, vec!["Next", "Third"]); |
| 1270 | assert_eq!(model.caption_lines[0].speaker, "Someone"); |
| 1271 | assert_eq!(model.visible_captions(start + Duration::from_secs(3)).len(), 2); |
| 1272 | assert_eq!(model.visible_captions(start + Duration::from_millis(5500)).len(), 1); |
| 1273 | assert!(model.visible_captions(start + Duration::from_secs(7)).is_empty()); |
| 1274 | model.apply(CallUpdate::Captions(CaptionState::Off)); |
| 1275 | assert!(model.caption_lines.is_empty() && !model.captions_on()); |
| 1276 | } |
| 1277 | |
| 1278 | fn one_to_one() -> CallModel { |
| 1279 | let mut model = CallModel::people("Bea", &[("8:orgid:bea".into(), "Bea".into())]); |
| 1280 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1281 | model |
| 1282 | } |
| 1283 | |
| 1284 | #[test] |
| 1285 | fn hold_and_transfer_belong_to_one_to_one_calls_and_follow_the_hold_state() { |
| 1286 | let mut model = one_to_one(); |
| 1287 | assert!(model.can_hold() && model.can_transfer()); |
| 1288 | model.apply(CallUpdate::Hold(HoldState::Local)); |
| 1289 | assert!(model.can_hold() && !model.can_transfer()); |
| 1290 | model.apply(CallUpdate::Hold(HoldState::Remote)); |
| 1291 | assert!(!model.can_hold() && !model.can_transfer()); |
| 1292 | let meeting = meeting_with_me(); |
| 1293 | assert!(!meeting.can_hold() && !meeting.can_transfer()); |
| 1294 | assert!(!CallModel::people("Group", &[("8:orgid:a".into(), "A".into()), ("8:orgid:b".into(), "B".into())]).can_hold()); |
| 1295 | } |
| 1296 | |
| 1297 | #[test] |
| 1298 | fn recording_and_consent_follow_the_call_updates_and_only_organizers_may_record() { |
| 1299 | let mut model = meeting_with_me(); |
| 1300 | model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() })); |
| 1301 | assert!(!model.can_record()); |
| 1302 | model.apply(CallUpdate::Roster(vec![organizer_entry("8:orgid:me")])); |
| 1303 | assert!(model.can_record()); |
| 1304 | model.apply(CallUpdate::Recording(true)); |
| 1305 | model.apply(CallUpdate::ConsentRequired(true)); |
| 1306 | assert!(model.recording && model.consent_required); |
| 1307 | model.apply(CallUpdate::ConsentRequired(false)); |
| 1308 | assert!(model.recording && !model.consent_required); |
| 1309 | model.apply(CallUpdate::Recording(false)); |
| 1310 | assert!(!model.recording); |
| 1311 | } |
| 1312 | |
| 1313 | #[test] |
| 1314 | fn a_whiteboard_share_names_the_presenter_and_offers_the_browser_only_with_a_link() { |
| 1315 | let mut model = meeting_with_me(); |
| 1316 | model.apply(CallUpdate::Roster(vec![entry("8:orgid:ana", "Ana", false, false)])); |
| 1317 | assert_eq!(model.whiteboard_label(), None); |
| 1318 | let share = |presenter: &str, url: Option<&str>| calling::ContentShare { |
| 1319 | session_id: "s".into(), |
| 1320 | presenter: Some(presenter.into()), |
| 1321 | subject: "Whiteboard".into(), |
| 1322 | url: url.map(str::to_owned), |
| 1323 | whiteboard: true, |
| 1324 | }; |
| 1325 | model.apply(CallUpdate::Whiteboard(Some(share("8:orgid:ana", Some("https://app.whiteboard.microsoft.com/x"))))); |
| 1326 | assert_eq!(model.whiteboard_label().as_deref(), Some("Ana is sharing a whiteboard")); |
| 1327 | assert_eq!(model.whiteboard_url(), Some("https://app.whiteboard.microsoft.com/x")); |
| 1328 | model.apply(CallUpdate::Whiteboard(Some(share("8:orgid:stranger", None)))); |
| 1329 | assert_eq!(model.whiteboard_label().as_deref(), Some("Someone is sharing a whiteboard")); |
| 1330 | assert_eq!(model.whiteboard_url(), None); |
| 1331 | model.apply(CallUpdate::Whiteboard(None)); |
| 1332 | assert_eq!(model.whiteboard_label(), None); |
| 1333 | } |
| 1334 | |
| 1335 | #[test] |
| 1336 | fn a_consultation_can_transfer_once_connected_with_a_replacement_link() { |
| 1337 | let (handle, _control) = calling::call_channel(); |
| 1338 | let mut consult = ConsultCall { |
| 1339 | id: 3, |
| 1340 | target: ("8:orgid:ana".into(), "Ana".into()), |
| 1341 | commands: handle.commands, |
| 1342 | state: CallState::Connecting, |
| 1343 | replacement: None, |
| 1344 | transferring: false, |
| 1345 | }; |
| 1346 | assert_eq!(consult.status_text(), "Calling Ana..."); |
| 1347 | assert!(!consult.ready_to_transfer()); |
| 1348 | consult.state = CallState::Connected { since: Instant::now() }; |
| 1349 | assert_eq!(consult.status_text(), "Consulting Ana"); |
| 1350 | assert!(!consult.ready_to_transfer()); |
| 1351 | consult.replacement = Some("https://cc/replacement".into()); |
| 1352 | assert!(consult.ready_to_transfer()); |
| 1353 | } |
| 1354 | } |