| 1 | use std::cell::Cell; |
| 2 | use std::collections::HashSet; |
| 3 | use std::rc::Rc; |
| 4 | use std::sync::Arc; |
| 5 | use std::time::{Duration, Instant}; |
| 6 | |
| 7 | use calling::{ |
| 8 | AttendeeLimits, CallCommand, CallState, CallUpdate, CameraDevice, CaptionEntry, CaptionState, ContentShare, |
| 9 | DeviceChoice, DeviceLists, EndKind, EndReason, HoldState, MeetingCapability, MeetingRole, |
| 10 | MeetingTarget, Progress, Reaction, Replacement, RosterEntry, ShareKind, ShareSource, SpeakingDetector, StatsSample, VideoHub, |
| 11 | VideoWant, |
| 12 | }; |
| 13 | use teams_core::RecordingPolicy; |
| 14 | use tokio::sync::mpsc::UnboundedSender; |
| 15 | |
| 16 | use super::background::BackgroundPick; |
| 17 | use super::health::{HealthBanner, HealthState}; |
| 18 | use super::bubbles::ChatBubble; |
| 19 | use super::fullscreen::FullscreenState; |
| 20 | use super::pictures::CallPictures; |
| 21 | use super::stage_layout::{FILMSTRIP_MAX, GALLERY_MAX, RECENT_SPEAKER, SPEAKER_HOLD, VisibleRotation, split_overflow}; |
| 22 | use super::transcript::{Transcript, TranscriptEntry}; |
| 23 | use super::view_settings::{CallLayout, ViewSettings}; |
| 24 | use super::options_dialog::OptionsDialog; |
| 25 | use super::push_to_talk::PushToTalk; |
| 26 | use super::report::SharedRecorder; |
| 27 | use super::share_view::ShareStage; |
| 28 | |
| 29 | pub const TEST_CALL_TITLE: &str = "Test call"; |
| 30 | pub const ECHO_NAME: &str = "Teams echo"; |
| 31 | pub const ECHO_MRI: &str = calling::signaling::ECHO_BOT_MRI; |
| 32 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 33 | const BOT_PREFIX: &str = "28:"; |
| 34 | const AVATAR_SHARE: f32 = 0.42; |
| 35 | const MIN_AVATAR: f32 = 32.; |
| 36 | const MAX_AVATAR: f32 = 96.; |
| 37 | pub const REACTION_SHOWN: Duration = Duration::from_secs(3); |
| 38 | const OWN_ECHO_WINDOW: Duration = Duration::from_secs(2); |
| 39 | pub const CAPTION_SHOWN: Duration = Duration::from_secs(4); |
| 40 | const CAPTION_LINES: usize = 2; |
| 41 | |
| 42 | pub struct ActiveCall { |
| 43 | pub id: u64, |
| 44 | pub model: CallModel, |
| 45 | pub commands: UnboundedSender<CallCommand>, |
| 46 | pub viewing: bool, |
| 47 | pub conversation_id: Option<String>, |
| 48 | pub video: Arc<VideoHub>, |
| 49 | pub pictures: CallPictures, |
| 50 | pub stage_fullscreen: bool, |
| 51 | pub fullscreen: Option<FullscreenState>, |
| 52 | pub stage_size: Rc<Cell<(f32, f32)>>, |
| 53 | pub sent_wants: Vec<VideoWant>, |
| 54 | pub share_stage: ShareStage, |
| 55 | pub bubbles: Vec<ChatBubble>, |
| 56 | pub share_menu_open: bool, |
| 57 | pub lobby_list_open: bool, |
| 58 | pub chat_open: bool, |
| 59 | pub transcript_open: bool, |
| 60 | pub people_open: bool, |
| 61 | pub people_scroll: gpui_kit::ScrollHandle, |
| 62 | pub options_dialog: Option<OptionsDialog>, |
| 63 | pub meeting_target: Option<MeetingTarget>, |
| 64 | pub pending_move: Option<PendingMove>, |
| 65 | pub consult: Option<ConsultCall>, |
| 66 | pub push_to_talk: PushToTalk, |
| 67 | pub report: Option<SharedRecorder>, |
| 68 | pub lobby_tone_at: Option<Instant>, |
| 69 | } |
| 70 | |
| 71 | #[derive(Debug, Clone, PartialEq)] |
| 72 | pub struct PendingMove { |
| 73 | pub target: MeetingTarget, |
| 74 | pub title: String, |
| 75 | pub breakout: Option<BreakoutState>, |
| 76 | } |
| 77 | |
| 78 | pub struct ConsultCall { |
| 79 | pub target: (String, String), |
| 80 | pub held_name: String, |
| 81 | pub replacement: Option<Replacement>, |
| 82 | pub transfer: ConsultTransfer, |
| 83 | } |
| 84 | |
| 85 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 86 | pub enum ConsultTransfer { |
| 87 | #[default] |
| 88 | Idle, |
| 89 | Pending, |
| 90 | Done, |
| 91 | } |
| 92 | |
| 93 | impl ConsultCall { |
| 94 | pub fn new(target: (String, String), held_name: String) -> Self { |
| 95 | ConsultCall { |
| 96 | target, |
| 97 | held_name, |
| 98 | replacement: None, |
| 99 | transfer: ConsultTransfer::Idle, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | pub fn ready_to_transfer(&self, model: &CallModel) -> bool { |
| 104 | model.is_live() && self.replacement.is_some() && self.transfer == ConsultTransfer::Idle |
| 105 | } |
| 106 | |
| 107 | pub fn banner_text(&self) -> String { |
| 108 | match self.transfer { |
| 109 | ConsultTransfer::Idle => format!("{} is on hold", self.held_name), |
| 110 | ConsultTransfer::Pending | ConsultTransfer::Done => { |
| 111 | format!("Transferring {} to {}...", self.held_name, self.target.1) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl ActiveCall { |
| 118 | pub fn new( |
| 119 | id: u64, |
| 120 | model: CallModel, |
| 121 | commands: UnboundedSender<CallCommand>, |
| 122 | conversation_id: Option<String>, |
| 123 | video: Arc<VideoHub>, |
| 124 | ) -> Self { |
| 125 | ActiveCall { |
| 126 | id, |
| 127 | model, |
| 128 | commands, |
| 129 | viewing: true, |
| 130 | conversation_id, |
| 131 | video, |
| 132 | pictures: CallPictures::default(), |
| 133 | stage_fullscreen: false, |
| 134 | fullscreen: None, |
| 135 | stage_size: Rc::default(), |
| 136 | sent_wants: Vec::new(), |
| 137 | share_stage: ShareStage::default(), |
| 138 | lobby_tone_at: None, |
| 139 | bubbles: Vec::new(), |
| 140 | share_menu_open: false, |
| 141 | lobby_list_open: false, |
| 142 | chat_open: false, |
| 143 | transcript_open: false, |
| 144 | people_open: false, |
| 145 | people_scroll: gpui_kit::ScrollHandle::new(), |
| 146 | options_dialog: None, |
| 147 | meeting_target: None, |
| 148 | pending_move: None, |
| 149 | consult: None, |
| 150 | push_to_talk: PushToTalk::default(), |
| 151 | report: None, |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | pub fn chat_thread(&self) -> Option<&str> { |
| 156 | chat_thread( |
| 157 | self.model.kind, |
| 158 | self.model.meeting_chat.as_deref(), |
| 159 | self.conversation_id.as_deref(), |
| 160 | ) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | pub fn chat_thread<'a>( |
| 165 | kind: CallKind, |
| 166 | meeting_chat: Option<&'a str>, |
| 167 | conversation_id: Option<&'a str>, |
| 168 | ) -> Option<&'a str> { |
| 169 | match kind { |
| 170 | CallKind::Meeting => meeting_chat.or(conversation_id), |
| 171 | CallKind::Test | CallKind::Direct | CallKind::Group => conversation_id, |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 176 | pub enum CallKind { |
| 177 | Test, |
| 178 | Direct, |
| 179 | Group, |
| 180 | Meeting, |
| 181 | } |
| 182 | |
| 183 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 184 | pub enum TileState { |
| 185 | Invited, |
| 186 | Present, |
| 187 | InLobby, |
| 188 | Left, |
| 189 | } |
| 190 | |
| 191 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 192 | pub struct Tile { |
| 193 | pub mri: String, |
| 194 | pub user_id: Option<String>, |
| 195 | pub name: String, |
| 196 | pub muted: bool, |
| 197 | pub state: TileState, |
| 198 | pub invited: bool, |
| 199 | pub has_video: bool, |
| 200 | pub hand: Option<u64>, |
| 201 | pub spotlight: Option<u64>, |
| 202 | pub role: Option<MeetingRole>, |
| 203 | pub limits: AttendeeLimits, |
| 204 | } |
| 205 | |
| 206 | impl Tile { |
| 207 | pub fn invited(mri: &str, name: &str) -> Tile { |
| 208 | Tile { |
| 209 | mri: mri.to_owned(), |
| 210 | user_id: mri.strip_prefix(ORGID_PREFIX).map(str::to_owned), |
| 211 | name: name.to_owned(), |
| 212 | muted: false, |
| 213 | state: TileState::Invited, |
| 214 | invited: true, |
| 215 | has_video: false, |
| 216 | hand: None, |
| 217 | spotlight: None, |
| 218 | role: None, |
| 219 | limits: AttendeeLimits::default(), |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #[derive(Debug, Clone, PartialEq)] |
| 225 | pub struct BreakoutState { |
| 226 | pub room_name: String, |
| 227 | pub main: Option<MeetingTarget>, |
| 228 | pub main_title: String, |
| 229 | } |
| 230 | |
| 231 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 232 | pub struct Focus { |
| 233 | pub mri: String, |
| 234 | pub own: bool, |
| 235 | pub spotlight: bool, |
| 236 | pub pinned: bool, |
| 237 | } |
| 238 | |
| 239 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 240 | pub struct CaptionLine { |
| 241 | pub id: String, |
| 242 | pub speaker: String, |
| 243 | pub text: String, |
| 244 | pub at: Instant, |
| 245 | } |
| 246 | |
| 247 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 248 | pub struct TileSize { |
| 249 | pub width: f32, |
| 250 | pub height: f32, |
| 251 | pub avatar: f32, |
| 252 | } |
| 253 | |
| 254 | impl TileSize { |
| 255 | pub fn sixteen_by_nine(width: f32) -> TileSize { |
| 256 | let height = width * 9. / 16.; |
| 257 | TileSize { |
| 258 | width, |
| 259 | height, |
| 260 | avatar: (height * AVATAR_SHARE).clamp(MIN_AVATAR, MAX_AVATAR), |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 266 | pub struct ReactionChip { |
| 267 | pub mri: String, |
| 268 | pub reaction: Reaction, |
| 269 | pub since: Instant, |
| 270 | pub serial: u64, |
| 271 | } |
| 272 | |
| 273 | #[derive(Debug, Clone)] |
| 274 | pub struct CallModel { |
| 275 | pub title: String, |
| 276 | pub kind: CallKind, |
| 277 | pub peer_name: String, |
| 278 | pub state: CallState, |
| 279 | pub progress: Option<Progress>, |
| 280 | pub muted: bool, |
| 281 | pub listen_only: bool, |
| 282 | pub audio_off: bool, |
| 283 | pub lobby: bool, |
| 284 | pub can_end_meeting: bool, |
| 285 | pub incoming: bool, |
| 286 | pub input: DeviceChoice, |
| 287 | pub output: DeviceChoice, |
| 288 | pub devices: DeviceLists, |
| 289 | pub local_speaking: bool, |
| 290 | pub remote_speaking: bool, |
| 291 | pub speaking_mris: Vec<String>, |
| 292 | pub connected_since: Option<Instant>, |
| 293 | pub own_mri: Option<String>, |
| 294 | pub tiles: Vec<Tile>, |
| 295 | pub screen_sharer: Option<String>, |
| 296 | pub camera_on: bool, |
| 297 | pub cameras: Vec<CameraDevice>, |
| 298 | pub camera: DeviceChoice, |
| 299 | pub local_share: Option<String>, |
| 300 | pub share_sources: Vec<ShareSource>, |
| 301 | pub last_sample: Option<StatsSample>, |
| 302 | pub share_sound: bool, |
| 303 | pub own_hand: Option<u64>, |
| 304 | pub own_spotlight: Option<u64>, |
| 305 | pub organizer: bool, |
| 306 | pub own_role: Option<MeetingRole>, |
| 307 | pub capability: MeetingCapability, |
| 308 | pub pinned: Option<String>, |
| 309 | pub fitted: HashSet<String>, |
| 310 | pub captions: CaptionState, |
| 311 | pub caption_lines: Vec<CaptionLine>, |
| 312 | pub transcript: Transcript, |
| 313 | pub caption_language: Option<String>, |
| 314 | language_pending: bool, |
| 315 | pub background: BackgroundPick, |
| 316 | pub recording: bool, |
| 317 | pub recording_audio_only: bool, |
| 318 | pub transcribing: bool, |
| 319 | pub record_pending: bool, |
| 320 | pub recording_policy: RecordingPolicy, |
| 321 | pub meeting_tenant: Option<String>, |
| 322 | pub own_tenant: Option<String>, |
| 323 | pub consent_required: bool, |
| 324 | pub hold: HoldState, |
| 325 | pub health: HealthState, |
| 326 | pub whiteboard: Option<ContentShare>, |
| 327 | pub breakout: Option<BreakoutState>, |
| 328 | pub main_meeting: Option<MeetingTarget>, |
| 329 | pub reactions: Vec<ReactionChip>, |
| 330 | reaction_serial: u64, |
| 331 | pub meeting_chat: Option<String>, |
| 332 | pub view: ViewSettings, |
| 333 | rotation: VisibleRotation, |
| 334 | stage_speaker: Option<String>, |
| 335 | recent_speakers: Vec<String>, |
| 336 | local_detector: SpeakingDetector, |
| 337 | remote_detector: SpeakingDetector, |
| 338 | } |
| 339 | |
| 340 | impl CallModel { |
| 341 | fn base(kind: CallKind, title: &str, peer_name: &str, tiles: Vec<Tile>) -> Self { |
| 342 | CallModel { |
| 343 | title: title.to_owned(), |
| 344 | kind, |
| 345 | peer_name: peer_name.to_owned(), |
| 346 | state: CallState::Connecting, |
| 347 | progress: None, |
| 348 | muted: false, |
| 349 | listen_only: false, |
| 350 | audio_off: false, |
| 351 | lobby: false, |
| 352 | can_end_meeting: false, |
| 353 | incoming: false, |
| 354 | input: DeviceChoice::SystemDefault, |
| 355 | output: DeviceChoice::SystemDefault, |
| 356 | devices: DeviceLists::default(), |
| 357 | local_speaking: false, |
| 358 | remote_speaking: false, |
| 359 | speaking_mris: Vec::new(), |
| 360 | connected_since: None, |
| 361 | own_mri: None, |
| 362 | tiles, |
| 363 | screen_sharer: None, |
| 364 | camera_on: false, |
| 365 | cameras: Vec::new(), |
| 366 | camera: DeviceChoice::SystemDefault, |
| 367 | local_share: None, |
| 368 | share_sources: Vec::new(), |
| 369 | last_sample: None, |
| 370 | share_sound: false, |
| 371 | own_hand: None, |
| 372 | own_spotlight: None, |
| 373 | organizer: false, |
| 374 | own_role: None, |
| 375 | capability: MeetingCapability::default(), |
| 376 | pinned: None, |
| 377 | fitted: HashSet::new(), |
| 378 | captions: CaptionState::Off, |
| 379 | caption_lines: Vec::new(), |
| 380 | transcript: Transcript::default(), |
| 381 | caption_language: None, |
| 382 | language_pending: false, |
| 383 | background: BackgroundPick::None, |
| 384 | recording: false, |
| 385 | recording_audio_only: false, |
| 386 | transcribing: false, |
| 387 | record_pending: false, |
| 388 | recording_policy: RecordingPolicy::default(), |
| 389 | meeting_tenant: None, |
| 390 | own_tenant: None, |
| 391 | consent_required: false, |
| 392 | hold: HoldState::Active, |
| 393 | health: HealthState::default(), |
| 394 | whiteboard: None, |
| 395 | breakout: None, |
| 396 | main_meeting: None, |
| 397 | reactions: Vec::new(), |
| 398 | reaction_serial: 0, |
| 399 | meeting_chat: None, |
| 400 | view: ViewSettings::default(), |
| 401 | rotation: VisibleRotation::default(), |
| 402 | stage_speaker: None, |
| 403 | recent_speakers: Vec::new(), |
| 404 | local_detector: SpeakingDetector::default(), |
| 405 | remote_detector: SpeakingDetector::default(), |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | pub fn test() -> Self { |
| 410 | Self::base( |
| 411 | CallKind::Test, |
| 412 | TEST_CALL_TITLE, |
| 413 | ECHO_NAME, |
| 414 | vec![Tile::invited(ECHO_MRI, ECHO_NAME)], |
| 415 | ) |
| 416 | } |
| 417 | |
| 418 | pub fn people(title: &str, callees: &[(String, String)]) -> Self { |
| 419 | let kind = if callees.len() == 1 { |
| 420 | CallKind::Direct |
| 421 | } else { |
| 422 | CallKind::Group |
| 423 | }; |
| 424 | let peer_name = match callees { |
| 425 | [(_, name)] => name.clone(), |
| 426 | _ => title.to_owned(), |
| 427 | }; |
| 428 | let tiles = callees |
| 429 | .iter() |
| 430 | .map(|(mri, name)| Tile::invited(mri, name)) |
| 431 | .collect(); |
| 432 | Self::base(kind, title, &peer_name, tiles) |
| 433 | } |
| 434 | |
| 435 | pub fn meeting(title: &str, can_end_meeting: bool) -> Self { |
| 436 | let mut model = Self::base(CallKind::Meeting, title, title, Vec::new()); |
| 437 | model.can_end_meeting = can_end_meeting; |
| 438 | model |
| 439 | } |
| 440 | |
| 441 | pub fn incoming(title: &str, caller_mri: &str, caller_name: &str) -> Self { |
| 442 | let mut model = Self::base( |
| 443 | CallKind::Direct, |
| 444 | title, |
| 445 | caller_name, |
| 446 | vec![Tile { |
| 447 | state: TileState::Present, |
| 448 | ..Tile::invited(caller_mri, caller_name) |
| 449 | }], |
| 450 | ); |
| 451 | model.incoming = true; |
| 452 | model |
| 453 | } |
| 454 | |
| 455 | pub fn apply(&mut self, update: CallUpdate) { |
| 456 | match update { |
| 457 | CallUpdate::State(state) => { |
| 458 | if let Some(since) = state.started_at() { |
| 459 | self.connected_since = Some(since); |
| 460 | } |
| 461 | if !matches!(state, CallState::Connected { .. }) { |
| 462 | self.silence(); |
| 463 | } |
| 464 | self.state = state; |
| 465 | } |
| 466 | CallUpdate::Muted(muted) => { |
| 467 | self.muted = muted; |
| 468 | if muted { |
| 469 | self.local_speaking = false; |
| 470 | self.local_detector.reset(); |
| 471 | } |
| 472 | } |
| 473 | CallUpdate::Levels { local, remote } => { |
| 474 | self.local_speaking = !self.muted && self.local_detector.update(local); |
| 475 | self.remote_speaking = self.remote_detector.update(remote); |
| 476 | } |
| 477 | CallUpdate::Devices(devices) => self.devices = devices, |
| 478 | CallUpdate::Selected { input, output } => { |
| 479 | self.input = input; |
| 480 | self.output = output; |
| 481 | } |
| 482 | CallUpdate::ListenOnly(listen_only) => self.listen_only = listen_only, |
| 483 | CallUpdate::Stats { .. } => {} |
| 484 | CallUpdate::Progress(progress) => self.progress = Some(progress), |
| 485 | CallUpdate::Roster(entries) => { |
| 486 | self.apply_roster(&entries); |
| 487 | self.refresh_layout(Instant::now()); |
| 488 | } |
| 489 | CallUpdate::Speakers(mris) => { |
| 490 | self.speaking_mris = mris; |
| 491 | self.refresh_layout(Instant::now()); |
| 492 | } |
| 493 | CallUpdate::Lobby(lobby) => self.lobby = lobby, |
| 494 | CallUpdate::OwnIdentity { mri } => self.own_mri = Some(mri), |
| 495 | CallUpdate::ScreenShare(sharer) => self.screen_sharer = sharer, |
| 496 | CallUpdate::VideoReady | CallUpdate::Notice(_) => {} |
| 497 | CallUpdate::Camera(on) => self.camera_on = on, |
| 498 | CallUpdate::Cameras(cameras) => self.cameras = cameras, |
| 499 | CallUpdate::LocalShare(label) => { |
| 500 | if label.is_none() { |
| 501 | self.share_sound = false; |
| 502 | } |
| 503 | self.local_share = label; |
| 504 | } |
| 505 | CallUpdate::ShareSources(sources) => self.share_sources = sources, |
| 506 | CallUpdate::ShareSound(active) => self.share_sound = active, |
| 507 | CallUpdate::OwnHand(rank) => self.own_hand = rank, |
| 508 | CallUpdate::Reaction { mri, reaction } => { |
| 509 | self.note_reaction(mri, reaction, Instant::now()) |
| 510 | } |
| 511 | CallUpdate::MeetingChat(thread) => self.meeting_chat = Some(thread), |
| 512 | CallUpdate::Captions(state) => { |
| 513 | if state != CaptionState::On { |
| 514 | self.caption_lines.clear(); |
| 515 | } |
| 516 | self.captions = state; |
| 517 | } |
| 518 | CallUpdate::Caption(entry) => self.note_caption(entry, Instant::now()), |
| 519 | CallUpdate::Recording(on) => self.recording = on, |
| 520 | CallUpdate::RecordingAudioOnly(audio_only) => self.recording_audio_only = audio_only, |
| 521 | CallUpdate::Transcribing(on) => self.transcribing = on, |
| 522 | CallUpdate::RecordingPending(pending) => self.record_pending = pending, |
| 523 | CallUpdate::MeetingTenant(tenant) => self.meeting_tenant = Some(tenant), |
| 524 | CallUpdate::ConsentRequired(required) => self.consent_required = required, |
| 525 | CallUpdate::Hold(hold) => self.hold = hold, |
| 526 | CallUpdate::Health(health) => self.health.apply(health), |
| 527 | CallUpdate::IncomingVideo(enabled) => self.health.incoming_video = enabled, |
| 528 | CallUpdate::Whiteboard(share) => self.whiteboard = share, |
| 529 | CallUpdate::Capability(capability) => self.capability = capability, |
| 530 | CallUpdate::BreakoutRoom { main } => self.main_meeting = Some(main), |
| 531 | CallUpdate::Sample(sample) => self.last_sample = Some(sample), |
| 532 | CallUpdate::BlurTiming(_) |
| 533 | | CallUpdate::Diagnostics(_) |
| 534 | | CallUpdate::Organizer { .. } |
| 535 | | CallUpdate::MeetingOptions(_) |
| 536 | | CallUpdate::MeetingOptionsSaved(_) |
| 537 | | CallUpdate::WhiteboardUrl(_) |
| 538 | | CallUpdate::BreakoutMove(_) |
| 539 | | CallUpdate::ReplacementLink(_) |
| 540 | | CallUpdate::TransferResult { .. } => {} |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | pub fn note_caption(&mut self, entry: CaptionEntry, at: Instant) { |
| 545 | if self.captions == CaptionState::Off { |
| 546 | return; |
| 547 | } |
| 548 | let speaker = if entry.display_name.is_empty() { |
| 549 | "Someone".to_owned() |
| 550 | } else { |
| 551 | entry.display_name |
| 552 | }; |
| 553 | let offset = self |
| 554 | .connected_since |
| 555 | .map_or(Duration::ZERO, |since| at.saturating_duration_since(since)); |
| 556 | self.transcript.note(TranscriptEntry { |
| 557 | id: entry.id.clone(), |
| 558 | speaker: speaker.clone(), |
| 559 | text: entry.text.clone(), |
| 560 | offset, |
| 561 | }); |
| 562 | let line = CaptionLine { |
| 563 | id: entry.id, |
| 564 | speaker, |
| 565 | text: entry.text, |
| 566 | at, |
| 567 | }; |
| 568 | match self.caption_lines.last_mut() { |
| 569 | Some(last) if last.id == line.id => *last = line, |
| 570 | _ => self.caption_lines.push(line), |
| 571 | } |
| 572 | let surplus = self.caption_lines.len().saturating_sub(CAPTION_LINES); |
| 573 | self.caption_lines.drain(..surplus); |
| 574 | } |
| 575 | |
| 576 | pub fn visible_captions(&self, now: Instant) -> Vec<&CaptionLine> { |
| 577 | self.caption_lines |
| 578 | .iter() |
| 579 | .filter(|line| now.saturating_duration_since(line.at) < CAPTION_SHOWN) |
| 580 | .collect() |
| 581 | } |
| 582 | |
| 583 | pub fn choose_caption_language(&mut self, tag: &str) { |
| 584 | self.caption_language = Some(tag.to_owned()); |
| 585 | self.language_pending = true; |
| 586 | } |
| 587 | |
| 588 | pub fn take_ready_caption_language(&mut self) -> Option<String> { |
| 589 | if self.language_pending && self.captions == CaptionState::On { |
| 590 | self.language_pending = false; |
| 591 | return self.caption_language.clone(); |
| 592 | } |
| 593 | None |
| 594 | } |
| 595 | |
| 596 | pub fn captions_on(&self) -> bool { |
| 597 | matches!(self.captions, CaptionState::Starting | CaptionState::On) |
| 598 | } |
| 599 | |
| 600 | fn silence(&mut self) { |
| 601 | self.local_speaking = false; |
| 602 | self.remote_speaking = false; |
| 603 | self.speaking_mris.clear(); |
| 604 | self.local_detector.reset(); |
| 605 | self.remote_detector.reset(); |
| 606 | } |
| 607 | |
| 608 | pub fn note_reaction(&mut self, mri: String, reaction: Reaction, at: Instant) { |
| 609 | let own_echo = Some(&mri) == self.own_mri.as_ref() |
| 610 | && self.reactions.iter().any(|chip| { |
| 611 | chip.mri == mri |
| 612 | && chip.reaction == reaction |
| 613 | && at.saturating_duration_since(chip.since) < OWN_ECHO_WINDOW |
| 614 | }); |
| 615 | if own_echo { |
| 616 | return; |
| 617 | } |
| 618 | self.reactions.retain(|chip| { |
| 619 | chip.mri != mri && at.saturating_duration_since(chip.since) < REACTION_SHOWN |
| 620 | }); |
| 621 | self.reaction_serial += 1; |
| 622 | self.reactions.push(ReactionChip { |
| 623 | mri, |
| 624 | reaction, |
| 625 | since: at, |
| 626 | serial: self.reaction_serial, |
| 627 | }); |
| 628 | } |
| 629 | |
| 630 | pub fn chip_of(&self, mri: &str, now: Instant) -> Option<&ReactionChip> { |
| 631 | self.reactions.iter().find(|chip| { |
| 632 | chip.mri == mri && now.saturating_duration_since(chip.since) < REACTION_SHOWN |
| 633 | }) |
| 634 | } |
| 635 | |
| 636 | pub fn own_chip(&self, now: Instant) -> Option<&ReactionChip> { |
| 637 | self.chip_of(self.own_mri.as_deref()?, now) |
| 638 | } |
| 639 | |
| 640 | pub fn hand_position(&self, rank: u64) -> usize { |
| 641 | let raised = self |
| 642 | .tiles |
| 643 | .iter() |
| 644 | .filter_map(|tile| tile.hand) |
| 645 | .chain(self.own_hand); |
| 646 | 1 + raised.filter(|other| *other < rank).count() |
| 647 | } |
| 648 | |
| 649 | pub fn can_manage(&self) -> bool { |
| 650 | self.can_end_meeting || self.organizer |
| 651 | } |
| 652 | |
| 653 | pub fn organizes_meeting(&self) -> bool { |
| 654 | self.kind == CallKind::Meeting && self.can_manage() |
| 655 | } |
| 656 | |
| 657 | pub fn present_others(&self) -> usize { |
| 658 | self.tiles |
| 659 | .iter() |
| 660 | .filter(|tile| tile.state == TileState::Present) |
| 661 | .count() |
| 662 | } |
| 663 | |
| 664 | pub fn can_invite(&self) -> bool { |
| 665 | let open_to_everyone = matches!(self.kind, CallKind::Direct | CallKind::Group); |
| 666 | self.is_live() && !self.lobby && (open_to_everyone || self.organizes_meeting()) |
| 667 | } |
| 668 | |
| 669 | pub fn tile_role(&self, mri: &str) -> Option<MeetingRole> { |
| 670 | self.tiles |
| 671 | .iter() |
| 672 | .find(|tile| tile.mri == mri) |
| 673 | .and_then(|tile| tile.role) |
| 674 | } |
| 675 | |
| 676 | pub fn can_change_roles(&self, mri: &str) -> bool { |
| 677 | self.organizes_meeting() && self.tile_role(mri) != Some(MeetingRole::Organizer) |
| 678 | } |
| 679 | |
| 680 | pub fn attendee_allowed(&self, mri: &str, modality: calling::Modality) -> bool { |
| 681 | let limits = self |
| 682 | .tiles |
| 683 | .iter() |
| 684 | .find(|tile| tile.mri == mri) |
| 685 | .map(|tile| tile.limits.clone()) |
| 686 | .unwrap_or_default(); |
| 687 | calling::modality_allowed(modality, self.capability, &limits) |
| 688 | } |
| 689 | |
| 690 | pub fn can_lower_hands(&self) -> bool { |
| 691 | self.can_manage() |
| 692 | } |
| 693 | |
| 694 | pub fn lobby_guests(&self) -> Vec<&Tile> { |
| 695 | self.tiles |
| 696 | .iter() |
| 697 | .filter(|tile| tile.state == TileState::InLobby) |
| 698 | .collect() |
| 699 | } |
| 700 | |
| 701 | pub fn lobby_mris(&self) -> Vec<String> { |
| 702 | self.lobby_guests().into_iter().map(|tile| tile.mri.clone()).collect() |
| 703 | } |
| 704 | |
| 705 | pub fn someone_entered_the_lobby(&self, before: &[String]) -> bool { |
| 706 | self.can_admit() && self.lobby_guests().iter().any(|tile| !before.contains(&tile.mri)) |
| 707 | } |
| 708 | |
| 709 | pub fn can_admit(&self) -> bool { |
| 710 | self.can_manage() && self.is_live() && !self.lobby |
| 711 | } |
| 712 | |
| 713 | pub fn is_pinned(&self, mri: &str) -> bool { |
| 714 | self.pinned.as_deref() == Some(mri) |
| 715 | } |
| 716 | |
| 717 | pub fn toggle_pin(&mut self, mri: &str) { |
| 718 | self.pinned = if self.is_pinned(mri) { |
| 719 | None |
| 720 | } else { |
| 721 | Some(mri.to_owned()) |
| 722 | }; |
| 723 | self.refresh_layout(Instant::now()); |
| 724 | } |
| 725 | |
| 726 | pub fn is_fitted(&self, mri: &str) -> bool { |
| 727 | self.fitted.contains(mri) |
| 728 | } |
| 729 | |
| 730 | pub fn toggle_fit(&mut self, mri: &str) { |
| 731 | if !self.fitted.remove(mri) { |
| 732 | self.fitted.insert(mri.to_owned()); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | pub fn is_spotlighted(&self, mri: &str) -> bool { |
| 737 | self.own_mri.as_deref() == Some(mri) && self.own_spotlight.is_some() |
| 738 | || self |
| 739 | .tiles |
| 740 | .iter() |
| 741 | .any(|tile| tile.mri == mri && tile.spotlight.is_some()) |
| 742 | } |
| 743 | |
| 744 | pub fn focus(&self) -> Option<Focus> { |
| 745 | let own = |mri: &str| self.own_mri.as_deref() == Some(mri); |
| 746 | let on_stage = |mri: &str| { |
| 747 | own(mri) |
| 748 | || self |
| 749 | .visible_tiles() |
| 750 | .iter() |
| 751 | .any(|tile| tile.mri == mri && tile.state == TileState::Present) |
| 752 | }; |
| 753 | if let Some(pinned) = self.pinned.as_deref().filter(|mri| on_stage(mri)) { |
| 754 | return Some(Focus { |
| 755 | mri: pinned.to_owned(), |
| 756 | own: own(pinned), |
| 757 | spotlight: self.is_spotlighted(pinned), |
| 758 | pinned: true, |
| 759 | }); |
| 760 | } |
| 761 | let remote = self |
| 762 | .tiles |
| 763 | .iter() |
| 764 | .filter(|tile| tile.state == TileState::Present) |
| 765 | .filter_map(|tile| Some((tile.spotlight?, tile.mri.as_str()))); |
| 766 | let (_, mri) = remote |
| 767 | .chain(self.own_spotlight.zip(self.own_mri.as_deref())) |
| 768 | .min_by_key(|(rank, _)| *rank)?; |
| 769 | Some(Focus { |
| 770 | mri: mri.to_owned(), |
| 771 | own: own(mri), |
| 772 | spotlight: true, |
| 773 | pinned: false, |
| 774 | }) |
| 775 | } |
| 776 | |
| 777 | pub fn raised_hands(&self) -> usize { |
| 778 | self.tiles.iter().filter(|tile| tile.hand.is_some()).count() |
| 779 | + usize::from(self.own_hand.is_some()) |
| 780 | } |
| 781 | |
| 782 | pub fn any_hand_raised(&self) -> bool { |
| 783 | self.own_hand.is_some() || self.tiles.iter().any(|tile| tile.hand.is_some()) |
| 784 | } |
| 785 | |
| 786 | pub fn own_cell_index(&self, remote_cells: usize) -> usize { |
| 787 | let Some(own) = self.own_hand else { |
| 788 | return remote_cells; |
| 789 | }; |
| 790 | let ahead = self |
| 791 | .visible_tiles() |
| 792 | .iter() |
| 793 | .filter(|tile| tile.hand.is_some_and(|rank| rank < own)) |
| 794 | .count(); |
| 795 | ahead.min(remote_cells) |
| 796 | } |
| 797 | |
| 798 | fn apply_roster(&mut self, entries: &[RosterEntry]) { |
| 799 | let own_entry = entries |
| 800 | .iter() |
| 801 | .find(|entry| Some(&entry.mri) == self.own_mri.as_ref()); |
| 802 | if let Some(entry) = own_entry { |
| 803 | self.own_hand = entry.hand.as_ref().map(|hand| hand.rank); |
| 804 | } |
| 805 | self.own_spotlight = |
| 806 | own_entry.and_then(|entry| entry.spotlight.as_ref().map(|spotlight| spotlight.rank)); |
| 807 | if let Some(entry) = own_entry { |
| 808 | self.organizer = entry.organizer; |
| 809 | self.own_role = entry.role; |
| 810 | } |
| 811 | for entry in entries |
| 812 | .iter() |
| 813 | .filter(|entry| Some(&entry.mri) != self.own_mri.as_ref()) |
| 814 | { |
| 815 | let state = if entry.in_lobby { |
| 816 | TileState::InLobby |
| 817 | } else { |
| 818 | TileState::Present |
| 819 | }; |
| 820 | match self.tiles.iter_mut().find(|tile| tile.mri == entry.mri) { |
| 821 | Some(tile) => { |
| 822 | tile.state = state; |
| 823 | tile.muted = entry.muted; |
| 824 | tile.has_video = entry.has_video; |
| 825 | tile.hand = entry.hand.as_ref().map(|hand| hand.rank); |
| 826 | tile.spotlight = entry.spotlight.as_ref().map(|spotlight| spotlight.rank); |
| 827 | tile.role = entry.role; |
| 828 | tile.limits = entry.limits.clone(); |
| 829 | if tile.name.is_empty() { |
| 830 | tile.name = entry.display_name.clone(); |
| 831 | } |
| 832 | } |
| 833 | None if entry.mri.starts_with(BOT_PREFIX) => {} |
| 834 | None => self.tiles.push(Tile { |
| 835 | mri: entry.mri.clone(), |
| 836 | user_id: entry.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned), |
| 837 | name: entry.display_name.clone(), |
| 838 | muted: entry.muted, |
| 839 | state, |
| 840 | invited: false, |
| 841 | has_video: entry.has_video, |
| 842 | hand: entry.hand.as_ref().map(|hand| hand.rank), |
| 843 | spotlight: entry.spotlight.as_ref().map(|spotlight| spotlight.rank), |
| 844 | role: entry.role, |
| 845 | limits: entry.limits.clone(), |
| 846 | }), |
| 847 | } |
| 848 | } |
| 849 | if self.kind == CallKind::Direct && self.present_others() > 1 { |
| 850 | self.kind = CallKind::Group; |
| 851 | } |
| 852 | let own_mri = self.own_mri.clone(); |
| 853 | let listed = |mri: &str| entries.iter().any(|entry| entry.mri == mri); |
| 854 | if self |
| 855 | .pinned |
| 856 | .as_deref() |
| 857 | .is_some_and(|mri| !listed(mri) && Some(mri) != own_mri.as_deref()) |
| 858 | { |
| 859 | self.pinned = None; |
| 860 | } |
| 861 | self.tiles.retain_mut(|tile| { |
| 862 | if listed(&tile.mri) || Some(&tile.mri) == own_mri.as_ref() { |
| 863 | return true; |
| 864 | } |
| 865 | match (tile.invited, tile.state) { |
| 866 | (true, TileState::Invited) => true, |
| 867 | (true, _) => { |
| 868 | tile.state = TileState::Left; |
| 869 | true |
| 870 | } |
| 871 | (false, _) => false, |
| 872 | } |
| 873 | }); |
| 874 | } |
| 875 | |
| 876 | pub fn visible_tiles(&self) -> Vec<&Tile> { |
| 877 | let mut tiles: Vec<&Tile> = self |
| 878 | .tiles |
| 879 | .iter() |
| 880 | .filter(|tile| tile.state != TileState::InLobby) |
| 881 | .collect(); |
| 882 | tiles.sort_by_key(|tile| { |
| 883 | let state = match tile.state { |
| 884 | TileState::Present => 0, |
| 885 | TileState::Invited => 1, |
| 886 | _ => 2, |
| 887 | }; |
| 888 | ( |
| 889 | !self.is_pinned(&tile.mri), |
| 890 | tile.hand.is_none(), |
| 891 | tile.hand.unwrap_or(0), |
| 892 | state, |
| 893 | ) |
| 894 | }); |
| 895 | tiles |
| 896 | } |
| 897 | |
| 898 | pub fn strip_tiles(&self) -> Vec<&Tile> { |
| 899 | let mut tiles = self.visible_tiles(); |
| 900 | tiles.sort_by_key(|tile| { |
| 901 | ( |
| 902 | !self.is_pinned(&tile.mri), |
| 903 | tile.hand.is_none(), |
| 904 | tile.hand.unwrap_or(0), |
| 905 | !self.tile_speaking(tile), |
| 906 | ) |
| 907 | }); |
| 908 | tiles |
| 909 | } |
| 910 | |
| 911 | pub fn has_content(&self) -> bool { |
| 912 | self.screen_sharer.is_some() || self.whiteboard.is_some() |
| 913 | } |
| 914 | |
| 915 | pub fn effective_layout(&self) -> CallLayout { |
| 916 | match self.view.layout { |
| 917 | CallLayout::Content if self.has_content() => CallLayout::Content, |
| 918 | CallLayout::Content => match self.view.layout_before_content { |
| 919 | CallLayout::Content => CallLayout::Gallery, |
| 920 | before => before, |
| 921 | }, |
| 922 | layout => layout, |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | fn is_prioritized(&self, tile: &Tile) -> bool { |
| 927 | self.is_pinned(&tile.mri) || tile.spotlight.is_some() || tile.hand.is_some() |
| 928 | } |
| 929 | |
| 930 | pub fn hidden_for_no_video(&self, tile: &Tile) -> bool { |
| 931 | self.view.hide_no_video |
| 932 | && matches!(self.kind, CallKind::Group | CallKind::Meeting) |
| 933 | && tile.state == TileState::Present |
| 934 | && !tile.has_video |
| 935 | && !self.is_pinned(&tile.mri) |
| 936 | && !self.tile_speaking(tile) |
| 937 | && !self.recent_speakers.contains(&tile.mri) |
| 938 | } |
| 939 | |
| 940 | pub fn hidden_without_video(&self) -> usize { |
| 941 | self.visible_tiles() |
| 942 | .into_iter() |
| 943 | .filter(|tile| self.hidden_for_no_video(tile)) |
| 944 | .count() |
| 945 | } |
| 946 | |
| 947 | fn layout_members(&self) -> Vec<&Tile> { |
| 948 | let mut tiles: Vec<&Tile> = self |
| 949 | .visible_tiles() |
| 950 | .into_iter() |
| 951 | .filter(|tile| !self.hidden_for_no_video(tile)) |
| 952 | .collect(); |
| 953 | tiles.sort_by_key(|tile| !self.is_prioritized(tile)); |
| 954 | tiles |
| 955 | } |
| 956 | |
| 957 | pub fn layout_tiles(&self) -> Vec<&Tile> { |
| 958 | let members = self.layout_members(); |
| 959 | let mut ordered: Vec<&Tile> = self |
| 960 | .rotation |
| 961 | .order() |
| 962 | .iter() |
| 963 | .filter_map(|mri| members.iter().find(|tile| &tile.mri == mri).copied()) |
| 964 | .collect(); |
| 965 | ordered.extend( |
| 966 | members |
| 967 | .into_iter() |
| 968 | .filter(|tile| !self.rotation.order().contains(&tile.mri)), |
| 969 | ); |
| 970 | ordered |
| 971 | } |
| 972 | |
| 973 | pub fn is_recent_speaker(&self, mri: &str) -> bool { |
| 974 | self.recent_speakers.iter().any(|recent| recent == mri) |
| 975 | } |
| 976 | |
| 977 | pub fn own_cells(&self) -> usize { |
| 978 | usize::from(!self.view.hide_me) |
| 979 | } |
| 980 | |
| 981 | fn rotation_capacity(&self, others: usize) -> usize { |
| 982 | let cells = match self.effective_layout() { |
| 983 | _ if self.has_content() => return others, |
| 984 | CallLayout::Speaker => FILMSTRIP_MAX, |
| 985 | CallLayout::Gallery | CallLayout::Content => GALLERY_MAX, |
| 986 | }; |
| 987 | split_overflow(others, self.own_cells(), cells).shown |
| 988 | } |
| 989 | |
| 990 | pub fn refresh_layout(&mut self, now: Instant) -> bool { |
| 991 | let members: Vec<String> = self |
| 992 | .layout_members() |
| 993 | .iter() |
| 994 | .map(|tile| tile.mri.clone()) |
| 995 | .collect(); |
| 996 | let priority: Vec<String> = self |
| 997 | .layout_members() |
| 998 | .iter() |
| 999 | .filter(|tile| self.is_prioritized(tile)) |
| 1000 | .map(|tile| tile.mri.clone()) |
| 1001 | .collect(); |
| 1002 | let capacity = self.rotation_capacity(members.len()); |
| 1003 | let speaking = self.speaking_mris.clone(); |
| 1004 | let reordered = self |
| 1005 | .rotation |
| 1006 | .update(now, &members, &priority, &speaking, capacity); |
| 1007 | let recent: Vec<String> = self |
| 1008 | .tiles |
| 1009 | .iter() |
| 1010 | .filter(|tile| self.rotation.spoke_within(&tile.mri, now, RECENT_SPEAKER)) |
| 1011 | .map(|tile| tile.mri.clone()) |
| 1012 | .collect(); |
| 1013 | let held = self.stage_speaker.as_deref().is_some_and(|mri| { |
| 1014 | members.iter().any(|member| member == mri) && self.rotation.spoke_within(mri, now, SPEAKER_HOLD) |
| 1015 | }); |
| 1016 | let stage_speaker = if held { |
| 1017 | self.stage_speaker.clone() |
| 1018 | } else { |
| 1019 | self.rotation |
| 1020 | .steady_speaker(now, &members) |
| 1021 | .map(str::to_owned) |
| 1022 | .or_else(|| self.stage_speaker.clone().filter(|mri| members.contains(mri))) |
| 1023 | }; |
| 1024 | let changed = reordered || recent != self.recent_speakers || stage_speaker != self.stage_speaker; |
| 1025 | self.recent_speakers = recent; |
| 1026 | self.stage_speaker = stage_speaker; |
| 1027 | changed |
| 1028 | } |
| 1029 | |
| 1030 | pub fn speaker_focus(&self) -> Option<Focus> { |
| 1031 | if let Some(focus) = self.focus() { |
| 1032 | return Some(focus); |
| 1033 | } |
| 1034 | let tiles = self.layout_tiles(); |
| 1035 | let spoken = self |
| 1036 | .stage_speaker |
| 1037 | .as_deref() |
| 1038 | .filter(|mri| tiles.iter().any(|tile| tile.mri == *mri)) |
| 1039 | .or_else(|| { |
| 1040 | self.rotation |
| 1041 | .last_speaker() |
| 1042 | .filter(|mri| tiles.iter().any(|tile| tile.mri == *mri)) |
| 1043 | }) |
| 1044 | .or_else(|| tiles.first().map(|tile| tile.mri.as_str())); |
| 1045 | let own_mri = self.own_mri.clone().filter(|_| self.own_cells() > 0); |
| 1046 | let (mri, own) = match (spoken, own_mri) { |
| 1047 | (Some(mri), _) => (mri.to_owned(), false), |
| 1048 | (None, Some(own_mri)) => (own_mri, true), |
| 1049 | (None, None) => return None, |
| 1050 | }; |
| 1051 | Some(Focus { |
| 1052 | mri, |
| 1053 | own, |
| 1054 | spotlight: false, |
| 1055 | pinned: false, |
| 1056 | }) |
| 1057 | } |
| 1058 | |
| 1059 | pub fn sharing_label(&self) -> Option<String> { |
| 1060 | let sharer = self.screen_sharer.as_ref()?; |
| 1061 | let name = self |
| 1062 | .tiles |
| 1063 | .iter() |
| 1064 | .find(|tile| &tile.mri == sharer) |
| 1065 | .map(|tile| tile.name.as_str()) |
| 1066 | .filter(|name| !name.is_empty()) |
| 1067 | .unwrap_or("Someone"); |
| 1068 | Some(format!("{name} is sharing")) |
| 1069 | } |
| 1070 | |
| 1071 | pub fn whiteboard_label(&self) -> Option<String> { |
| 1072 | let share = self.whiteboard.as_ref()?; |
| 1073 | let name = share |
| 1074 | .presenter |
| 1075 | .as_deref() |
| 1076 | .and_then(|presenter| self.tiles.iter().find(|tile| tile.mri == presenter)) |
| 1077 | .map(|tile| tile.name.as_str()) |
| 1078 | .filter(|name| !name.is_empty()) |
| 1079 | .unwrap_or("Someone"); |
| 1080 | Some(format!("{name} is sharing a whiteboard")) |
| 1081 | } |
| 1082 | |
| 1083 | pub fn whiteboard_url(&self) -> Option<&str> { |
| 1084 | self.whiteboard.as_ref()?.url.as_deref() |
| 1085 | } |
| 1086 | |
| 1087 | pub fn health_banner(&self) -> Option<HealthBanner> { |
| 1088 | self.health.banner(self.background != BackgroundPick::None) |
| 1089 | } |
| 1090 | |
| 1091 | pub fn is_one_to_one(&self) -> bool { |
| 1092 | self.kind == CallKind::Direct |
| 1093 | } |
| 1094 | |
| 1095 | pub fn can_hold(&self) -> bool { |
| 1096 | self.is_one_to_one() && self.is_live() && self.hold != HoldState::Remote |
| 1097 | } |
| 1098 | |
| 1099 | pub fn can_transfer(&self) -> bool { |
| 1100 | self.is_one_to_one() && self.is_live() && self.hold == HoldState::Active |
| 1101 | } |
| 1102 | |
| 1103 | pub fn can_open_whiteboard(&self) -> bool { |
| 1104 | self.kind == CallKind::Meeting && self.is_live() && !self.lobby |
| 1105 | } |
| 1106 | |
| 1107 | pub fn tile_speaking(&self, tile: &Tile) -> bool { |
| 1108 | if self.speaking_mris.contains(&tile.mri) { |
| 1109 | return true; |
| 1110 | } |
| 1111 | let only_remote = self.tiles.len() == 1; |
| 1112 | only_remote && tile.state != TileState::Left && self.remote_speaking |
| 1113 | } |
| 1114 | |
| 1115 | pub fn tile_caption(&self, tile: &Tile) -> Option<&'static str> { |
| 1116 | match tile.state { |
| 1117 | TileState::Invited => Some(match self.progress { |
| 1118 | Some(Progress::Ringing) => "Ringing", |
| 1119 | _ => "Calling...", |
| 1120 | }), |
| 1121 | TileState::Left => Some("Left"), |
| 1122 | TileState::Present | TileState::InLobby => None, |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | pub fn elapsed(&self, now: Instant) -> Option<Duration> { |
| 1127 | self.connected_since |
| 1128 | .map(|since| now.saturating_duration_since(since)) |
| 1129 | } |
| 1130 | |
| 1131 | pub fn connecting_text(&self) -> &'static str { |
| 1132 | match (self.kind, self.progress) { |
| 1133 | (CallKind::Meeting, _) => "Joining...", |
| 1134 | (_, _) if self.incoming => "Connecting...", |
| 1135 | (_, Some(Progress::Ringing)) => "Ringing", |
| 1136 | _ => "Calling...", |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | pub fn is_active(&self) -> bool { |
| 1141 | matches!( |
| 1142 | self.state, |
| 1143 | CallState::Connecting | CallState::Connected { .. } | CallState::Reconnecting { .. } |
| 1144 | ) |
| 1145 | } |
| 1146 | |
| 1147 | pub fn is_connecting(&self) -> bool { |
| 1148 | matches!(self.state, CallState::Connecting) |
| 1149 | } |
| 1150 | |
| 1151 | pub fn is_reconnecting(&self) -> bool { |
| 1152 | matches!(self.state, CallState::Reconnecting { .. }) |
| 1153 | } |
| 1154 | |
| 1155 | pub fn ended_reason(&self) -> Option<&EndReason> { |
| 1156 | match &self.state { |
| 1157 | CallState::Ended { reason } => Some(reason), |
| 1158 | _ => None, |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | pub fn timer_text(&self, now: Instant) -> String { |
| 1163 | self.elapsed(now).map(format_elapsed).unwrap_or_default() |
| 1164 | } |
| 1165 | |
| 1166 | pub fn has_reaction_controls(&self) -> bool { |
| 1167 | matches!(self.kind, CallKind::Group | CallKind::Meeting) |
| 1168 | } |
| 1169 | |
| 1170 | pub fn can_react(&self) -> bool { |
| 1171 | self.is_live() && !self.lobby |
| 1172 | } |
| 1173 | |
| 1174 | pub fn can_use_camera(&self) -> bool { |
| 1175 | !self.cameras.is_empty() |
| 1176 | } |
| 1177 | |
| 1178 | pub fn can_share(&self) -> bool { |
| 1179 | !self.share_sources.is_empty() && self.is_live() |
| 1180 | } |
| 1181 | |
| 1182 | pub fn is_live(&self) -> bool { |
| 1183 | matches!( |
| 1184 | self.state, |
| 1185 | CallState::Connected { .. } | CallState::Reconnecting { .. } |
| 1186 | ) |
| 1187 | } |
| 1188 | |
| 1189 | pub fn screens(&self) -> Vec<&ShareSource> { |
| 1190 | self.share_sources |
| 1191 | .iter() |
| 1192 | .filter(|source| source.kind == ShareKind::Screen) |
| 1193 | .collect() |
| 1194 | } |
| 1195 | |
| 1196 | pub fn windows(&self) -> Vec<&ShareSource> { |
| 1197 | self.share_sources |
| 1198 | .iter() |
| 1199 | .filter(|source| source.kind == ShareKind::Window) |
| 1200 | .collect() |
| 1201 | } |
| 1202 | |
| 1203 | pub fn can_unmute(&self) -> bool { |
| 1204 | !self.listen_only && !self.audio_off |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | pub fn format_elapsed(elapsed: Duration) -> String { |
| 1209 | let seconds = elapsed.as_secs(); |
| 1210 | let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60); |
| 1211 | if hours > 0 { |
| 1212 | format!("{hours}:{minutes:02}:{seconds:02}") |
| 1213 | } else { |
| 1214 | format!("{minutes:02}:{seconds:02}") |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | pub fn ended_notice(reason: &EndReason, elapsed: Option<Duration>, peer_name: &str) -> String { |
| 1219 | let ended = || match elapsed { |
| 1220 | Some(elapsed) => format!("Call ended {}", format_elapsed(elapsed)), |
| 1221 | None => "Call ended".to_owned(), |
| 1222 | }; |
| 1223 | match reason { |
| 1224 | EndReason::Cancelled => "Call cancelled".to_owned(), |
| 1225 | EndReason::Dropped => "Call dropped".to_owned(), |
| 1226 | EndReason::Failed(message) => format!("Call failed: {message}"), |
| 1227 | EndReason::LocalHangup => ended(), |
| 1228 | EndReason::Remote(EndKind::Declined) => format!("{peer_name} declined"), |
| 1229 | EndReason::Remote(EndKind::NoAnswer) => "No answer".to_owned(), |
| 1230 | EndReason::Remote(EndKind::Unavailable) => format!("{peer_name} is not available"), |
| 1231 | EndReason::Remote(_) => ended(), |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | #[cfg(test)] |
| 1236 | mod tests { |
| 1237 | use super::*; |
| 1238 | |
| 1239 | fn connected_model() -> CallModel { |
| 1240 | let mut model = CallModel::test(); |
| 1241 | model.apply(CallUpdate::State(CallState::Connected { |
| 1242 | since: Instant::now(), |
| 1243 | })); |
| 1244 | model |
| 1245 | } |
| 1246 | |
| 1247 | fn entry(mri: &str, name: &str, muted: bool, in_lobby: bool) -> RosterEntry { |
| 1248 | RosterEntry { |
| 1249 | mri: mri.into(), |
| 1250 | display_name: name.into(), |
| 1251 | muted, |
| 1252 | in_lobby, |
| 1253 | has_video: false, |
| 1254 | sharing: false, |
| 1255 | hand: None, |
| 1256 | spotlight: None, |
| 1257 | organizer: false, |
| 1258 | role: None, |
| 1259 | limits: AttendeeLimits::default(), |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | fn spotlit(mri: &str, name: &str, rank: u64) -> RosterEntry { |
| 1264 | RosterEntry { |
| 1265 | spotlight: Some(calling::Spotlight { |
| 1266 | state_id: format!("sp-{rank}"), |
| 1267 | rank, |
| 1268 | }), |
| 1269 | ..entry(mri, name, false, false) |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | fn organizer_entry(mri: &str) -> RosterEntry { |
| 1274 | RosterEntry { |
| 1275 | organizer: true, |
| 1276 | ..entry(mri, "Me", false, false) |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | fn raised(mri: &str, name: &str, rank: u64) -> RosterEntry { |
| 1281 | RosterEntry { |
| 1282 | hand: Some(calling::RaisedHand { |
| 1283 | state_id: format!("s-{rank}"), |
| 1284 | rank, |
| 1285 | }), |
| 1286 | ..entry(mri, name, false, false) |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | fn meeting_with_me() -> CallModel { |
| 1291 | let mut model = CallModel::meeting("Standup", false); |
| 1292 | model.apply(CallUpdate::OwnIdentity { |
| 1293 | mri: "8:orgid:me".into(), |
| 1294 | }); |
| 1295 | model |
| 1296 | } |
| 1297 | |
| 1298 | #[test] |
| 1299 | fn elapsed_text_is_minutes_and_seconds() { |
| 1300 | assert_eq!(format_elapsed(Duration::from_secs(42)), "00:42"); |
| 1301 | assert_eq!(format_elapsed(Duration::from_secs(61)), "01:01"); |
| 1302 | assert_eq!(format_elapsed(Duration::from_secs(3725)), "1:02:05"); |
| 1303 | } |
| 1304 | |
| 1305 | #[test] |
| 1306 | fn levels_drive_the_speaking_rings() { |
| 1307 | let mut model = connected_model(); |
| 1308 | model.apply(CallUpdate::Levels { |
| 1309 | local: 0.0, |
| 1310 | remote: 0.4, |
| 1311 | }); |
| 1312 | assert!(model.remote_speaking); |
| 1313 | assert!(!model.local_speaking); |
| 1314 | model.apply(CallUpdate::Levels { |
| 1315 | local: 0.4, |
| 1316 | remote: 0.0, |
| 1317 | }); |
| 1318 | assert!(model.local_speaking); |
| 1319 | } |
| 1320 | |
| 1321 | #[test] |
| 1322 | fn muting_switches_the_local_ring_off_at_once() { |
| 1323 | let mut model = connected_model(); |
| 1324 | model.apply(CallUpdate::Levels { |
| 1325 | local: 0.5, |
| 1326 | remote: 0.0, |
| 1327 | }); |
| 1328 | assert!(model.local_speaking); |
| 1329 | model.apply(CallUpdate::Muted(true)); |
| 1330 | assert!(!model.local_speaking); |
| 1331 | model.apply(CallUpdate::Levels { |
| 1332 | local: 0.5, |
| 1333 | remote: 0.0, |
| 1334 | }); |
| 1335 | assert!(!model.local_speaking); |
| 1336 | } |
| 1337 | |
| 1338 | #[test] |
| 1339 | fn reconnecting_keeps_the_timer_and_clears_the_rings() { |
| 1340 | let mut model = connected_model(); |
| 1341 | let since = model.connected_since.unwrap(); |
| 1342 | model.apply(CallUpdate::Levels { |
| 1343 | local: 0.5, |
| 1344 | remote: 0.5, |
| 1345 | }); |
| 1346 | model.apply(CallUpdate::State(CallState::Reconnecting { since })); |
| 1347 | assert!(model.is_reconnecting()); |
| 1348 | assert_eq!(model.connected_since, Some(since)); |
| 1349 | assert!(!model.remote_speaking && !model.local_speaking); |
| 1350 | } |
| 1351 | |
| 1352 | #[test] |
| 1353 | fn notices_for_every_end_reason() { |
| 1354 | let elapsed = Some(Duration::from_secs(42)); |
| 1355 | let notice = |reason: EndReason, elapsed| ended_notice(&reason, elapsed, "Bea"); |
| 1356 | assert_eq!(notice(EndReason::LocalHangup, elapsed), "Call ended 00:42"); |
| 1357 | assert_eq!( |
| 1358 | notice(EndReason::Remote(EndKind::Normal), elapsed), |
| 1359 | "Call ended 00:42" |
| 1360 | ); |
| 1361 | assert_eq!( |
| 1362 | notice(EndReason::Remote(EndKind::Normal), None), |
| 1363 | "Call ended" |
| 1364 | ); |
| 1365 | assert_eq!(notice(EndReason::Cancelled, None), "Call cancelled"); |
| 1366 | assert_eq!(notice(EndReason::Dropped, elapsed), "Call dropped"); |
| 1367 | assert_eq!( |
| 1368 | notice(EndReason::Failed("no route".into()), None), |
| 1369 | "Call failed: no route" |
| 1370 | ); |
| 1371 | assert_eq!( |
| 1372 | notice(EndReason::Remote(EndKind::Declined), None), |
| 1373 | "Bea declined" |
| 1374 | ); |
| 1375 | assert_eq!( |
| 1376 | notice(EndReason::Remote(EndKind::NoAnswer), None), |
| 1377 | "No answer" |
| 1378 | ); |
| 1379 | assert_eq!( |
| 1380 | notice(EndReason::Remote(EndKind::Unavailable), None), |
| 1381 | "Bea is not available" |
| 1382 | ); |
| 1383 | assert_eq!( |
| 1384 | notice(EndReason::Remote(EndKind::Cancelled), None), |
| 1385 | "Call ended" |
| 1386 | ); |
| 1387 | assert_eq!( |
| 1388 | notice(EndReason::Remote(EndKind::Other), None), |
| 1389 | "Call ended" |
| 1390 | ); |
| 1391 | assert_eq!( |
| 1392 | notice(EndReason::Remote(EndKind::AnsweredElsewhere), None), |
| 1393 | "Call ended" |
| 1394 | ); |
| 1395 | } |
| 1396 | |
| 1397 | #[test] |
| 1398 | fn the_connecting_text_follows_the_kind_of_call() { |
| 1399 | let mut direct = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 1400 | assert_eq!(direct.connecting_text(), "Calling..."); |
| 1401 | direct.apply(CallUpdate::Progress(Progress::Ringing)); |
| 1402 | assert_eq!(direct.connecting_text(), "Ringing"); |
| 1403 | assert_eq!( |
| 1404 | CallModel::meeting("Standup", false).connecting_text(), |
| 1405 | "Joining..." |
| 1406 | ); |
| 1407 | assert_eq!( |
| 1408 | CallModel::incoming("Bea", "8:orgid:b", "Bea").connecting_text(), |
| 1409 | "Connecting..." |
| 1410 | ); |
| 1411 | assert_eq!(CallModel::test().connecting_text(), "Calling..."); |
| 1412 | } |
| 1413 | |
| 1414 | #[test] |
| 1415 | fn listen_only_blocks_unmuting() { |
| 1416 | let mut model = connected_model(); |
| 1417 | assert!(model.can_unmute()); |
| 1418 | model.apply(CallUpdate::ListenOnly(true)); |
| 1419 | assert!(!model.can_unmute()); |
| 1420 | } |
| 1421 | |
| 1422 | #[test] |
| 1423 | fn a_call_without_audio_blocks_unmuting() { |
| 1424 | let mut model = connected_model(); |
| 1425 | model.audio_off = true; |
| 1426 | assert!(!model.can_unmute()); |
| 1427 | } |
| 1428 | |
| 1429 | #[test] |
| 1430 | fn callees_start_invited_and_follow_the_progress() { |
| 1431 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 1432 | assert_eq!(model.kind, CallKind::Direct); |
| 1433 | assert_eq!(model.peer_name, "Bea"); |
| 1434 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Calling...")); |
| 1435 | model.apply(CallUpdate::Progress(Progress::Ringing)); |
| 1436 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Ringing")); |
| 1437 | model.apply(CallUpdate::Roster(vec![entry( |
| 1438 | "8:orgid:b", |
| 1439 | "Bea", |
| 1440 | false, |
| 1441 | false, |
| 1442 | )])); |
| 1443 | assert_eq!(model.tiles[0].state, TileState::Present); |
| 1444 | assert_eq!(model.tile_caption(&model.tiles[0]), None); |
| 1445 | } |
| 1446 | |
| 1447 | #[test] |
| 1448 | fn several_callees_make_a_group_call_named_after_the_chat() { |
| 1449 | let model = CallModel::people( |
| 1450 | "Retro team", |
| 1451 | &[ |
| 1452 | ("8:orgid:b".into(), "Bea".into()), |
| 1453 | ("8:orgid:c".into(), "Cy".into()), |
| 1454 | ], |
| 1455 | ); |
| 1456 | assert_eq!(model.kind, CallKind::Group); |
| 1457 | assert_eq!(model.peer_name, "Retro team"); |
| 1458 | assert_eq!(model.tiles.len(), 2); |
| 1459 | } |
| 1460 | |
| 1461 | #[test] |
| 1462 | fn roster_adds_guests_updates_mute_and_drops_leavers() { |
| 1463 | let mut model = meeting_with_me(); |
| 1464 | model.apply(CallUpdate::Roster(vec![ |
| 1465 | entry("8:orgid:me", "Me", false, false), |
| 1466 | entry("8:orgid:a", "Ana", true, false), |
| 1467 | entry("8:orgid:b", "Bo", false, false), |
| 1468 | ])); |
| 1469 | assert_eq!(model.tiles.len(), 2); |
| 1470 | assert!( |
| 1471 | model |
| 1472 | .tiles |
| 1473 | .iter() |
| 1474 | .find(|tile| tile.name == "Ana") |
| 1475 | .unwrap() |
| 1476 | .muted |
| 1477 | ); |
| 1478 | model.apply(CallUpdate::Roster(vec![ |
| 1479 | entry("8:orgid:me", "Me", false, false), |
| 1480 | entry("8:orgid:b", "Bo", false, false), |
| 1481 | ])); |
| 1482 | assert_eq!(model.tiles.len(), 1); |
| 1483 | assert_eq!(model.tiles[0].name, "Bo"); |
| 1484 | } |
| 1485 | |
| 1486 | #[test] |
| 1487 | fn meeting_bots_get_no_tile_but_the_called_echo_bot_keeps_its_own() { |
| 1488 | let mut model = meeting_with_me(); |
| 1489 | model.apply(CallUpdate::Roster(vec![ |
| 1490 | entry("8:orgid:me", "Me", false, false), |
| 1491 | entry("8:orgid:a", "Ana", false, false), |
| 1492 | entry("28:bdd75849-e0a6-4cce-8fc1-d7c0d4da43e5", "", false, false), |
| 1493 | ])); |
| 1494 | let names: Vec<&str> = model.strip_tiles().iter().map(|tile| tile.name.as_str()).collect(); |
| 1495 | assert_eq!(names, vec!["Ana"]); |
| 1496 | assert_eq!(model.present_others(), 1); |
| 1497 | let mut echo = CallModel::people(ECHO_NAME, &[(ECHO_MRI.into(), ECHO_NAME.into())]); |
| 1498 | echo.apply(CallUpdate::Roster(vec![entry(ECHO_MRI, "", false, false)])); |
| 1499 | assert_eq!(echo.tiles.len(), 1); |
| 1500 | assert_eq!(echo.tiles[0].state, TileState::Present); |
| 1501 | } |
| 1502 | |
| 1503 | #[test] |
| 1504 | fn an_invited_callee_who_leaves_stays_as_left() { |
| 1505 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 1506 | model.apply(CallUpdate::Roster(vec![entry( |
| 1507 | "8:orgid:b", |
| 1508 | "Bea", |
| 1509 | false, |
| 1510 | false, |
| 1511 | )])); |
| 1512 | model.apply(CallUpdate::Roster(Vec::new())); |
| 1513 | assert_eq!(model.tiles[0].state, TileState::Left); |
| 1514 | assert_eq!(model.tile_caption(&model.tiles[0]), Some("Left")); |
| 1515 | } |
| 1516 | |
| 1517 | #[test] |
| 1518 | fn lobby_guests_get_no_tile() { |
| 1519 | let mut model = meeting_with_me(); |
| 1520 | model.apply(CallUpdate::Roster(vec![ |
| 1521 | entry("8:orgid:a", "Ana", false, false), |
| 1522 | entry("8:orgid:g", "Guest", false, true), |
| 1523 | ])); |
| 1524 | let names: Vec<&str> = model |
| 1525 | .visible_tiles() |
| 1526 | .iter() |
| 1527 | .map(|tile| tile.name.as_str()) |
| 1528 | .collect(); |
| 1529 | assert_eq!(names, vec!["Ana"]); |
| 1530 | } |
| 1531 | |
| 1532 | #[test] |
| 1533 | fn the_lobby_flag_follows_the_call() { |
| 1534 | let mut model = meeting_with_me(); |
| 1535 | model.apply(CallUpdate::Lobby(true)); |
| 1536 | assert!(model.lobby); |
| 1537 | model.apply(CallUpdate::Lobby(false)); |
| 1538 | assert!(!model.lobby); |
| 1539 | } |
| 1540 | |
| 1541 | #[test] |
| 1542 | fn speakers_light_their_own_tile_and_a_lone_remote_follows_the_level() { |
| 1543 | let mut model = meeting_with_me(); |
| 1544 | model.apply(CallUpdate::Roster(vec![ |
| 1545 | entry("8:orgid:a", "Ana", false, false), |
| 1546 | entry("8:orgid:b", "Bo", false, false), |
| 1547 | ])); |
| 1548 | model.apply(CallUpdate::State(CallState::Connected { |
| 1549 | since: Instant::now(), |
| 1550 | })); |
| 1551 | model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()])); |
| 1552 | let tiles: Vec<Tile> = model.tiles.clone(); |
| 1553 | assert!(!model.tile_speaking(&tiles[0])); |
| 1554 | assert!(model.tile_speaking(&tiles[1])); |
| 1555 | let mut lone = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 1556 | lone.apply(CallUpdate::State(CallState::Connected { |
| 1557 | since: Instant::now(), |
| 1558 | })); |
| 1559 | lone.apply(CallUpdate::Levels { |
| 1560 | local: 0.0, |
| 1561 | remote: 0.5, |
| 1562 | }); |
| 1563 | assert!(lone.tile_speaking(&lone.tiles[0].clone())); |
| 1564 | } |
| 1565 | |
| 1566 | fn crowd(count: usize) -> CallModel { |
| 1567 | let mut model = meeting_with_me(); |
| 1568 | model.apply(CallUpdate::Roster( |
| 1569 | (0..count) |
| 1570 | .map(|index| { |
| 1571 | entry( |
| 1572 | &format!("8:orgid:p{index}"), |
| 1573 | &format!("P{index}"), |
| 1574 | false, |
| 1575 | false, |
| 1576 | ) |
| 1577 | }) |
| 1578 | .collect(), |
| 1579 | )); |
| 1580 | model |
| 1581 | } |
| 1582 | |
| 1583 | fn shown_mris(model: &CallModel) -> Vec<String> { |
| 1584 | model |
| 1585 | .layout_tiles() |
| 1586 | .iter() |
| 1587 | .map(|tile| tile.mri.clone()) |
| 1588 | .collect() |
| 1589 | } |
| 1590 | |
| 1591 | #[test] |
| 1592 | fn fit_to_frame_is_per_tile_and_toggles_back_to_fill() { |
| 1593 | let mut model = meeting_with_me(); |
| 1594 | assert!(!model.is_fitted("8:orgid:a")); |
| 1595 | model.toggle_fit("8:orgid:a"); |
| 1596 | assert!(model.is_fitted("8:orgid:a")); |
| 1597 | assert!(!model.is_fitted("8:orgid:b")); |
| 1598 | model.toggle_fit("8:orgid:a"); |
| 1599 | assert!(!model.is_fitted("8:orgid:a")); |
| 1600 | } |
| 1601 | |
| 1602 | #[test] |
| 1603 | fn hiding_people_without_video_keeps_speakers_and_the_pinned_person() { |
| 1604 | let mut model = meeting_with_me(); |
| 1605 | model.apply(CallUpdate::Roster(vec![ |
| 1606 | entry("8:orgid:a", "Ana", false, false), |
| 1607 | RosterEntry { |
| 1608 | has_video: true, |
| 1609 | ..entry("8:orgid:b", "Bo", false, false) |
| 1610 | }, |
| 1611 | entry("8:orgid:c", "Cy", false, false), |
| 1612 | ])); |
| 1613 | model.view.hide_no_video = true; |
| 1614 | assert_eq!(model.hidden_without_video(), 2); |
| 1615 | assert_eq!(shown_mris(&model), vec!["8:orgid:b"]); |
| 1616 | model.apply(CallUpdate::Speakers(vec!["8:orgid:a".into()])); |
| 1617 | assert_eq!(model.hidden_without_video(), 1); |
| 1618 | assert!(shown_mris(&model).contains(&"8:orgid:a".to_owned())); |
| 1619 | model.toggle_pin("8:orgid:c"); |
| 1620 | assert_eq!(model.hidden_without_video(), 0); |
| 1621 | } |
| 1622 | |
| 1623 | #[test] |
| 1624 | fn people_without_video_stay_in_one_to_one_calls() { |
| 1625 | let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]); |
| 1626 | model.view.hide_no_video = true; |
| 1627 | model.apply(CallUpdate::Roster(vec![entry( |
| 1628 | "8:orgid:b", |
| 1629 | "Bea", |
| 1630 | false, |
| 1631 | false, |
| 1632 | )])); |
| 1633 | assert_eq!(model.hidden_without_video(), 0); |
| 1634 | } |
| 1635 | |
| 1636 | #[test] |
| 1637 | fn focus_on_content_falls_back_to_the_previous_layout_when_the_share_ends() { |
| 1638 | let mut model = meeting_with_me(); |
| 1639 | model.view.choose(CallLayout::Speaker); |
| 1640 | model.view.choose(CallLayout::Content); |
| 1641 | assert_eq!(model.effective_layout(), CallLayout::Speaker); |
| 1642 | model.apply(CallUpdate::ScreenShare(Some("8:orgid:a".into()))); |
| 1643 | assert_eq!(model.effective_layout(), CallLayout::Content); |
| 1644 | model.apply(CallUpdate::ScreenShare(None)); |
| 1645 | assert_eq!(model.effective_layout(), CallLayout::Speaker); |
| 1646 | } |
| 1647 | |
| 1648 | #[test] |
| 1649 | fn the_speaker_stage_follows_the_active_speaker_and_keeps_the_last_one() { |
| 1650 | let mut model = crowd(3); |
| 1651 | model.apply(CallUpdate::Speakers(vec!["8:orgid:p1".into()])); |
| 1652 | assert_eq!(model.speaker_focus().unwrap().mri, "8:orgid:p1"); |
| 1653 | model.apply(CallUpdate::Speakers(Vec::new())); |
| 1654 | assert_eq!(model.speaker_focus().unwrap().mri, "8:orgid:p1"); |
| 1655 | model.toggle_pin("8:orgid:p2"); |
| 1656 | let focus = model.speaker_focus().unwrap(); |
| 1657 | assert_eq!((focus.mri.as_str(), focus.pinned), ("8:orgid:p2", true)); |
| 1658 | } |
| 1659 | |
| 1660 | #[test] |
| 1661 | fn a_pinned_person_outside_the_grid_moves_into_it() { |
| 1662 | let mut model = crowd(12); |
| 1663 | let grid = split_overflow(12, 1, GALLERY_MAX).shown; |
| 1664 | assert_eq!(grid, 7); |
| 1665 | assert!(!shown_mris(&model)[..grid].contains(&"8:orgid:p11".to_owned())); |
| 1666 | model.toggle_pin("8:orgid:p11"); |
| 1667 | assert!(shown_mris(&model)[..grid].contains(&"8:orgid:p11".to_owned())); |
| 1668 | assert_eq!(model.layout_tiles().len(), 12); |
| 1669 | } |
| 1670 | |
| 1671 | #[test] |
| 1672 | fn hiding_me_frees_a_grid_cell() { |
| 1673 | let mut model = crowd(12); |
| 1674 | assert_eq!(model.own_cells(), 1); |
| 1675 | model.view.hide_me = true; |
| 1676 | assert_eq!(model.own_cells(), 0); |
| 1677 | assert_eq!(split_overflow(12, model.own_cells(), GALLERY_MAX).shown, 8); |
| 1678 | } |
| 1679 | |
| 1680 | #[test] |
| 1681 | fn the_stage_label_names_the_sharer_and_the_strip_puts_the_speaker_first() { |
| 1682 | let mut model = meeting_with_me(); |
| 1683 | model.apply(CallUpdate::Roster(vec![ |
| 1684 | entry("8:orgid:a", "Ana", false, false), |
| 1685 | entry("8:orgid:b", "Bo", false, false), |
| 1686 | ])); |
| 1687 | assert_eq!(model.sharing_label(), None); |
| 1688 | model.apply(CallUpdate::ScreenShare(Some("8:orgid:b".into()))); |
| 1689 | assert_eq!(model.sharing_label().as_deref(), Some("Bo is sharing")); |
| 1690 | model.apply(CallUpdate::State(CallState::Connected { |
| 1691 | since: Instant::now(), |
| 1692 | })); |
| 1693 | model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()])); |
| 1694 | let names: Vec<&str> = model |
| 1695 | .strip_tiles() |
| 1696 | .iter() |
| 1697 | .map(|tile| tile.name.as_str()) |
| 1698 | .collect(); |
| 1699 | assert_eq!(names, vec!["Bo", "Ana"]); |
| 1700 | model.apply(CallUpdate::ScreenShare(None)); |
| 1701 | assert_eq!(model.sharing_label(), None); |
| 1702 | } |
| 1703 | |
| 1704 | #[test] |
| 1705 | fn roster_entries_carry_the_camera_flag_to_the_tile() { |
| 1706 | let mut model = meeting_with_me(); |
| 1707 | let mut with_camera = entry("8:orgid:a", "Ana", false, false); |
| 1708 | with_camera.has_video = true; |
| 1709 | model.apply(CallUpdate::Roster(vec![with_camera])); |
| 1710 | assert!(model.tiles[0].has_video); |
| 1711 | model.apply(CallUpdate::Roster(vec![entry( |
| 1712 | "8:orgid:a", |
| 1713 | "Ana", |
| 1714 | false, |
| 1715 | false, |
| 1716 | )])); |
| 1717 | assert!(!model.tiles[0].has_video); |
| 1718 | } |
| 1719 | |
| 1720 | #[test] |
| 1721 | fn present_guests_sort_before_waiting_ones() { |
| 1722 | let mut model = CallModel::people( |
| 1723 | "Group", |
| 1724 | &[ |
| 1725 | ("8:orgid:b".into(), "Bea".into()), |
| 1726 | ("8:orgid:c".into(), "Cy".into()), |
| 1727 | ], |
| 1728 | ); |
| 1729 | model.apply(CallUpdate::Roster(vec![entry( |
| 1730 | "8:orgid:c", |
| 1731 | "Cy", |
| 1732 | false, |
| 1733 | false, |
| 1734 | )])); |
| 1735 | let names: Vec<&str> = model |
| 1736 | .visible_tiles() |
| 1737 | .iter() |
| 1738 | .map(|tile| tile.name.as_str()) |
| 1739 | .collect(); |
| 1740 | assert_eq!(names, vec!["Cy", "Bea"]); |
| 1741 | } |
| 1742 | |
| 1743 | #[test] |
| 1744 | fn raised_hands_get_queue_positions_by_raise_time_and_sort_first() { |
| 1745 | let mut model = meeting_with_me(); |
| 1746 | model.apply(CallUpdate::Roster(vec![ |
| 1747 | entry("8:orgid:a", "Ana", false, false), |
| 1748 | raised("8:orgid:b", "Bo", 9), |
| 1749 | raised("8:orgid:c", "Cy", 4), |
| 1750 | raised("8:orgid:me", "Me", 6), |
| 1751 | ])); |
| 1752 | assert_eq!(model.own_hand, Some(6)); |
| 1753 | assert!(model.any_hand_raised()); |
| 1754 | let names: Vec<&str> = model |
| 1755 | .visible_tiles() |
| 1756 | .iter() |
| 1757 | .map(|tile| tile.name.as_str()) |
| 1758 | .collect(); |
| 1759 | assert_eq!(names, vec!["Cy", "Bo", "Ana"]); |
| 1760 | let positions: Vec<usize> = [4, 6, 9] |
| 1761 | .into_iter() |
| 1762 | .map(|rank| model.hand_position(rank)) |
| 1763 | .collect(); |
| 1764 | assert_eq!(positions, vec![1, 2, 3]); |
| 1765 | assert_eq!(model.own_cell_index(3), 1); |
| 1766 | let strip: Vec<&str> = model |
| 1767 | .strip_tiles() |
| 1768 | .iter() |
| 1769 | .map(|tile| tile.name.as_str()) |
| 1770 | .collect(); |
| 1771 | assert_eq!(strip, vec!["Cy", "Bo", "Ana"]); |
| 1772 | } |
| 1773 | |
| 1774 | #[test] |
| 1775 | fn a_roster_without_the_own_entry_keeps_the_own_hand() { |
| 1776 | let mut model = meeting_with_me(); |
| 1777 | model.apply(CallUpdate::OwnHand(Some(5))); |
| 1778 | model.apply(CallUpdate::Roster(vec![entry( |
| 1779 | "8:orgid:a", |
| 1780 | "Ana", |
| 1781 | false, |
| 1782 | false, |
| 1783 | )])); |
| 1784 | assert_eq!(model.own_hand, Some(5)); |
| 1785 | model.apply(CallUpdate::Roster(vec![entry( |
| 1786 | "8:orgid:me", |
| 1787 | "Me", |
| 1788 | false, |
| 1789 | false, |
| 1790 | )])); |
| 1791 | assert_eq!(model.own_hand, None); |
| 1792 | } |
| 1793 | |
| 1794 | #[test] |
| 1795 | fn reaction_controls_show_in_group_calls_and_meetings_only() { |
| 1796 | let mut model = CallModel::test(); |
| 1797 | for (kind, expected) in [ |
| 1798 | (CallKind::Test, false), |
| 1799 | (CallKind::Direct, false), |
| 1800 | (CallKind::Group, true), |
| 1801 | (CallKind::Meeting, true), |
| 1802 | ] { |
| 1803 | model.kind = kind; |
| 1804 | assert_eq!(model.has_reaction_controls(), expected); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | #[test] |
| 1809 | fn the_server_echo_of_an_own_reaction_does_not_restart_the_chip() { |
| 1810 | let mut model = meeting_with_me(); |
| 1811 | let start = Instant::now(); |
| 1812 | model.note_reaction("8:orgid:me".into(), Reaction::Like, start); |
| 1813 | let serial = model.reactions[0].serial; |
| 1814 | model.note_reaction( |
| 1815 | "8:orgid:me".into(), |
| 1816 | Reaction::Like, |
| 1817 | start + Duration::from_millis(1500), |
| 1818 | ); |
| 1819 | assert_eq!(model.reactions.len(), 1); |
| 1820 | assert_eq!(model.reactions[0].serial, serial); |
| 1821 | model.note_reaction( |
| 1822 | "8:orgid:me".into(), |
| 1823 | Reaction::Heart, |
| 1824 | start + Duration::from_millis(1600), |
| 1825 | ); |
| 1826 | assert_eq!(model.reactions[0].reaction, Reaction::Heart); |
| 1827 | model.note_reaction( |
| 1828 | "8:orgid:me".into(), |
| 1829 | Reaction::Heart, |
| 1830 | start + Duration::from_secs(4), |
| 1831 | ); |
| 1832 | assert_ne!(model.reactions[0].serial, serial); |
| 1833 | } |
| 1834 | |
| 1835 | #[test] |
| 1836 | fn another_persons_repeated_reaction_restarts_the_chip() { |
| 1837 | let mut model = meeting_with_me(); |
| 1838 | let start = Instant::now(); |
| 1839 | model.note_reaction("8:orgid:a".into(), Reaction::Like, start); |
| 1840 | let serial = model.reactions[0].serial; |
| 1841 | model.note_reaction( |
| 1842 | "8:orgid:a".into(), |
| 1843 | Reaction::Like, |
| 1844 | start + Duration::from_secs(1), |
| 1845 | ); |
| 1846 | assert_ne!(model.reactions[0].serial, serial); |
| 1847 | } |
| 1848 | |
| 1849 | #[test] |
| 1850 | fn lowering_clears_the_hand_everywhere() { |
| 1851 | let mut model = meeting_with_me(); |
| 1852 | model.apply(CallUpdate::Roster(vec![ |
| 1853 | raised("8:orgid:a", "Ana", 1), |
| 1854 | raised("8:orgid:me", "Me", 2), |
| 1855 | ])); |
| 1856 | model.apply(CallUpdate::Roster(vec![ |
| 1857 | entry("8:orgid:a", "Ana", false, false), |
| 1858 | entry("8:orgid:me", "Me", false, false), |
| 1859 | ])); |
| 1860 | assert!(!model.any_hand_raised()); |
| 1861 | assert_eq!(model.own_cell_index(1), 1); |
| 1862 | } |
| 1863 | |
| 1864 | #[test] |
| 1865 | fn only_organizers_may_lower_hands() { |
| 1866 | assert!(!CallModel::meeting("Standup", false).can_lower_hands()); |
| 1867 | assert!(CallModel::meeting("Standup", true).can_lower_hands()); |
| 1868 | } |
| 1869 | |
| 1870 | #[test] |
| 1871 | fn a_reaction_shows_for_three_seconds_on_its_sender_and_the_latest_wins() { |
| 1872 | let mut model = meeting_with_me(); |
| 1873 | let start = Instant::now(); |
| 1874 | model.note_reaction("8:orgid:a".into(), Reaction::Heart, start); |
| 1875 | model.note_reaction("8:orgid:me".into(), Reaction::Like, start); |
| 1876 | let reaction_at = |model: &CallModel, mri: &str, seconds: u64| { |
| 1877 | model |
| 1878 | .chip_of(mri, start + Duration::from_secs(seconds)) |
| 1879 | .map(|chip| chip.reaction.clone()) |
| 1880 | }; |
| 1881 | assert_eq!(reaction_at(&model, "8:orgid:a", 2), Some(Reaction::Heart)); |
| 1882 | assert_eq!( |
| 1883 | model |
| 1884 | .own_chip(start + Duration::from_secs(2)) |
| 1885 | .map(|chip| chip.reaction.clone()), |
| 1886 | Some(Reaction::Like) |
| 1887 | ); |
| 1888 | assert_eq!(reaction_at(&model, "8:orgid:a", 3), None); |
| 1889 | model.note_reaction( |
| 1890 | "8:orgid:a".into(), |
| 1891 | Reaction::Laugh, |
| 1892 | start + Duration::from_secs(1), |
| 1893 | ); |
| 1894 | assert_eq!(reaction_at(&model, "8:orgid:a", 2), Some(Reaction::Laugh)); |
| 1895 | assert_eq!(model.reactions.len(), 2); |
| 1896 | } |
| 1897 | |
| 1898 | #[test] |
| 1899 | fn the_chat_panel_picks_the_meeting_thread_or_the_call_chat() { |
| 1900 | assert_eq!( |
| 1901 | chat_thread(CallKind::Meeting, Some("19:meeting_x"), Some("19:list")), |
| 1902 | Some("19:meeting_x") |
| 1903 | ); |
| 1904 | assert_eq!( |
| 1905 | chat_thread(CallKind::Meeting, None, Some("19:list")), |
| 1906 | Some("19:list") |
| 1907 | ); |
| 1908 | assert_eq!( |
| 1909 | chat_thread(CallKind::Direct, Some("19:meeting_x"), Some("19:dm")), |
| 1910 | Some("19:dm") |
| 1911 | ); |
| 1912 | assert_eq!( |
| 1913 | chat_thread(CallKind::Group, None, Some("19:group")), |
| 1914 | Some("19:group") |
| 1915 | ); |
| 1916 | assert_eq!(chat_thread(CallKind::Test, None, None), None); |
| 1917 | } |
| 1918 | |
| 1919 | #[test] |
| 1920 | fn computer_sound_follows_the_share() { |
| 1921 | let mut model = connected_model(); |
| 1922 | model.apply(CallUpdate::LocalShare(Some("Screen".into()))); |
| 1923 | model.apply(CallUpdate::ShareSound(true)); |
| 1924 | assert!(model.share_sound); |
| 1925 | model.apply(CallUpdate::LocalShare(None)); |
| 1926 | assert!(!model.share_sound); |
| 1927 | } |
| 1928 | |
| 1929 | #[test] |
| 1930 | fn lobby_guests_are_listed_for_organizers_who_may_admit() { |
| 1931 | let mut model = meeting_with_me(); |
| 1932 | model.apply(CallUpdate::State(CallState::Connected { |
| 1933 | since: Instant::now(), |
| 1934 | })); |
| 1935 | model.apply(CallUpdate::Roster(vec![ |
| 1936 | entry("8:orgid:a", "Ana", false, false), |
| 1937 | entry("8:orgid:g1", "Gast Eins", false, true), |
| 1938 | entry("8:orgid:g2", "Gast Zwei", false, true), |
| 1939 | ])); |
| 1940 | let waiting: Vec<&str> = model |
| 1941 | .lobby_guests() |
| 1942 | .iter() |
| 1943 | .map(|tile| tile.name.as_str()) |
| 1944 | .collect(); |
| 1945 | assert_eq!(waiting, vec!["Gast Eins", "Gast Zwei"]); |
| 1946 | assert!(!model.can_admit()); |
| 1947 | model.apply(CallUpdate::Roster(vec![ |
| 1948 | organizer_entry("8:orgid:me"), |
| 1949 | entry("8:orgid:g1", "Gast Eins", false, true), |
| 1950 | ])); |
| 1951 | assert!(model.organizer && model.can_admit() && model.organizes_meeting()); |
| 1952 | let mut owner = CallModel::meeting("Standup", true); |
| 1953 | owner.apply(CallUpdate::State(CallState::Connected { |
| 1954 | since: Instant::now(), |
| 1955 | })); |
| 1956 | assert!(owner.can_admit()); |
| 1957 | owner.apply(CallUpdate::Lobby(true)); |
| 1958 | assert!(!owner.can_admit()); |
| 1959 | } |
| 1960 | |
| 1961 | #[test] |
| 1962 | fn only_a_new_lobby_guest_counts_as_an_arrival_for_an_admitting_organizer() { |
| 1963 | let mut owner = CallModel::meeting("Standup", true); |
| 1964 | owner.apply(CallUpdate::State(CallState::Connected { |
| 1965 | since: Instant::now(), |
| 1966 | })); |
| 1967 | let empty = owner.lobby_mris(); |
| 1968 | owner.apply(CallUpdate::Roster(vec![entry("8:orgid:g1", "Gast Eins", false, true)])); |
| 1969 | assert!(owner.someone_entered_the_lobby(&empty)); |
| 1970 | let waiting = owner.lobby_mris(); |
| 1971 | assert!(!owner.someone_entered_the_lobby(&waiting)); |
| 1972 | let mut guest = meeting_with_me(); |
| 1973 | guest.apply(CallUpdate::Roster(vec![entry("8:orgid:g1", "Gast Eins", false, true)])); |
| 1974 | assert!(!guest.someone_entered_the_lobby(&empty)); |
| 1975 | } |
| 1976 | |
| 1977 | #[test] |
| 1978 | fn a_spotlight_takes_the_stage_and_the_lowest_rank_wins() { |
| 1979 | let mut model = meeting_with_me(); |
| 1980 | model.apply(CallUpdate::Roster(vec![ |
| 1981 | entry("8:orgid:a", "Ana", false, false), |
| 1982 | spotlit("8:orgid:b", "Bo", 4), |
| 1983 | spotlit("8:orgid:c", "Cy", 2), |
| 1984 | ])); |
| 1985 | assert_eq!( |
| 1986 | model.focus(), |
| 1987 | Some(Focus { |
| 1988 | mri: "8:orgid:c".into(), |
| 1989 | own: false, |
| 1990 | spotlight: true, |
| 1991 | pinned: false |
| 1992 | }) |
| 1993 | ); |
| 1994 | assert!(model.is_spotlighted("8:orgid:b") && !model.is_spotlighted("8:orgid:a")); |
| 1995 | model.apply(CallUpdate::Roster(vec![ |
| 1996 | entry("8:orgid:a", "Ana", false, false), |
| 1997 | spotlit("8:orgid:b", "Bo", 4), |
| 1998 | entry("8:orgid:c", "Cy", false, false), |
| 1999 | ])); |
| 2000 | assert_eq!( |
| 2001 | model.focus().map(|focus| focus.mri), |
| 2002 | Some("8:orgid:b".to_owned()) |
| 2003 | ); |
| 2004 | model.apply(CallUpdate::Roster(vec![entry( |
| 2005 | "8:orgid:a", |
| 2006 | "Ana", |
| 2007 | false, |
| 2008 | false, |
| 2009 | )])); |
| 2010 | assert_eq!(model.focus(), None); |
| 2011 | } |
| 2012 | |
| 2013 | #[test] |
| 2014 | fn spotlighting_yourself_puts_your_own_tile_on_the_stage() { |
| 2015 | let mut model = meeting_with_me(); |
| 2016 | model.apply(CallUpdate::Roster(vec![ |
| 2017 | entry("8:orgid:a", "Ana", false, false), |
| 2018 | spotlit("8:orgid:me", "Me", 1), |
| 2019 | ])); |
| 2020 | assert_eq!( |
| 2021 | model.focus(), |
| 2022 | Some(Focus { |
| 2023 | mri: "8:orgid:me".into(), |
| 2024 | own: true, |
| 2025 | spotlight: true, |
| 2026 | pinned: false |
| 2027 | }) |
| 2028 | ); |
| 2029 | } |
| 2030 | |
| 2031 | #[test] |
| 2032 | fn a_pin_is_local_beats_the_spotlight_and_ends_when_the_person_leaves() { |
| 2033 | let mut model = meeting_with_me(); |
| 2034 | model.apply(CallUpdate::State(CallState::Connected { |
| 2035 | since: Instant::now(), |
| 2036 | })); |
| 2037 | model.apply(CallUpdate::Roster(vec![ |
| 2038 | entry("8:orgid:a", "Ana", false, false), |
| 2039 | spotlit("8:orgid:b", "Bo", 1), |
| 2040 | ])); |
| 2041 | model.toggle_pin("8:orgid:a"); |
| 2042 | assert_eq!( |
| 2043 | model.focus(), |
| 2044 | Some(Focus { |
| 2045 | mri: "8:orgid:a".into(), |
| 2046 | own: false, |
| 2047 | spotlight: false, |
| 2048 | pinned: true |
| 2049 | }) |
| 2050 | ); |
| 2051 | let names: Vec<&str> = model |
| 2052 | .strip_tiles() |
| 2053 | .iter() |
| 2054 | .map(|tile| tile.name.as_str()) |
| 2055 | .collect(); |
| 2056 | assert_eq!(names[0], "Ana"); |
| 2057 | model.toggle_pin("8:orgid:b"); |
| 2058 | assert_eq!( |
| 2059 | model |
| 2060 | .focus() |
| 2061 | .map(|focus| (focus.mri, focus.spotlight, focus.pinned)), |
| 2062 | Some(("8:orgid:b".to_owned(), true, true)) |
| 2063 | ); |
| 2064 | model.toggle_pin("8:orgid:b"); |
| 2065 | assert_eq!(model.pinned, None); |
| 2066 | model.toggle_pin("8:orgid:a"); |
| 2067 | model.apply(CallUpdate::Roster(vec![spotlit("8:orgid:b", "Bo", 1)])); |
| 2068 | assert_eq!(model.pinned, None); |
| 2069 | } |
| 2070 | |
| 2071 | fn caption(id: &str, speaker: &str, text: &str, is_final: bool) -> CaptionEntry { |
| 2072 | CaptionEntry { |
| 2073 | id: id.into(), |
| 2074 | user_id: "u".into(), |
| 2075 | display_name: speaker.into(), |
| 2076 | text: text.into(), |
| 2077 | is_final, |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | #[test] |
| 2082 | fn captions_keep_two_lines_update_partials_and_fade_after_four_seconds() { |
| 2083 | let mut model = meeting_with_me(); |
| 2084 | let start = Instant::now(); |
| 2085 | model.note_caption(caption("1", "Ana", "ignored while off", false), start); |
| 2086 | assert!(model.caption_lines.is_empty()); |
| 2087 | model.apply(CallUpdate::Captions(CaptionState::On)); |
| 2088 | model.note_caption(caption("1", "Ana", "Hello", false), start); |
| 2089 | model.note_caption(caption("1", "Ana", "Hello there", true), start); |
| 2090 | assert_eq!(model.caption_lines.len(), 1); |
| 2091 | assert_eq!(model.caption_lines[0].text, "Hello there"); |
| 2092 | model.note_caption( |
| 2093 | caption("2", "", "Next", false), |
| 2094 | start + Duration::from_secs(1), |
| 2095 | ); |
| 2096 | model.note_caption( |
| 2097 | caption("3", "Bo", "Third", false), |
| 2098 | start + Duration::from_secs(2), |
| 2099 | ); |
| 2100 | let texts: Vec<&str> = model |
| 2101 | .caption_lines |
| 2102 | .iter() |
| 2103 | .map(|line| line.text.as_str()) |
| 2104 | .collect(); |
| 2105 | assert_eq!(texts, vec!["Next", "Third"]); |
| 2106 | assert_eq!(model.caption_lines[0].speaker, "Someone"); |
| 2107 | assert_eq!( |
| 2108 | model.visible_captions(start + Duration::from_secs(3)).len(), |
| 2109 | 2 |
| 2110 | ); |
| 2111 | assert_eq!( |
| 2112 | model |
| 2113 | .visible_captions(start + Duration::from_millis(5500)) |
| 2114 | .len(), |
| 2115 | 1 |
| 2116 | ); |
| 2117 | assert!( |
| 2118 | model |
| 2119 | .visible_captions(start + Duration::from_secs(7)) |
| 2120 | .is_empty() |
| 2121 | ); |
| 2122 | model.apply(CallUpdate::Captions(CaptionState::Off)); |
| 2123 | assert!(model.caption_lines.is_empty() && !model.captions_on()); |
| 2124 | } |
| 2125 | |
| 2126 | fn one_to_one() -> CallModel { |
| 2127 | let mut model = CallModel::people("Bea", &[("8:orgid:bea".into(), "Bea".into())]); |
| 2128 | model.apply(CallUpdate::State(CallState::Connected { |
| 2129 | since: Instant::now(), |
| 2130 | })); |
| 2131 | model |
| 2132 | } |
| 2133 | |
| 2134 | #[test] |
| 2135 | fn hold_and_transfer_belong_to_one_to_one_calls_and_follow_the_hold_state() { |
| 2136 | let mut model = one_to_one(); |
| 2137 | assert!(model.can_hold() && model.can_transfer()); |
| 2138 | model.apply(CallUpdate::Hold(HoldState::Local)); |
| 2139 | assert!(model.can_hold() && !model.can_transfer()); |
| 2140 | model.apply(CallUpdate::Hold(HoldState::Remote)); |
| 2141 | assert!(!model.can_hold() && !model.can_transfer()); |
| 2142 | let meeting = meeting_with_me(); |
| 2143 | assert!(!meeting.can_hold() && !meeting.can_transfer()); |
| 2144 | assert!( |
| 2145 | !CallModel::people( |
| 2146 | "Group", |
| 2147 | &[ |
| 2148 | ("8:orgid:a".into(), "A".into()), |
| 2149 | ("8:orgid:b".into(), "B".into()) |
| 2150 | ] |
| 2151 | ) |
| 2152 | .can_hold() |
| 2153 | ); |
| 2154 | } |
| 2155 | |
| 2156 | #[test] |
| 2157 | fn the_transcript_keeps_every_caption_with_its_offset_while_the_overlay_keeps_two() { |
| 2158 | let mut model = meeting_with_me(); |
| 2159 | let start = Instant::now(); |
| 2160 | model.apply(CallUpdate::State(CallState::Connected { since: start })); |
| 2161 | model.apply(CallUpdate::Captions(CaptionState::On)); |
| 2162 | model.note_caption(caption("1", "Ana Example", "One", true), start + Duration::from_secs(5)); |
| 2163 | model.note_caption(caption("2", "Bo Sample", "Two", true), start + Duration::from_secs(65)); |
| 2164 | model.note_caption(caption("3", "Ana Example", "Three", true), start + Duration::from_secs(70)); |
| 2165 | assert_eq!(model.caption_lines.len(), 2); |
| 2166 | assert_eq!(model.transcript.filtered("").len(), 3); |
| 2167 | assert_eq!(model.transcript.filtered("")[1].offset, Duration::from_secs(65)); |
| 2168 | model.apply(CallUpdate::Captions(CaptionState::Off)); |
| 2169 | model.note_caption(caption("4", "Bo Sample", "Ignored", true), start + Duration::from_secs(80)); |
| 2170 | assert!(model.caption_lines.is_empty()); |
| 2171 | assert_eq!(model.transcript.filtered("").len(), 3); |
| 2172 | } |
| 2173 | |
| 2174 | #[test] |
| 2175 | fn a_chosen_spoken_language_is_handed_out_once_the_captions_are_on() { |
| 2176 | let mut model = meeting_with_me(); |
| 2177 | model.choose_caption_language("de-DE"); |
| 2178 | model.apply(CallUpdate::Captions(CaptionState::Starting)); |
| 2179 | assert_eq!(model.take_ready_caption_language(), None); |
| 2180 | model.apply(CallUpdate::Captions(CaptionState::On)); |
| 2181 | assert_eq!(model.take_ready_caption_language().as_deref(), Some("de-DE")); |
| 2182 | assert_eq!(model.take_ready_caption_language(), None); |
| 2183 | assert_eq!(model.caption_language.as_deref(), Some("de-DE")); |
| 2184 | } |
| 2185 | |
| 2186 | #[test] |
| 2187 | fn audio_only_and_transcription_follow_their_own_updates() { |
| 2188 | let mut model = meeting_with_me(); |
| 2189 | model.apply(CallUpdate::Transcribing(true)); |
| 2190 | assert!(model.transcribing && !model.recording); |
| 2191 | model.apply(CallUpdate::Recording(true)); |
| 2192 | model.apply(CallUpdate::RecordingAudioOnly(true)); |
| 2193 | assert!(model.recording && model.recording_audio_only); |
| 2194 | model.apply(CallUpdate::Transcribing(false)); |
| 2195 | assert!(!model.transcribing && model.recording); |
| 2196 | } |
| 2197 | |
| 2198 | #[test] |
| 2199 | fn recording_and_consent_follow_the_call_updates() { |
| 2200 | let mut model = meeting_with_me(); |
| 2201 | assert!(!model.can_record()); |
| 2202 | model.apply(CallUpdate::State(CallState::Connected { |
| 2203 | since: Instant::now(), |
| 2204 | })); |
| 2205 | assert!(model.can_record()); |
| 2206 | model.apply(CallUpdate::Recording(true)); |
| 2207 | model.apply(CallUpdate::ConsentRequired(true)); |
| 2208 | assert!(model.recording && model.consent_required); |
| 2209 | model.apply(CallUpdate::ConsentRequired(false)); |
| 2210 | assert!(model.recording && !model.consent_required); |
| 2211 | model.apply(CallUpdate::Recording(false)); |
| 2212 | assert!(!model.recording); |
| 2213 | } |
| 2214 | |
| 2215 | #[test] |
| 2216 | fn a_whiteboard_share_names_the_presenter_and_offers_the_browser_only_with_a_link() { |
| 2217 | let mut model = meeting_with_me(); |
| 2218 | model.apply(CallUpdate::Roster(vec![entry( |
| 2219 | "8:orgid:ana", |
| 2220 | "Ana", |
| 2221 | false, |
| 2222 | false, |
| 2223 | )])); |
| 2224 | assert_eq!(model.whiteboard_label(), None); |
| 2225 | let share = |presenter: &str, url: Option<&str>| calling::ContentShare { |
| 2226 | session_id: "s".into(), |
| 2227 | presenter: Some(presenter.into()), |
| 2228 | subject: "Whiteboard".into(), |
| 2229 | url: url.map(str::to_owned), |
| 2230 | whiteboard: true, |
| 2231 | }; |
| 2232 | model.apply(CallUpdate::Whiteboard(Some(share( |
| 2233 | "8:orgid:ana", |
| 2234 | Some("https://app.whiteboard.microsoft.com/x"), |
| 2235 | )))); |
| 2236 | assert_eq!( |
| 2237 | model.whiteboard_label().as_deref(), |
| 2238 | Some("Ana is sharing a whiteboard") |
| 2239 | ); |
| 2240 | assert_eq!( |
| 2241 | model.whiteboard_url(), |
| 2242 | Some("https://app.whiteboard.microsoft.com/x") |
| 2243 | ); |
| 2244 | model.apply(CallUpdate::Whiteboard(Some(share( |
| 2245 | "8:orgid:stranger", |
| 2246 | None, |
| 2247 | )))); |
| 2248 | assert_eq!( |
| 2249 | model.whiteboard_label().as_deref(), |
| 2250 | Some("Someone is sharing a whiteboard") |
| 2251 | ); |
| 2252 | assert_eq!(model.whiteboard_url(), None); |
| 2253 | model.apply(CallUpdate::Whiteboard(None)); |
| 2254 | assert_eq!(model.whiteboard_label(), None); |
| 2255 | } |
| 2256 | |
| 2257 | #[test] |
| 2258 | fn a_consultation_can_transfer_once_connected_with_a_replacement_link() { |
| 2259 | let mut model = CallModel::people("Ana", &[("8:orgid:ana".into(), "Ana".into())]); |
| 2260 | let mut consult = ConsultCall::new(("8:orgid:ana".into(), "Ana".into()), "Chris".into()); |
| 2261 | assert_eq!(consult.banner_text(), "Chris is on hold"); |
| 2262 | consult.replacement = Some(Replacement::of_call("https://cc/replacement")); |
| 2263 | assert!(!consult.ready_to_transfer(&model)); |
| 2264 | model.apply(CallUpdate::State(CallState::Connected { |
| 2265 | since: Instant::now(), |
| 2266 | })); |
| 2267 | assert!(consult.ready_to_transfer(&model)); |
| 2268 | consult.transfer = ConsultTransfer::Pending; |
| 2269 | assert!(!consult.ready_to_transfer(&model)); |
| 2270 | assert_eq!(consult.banner_text(), "Transferring Chris to Ana..."); |
| 2271 | } |
| 2272 | } |