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