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