| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | use app_base::ui_locale::localize_notice; |
| 4 | use calling::meeting::LiveMeeting; |
| 5 | use calling::{ |
| 6 | AcceptMode, CallCommand, CallHandle, CallSpec, CallState, CallUpdate, Callee, CameraEffects, |
| 7 | DeviceChoice, EndReason, EngineEvent, MicSettings, MuteCommand, Reaction, RingSignal, |
| 8 | ShareKind, ShareSource, VideoKey, |
| 9 | }; |
| 10 | use gpui_kit::*; |
| 11 | |
| 12 | use super::background::{ |
| 13 | BACKGROUND_META_KEY, BackgroundPick, SHOWN_IMAGES, is_image_file, save_custom_backgrounds, |
| 14 | }; |
| 15 | use super::demo::{ |
| 16 | DemoCall, DemoScene, demo_caller, demo_guests, demo_ring, demo_video_ring, start_demo_call, |
| 17 | start_demo_scene_call, |
| 18 | }; |
| 19 | use super::end_screen::{CallEndScreen, end_tone}; |
| 20 | use super::features::{TransferCandidate, room_closed_move}; |
| 21 | use super::media::CallMedia; |
| 22 | use super::mic::save_mic; |
| 23 | use super::model::{ActiveCall, CallKind, CallModel, ConsultTransfer, ended_notice}; |
| 24 | use super::prejoin::{DemoMeeting, MeetingJoin}; |
| 25 | use super::report::{ |
| 26 | CallRecorder, OwnIdentity, SETTING_META_KEY, SharedRecorder, report_directory, save_report, |
| 27 | shared, |
| 28 | }; |
| 29 | use super::ring::{MissedCall, RingOutcome}; |
| 30 | use super::target::{is_echo_chat, is_organizer, plan_for_chat}; |
| 31 | use crate::app_state::{AppEvent, AppState, Selection, chat_title, selection_for}; |
| 32 | use crate::notice::NoticeAction; |
| 33 | |
| 34 | const LOBBY_TONE_THROTTLE: Duration = Duration::from_secs(30); |
| 35 | const TIMER_REFRESH: Duration = Duration::from_millis(500); |
| 36 | const SHARE_SOURCES_EVERY_TICKS: u32 = 10; |
| 37 | pub(super) const NOT_CONNECTED_NOTICE: &str = "Not connected to Teams yet"; |
| 38 | const READ_ONLY_NOTICE: &str = "Calls are off in read-only mode"; |
| 39 | const NOT_CALLABLE_NOTICE: &str = "This chat cannot be called"; |
| 40 | pub(super) const MEETING_UNAVAILABLE_NOTICE: &str = "This meeting cannot be joined"; |
| 41 | const ANSWER_FAILED_NOTICE: &str = "Could not answer the call"; |
| 42 | const CONSULT_ENDED_NOTICE: &str = "The consultation ended"; |
| 43 | const DEMO_CONSULT_TARGET: (&str, &str) = ("8:orgid:demo-guest-1", "Jonas Ortega"); |
| 44 | const MEETING_KIND: &str = "meeting"; |
| 45 | const DEMO_LOBBY_MEETING: &str = "demo-chat-meeting-retro"; |
| 46 | const DEMO_CROWD: usize = 11; |
| 47 | const DEMO_SMALL_MEETING: usize = 4; |
| 48 | const DEMO_LIVE_MINUTES: i64 = 60; |
| 49 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 50 | pub const SHARE_SOUND_META_KEY: &str = "call_share_sound"; |
| 51 | const CAMERA_META_KEY: &str = "call_camera"; |
| 52 | const VIDEO_QUALITY_META_KEY: &str = "call_video_quality"; |
| 53 | const INPUT_META_KEY: &str = "call_input"; |
| 54 | pub fn apply_sound_output(choice: &DeviceChoice) { |
| 55 | let device_key = match choice { |
| 56 | DeviceChoice::SystemDefault => None, |
| 57 | DeviceChoice::Device(device_key) => Some(device_key.clone()), |
| 58 | }; |
| 59 | app_base::notify::platform::set_sound_output(device_key); |
| 60 | } |
| 61 | |
| 62 | const OUTPUT_META_KEY: &str = "call_output"; |
| 63 | const EFFECTS_META_KEY: &str = "call_camera_effects"; |
| 64 | const DEFAULT_DEVICE_META: &str = "default"; |
| 65 | const REPORT_SAVED_NOTICE: &str = "Test report saved"; |
| 66 | |
| 67 | pub fn load_share_sound(store: &store::Store) -> bool { |
| 68 | store.meta(SHARE_SOUND_META_KEY).ok().flatten().as_deref() == Some("1") |
| 69 | } |
| 70 | |
| 71 | pub fn load_camera(store: &store::Store) -> DeviceChoice { |
| 72 | load_device(store, CAMERA_META_KEY) |
| 73 | } |
| 74 | |
| 75 | pub fn load_input(store: &store::Store) -> DeviceChoice { |
| 76 | load_device(store, INPUT_META_KEY) |
| 77 | } |
| 78 | |
| 79 | pub fn load_output(store: &store::Store) -> DeviceChoice { |
| 80 | load_device(store, OUTPUT_META_KEY) |
| 81 | } |
| 82 | |
| 83 | fn load_device(store: &store::Store, key: &str) -> DeviceChoice { |
| 84 | match store.meta(key).ok().flatten() { |
| 85 | Some(key) if !key.is_empty() && key != DEFAULT_DEVICE_META => DeviceChoice::Device(key), |
| 86 | _ => DeviceChoice::SystemDefault, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn load_camera_effects(store: &store::Store) -> CameraEffects { |
| 91 | store |
| 92 | .meta(EFFECTS_META_KEY) |
| 93 | .ok() |
| 94 | .flatten() |
| 95 | .map_or_else(CameraEffects::default, |value| { |
| 96 | CameraEffects::from_meta(&value) |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | pub fn load_video_quality(store: &store::Store) -> calling::VideoQuality { |
| 101 | store |
| 102 | .meta(VIDEO_QUALITY_META_KEY) |
| 103 | .ok() |
| 104 | .flatten() |
| 105 | .as_deref() |
| 106 | .map_or_else( |
| 107 | calling::VideoQuality::default, |
| 108 | calling::VideoQuality::from_meta, |
| 109 | ) |
| 110 | } |
| 111 | |
| 112 | pub fn save_video_quality(store: &store::Store, quality: calling::VideoQuality) { |
| 113 | let _ = store.set_meta(VIDEO_QUALITY_META_KEY, quality.to_meta()); |
| 114 | } |
| 115 | |
| 116 | fn device_meta(choice: &DeviceChoice) -> &str { |
| 117 | match choice { |
| 118 | DeviceChoice::SystemDefault => DEFAULT_DEVICE_META, |
| 119 | DeviceChoice::Device(key) => key, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | fn demo_consult_target() -> TransferCandidate { |
| 124 | TransferCandidate { |
| 125 | mri: DEMO_CONSULT_TARGET.0.to_owned(), |
| 126 | name: DEMO_CONSULT_TARGET.1.to_owned(), |
| 127 | user_id: None, |
| 128 | chat_id: "demo-chat-jonas".to_owned(), |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn now_unix() -> i64 { |
| 133 | chrono::Utc::now().timestamp() |
| 134 | } |
| 135 | |
| 136 | impl AppState { |
| 137 | pub fn start_test_call(&mut self, cx: &mut Context<Self>) { |
| 138 | if self.call.is_some() { |
| 139 | self.show_call(true, cx); |
| 140 | return; |
| 141 | } |
| 142 | let Some(handle) = self.place_call(CallSpec::Echo, DemoCall::Test, false, cx) else { |
| 143 | return; |
| 144 | }; |
| 145 | self.begin_call(handle, CallModel::test(), None, cx); |
| 146 | } |
| 147 | |
| 148 | pub fn start_chat_call(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 149 | self.start_chat_call_with(conversation_id, false, cx); |
| 150 | } |
| 151 | |
| 152 | pub fn start_chat_video_call(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 153 | self.start_chat_call_with(conversation_id, true, cx); |
| 154 | } |
| 155 | |
| 156 | fn start_chat_call_with( |
| 157 | &mut self, |
| 158 | conversation_id: &str, |
| 159 | with_video: bool, |
| 160 | cx: &mut Context<Self>, |
| 161 | ) { |
| 162 | if self.call.is_some() { |
| 163 | self.show_call(true, cx); |
| 164 | return; |
| 165 | } |
| 166 | let chat = self |
| 167 | .sidebar |
| 168 | .chats |
| 169 | .iter() |
| 170 | .find(|chat| chat.id == conversation_id); |
| 171 | if chat.is_some_and(is_echo_chat) { |
| 172 | self.start_test_call(cx); |
| 173 | return; |
| 174 | } |
| 175 | let chat = self |
| 176 | .sidebar |
| 177 | .chats |
| 178 | .iter() |
| 179 | .find(|chat| chat.id == conversation_id); |
| 180 | let plan = chat.and_then(|chat| plan_for_chat(chat, self.directory.me.as_ref())); |
| 181 | let Some(plan) = plan else { |
| 182 | self.raise_notice(NOT_CALLABLE_NOTICE.to_owned(), None, cx); |
| 183 | return; |
| 184 | }; |
| 185 | let spec = CallSpec::People { |
| 186 | callees: plan |
| 187 | .callees |
| 188 | .iter() |
| 189 | .map(|(mri, name)| Callee { |
| 190 | mri: mri.clone(), |
| 191 | display_name: name.clone(), |
| 192 | }) |
| 193 | .collect(), |
| 194 | thread_id: conversation_id.to_owned(), |
| 195 | }; |
| 196 | let Some(handle) = |
| 197 | self.place_call(spec, DemoCall::People(plan.callees.clone()), with_video, cx) |
| 198 | else { |
| 199 | return; |
| 200 | }; |
| 201 | let user_ids = plan |
| 202 | .callees |
| 203 | .iter() |
| 204 | .filter_map(|(mri, _)| mri.strip_prefix(ORGID_PREFIX).map(str::to_owned)) |
| 205 | .collect(); |
| 206 | self.request_avatars(user_ids, cx); |
| 207 | let model = CallModel::people(&plan.title, &plan.callees); |
| 208 | self.begin_call(handle, model, Some(conversation_id.to_owned()), cx); |
| 209 | if with_video && self.mode.demo { |
| 210 | self.send_call_command(CallCommand::SetCamera(true)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | pub fn join_meeting_call(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 215 | if self.call.is_some() { |
| 216 | self.show_call(true, cx); |
| 217 | return; |
| 218 | } |
| 219 | let Some(meeting) = self.running_meeting(conversation_id).cloned() else { |
| 220 | self.raise_notice(MEETING_UNAVAILABLE_NOTICE.to_owned(), None, cx); |
| 221 | return; |
| 222 | }; |
| 223 | let Some(target) = meeting.target() else { |
| 224 | self.raise_notice(MEETING_UNAVAILABLE_NOTICE.to_owned(), None, cx); |
| 225 | return; |
| 226 | }; |
| 227 | let demo = DemoMeeting { |
| 228 | guests: if conversation_id == DEMO_LOBBY_MEETING { |
| 229 | DEMO_SMALL_MEETING |
| 230 | } else { |
| 231 | DEMO_CROWD |
| 232 | }, |
| 233 | lobby: conversation_id == DEMO_LOBBY_MEETING, |
| 234 | organizer: conversation_id != DEMO_LOBBY_MEETING, |
| 235 | }; |
| 236 | let title = selection_for(&self.sidebar, conversation_id) |
| 237 | .map(|selection| crate::app_state::selection_title(&self.sidebar, &selection)) |
| 238 | .unwrap_or_else(|| "Meeting".to_owned()); |
| 239 | let join = MeetingJoin { |
| 240 | organizer: is_organizer(&meeting, self.directory.me.as_ref()), |
| 241 | conversation_id: Some(conversation_id.to_owned()), |
| 242 | target, |
| 243 | title, |
| 244 | demo, |
| 245 | }; |
| 246 | self.stage_meeting_join(join, cx); |
| 247 | } |
| 248 | |
| 249 | pub(super) fn place_call( |
| 250 | &mut self, |
| 251 | spec: CallSpec, |
| 252 | demo: DemoCall, |
| 253 | with_video: bool, |
| 254 | cx: &mut Context<Self>, |
| 255 | ) -> Option<CallHandle> { |
| 256 | if self.mode.demo { |
| 257 | return Some(start_demo_call(demo)); |
| 258 | } |
| 259 | if self.mode.read_only { |
| 260 | self.raise_notice(READ_ONLY_NOTICE.to_owned(), None, cx); |
| 261 | return None; |
| 262 | } |
| 263 | match self.call_launcher.clone() { |
| 264 | Some(launcher) if with_video => Some(launcher.start_video_call(spec)), |
| 265 | Some(launcher) => Some(launcher.start_call(spec)), |
| 266 | None => { |
| 267 | self.raise_notice(NOT_CONNECTED_NOTICE.to_owned(), None, cx); |
| 268 | None |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | pub(super) fn begin_call( |
| 274 | &mut self, |
| 275 | handle: CallHandle, |
| 276 | model: CallModel, |
| 277 | conversation_id: Option<String>, |
| 278 | cx: &mut Context<Self>, |
| 279 | ) { |
| 280 | self.call_count += 1; |
| 281 | let call_id = self.call_count; |
| 282 | self.call_end = None; |
| 283 | self.prejoin = None; |
| 284 | let CallHandle { |
| 285 | commands, |
| 286 | updates, |
| 287 | video, |
| 288 | } = handle; |
| 289 | self.call = Some(ActiveCall::new( |
| 290 | call_id, |
| 291 | model, |
| 292 | commands, |
| 293 | conversation_id, |
| 294 | video, |
| 295 | )); |
| 296 | if let Some(call) = self.call.as_ref() { |
| 297 | crate::diagnostics::record(format!( |
| 298 | "calls: start kind={:?} incoming={} peer_chars={}", |
| 299 | call.model.kind, |
| 300 | call.model.incoming, |
| 301 | call.model.peer_name.chars().count() |
| 302 | )); |
| 303 | } |
| 304 | if self.call_share_sound { |
| 305 | self.send_call_command(CallCommand::SetShareSound(true)); |
| 306 | } |
| 307 | if self.call_share_motion { |
| 308 | self.send_call_command(CallCommand::SetShareMotion(true)); |
| 309 | } |
| 310 | if self.call_share_standout { |
| 311 | self.send_call_command(CallCommand::SetShareStandout(true)); |
| 312 | } |
| 313 | if self.call_mic != MicSettings::default() { |
| 314 | self.send_call_command(CallCommand::SetMicSettings(self.call_mic)); |
| 315 | } |
| 316 | self.send_call_command(CallCommand::SetVideoQuality(self.call_video_quality)); |
| 317 | if let Some(call) = self.call.as_mut() { |
| 318 | call.model.background = self.call_background.clone(); |
| 319 | call.model.camera = self.call_camera.clone(); |
| 320 | call.model.view = self.call_view; |
| 321 | call.model.refresh_layout(Instant::now()); |
| 322 | } |
| 323 | if self.call_camera != DeviceChoice::SystemDefault { |
| 324 | self.send_call_command(CallCommand::SelectCamera(self.call_camera.clone())); |
| 325 | } |
| 326 | if self.call_input != DeviceChoice::SystemDefault { |
| 327 | self.send_call_command(CallCommand::SelectInput(self.call_input.clone())); |
| 328 | } |
| 329 | if self.call_output != DeviceChoice::SystemDefault { |
| 330 | self.send_call_command(CallCommand::SelectOutput(self.call_output.clone())); |
| 331 | } |
| 332 | if self.call_background != BackgroundPick::None { |
| 333 | self.send_background_command(cx); |
| 334 | } |
| 335 | self.send_call_command(CallCommand::SetCameraEffects(self.call_effects)); |
| 336 | self.refresh_call_backgrounds(cx); |
| 337 | self.new_chat = false; |
| 338 | let report = self.start_call_report(); |
| 339 | if let Some(call) = self.call.as_mut() { |
| 340 | call.report.clone_from(&report); |
| 341 | } |
| 342 | let rights_conversation = self |
| 343 | .call |
| 344 | .as_ref() |
| 345 | .and_then(|call| call.conversation_id.clone()); |
| 346 | self.apply_recording_rights(rights_conversation.as_deref(), cx); |
| 347 | self.follow_call(call_id, updates, report, cx); |
| 348 | cx.emit(AppEvent::Call); |
| 349 | cx.notify(); |
| 350 | } |
| 351 | |
| 352 | fn start_call_report(&self) -> Option<SharedRecorder> { |
| 353 | let call = self.call.as_ref().filter(|_| self.call_report_enabled)?; |
| 354 | let me = self.directory.me.as_ref(); |
| 355 | let own = OwnIdentity { |
| 356 | mri: me.map_or_else(String::new, |me| format!("{ORGID_PREFIX}{}", me.user_id)), |
| 357 | display_name: me.map_or_else(String::new, |me| me.display_name.clone()), |
| 358 | email: self.own_email.clone(), |
| 359 | }; |
| 360 | let mut people: Vec<(String, String)> = call |
| 361 | .model |
| 362 | .tiles |
| 363 | .iter() |
| 364 | .map(|tile| (tile.mri.clone(), tile.name.clone())) |
| 365 | .collect(); |
| 366 | if people.is_empty() { |
| 367 | people.push((String::new(), call.model.peer_name.clone())); |
| 368 | } |
| 369 | let recorder = CallRecorder::begin( |
| 370 | call.model.kind, |
| 371 | call.model.incoming, |
| 372 | self.mode.demo, |
| 373 | own, |
| 374 | people, |
| 375 | ); |
| 376 | Some(shared(recorder)) |
| 377 | } |
| 378 | |
| 379 | pub(super) fn note_call_feature(&self, feature: &'static str, detail: String) { |
| 380 | if let Some(report) = self.call.as_ref().and_then(|call| call.report.as_ref()) { |
| 381 | report.borrow_mut().local_feature(feature, detail); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | fn report_saved(&mut self, path: std::path::PathBuf, cx: &mut Context<Self>) { |
| 386 | let action = NoticeAction::new("Open folder", move |_, _| { |
| 387 | crate::downloads::reveal_in_file_manager(&path) |
| 388 | }); |
| 389 | let text = match self |
| 390 | .notice |
| 391 | .as_ref() |
| 392 | .filter(|notice| notice.action.is_none()) |
| 393 | { |
| 394 | Some(ended) => format!( |
| 395 | "{}. {REPORT_SAVED_NOTICE}", |
| 396 | ended.text.trim_end_matches('.') |
| 397 | ), |
| 398 | None => REPORT_SAVED_NOTICE.to_owned(), |
| 399 | }; |
| 400 | self.raise_notice(text, Some(action), cx); |
| 401 | } |
| 402 | |
| 403 | pub fn set_call_report_enabled(&mut self, on: bool, cx: &mut Context<Self>) { |
| 404 | self.call_report_enabled = on; |
| 405 | let _ = self |
| 406 | .store |
| 407 | .set_meta(SETTING_META_KEY, if on { "1" } else { "0" }); |
| 408 | cx.notify(); |
| 409 | } |
| 410 | |
| 411 | fn follow_call( |
| 412 | &mut self, |
| 413 | call_id: u64, |
| 414 | mut updates: tokio::sync::mpsc::UnboundedReceiver<CallUpdate>, |
| 415 | report: Option<SharedRecorder>, |
| 416 | cx: &mut Context<Self>, |
| 417 | ) { |
| 418 | cx.spawn(async move |this, cx| { |
| 419 | while let Some(update) = updates.recv().await { |
| 420 | if let Some(report) = &report { |
| 421 | report.borrow_mut().observe(&update); |
| 422 | } |
| 423 | let alive = |
| 424 | this.update(cx, |state, cx| state.apply_call_update(call_id, update, cx)); |
| 425 | if alive.is_err() { |
| 426 | return; |
| 427 | } |
| 428 | } |
| 429 | let _ = this.update(cx, |state, cx| state.end_orphaned_call(call_id, cx)); |
| 430 | let Some(report) = report else { |
| 431 | return; |
| 432 | }; |
| 433 | let reason = EndReason::Failed("The call stopped unexpectedly".to_owned()); |
| 434 | report |
| 435 | .borrow_mut() |
| 436 | .observe(&CallUpdate::State(CallState::Ended { reason })); |
| 437 | let recorder = report.borrow().clone(); |
| 438 | let saved = cx |
| 439 | .background_executor() |
| 440 | .spawn(async move { save_report(&report_directory(), &recorder.document()) }) |
| 441 | .await; |
| 442 | match saved { |
| 443 | Ok(saved) => { |
| 444 | let _ = this.update(cx, |state, cx| state.report_saved(saved.json, cx)); |
| 445 | } |
| 446 | Err(error) => log::warn!("call test report not saved: {error}"), |
| 447 | } |
| 448 | }) |
| 449 | .detach(); |
| 450 | cx.spawn(async move |this, cx| { |
| 451 | let mut ticks = 0u32; |
| 452 | loop { |
| 453 | cx.background_executor().timer(TIMER_REFRESH).await; |
| 454 | ticks += 1; |
| 455 | let running = this.update(cx, |state, cx| { |
| 456 | let visible = state.call.as_ref().is_some_and(|call| call.id == call_id); |
| 457 | if visible { |
| 458 | if ticks.is_multiple_of(SHARE_SOURCES_EVERY_TICKS) { |
| 459 | state.refresh_share_sources(); |
| 460 | } |
| 461 | state.tick_call_layout(cx); |
| 462 | CallMedia::changed(&state.call_media, cx); |
| 463 | } |
| 464 | visible || state.is_held_call(call_id) |
| 465 | }); |
| 466 | if !running.unwrap_or(false) { |
| 467 | return; |
| 468 | } |
| 469 | } |
| 470 | }) |
| 471 | .detach(); |
| 472 | } |
| 473 | |
| 474 | fn end_orphaned_call(&mut self, call_id: u64, cx: &mut Context<Self>) { |
| 475 | let still_open = self |
| 476 | .call |
| 477 | .iter() |
| 478 | .chain(&self.held_call) |
| 479 | .any(|call| call.id == call_id && call.model.is_active()); |
| 480 | if still_open { |
| 481 | let reason = EndReason::Failed("The call stopped unexpectedly".to_owned()); |
| 482 | self.apply_call_update(call_id, CallUpdate::State(CallState::Ended { reason }), cx); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | fn with_known_names(&self, update: CallUpdate) -> CallUpdate { |
| 487 | let mut entries = match update { |
| 488 | CallUpdate::Roster(entries) => entries, |
| 489 | other => return other, |
| 490 | }; |
| 491 | let user_id = |
| 492 | |entry: &calling::RosterEntry| entry.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned); |
| 493 | let missing: Vec<String> = entries |
| 494 | .iter() |
| 495 | .filter(|entry| entry.display_name.is_empty()) |
| 496 | .filter_map(user_id) |
| 497 | .collect(); |
| 498 | if !missing.is_empty() { |
| 499 | let names = crate::people::resolve_names(self, &missing); |
| 500 | for entry in entries |
| 501 | .iter_mut() |
| 502 | .filter(|entry| entry.display_name.is_empty()) |
| 503 | { |
| 504 | if let Some(name) = user_id(entry).and_then(|id| names.get(&id)) { |
| 505 | entry.display_name = name.clone(); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | CallUpdate::Roster(entries) |
| 510 | } |
| 511 | |
| 512 | pub(super) fn apply_call_update( |
| 513 | &mut self, |
| 514 | call_id: u64, |
| 515 | update: CallUpdate, |
| 516 | cx: &mut Context<Self>, |
| 517 | ) { |
| 518 | let update = self.with_known_names(update); |
| 519 | if self.is_held_call(call_id) { |
| 520 | self.apply_held_call_update(update, cx); |
| 521 | return; |
| 522 | } |
| 523 | let media = self.call_media.clone(); |
| 524 | let Some(call) = self.call.as_mut().filter(|call| call.id == call_id) else { |
| 525 | return; |
| 526 | }; |
| 527 | match update { |
| 528 | CallUpdate::VideoReady => { |
| 529 | call.pictures.apply(&call.video, cx); |
| 530 | CallMedia::changed(&media, cx); |
| 531 | return; |
| 532 | } |
| 533 | CallUpdate::VideoStopped(key) => { |
| 534 | call.pictures.forget(&key, cx); |
| 535 | CallMedia::changed(&media, cx); |
| 536 | return; |
| 537 | } |
| 538 | CallUpdate::Stats { .. } => return, |
| 539 | CallUpdate::Levels { local, remote } => { |
| 540 | let before = (call.model.local_speaking, call.model.remote_speaking); |
| 541 | call.model.apply(CallUpdate::Levels { local, remote }); |
| 542 | if before != (call.model.local_speaking, call.model.remote_speaking) { |
| 543 | CallMedia::changed(&media, cx); |
| 544 | } |
| 545 | return; |
| 546 | } |
| 547 | CallUpdate::Notice(text) => { |
| 548 | self.raise_notice(localize_notice(&text), None, cx); |
| 549 | return; |
| 550 | } |
| 551 | CallUpdate::WhiteboardUrl(url) => { |
| 552 | app_base::open_link::open_url(cx, &url); |
| 553 | return; |
| 554 | } |
| 555 | CallUpdate::BreakoutMove(moved) => { |
| 556 | self.begin_breakout_move(moved, cx); |
| 557 | return; |
| 558 | } |
| 559 | CallUpdate::MeetingOptions(result) => { |
| 560 | if let Some(dialog) = call.options_dialog.as_mut() { |
| 561 | dialog.loaded(result); |
| 562 | } |
| 563 | cx.notify(); |
| 564 | return; |
| 565 | } |
| 566 | CallUpdate::ReplacementLink(replacement) => { |
| 567 | if let Some(consult) = call.consult.as_mut() { |
| 568 | consult.replacement = Some(replacement); |
| 569 | cx.notify(); |
| 570 | } |
| 571 | return; |
| 572 | } |
| 573 | CallUpdate::MeetingOptionsSaved(result) => { |
| 574 | if call |
| 575 | .options_dialog |
| 576 | .as_mut() |
| 577 | .is_some_and(|dialog| dialog.saved(result)) |
| 578 | { |
| 579 | call.options_dialog = None; |
| 580 | } |
| 581 | cx.notify(); |
| 582 | return; |
| 583 | } |
| 584 | _ => {} |
| 585 | } |
| 586 | if let (CallUpdate::Caption(entry), Some(thread)) = (&update, call.chat_thread()) { |
| 587 | let speaker = if entry.display_name.is_empty() { |
| 588 | "Someone" |
| 589 | } else { |
| 590 | &entry.display_name |
| 591 | }; |
| 592 | crate::agent_api::note_caption(thread, &entry.id, speaker, &entry.text, cx); |
| 593 | } |
| 594 | let sharer_before = call.model.screen_sharer.clone(); |
| 595 | let local_share_before = call.model.local_share.is_some(); |
| 596 | let lobby_before = call.model.lobby_mris(); |
| 597 | call.model.apply(update); |
| 598 | if call.model.someone_entered_the_lobby(&lobby_before) |
| 599 | && call |
| 600 | .lobby_tone_at |
| 601 | .is_none_or(|played_at| played_at.elapsed() >= LOBBY_TONE_THROTTLE) |
| 602 | { |
| 603 | call.lobby_tone_at = Some(Instant::now()); |
| 604 | cx.emit(AppEvent::CallTone( |
| 605 | chatsvc::sounds::CallTone::MeetingReminder, |
| 606 | )); |
| 607 | } |
| 608 | if !local_share_before && call.model.local_share.is_some() && call.chat_thread().is_some() { |
| 609 | call.chat_open = true; |
| 610 | call.transcript_open = false; |
| 611 | call.people_open = false; |
| 612 | self.transcript_panel = None; |
| 613 | } |
| 614 | if call.model.local_share.is_none() { |
| 615 | call.pictures.forget(&VideoKey::LocalScreen, cx); |
| 616 | } |
| 617 | if call.model.screen_sharer != sharer_before { |
| 618 | call.pictures.forget(&VideoKey::Screen, cx); |
| 619 | } |
| 620 | if let Some(language) = call.model.take_ready_caption_language() { |
| 621 | let _ = call |
| 622 | .commands |
| 623 | .send(CallCommand::SetCaptionLanguage(language)); |
| 624 | } |
| 625 | if call.model.screen_sharer.is_none() { |
| 626 | call.stage_fullscreen = false; |
| 627 | call.share_stage.end(); |
| 628 | call.pictures.forget(&VideoKey::Screen, cx); |
| 629 | } |
| 630 | if !call.model.camera_on { |
| 631 | call.pictures.forget(&VideoKey::LocalCamera, cx); |
| 632 | } |
| 633 | let Some(reason) = call.model.ended_reason().cloned() else { |
| 634 | cx.emit(AppEvent::Call); |
| 635 | cx.notify(); |
| 636 | return; |
| 637 | }; |
| 638 | let elapsed = call.model.elapsed(Instant::now()); |
| 639 | crate::diagnostics::record(format!( |
| 640 | "calls: end kind={:?} reason={reason:?} elapsed_ms={}", |
| 641 | call.model.kind, |
| 642 | elapsed.map(|value| value.as_millis()).unwrap_or(0) |
| 643 | )); |
| 644 | let consult = call.consult.take(); |
| 645 | let notice = match &consult { |
| 646 | Some(consult) if consult.transfer == ConsultTransfer::Done => { |
| 647 | format!("Call transferred to {}", consult.target.1) |
| 648 | } |
| 649 | Some(_) if self.held_call.is_some() => CONSULT_ENDED_NOTICE.to_owned(), |
| 650 | _ => ended_notice(&reason, elapsed, &call.model.peer_name), |
| 651 | }; |
| 652 | let back_to_held = consult |
| 653 | .as_ref() |
| 654 | .is_some_and(|consult| consult.transfer != ConsultTransfer::Done); |
| 655 | let next_move = call |
| 656 | .pending_move |
| 657 | .take() |
| 658 | .or_else(|| room_closed_move(call, &reason)); |
| 659 | if let Some(tone) = end_tone(call.model.kind, next_move.is_some()) { |
| 660 | cx.emit(AppEvent::CallTone(tone)); |
| 661 | } |
| 662 | let end_screen = (call.viewing && !back_to_held && next_move.is_none()) |
| 663 | .then(|| CallEndScreen::for_call(call, &reason)) |
| 664 | .flatten(); |
| 665 | call.pictures.clear(cx); |
| 666 | if let Some(thread) = call.chat_thread() { |
| 667 | crate::agent_api::clear_captions(thread, cx); |
| 668 | } |
| 669 | self.call = None; |
| 670 | self.transcript_panel = None; |
| 671 | if back_to_held { |
| 672 | self.call = self.held_call.take(); |
| 673 | } |
| 674 | match (next_move, end_screen) { |
| 675 | (Some(next_move), _) => self.join_moved_call(next_move, cx), |
| 676 | (None, Some(end_screen)) => self.call_end = Some(end_screen), |
| 677 | (None, None) => self.raise_notice(notice, None, cx), |
| 678 | } |
| 679 | self.refresh_call_history_after_call(cx); |
| 680 | cx.emit(AppEvent::Call); |
| 681 | cx.notify(); |
| 682 | } |
| 683 | |
| 684 | pub(super) fn send_call_command(&self, command: CallCommand) { |
| 685 | if let Some(call) = &self.call { |
| 686 | if let Some(report) = &call.report { |
| 687 | report.borrow_mut().command(&command); |
| 688 | } |
| 689 | let _ = call.commands.send(command); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | pub fn toggle_call_mute(&mut self, _cx: &mut Context<Self>) { |
| 694 | let can_toggle = self |
| 695 | .call |
| 696 | .as_ref() |
| 697 | .is_some_and(|call| call.model.can_unmute()); |
| 698 | if can_toggle { |
| 699 | self.send_call_command(CallCommand::Mute(MuteCommand::Toggle)); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | pub fn toggle_call_camera(&mut self, _cx: &mut Context<Self>) { |
| 704 | let Some(call) = self |
| 705 | .call |
| 706 | .as_ref() |
| 707 | .filter(|call| call.model.can_use_camera()) |
| 708 | else { |
| 709 | return; |
| 710 | }; |
| 711 | let on = !call.model.camera_on; |
| 712 | self.send_call_command(CallCommand::SetCamera(on)); |
| 713 | } |
| 714 | |
| 715 | pub fn set_call_mic(&mut self, settings: MicSettings, cx: &mut Context<Self>) { |
| 716 | self.call_mic = settings; |
| 717 | save_mic(&self.store, settings); |
| 718 | if let Some(launcher) = &self.call_launcher { |
| 719 | launcher.set_default_mic(settings); |
| 720 | } |
| 721 | self.send_call_command(CallCommand::SetMicSettings(settings)); |
| 722 | cx.notify(); |
| 723 | } |
| 724 | |
| 725 | pub fn set_call_video_quality( |
| 726 | &mut self, |
| 727 | quality: calling::VideoQuality, |
| 728 | cx: &mut Context<Self>, |
| 729 | ) { |
| 730 | self.call_video_quality = quality; |
| 731 | save_video_quality(&self.store, quality); |
| 732 | self.send_call_command(CallCommand::SetVideoQuality(quality)); |
| 733 | cx.notify(); |
| 734 | } |
| 735 | |
| 736 | pub fn press_push_to_talk(&mut self, _cx: &mut Context<Self>) { |
| 737 | let Some(call) = self.call.as_mut() else { |
| 738 | return; |
| 739 | }; |
| 740 | let command = call |
| 741 | .push_to_talk |
| 742 | .press(call.model.muted, call.model.can_unmute()); |
| 743 | if let Some(command) = command { |
| 744 | self.send_call_command(CallCommand::Mute(command)); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | pub fn release_push_to_talk(&mut self, _cx: &mut Context<Self>) { |
| 749 | let command = self |
| 750 | .call |
| 751 | .as_mut() |
| 752 | .and_then(|call| call.push_to_talk.release()); |
| 753 | if let Some(command) = command { |
| 754 | self.send_call_command(CallCommand::Mute(command)); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | pub fn select_call_camera(&mut self, choice: DeviceChoice, _cx: &mut Context<Self>) { |
| 759 | let _ = self.store.set_meta(CAMERA_META_KEY, device_meta(&choice)); |
| 760 | self.call_camera = choice.clone(); |
| 761 | if let Some(launcher) = &self.call_launcher { |
| 762 | launcher.set_default_camera(choice.clone()); |
| 763 | } |
| 764 | if let Some(call) = self.call.as_mut() { |
| 765 | call.model.camera = choice.clone(); |
| 766 | } |
| 767 | self.send_call_command(CallCommand::SelectCamera(choice)); |
| 768 | } |
| 769 | |
| 770 | pub fn start_call_share(&mut self, source: ShareSource, _cx: &mut Context<Self>) { |
| 771 | self.send_call_command(CallCommand::StartShare(source)); |
| 772 | } |
| 773 | |
| 774 | pub fn start_portal_share(&mut self, kind: ShareKind, _cx: &mut Context<Self>) { |
| 775 | self.send_call_command(CallCommand::StartPortalShare(kind)); |
| 776 | } |
| 777 | |
| 778 | pub fn stop_call_share(&mut self, _cx: &mut Context<Self>) { |
| 779 | self.send_call_command(CallCommand::StopShare); |
| 780 | } |
| 781 | |
| 782 | pub fn refresh_share_sources(&self) { |
| 783 | let idle = self |
| 784 | .call |
| 785 | .as_ref() |
| 786 | .is_some_and(|call| call.model.local_share.is_none()); |
| 787 | if idle { |
| 788 | self.send_call_command(CallCommand::RefreshShareSources); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | pub fn leave_call(&mut self, _cx: &mut Context<Self>) { |
| 793 | self.send_call_command(CallCommand::Hangup); |
| 794 | } |
| 795 | |
| 796 | pub fn end_meeting_for_all(&mut self, _cx: &mut Context<Self>) { |
| 797 | let allowed = self |
| 798 | .call |
| 799 | .as_ref() |
| 800 | .is_some_and(|call| call.model.can_end_meeting); |
| 801 | if allowed { |
| 802 | self.send_call_command(CallCommand::EndMeeting); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | pub fn select_call_input(&mut self, choice: DeviceChoice, _cx: &mut Context<Self>) { |
| 807 | let _ = self.store.set_meta(INPUT_META_KEY, device_meta(&choice)); |
| 808 | self.call_input = choice.clone(); |
| 809 | self.send_call_command(CallCommand::SelectInput(choice)); |
| 810 | } |
| 811 | |
| 812 | pub fn select_call_output(&mut self, choice: DeviceChoice, _cx: &mut Context<Self>) { |
| 813 | let _ = self.store.set_meta(OUTPUT_META_KEY, device_meta(&choice)); |
| 814 | self.call_output = choice.clone(); |
| 815 | apply_sound_output(&choice); |
| 816 | self.send_call_command(CallCommand::SelectOutput(choice)); |
| 817 | } |
| 818 | |
| 819 | pub fn show_call(&mut self, viewing: bool, cx: &mut Context<Self>) { |
| 820 | let Some(call) = self.call.as_mut().filter(|call| call.viewing != viewing) else { |
| 821 | return; |
| 822 | }; |
| 823 | call.viewing = viewing; |
| 824 | if viewing { |
| 825 | self.new_chat = false; |
| 826 | } |
| 827 | cx.emit(AppEvent::Call); |
| 828 | cx.notify(); |
| 829 | } |
| 830 | |
| 831 | pub fn toggle_call_chat(&mut self, cx: &mut Context<Self>) { |
| 832 | let Some(call) = self |
| 833 | .call |
| 834 | .as_mut() |
| 835 | .filter(|call| call.chat_thread().is_some()) |
| 836 | else { |
| 837 | return; |
| 838 | }; |
| 839 | call.chat_open = !call.chat_open; |
| 840 | let chat_state = if call.chat_open { "open" } else { "close" }; |
| 841 | if call.chat_open { |
| 842 | call.transcript_open = false; |
| 843 | self.transcript_panel = None; |
| 844 | call.people_open = false; |
| 845 | } |
| 846 | self.note_call_feature("chat", chat_state.to_owned()); |
| 847 | cx.emit(AppEvent::Call); |
| 848 | cx.notify(); |
| 849 | } |
| 850 | |
| 851 | pub fn call_chat_thread(&self) -> Option<String> { |
| 852 | let call = self.call.as_ref().filter(|call| call.chat_open)?; |
| 853 | call.chat_thread().map(str::to_owned) |
| 854 | } |
| 855 | |
| 856 | pub fn call_chat_unread(&self) -> u32 { |
| 857 | let Some(thread) = self |
| 858 | .call |
| 859 | .as_ref() |
| 860 | .filter(|call| !call.chat_open) |
| 861 | .and_then(|call| call.chat_thread()) |
| 862 | else { |
| 863 | return 0; |
| 864 | }; |
| 865 | let counted = self.directory.unread_counts.get(thread).copied(); |
| 866 | counted.unwrap_or_else(|| { |
| 867 | u32::from( |
| 868 | self.sidebar |
| 869 | .chats |
| 870 | .iter() |
| 871 | .any(|chat| chat.id == thread && chat.unread), |
| 872 | ) |
| 873 | }) |
| 874 | } |
| 875 | |
| 876 | pub fn toggle_call_hand(&mut self, cx: &mut Context<Self>) { |
| 877 | let Some(call) = self |
| 878 | .call |
| 879 | .as_mut() |
| 880 | .filter(|call| call.model.is_active() && call.model.has_reaction_controls()) |
| 881 | else { |
| 882 | return; |
| 883 | }; |
| 884 | let raised = call.model.own_hand.is_none(); |
| 885 | call.model.own_hand = raised.then_some(u64::MAX); |
| 886 | self.send_call_command(CallCommand::SetHand(raised)); |
| 887 | if raised { |
| 888 | self.raise_notice("Your hand is raised".to_owned(), None, cx); |
| 889 | } |
| 890 | cx.emit(AppEvent::Call); |
| 891 | cx.notify(); |
| 892 | } |
| 893 | |
| 894 | pub fn lower_call_hand(&mut self, mri: String, _cx: &mut Context<Self>) { |
| 895 | let allowed = self |
| 896 | .call |
| 897 | .as_ref() |
| 898 | .is_some_and(|call| call.model.can_lower_hands()); |
| 899 | if allowed { |
| 900 | self.send_call_command(CallCommand::LowerHand { mri }); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | pub fn lower_all_call_hands(&mut self, _cx: &mut Context<Self>) { |
| 905 | let allowed = self |
| 906 | .call |
| 907 | .as_ref() |
| 908 | .is_some_and(|call| call.model.can_lower_hands() && call.model.any_hand_raised()); |
| 909 | if allowed { |
| 910 | self.send_call_command(CallCommand::LowerAllHands); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | pub fn send_call_reaction(&mut self, reaction: Reaction, cx: &mut Context<Self>) { |
| 915 | let Some(call) = self |
| 916 | .call |
| 917 | .as_mut() |
| 918 | .filter(|call| call.model.is_active() && call.model.has_reaction_controls()) |
| 919 | else { |
| 920 | return; |
| 921 | }; |
| 922 | if let Some(own_mri) = call.model.own_mri.clone() { |
| 923 | call.model |
| 924 | .note_reaction(own_mri, reaction.clone(), Instant::now()); |
| 925 | } |
| 926 | self.send_call_command(CallCommand::SendReaction(reaction)); |
| 927 | cx.emit(AppEvent::Call); |
| 928 | cx.notify(); |
| 929 | } |
| 930 | |
| 931 | pub fn set_call_share_sound(&mut self, on: bool, cx: &mut Context<Self>) { |
| 932 | self.call_share_sound = on; |
| 933 | let _ = self |
| 934 | .store |
| 935 | .set_meta(SHARE_SOUND_META_KEY, if on { "1" } else { "0" }); |
| 936 | self.send_call_command(CallCommand::SetShareSound(on)); |
| 937 | cx.notify(); |
| 938 | } |
| 939 | |
| 940 | pub fn admit_call_guest(&mut self, mri: String, _cx: &mut Context<Self>) { |
| 941 | if self |
| 942 | .call |
| 943 | .as_ref() |
| 944 | .is_some_and(|call| call.model.can_admit()) |
| 945 | { |
| 946 | self.send_call_command(CallCommand::Admit { mri }); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | pub fn admit_all_call_guests(&mut self, _cx: &mut Context<Self>) { |
| 951 | if self |
| 952 | .call |
| 953 | .as_ref() |
| 954 | .is_some_and(|call| call.model.can_admit() && !call.model.lobby_guests().is_empty()) |
| 955 | { |
| 956 | self.send_call_command(CallCommand::AdmitAll); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | pub fn deny_call_guest(&mut self, mri: String, _cx: &mut Context<Self>) { |
| 961 | if self |
| 962 | .call |
| 963 | .as_ref() |
| 964 | .is_some_and(|call| call.model.can_admit()) |
| 965 | { |
| 966 | self.send_call_command(CallCommand::Deny { mri }); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | pub fn mute_call_participant(&mut self, mri: String, _cx: &mut Context<Self>) { |
| 971 | if self |
| 972 | .call |
| 973 | .as_ref() |
| 974 | .is_some_and(|call| call.model.can_manage()) |
| 975 | { |
| 976 | self.send_call_command(CallCommand::MuteParticipant { mri }); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | pub fn mute_all_call(&mut self, _cx: &mut Context<Self>) { |
| 981 | if self |
| 982 | .call |
| 983 | .as_ref() |
| 984 | .is_some_and(|call| call.model.can_manage()) |
| 985 | { |
| 986 | self.send_call_command(CallCommand::MuteAll); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | pub fn remove_call_participant(&mut self, mri: String, _cx: &mut Context<Self>) { |
| 991 | if self |
| 992 | .call |
| 993 | .as_ref() |
| 994 | .is_some_and(|call| call.model.can_manage()) |
| 995 | { |
| 996 | self.send_call_command(CallCommand::RemoveParticipant { mri }); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | pub fn toggle_call_spotlight(&mut self, mri: String, _cx: &mut Context<Self>) { |
| 1001 | let Some(call) = self |
| 1002 | .call |
| 1003 | .as_ref() |
| 1004 | .filter(|call| call.model.can_manage() && call.model.is_active()) |
| 1005 | else { |
| 1006 | return; |
| 1007 | }; |
| 1008 | let command = if call.model.is_spotlighted(&mri) { |
| 1009 | CallCommand::StopSpotlight { mri } |
| 1010 | } else { |
| 1011 | CallCommand::Spotlight { mri } |
| 1012 | }; |
| 1013 | self.send_call_command(command); |
| 1014 | } |
| 1015 | |
| 1016 | pub fn toggle_call_pin(&mut self, mri: &str, cx: &mut Context<Self>) { |
| 1017 | if let Some(call) = self.call.as_mut() { |
| 1018 | call.model.toggle_pin(mri); |
| 1019 | cx.notify(); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | pub fn toggle_call_fit(&mut self, mri: &str, cx: &mut Context<Self>) { |
| 1024 | if let Some(call) = self.call.as_mut() { |
| 1025 | call.model.toggle_fit(mri); |
| 1026 | cx.notify(); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | pub fn toggle_call_captions(&mut self, _cx: &mut Context<Self>) { |
| 1031 | let Some(call) = self |
| 1032 | .call |
| 1033 | .as_ref() |
| 1034 | .filter(|call| call.model.is_active() && call.model.kind == CallKind::Meeting) |
| 1035 | else { |
| 1036 | return; |
| 1037 | }; |
| 1038 | let on = !call.model.captions_on(); |
| 1039 | self.send_call_command(CallCommand::SetCaptions(on)); |
| 1040 | } |
| 1041 | |
| 1042 | pub fn set_call_effects(&mut self, effects: CameraEffects, cx: &mut Context<Self>) { |
| 1043 | let _ = self.store.set_meta(EFFECTS_META_KEY, &effects.to_meta()); |
| 1044 | self.call_effects = effects; |
| 1045 | self.send_call_command(CallCommand::SetCameraEffects(effects)); |
| 1046 | cx.notify(); |
| 1047 | } |
| 1048 | |
| 1049 | pub fn set_call_background(&mut self, pick: BackgroundPick, cx: &mut Context<Self>) { |
| 1050 | let _ = self.store.set_meta(BACKGROUND_META_KEY, &pick.to_meta()); |
| 1051 | self.call_background = pick.clone(); |
| 1052 | if let Some(call) = self.call.as_mut() { |
| 1053 | call.model.background = pick; |
| 1054 | } |
| 1055 | self.send_background_command(cx); |
| 1056 | cx.notify(); |
| 1057 | } |
| 1058 | |
| 1059 | pub fn toggle_call_background_blur(&mut self, cx: &mut Context<Self>) { |
| 1060 | if self.call.is_none() { |
| 1061 | return; |
| 1062 | } |
| 1063 | let pick = if self.call_background == BackgroundPick::Blur { |
| 1064 | BackgroundPick::None |
| 1065 | } else { |
| 1066 | BackgroundPick::Blur |
| 1067 | }; |
| 1068 | self.set_call_background(pick, cx); |
| 1069 | } |
| 1070 | |
| 1071 | pub(super) fn send_background_command(&mut self, cx: &mut Context<Self>) { |
| 1072 | let pick = self.call_background.clone(); |
| 1073 | if let Some(choice) = self.call_backgrounds.choice_for(&pick) { |
| 1074 | self.send_call_command(CallCommand::SetBackground(choice)); |
| 1075 | return; |
| 1076 | } |
| 1077 | let BackgroundPick::Default(id) = pick else { |
| 1078 | return; |
| 1079 | }; |
| 1080 | let Some(image) = self.call_backgrounds.find(&id).cloned() else { |
| 1081 | return; |
| 1082 | }; |
| 1083 | let Some(launcher) = self.call_launcher.clone() else { |
| 1084 | return; |
| 1085 | }; |
| 1086 | let cache = self.call_backgrounds.cache.clone(); |
| 1087 | let receiver = |
| 1088 | crate::runtime::spawn( |
| 1089 | async move { launcher.download_background(&cache, &image).await }, |
| 1090 | ); |
| 1091 | cx.spawn(async move |this, cx| { |
| 1092 | let path = receiver.await.ok().flatten(); |
| 1093 | this.update(cx, |state, cx| match path { |
| 1094 | Some(path) if state.call_background == BackgroundPick::Default(id) => { |
| 1095 | state.send_call_command(CallCommand::SetBackground( |
| 1096 | calling::BackgroundChoice::Image(path), |
| 1097 | )); |
| 1098 | } |
| 1099 | Some(_) => {} |
| 1100 | None => state.raise_notice( |
| 1101 | "That background could not be downloaded".to_owned(), |
| 1102 | None, |
| 1103 | cx, |
| 1104 | ), |
| 1105 | }) |
| 1106 | .ok(); |
| 1107 | }) |
| 1108 | .detach(); |
| 1109 | } |
| 1110 | |
| 1111 | pub fn refresh_call_backgrounds(&mut self, cx: &mut Context<Self>) { |
| 1112 | if self.mode.demo || !self.call_backgrounds.needs_refresh() { |
| 1113 | return; |
| 1114 | } |
| 1115 | let Some(launcher) = self.call_launcher.clone() else { |
| 1116 | return; |
| 1117 | }; |
| 1118 | self.call_backgrounds.refreshing = true; |
| 1119 | let cache = self.call_backgrounds.cache.clone(); |
| 1120 | let wanted = SHOWN_IMAGES; |
| 1121 | let receiver = |
| 1122 | crate::runtime::spawn( |
| 1123 | async move { launcher.refresh_backgrounds(&cache, wanted).await }, |
| 1124 | ); |
| 1125 | cx.spawn(async move |this, cx| { |
| 1126 | let catalog = receiver.await.ok().flatten(); |
| 1127 | this.update(cx, |state, cx| { |
| 1128 | state.call_backgrounds.refreshing = false; |
| 1129 | if let Some(catalog) = catalog { |
| 1130 | state.call_backgrounds.images = catalog; |
| 1131 | } |
| 1132 | cx.notify(); |
| 1133 | }) |
| 1134 | .ok(); |
| 1135 | }) |
| 1136 | .detach(); |
| 1137 | } |
| 1138 | |
| 1139 | pub fn add_call_background(&mut self, cx: &mut Context<Self>) { |
| 1140 | let receiver = cx.prompt_for_paths(PathPromptOptions { |
| 1141 | files: true, |
| 1142 | directories: false, |
| 1143 | multiple: false, |
| 1144 | prompt: None, |
| 1145 | }); |
| 1146 | cx.spawn(async move |this, cx| { |
| 1147 | let Some(path) = receiver |
| 1148 | .await |
| 1149 | .ok() |
| 1150 | .and_then(Result::ok) |
| 1151 | .flatten() |
| 1152 | .and_then(|paths| paths.into_iter().next()) |
| 1153 | else { |
| 1154 | return; |
| 1155 | }; |
| 1156 | this.update(cx, |state, cx| { |
| 1157 | if !is_image_file(&path) { |
| 1158 | state.raise_notice("Pick a JPG or PNG picture".to_owned(), None, cx); |
| 1159 | return; |
| 1160 | } |
| 1161 | state.call_backgrounds.add_custom(path.clone()); |
| 1162 | save_custom_backgrounds(&state.store, &state.call_backgrounds.customs); |
| 1163 | state.set_call_background(BackgroundPick::Custom(path), cx); |
| 1164 | }) |
| 1165 | .ok(); |
| 1166 | }) |
| 1167 | .detach(); |
| 1168 | } |
| 1169 | |
| 1170 | pub fn toggle_stage_fullscreen(&mut self, cx: &mut Context<Self>) { |
| 1171 | let Some(call) = self |
| 1172 | .call |
| 1173 | .as_mut() |
| 1174 | .filter(|call| call.model.screen_sharer.is_some()) |
| 1175 | else { |
| 1176 | return; |
| 1177 | }; |
| 1178 | call.stage_fullscreen = !call.stage_fullscreen; |
| 1179 | cx.emit(AppEvent::Call); |
| 1180 | cx.notify(); |
| 1181 | } |
| 1182 | |
| 1183 | pub fn leave_stage_fullscreen(&mut self, cx: &mut Context<Self>) { |
| 1184 | let Some(call) = self.call.as_mut().filter(|call| call.stage_fullscreen) else { |
| 1185 | return; |
| 1186 | }; |
| 1187 | call.stage_fullscreen = false; |
| 1188 | cx.emit(AppEvent::Call); |
| 1189 | cx.notify(); |
| 1190 | } |
| 1191 | |
| 1192 | pub fn stage_fullscreen(&self) -> bool { |
| 1193 | self.call |
| 1194 | .as_ref() |
| 1195 | .is_some_and(|call| call.stage_fullscreen && call.model.screen_sharer.is_some()) |
| 1196 | } |
| 1197 | |
| 1198 | pub fn viewing_call(&self) -> bool { |
| 1199 | self.call.as_ref().is_some_and(|call| call.viewing) |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | impl AppState { |
| 1204 | pub fn on_engine_event(&mut self, event: EngineEvent, cx: &mut Context<Self>) { |
| 1205 | match event { |
| 1206 | EngineEvent::Incoming(ring) => self.push_ring(ring, false, cx), |
| 1207 | EngineEvent::RingEnded { ring_id, kind, .. } => { |
| 1208 | self.signal_ring(ring_id, RingSignal::Remote(kind), cx); |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | fn push_ring(&mut self, ring: calling::IncomingRing, demo: bool, cx: &mut Context<Self>) { |
| 1214 | let caller_user_id = ring |
| 1215 | .caller |
| 1216 | .mri |
| 1217 | .strip_prefix(ORGID_PREFIX) |
| 1218 | .map(str::to_owned); |
| 1219 | self.rings.push(ring, demo, Instant::now()); |
| 1220 | if let Some(user_id) = caller_user_id { |
| 1221 | self.request_avatars(vec![user_id], cx); |
| 1222 | } |
| 1223 | cx.emit(AppEvent::Ring); |
| 1224 | cx.notify(); |
| 1225 | } |
| 1226 | |
| 1227 | pub fn demo_incoming_ring(&mut self, cx: &mut Context<Self>) { |
| 1228 | self.push_ring(demo_ring(), true, cx); |
| 1229 | } |
| 1230 | |
| 1231 | pub fn demo_incoming_video_ring(&mut self, cx: &mut Context<Self>) { |
| 1232 | self.push_ring(demo_video_ring(), true, cx); |
| 1233 | } |
| 1234 | |
| 1235 | pub fn start_demo_scene(&mut self, scene: DemoScene, cx: &mut Context<Self>) { |
| 1236 | if self.call.is_some() { |
| 1237 | return; |
| 1238 | } |
| 1239 | if let Some(cause) = scene.end_cause() { |
| 1240 | self.demo_call_end(cause, cx); |
| 1241 | return; |
| 1242 | } |
| 1243 | let (script, model, conversation_id) = if scene.is_one_to_one() { |
| 1244 | ( |
| 1245 | DemoCall::People(vec![demo_caller()]), |
| 1246 | CallModel::people("Mara Lindqvist", &[demo_caller()]), |
| 1247 | "demo-chat-mara", |
| 1248 | ) |
| 1249 | } else { |
| 1250 | let script = DemoCall::Meeting { |
| 1251 | guests: demo_guests(scene.guests().unwrap_or(DEMO_SMALL_MEETING)), |
| 1252 | lobby: false, |
| 1253 | organizer: true, |
| 1254 | }; |
| 1255 | ( |
| 1256 | script, |
| 1257 | CallModel::meeting("Standup", true), |
| 1258 | "demo-chat-meeting-standup", |
| 1259 | ) |
| 1260 | }; |
| 1261 | let handle = start_demo_scene_call(script, scene); |
| 1262 | self.begin_call(handle, model, Some(conversation_id.to_owned()), cx); |
| 1263 | if let Some(call) = self.call.as_mut().filter(|_| !scene.is_one_to_one()) { |
| 1264 | call.meeting_target = Some(calling::MeetingTarget { |
| 1265 | thread_id: conversation_id.to_owned(), |
| 1266 | tenant_id: "demo-tenant".to_owned(), |
| 1267 | organizer_id: "demo-me".to_owned(), |
| 1268 | meeting_data: None, |
| 1269 | }); |
| 1270 | } |
| 1271 | if scene.minimized() { |
| 1272 | self.show_call(false, cx); |
| 1273 | } |
| 1274 | if let Some(call) = self.call.as_mut().filter(|_| scene == DemoScene::Blocked) { |
| 1275 | call.model.recording_policy.call_recording = false; |
| 1276 | call.model.recording_policy.call_transcription = false; |
| 1277 | } |
| 1278 | if scene == DemoScene::Gallery { |
| 1279 | self.queue_demo_bubbles(conversation_id, cx); |
| 1280 | } |
| 1281 | if scene == DemoScene::Consult { |
| 1282 | if let Some(call) = self.call.as_mut() { |
| 1283 | call.model.apply(CallUpdate::State(CallState::Connected { |
| 1284 | since: Instant::now(), |
| 1285 | })); |
| 1286 | } |
| 1287 | self.start_consult(demo_consult_target(), cx); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | fn signal_ring(&mut self, ring_id: u64, signal: RingSignal, cx: &mut Context<Self>) { |
| 1292 | if let Some(outcome) = self.rings.signal(ring_id, signal, Instant::now()) { |
| 1293 | self.finish_ring(ring_id, outcome, cx); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | fn finish_ring(&mut self, ring_id: u64, outcome: RingOutcome, cx: &mut Context<Self>) { |
| 1298 | if let RingOutcome::Missed(missed) = outcome { |
| 1299 | if let Some(launcher) = &self.call_launcher { |
| 1300 | launcher.drop_ring(ring_id); |
| 1301 | } |
| 1302 | cx.emit(AppEvent::MissedCall(missed)); |
| 1303 | } |
| 1304 | cx.emit(AppEvent::Ring); |
| 1305 | cx.notify(); |
| 1306 | } |
| 1307 | |
| 1308 | pub fn tick_rings(&mut self, cx: &mut Context<Self>) { |
| 1309 | if self.rings.is_empty() { |
| 1310 | return; |
| 1311 | } |
| 1312 | for (ring_id, outcome) in self.rings.tick(Instant::now()) { |
| 1313 | self.finish_ring(ring_id, outcome, cx); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | pub fn accept_ringing_call(&mut self, mode: AcceptMode, cx: &mut Context<Self>) { |
| 1318 | if let Some(ring_id) = self.rings.first_ringing_id() { |
| 1319 | self.accept_ring(ring_id, mode, cx); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | pub fn decline_ringing_call(&mut self, cx: &mut Context<Self>) { |
| 1324 | if let Some(ring_id) = self.rings.first_ringing_id() { |
| 1325 | self.decline_ring(ring_id, cx); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | pub fn accept_ring(&mut self, ring_id: u64, mode: AcceptMode, cx: &mut Context<Self>) { |
| 1330 | let Some(entry) = self.rings.get(ring_id).cloned() else { |
| 1331 | return; |
| 1332 | }; |
| 1333 | if self.call.is_some() { |
| 1334 | self.leave_call(cx); |
| 1335 | } |
| 1336 | if let Some(held) = &self.held_call { |
| 1337 | let _ = held.commands.send(CallCommand::Hangup); |
| 1338 | } |
| 1339 | self.signal_ring(ring_id, RingSignal::Accept, cx); |
| 1340 | let conversation_id = self.conversation_of_ring(&entry.ring); |
| 1341 | let title = conversation_id |
| 1342 | .as_deref() |
| 1343 | .and_then(|id| self.sidebar.chats.iter().find(|chat| chat.id == id)) |
| 1344 | .filter(|_| entry.ring.is_group) |
| 1345 | .map(chat_title) |
| 1346 | .unwrap_or_else(|| entry.caller_name().to_owned()); |
| 1347 | let model = CallModel::incoming(&title, &entry.ring.caller.mri, entry.caller_name()); |
| 1348 | if entry.demo { |
| 1349 | let handle = start_demo_call(DemoCall::Incoming(demo_caller())); |
| 1350 | self.begin_call(handle, model, conversation_id, cx); |
| 1351 | if mode == AcceptMode::Video { |
| 1352 | self.send_call_command(CallCommand::SetCamera(true)); |
| 1353 | } |
| 1354 | return; |
| 1355 | } |
| 1356 | let Some(launcher) = self.call_launcher.clone() else { |
| 1357 | self.raise_notice(ANSWER_FAILED_NOTICE.to_owned(), None, cx); |
| 1358 | return; |
| 1359 | }; |
| 1360 | let receiver = |
| 1361 | crate::runtime::spawn(async move { launcher.accept_ring(ring_id, mode).await }); |
| 1362 | cx.spawn(async move |this, cx| { |
| 1363 | let handle = receiver.await.ok().flatten(); |
| 1364 | this.update(cx, |state, cx| match handle { |
| 1365 | Some(handle) => state.begin_call(handle, model, conversation_id, cx), |
| 1366 | None => state.raise_notice(ANSWER_FAILED_NOTICE.to_owned(), None, cx), |
| 1367 | }) |
| 1368 | .ok(); |
| 1369 | }) |
| 1370 | .detach(); |
| 1371 | } |
| 1372 | |
| 1373 | pub fn decline_ring(&mut self, ring_id: u64, cx: &mut Context<Self>) { |
| 1374 | let Some(entry) = self.rings.get(ring_id) else { |
| 1375 | return; |
| 1376 | }; |
| 1377 | let demo = entry.demo; |
| 1378 | if !demo && let Some(launcher) = &self.call_launcher { |
| 1379 | launcher.decline_ring(ring_id); |
| 1380 | } |
| 1381 | self.signal_ring(ring_id, RingSignal::Decline, cx); |
| 1382 | } |
| 1383 | |
| 1384 | pub fn conversation_of_missed_call(&self, missed: &MissedCall) -> Option<String> { |
| 1385 | if let Some(thread_id) = missed |
| 1386 | .thread_id |
| 1387 | .as_ref() |
| 1388 | .filter(|id| self.sidebar.chats.iter().any(|chat| &chat.id == *id)) |
| 1389 | { |
| 1390 | return Some(thread_id.clone()); |
| 1391 | } |
| 1392 | self.one_on_one_chat_with_mri(&missed.caller_mri) |
| 1393 | } |
| 1394 | |
| 1395 | fn conversation_of_ring(&self, ring: &calling::IncomingRing) -> Option<String> { |
| 1396 | ring.thread_id.clone().or_else(|| { |
| 1397 | (!ring.is_group) |
| 1398 | .then(|| self.one_on_one_chat_with_mri(&ring.caller.mri)) |
| 1399 | .flatten() |
| 1400 | }) |
| 1401 | } |
| 1402 | |
| 1403 | fn one_on_one_chat_with_mri(&self, mri: &str) -> Option<String> { |
| 1404 | self.one_on_one_chat_with(mri.strip_prefix(ORGID_PREFIX)?) |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | impl AppState { |
| 1409 | pub fn running_meeting(&self, conversation_id: &str) -> Option<&LiveMeeting> { |
| 1410 | self.live_meetings |
| 1411 | .get(conversation_id) |
| 1412 | .filter(|meeting| meeting.is_running(now_unix()) && meeting.target().is_some()) |
| 1413 | } |
| 1414 | |
| 1415 | pub fn demo_live_meetings(&mut self) { |
| 1416 | for conversation_id in ["demo-chat-meeting-standup", DEMO_LOBBY_MEETING] { |
| 1417 | self.live_meetings.insert( |
| 1418 | conversation_id.to_owned(), |
| 1419 | LiveMeeting { |
| 1420 | thread_id: conversation_id.to_owned(), |
| 1421 | conversation_url: None, |
| 1422 | expiration: Some(now_unix() + DEMO_LIVE_MINUTES * 60), |
| 1423 | organizer_id: Some("demo-jonas".to_owned()), |
| 1424 | tenant_id: Some("demo-tenant".to_owned()), |
| 1425 | meeting_code: None, |
| 1426 | passcode: None, |
| 1427 | }, |
| 1428 | ); |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | pub fn watches_live_meeting(&self, conversation_id: &str) -> bool { |
| 1433 | let meeting_chat = |
| 1434 | self.sidebar.chats.iter().any(|chat| { |
| 1435 | chat.id == conversation_id && chat.kind.eq_ignore_ascii_case(MEETING_KIND) |
| 1436 | }); |
| 1437 | meeting_chat |
| 1438 | || matches!( |
| 1439 | selection_for(&self.sidebar, conversation_id), |
| 1440 | Some(Selection::Channel(_)) |
| 1441 | ) |
| 1442 | } |
| 1443 | |
| 1444 | pub fn refresh_live_meeting(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 1445 | if self.watches_live_meeting(conversation_id) { |
| 1446 | self.fetch_live_meeting(conversation_id, cx); |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | pub fn fetch_live_meeting(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 1451 | if self.mode.demo { |
| 1452 | return; |
| 1453 | } |
| 1454 | let Some(launcher) = self.call_launcher.clone() else { |
| 1455 | return; |
| 1456 | }; |
| 1457 | if !self.live_refreshing.insert(conversation_id.to_owned()) { |
| 1458 | return; |
| 1459 | } |
| 1460 | let thread_id = conversation_id.to_owned(); |
| 1461 | let receiver = |
| 1462 | crate::runtime::spawn(async move { launcher.live_meeting(&thread_id).await }); |
| 1463 | let conversation_id = conversation_id.to_owned(); |
| 1464 | cx.spawn(async move |this, cx| { |
| 1465 | let meeting = receiver.await.ok().flatten(); |
| 1466 | this.update(cx, |state, cx| { |
| 1467 | state.apply_live_meeting(conversation_id, meeting, cx) |
| 1468 | }) |
| 1469 | .ok(); |
| 1470 | }) |
| 1471 | .detach(); |
| 1472 | } |
| 1473 | |
| 1474 | pub fn apply_live_meeting( |
| 1475 | &mut self, |
| 1476 | conversation_id: String, |
| 1477 | meeting: Option<LiveMeeting>, |
| 1478 | cx: &mut Context<Self>, |
| 1479 | ) { |
| 1480 | self.live_refreshing.remove(&conversation_id); |
| 1481 | let changed = match meeting { |
| 1482 | Some(meeting) => { |
| 1483 | self.live_meetings |
| 1484 | .insert(conversation_id.clone(), meeting.clone()) |
| 1485 | != Some(meeting) |
| 1486 | } |
| 1487 | None => self.live_meetings.remove(&conversation_id).is_some(), |
| 1488 | }; |
| 1489 | if changed { |
| 1490 | cx.emit(AppEvent::LiveMeeting); |
| 1491 | cx.notify(); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | pub fn on_thread_changed(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 1496 | self.refresh_live_meeting(conversation_id, cx); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | #[cfg(test)] |
| 1501 | mod tests { |
| 1502 | use std::sync::Arc; |
| 1503 | use std::time::{Duration, Instant}; |
| 1504 | |
| 1505 | use std::path::PathBuf; |
| 1506 | |
| 1507 | use calling::{ |
| 1508 | BackgroundChoice, CallCommand, CallControl, CallState, CallUpdate, CameraEffects, |
| 1509 | DeviceChoice, EndKind, EndReason, EngineEvent, HoldState, Reaction, RecordingKind, |
| 1510 | StopScope, VideoQuality, call_channel, |
| 1511 | }; |
| 1512 | use gpui_kit::{AppContext as _, Entity, TestAppContext}; |
| 1513 | use store::{ChatRecord, MemberRecord, Store}; |
| 1514 | |
| 1515 | use super::{ |
| 1516 | ActiveCall, CallModel, load_camera, load_camera_effects, load_input, load_output, |
| 1517 | load_video_quality, |
| 1518 | }; |
| 1519 | use crate::app_state::{AppEvent, AppState, Mode, Selection}; |
| 1520 | use crate::call::TransferCandidate; |
| 1521 | use crate::call::background::BackgroundPick; |
| 1522 | use crate::call::demo::demo_ring; |
| 1523 | use crate::call::features::room_closed_move; |
| 1524 | use crate::call::model::BreakoutState; |
| 1525 | use crate::call::{CaptionPosition, CaptionSize, RecordAction, ViewSettings}; |
| 1526 | use crate::data::Person; |
| 1527 | use chatsvc::sounds::CallTone; |
| 1528 | |
| 1529 | fn app_with_call(cx: &mut TestAppContext) -> Entity<AppState> { |
| 1530 | cx.update(gpui_kit::init); |
| 1531 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1532 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 1533 | let (handle, _control) = call_channel(); |
| 1534 | cx.update(|cx| { |
| 1535 | app.update(cx, |state, _| { |
| 1536 | state.call = Some(ActiveCall::new( |
| 1537 | 7, |
| 1538 | CallModel::test(), |
| 1539 | handle.commands, |
| 1540 | None, |
| 1541 | handle.video, |
| 1542 | )); |
| 1543 | }) |
| 1544 | }); |
| 1545 | app |
| 1546 | } |
| 1547 | |
| 1548 | fn app_with_commands( |
| 1549 | cx: &mut TestAppContext, |
| 1550 | model: CallModel, |
| 1551 | conversation_id: Option<&str>, |
| 1552 | ) -> (Entity<AppState>, CallControl) { |
| 1553 | cx.update(gpui_kit::init); |
| 1554 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1555 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 1556 | let (handle, control) = call_channel(); |
| 1557 | let conversation_id = conversation_id.map(str::to_owned); |
| 1558 | cx.update(|cx| { |
| 1559 | app.update(cx, |state, _| { |
| 1560 | state.call = Some(ActiveCall::new( |
| 1561 | 7, |
| 1562 | model, |
| 1563 | handle.commands, |
| 1564 | conversation_id, |
| 1565 | handle.video, |
| 1566 | )); |
| 1567 | }) |
| 1568 | }); |
| 1569 | (app, control) |
| 1570 | } |
| 1571 | |
| 1572 | fn live_meeting(can_end: bool) -> CallModel { |
| 1573 | let mut model = CallModel::meeting("Standup", can_end); |
| 1574 | model.apply(CallUpdate::State(CallState::Connected { |
| 1575 | since: Instant::now(), |
| 1576 | })); |
| 1577 | model.apply(CallUpdate::OwnIdentity { |
| 1578 | mri: "8:orgid:me".into(), |
| 1579 | }); |
| 1580 | model |
| 1581 | } |
| 1582 | |
| 1583 | #[gpui_kit::test] |
| 1584 | fn high_rate_updates_do_not_emit_call_events(cx: &mut TestAppContext) { |
| 1585 | let app = app_with_call(cx); |
| 1586 | let emitted = Arc::new(std::sync::atomic::AtomicUsize::new(0)); |
| 1587 | let counter = emitted.clone(); |
| 1588 | cx.update(|cx| { |
| 1589 | cx.subscribe(&app, move |_, event: &AppEvent, _| { |
| 1590 | if matches!(event, AppEvent::Call) { |
| 1591 | counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 1592 | } |
| 1593 | }) |
| 1594 | .detach(); |
| 1595 | app.update(cx, |state, cx| { |
| 1596 | state.apply_call_update(7, CallUpdate::VideoReady, cx); |
| 1597 | state.apply_call_update( |
| 1598 | 7, |
| 1599 | CallUpdate::Levels { |
| 1600 | local: 0.5, |
| 1601 | remote: 0.5, |
| 1602 | }, |
| 1603 | cx, |
| 1604 | ); |
| 1605 | state.apply_call_update( |
| 1606 | 7, |
| 1607 | CallUpdate::Stats { |
| 1608 | inbound_packets: 1, |
| 1609 | outbound_packets: 1, |
| 1610 | }, |
| 1611 | cx, |
| 1612 | ); |
| 1613 | }); |
| 1614 | }); |
| 1615 | assert_eq!(emitted.load(std::sync::atomic::Ordering::SeqCst), 0); |
| 1616 | assert!(cx.update(|cx| app.read(cx).call.as_ref().unwrap().model.remote_speaking)); |
| 1617 | cx.update(|cx| { |
| 1618 | app.update(cx, |state, cx| { |
| 1619 | state.apply_call_update(7, CallUpdate::Muted(true), cx) |
| 1620 | }) |
| 1621 | }); |
| 1622 | assert_eq!(emitted.load(std::sync::atomic::Ordering::SeqCst), 1); |
| 1623 | } |
| 1624 | |
| 1625 | #[gpui_kit::test] |
| 1626 | fn a_camera_the_peer_stops_leaves_the_tile_at_once(cx: &mut TestAppContext) { |
| 1627 | let app = app_with_call(cx); |
| 1628 | let key = calling::VideoKey::Person("8:orgid:peer".into()); |
| 1629 | let shown = |app: &Entity<AppState>, cx: &mut TestAppContext| { |
| 1630 | cx.update(|cx| { |
| 1631 | app.read(cx) |
| 1632 | .call |
| 1633 | .as_ref() |
| 1634 | .unwrap() |
| 1635 | .pictures |
| 1636 | .live(&key, Instant::now()) |
| 1637 | .is_some() |
| 1638 | }) |
| 1639 | }; |
| 1640 | cx.update(|cx| { |
| 1641 | app.update(cx, |state, cx| { |
| 1642 | let hub = state.call.as_ref().unwrap().video.clone(); |
| 1643 | hub.publish(key.clone(), calling::VideoPicture::solid(4, 4, 10, 20, 30)); |
| 1644 | state.apply_call_update(7, CallUpdate::VideoReady, cx); |
| 1645 | }) |
| 1646 | }); |
| 1647 | assert!(shown(&app, cx)); |
| 1648 | cx.update(|cx| { |
| 1649 | app.update(cx, |state, cx| { |
| 1650 | state.apply_call_update(7, CallUpdate::VideoStopped(key.clone()), cx) |
| 1651 | }) |
| 1652 | }); |
| 1653 | assert!(!shown(&app, cx)); |
| 1654 | } |
| 1655 | |
| 1656 | #[gpui_kit::test] |
| 1657 | fn the_computer_sound_switch_is_remembered_and_sent_to_the_call(cx: &mut TestAppContext) { |
| 1658 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 1659 | let store = cx.update(|cx| app.read(cx).store.clone()); |
| 1660 | assert!(!cx.update(|cx| app.read(cx).call_share_sound)); |
| 1661 | cx.update(|cx| app.update(cx, |state, cx| state.set_call_share_sound(true, cx))); |
| 1662 | assert_eq!( |
| 1663 | control.try_recv_command(), |
| 1664 | Some(CallCommand::SetShareSound(true)) |
| 1665 | ); |
| 1666 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1667 | assert!(cx.update(|cx| reopened.read(cx).call_share_sound)); |
| 1668 | cx.update(|cx| app.update(cx, |state, cx| state.set_call_share_sound(false, cx))); |
| 1669 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1670 | assert!(!cx.update(|cx| reopened.read(cx).call_share_sound)); |
| 1671 | } |
| 1672 | |
| 1673 | #[gpui_kit::test] |
| 1674 | fn in_call_device_picks_are_saved_including_system_default(cx: &mut TestAppContext) { |
| 1675 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 1676 | let store = cx.update(|cx| app.read(cx).store.clone()); |
| 1677 | assert_eq!(load_input(&store), DeviceChoice::SystemDefault); |
| 1678 | cx.update(|cx| { |
| 1679 | app.update(cx, |state, cx| { |
| 1680 | state.select_call_input(DeviceChoice::Device("headset-mic".into()), cx); |
| 1681 | state.select_call_output(DeviceChoice::Device("headset".into()), cx); |
| 1682 | }) |
| 1683 | }); |
| 1684 | assert_eq!( |
| 1685 | load_input(&store), |
| 1686 | DeviceChoice::Device("headset-mic".into()) |
| 1687 | ); |
| 1688 | assert_eq!(load_output(&store), DeviceChoice::Device("headset".into())); |
| 1689 | assert_eq!( |
| 1690 | control.try_recv_command(), |
| 1691 | Some(CallCommand::SelectInput(DeviceChoice::Device( |
| 1692 | "headset-mic".into() |
| 1693 | ))) |
| 1694 | ); |
| 1695 | cx.update(|cx| { |
| 1696 | app.update(cx, |state, cx| { |
| 1697 | state.select_call_input(DeviceChoice::SystemDefault, cx) |
| 1698 | }) |
| 1699 | }); |
| 1700 | assert_eq!(load_input(&store), DeviceChoice::SystemDefault); |
| 1701 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1702 | assert_eq!( |
| 1703 | cx.update(|cx| reopened.read(cx).call_output.clone()), |
| 1704 | DeviceChoice::Device("headset".into()) |
| 1705 | ); |
| 1706 | } |
| 1707 | |
| 1708 | #[gpui_kit::test] |
| 1709 | fn every_call_starts_with_the_saved_devices_even_when_they_are_not_present( |
| 1710 | cx: &mut TestAppContext, |
| 1711 | ) { |
| 1712 | cx.update(gpui_kit::init); |
| 1713 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 1714 | let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1715 | cx.update(|cx| { |
| 1716 | app.update(cx, |state, cx| { |
| 1717 | state.select_call_input(DeviceChoice::Device("unplugged-mic".into()), cx); |
| 1718 | state.select_call_output(DeviceChoice::Device("headset".into()), cx); |
| 1719 | }) |
| 1720 | }); |
| 1721 | let (handle, mut control) = call_channel(); |
| 1722 | cx.update(|cx| { |
| 1723 | app.update(cx, |state, cx| { |
| 1724 | state.begin_call(handle, CallModel::test(), None, cx) |
| 1725 | }) |
| 1726 | }); |
| 1727 | let mut commands = Vec::new(); |
| 1728 | while let Some(command) = control.try_recv_command() { |
| 1729 | commands.push(command); |
| 1730 | } |
| 1731 | assert!( |
| 1732 | commands.contains(&CallCommand::SelectInput(DeviceChoice::Device( |
| 1733 | "unplugged-mic".into() |
| 1734 | ))) |
| 1735 | ); |
| 1736 | assert!( |
| 1737 | commands.contains(&CallCommand::SelectOutput(DeviceChoice::Device( |
| 1738 | "headset".into() |
| 1739 | ))) |
| 1740 | ); |
| 1741 | assert!(commands.contains(&CallCommand::SetVideoQuality(VideoQuality::Auto))); |
| 1742 | assert_eq!( |
| 1743 | load_input(&store), |
| 1744 | DeviceChoice::Device("unplugged-mic".into()) |
| 1745 | ); |
| 1746 | } |
| 1747 | |
| 1748 | #[gpui_kit::test] |
| 1749 | fn the_camera_pick_is_remembered_and_the_next_call_starts_with_it(cx: &mut TestAppContext) { |
| 1750 | let (app, _control) = app_with_commands(cx, live_meeting(false), None); |
| 1751 | let store = cx.update(|cx| app.read(cx).store.clone()); |
| 1752 | assert_eq!(load_camera(&store), DeviceChoice::SystemDefault); |
| 1753 | cx.update(|cx| { |
| 1754 | app.update(cx, |state, cx| { |
| 1755 | state.select_call_camera(DeviceChoice::Device("cam-2".into()), cx) |
| 1756 | }) |
| 1757 | }); |
| 1758 | assert_eq!(load_camera(&store), DeviceChoice::Device("cam-2".into())); |
| 1759 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1760 | assert_eq!( |
| 1761 | cx.update(|cx| reopened.read(cx).call_camera.clone()), |
| 1762 | DeviceChoice::Device("cam-2".into()) |
| 1763 | ); |
| 1764 | cx.update(|cx| { |
| 1765 | app.update(cx, |state, cx| { |
| 1766 | state.select_call_camera(DeviceChoice::SystemDefault, cx) |
| 1767 | }) |
| 1768 | }); |
| 1769 | assert_eq!(load_camera(&store), DeviceChoice::SystemDefault); |
| 1770 | } |
| 1771 | |
| 1772 | #[gpui_kit::test] |
| 1773 | fn the_camera_effects_are_remembered_and_sent_to_the_call(cx: &mut TestAppContext) { |
| 1774 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 1775 | let store = cx.update(|cx| app.read(cx).store.clone()); |
| 1776 | assert_eq!(load_camera_effects(&store), CameraEffects::default()); |
| 1777 | let effects = CameraEffects { |
| 1778 | mirror: false, |
| 1779 | auto_framing: true, |
| 1780 | soft_focus: true, |
| 1781 | soft_focus_strength: 75, |
| 1782 | ..CameraEffects::default() |
| 1783 | }; |
| 1784 | cx.update(|cx| app.update(cx, |state, cx| state.set_call_effects(effects, cx))); |
| 1785 | assert_eq!( |
| 1786 | control.try_recv_command(), |
| 1787 | Some(CallCommand::SetCameraEffects(effects)) |
| 1788 | ); |
| 1789 | assert_eq!(load_camera_effects(&store), effects); |
| 1790 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1791 | assert_eq!(cx.update(|cx| reopened.read(cx).call_effects), effects); |
| 1792 | } |
| 1793 | |
| 1794 | #[gpui_kit::test] |
| 1795 | fn the_video_quality_is_remembered_and_sent_to_the_call(cx: &mut TestAppContext) { |
| 1796 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 1797 | let store = cx.update(|cx| app.read(cx).store.clone()); |
| 1798 | assert_eq!(load_video_quality(&store), VideoQuality::Auto); |
| 1799 | cx.update(|cx| { |
| 1800 | app.update(cx, |state, cx| { |
| 1801 | state.set_call_video_quality(VideoQuality::P1080, cx) |
| 1802 | }) |
| 1803 | }); |
| 1804 | assert_eq!( |
| 1805 | control.try_recv_command(), |
| 1806 | Some(CallCommand::SetVideoQuality(VideoQuality::P1080)) |
| 1807 | ); |
| 1808 | assert_eq!(load_video_quality(&store), VideoQuality::P1080); |
| 1809 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 1810 | assert_eq!( |
| 1811 | cx.update(|cx| reopened.read(cx).call_video_quality), |
| 1812 | VideoQuality::P1080 |
| 1813 | ); |
| 1814 | } |
| 1815 | |
| 1816 | #[gpui_kit::test] |
| 1817 | fn hands_and_reactions_become_call_commands_and_lowering_needs_the_organizer( |
| 1818 | cx: &mut TestAppContext, |
| 1819 | ) { |
| 1820 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 1821 | cx.update(|cx| { |
| 1822 | app.update(cx, |state, cx| { |
| 1823 | state.toggle_call_hand(cx); |
| 1824 | state.send_call_reaction(Reaction::Heart, cx); |
| 1825 | state.lower_call_hand("8:orgid:a".into(), cx); |
| 1826 | state.lower_all_call_hands(cx); |
| 1827 | }) |
| 1828 | }); |
| 1829 | assert_eq!(control.try_recv_command(), Some(CallCommand::SetHand(true))); |
| 1830 | assert_eq!( |
| 1831 | control.try_recv_command(), |
| 1832 | Some(CallCommand::SendReaction(Reaction::Heart)) |
| 1833 | ); |
| 1834 | assert_eq!(control.try_recv_command(), None); |
| 1835 | let (organizer, mut organizer_control) = app_with_commands(cx, live_meeting(true), None); |
| 1836 | cx.update(|cx| { |
| 1837 | organizer.update(cx, |state, cx| { |
| 1838 | state.apply_call_update( |
| 1839 | 7, |
| 1840 | CallUpdate::Roster(vec![calling::RosterEntry { |
| 1841 | mri: "8:orgid:a".into(), |
| 1842 | display_name: "Ana".into(), |
| 1843 | muted: false, |
| 1844 | in_lobby: false, |
| 1845 | has_video: false, |
| 1846 | sharing: false, |
| 1847 | hand: Some(calling::RaisedHand { |
| 1848 | state_id: "s".into(), |
| 1849 | rank: 1, |
| 1850 | }), |
| 1851 | spotlight: None, |
| 1852 | organizer: false, |
| 1853 | ..Default::default() |
| 1854 | }]), |
| 1855 | cx, |
| 1856 | ); |
| 1857 | state.lower_call_hand("8:orgid:a".into(), cx); |
| 1858 | state.lower_all_call_hands(cx); |
| 1859 | }) |
| 1860 | }); |
| 1861 | assert_eq!( |
| 1862 | organizer_control.try_recv_command(), |
| 1863 | Some(CallCommand::LowerHand { |
| 1864 | mri: "8:orgid:a".into() |
| 1865 | }) |
| 1866 | ); |
| 1867 | assert_eq!( |
| 1868 | organizer_control.try_recv_command(), |
| 1869 | Some(CallCommand::LowerAllHands) |
| 1870 | ); |
| 1871 | } |
| 1872 | |
| 1873 | fn guest(mri: &str, name: &str, in_lobby: bool, spotlit: bool) -> calling::RosterEntry { |
| 1874 | calling::RosterEntry { |
| 1875 | mri: mri.into(), |
| 1876 | display_name: name.into(), |
| 1877 | muted: false, |
| 1878 | in_lobby, |
| 1879 | has_video: false, |
| 1880 | sharing: false, |
| 1881 | hand: None, |
| 1882 | spotlight: spotlit.then(|| calling::Spotlight { |
| 1883 | state_id: "sp".into(), |
| 1884 | rank: 1, |
| 1885 | }), |
| 1886 | organizer: false, |
| 1887 | ..Default::default() |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | #[gpui_kit::test] |
| 1892 | fn organizer_controls_become_commands_and_attendees_send_nothing(cx: &mut TestAppContext) { |
| 1893 | let (attendee, mut attendee_control) = app_with_commands(cx, live_meeting(false), None); |
| 1894 | cx.update(|cx| { |
| 1895 | attendee.update(cx, |state, cx| { |
| 1896 | state.admit_call_guest("8:orgid:g".into(), cx); |
| 1897 | state.admit_all_call_guests(cx); |
| 1898 | state.deny_call_guest("8:orgid:g".into(), cx); |
| 1899 | state.mute_call_participant("8:orgid:a".into(), cx); |
| 1900 | state.mute_all_call(cx); |
| 1901 | state.remove_call_participant("8:orgid:a".into(), cx); |
| 1902 | state.toggle_call_spotlight("8:orgid:a".into(), cx); |
| 1903 | }) |
| 1904 | }); |
| 1905 | assert_eq!(attendee_control.try_recv_command(), None); |
| 1906 | let (organizer, mut control) = app_with_commands(cx, live_meeting(true), None); |
| 1907 | cx.update(|cx| { |
| 1908 | organizer.update(cx, |state, cx| { |
| 1909 | state.apply_call_update( |
| 1910 | 7, |
| 1911 | CallUpdate::Roster(vec![ |
| 1912 | guest("8:orgid:a", "Ana", false, false), |
| 1913 | guest("8:orgid:g", "Gast", true, false), |
| 1914 | ]), |
| 1915 | cx, |
| 1916 | ); |
| 1917 | state.admit_call_guest("8:orgid:g".into(), cx); |
| 1918 | state.admit_all_call_guests(cx); |
| 1919 | state.deny_call_guest("8:orgid:g".into(), cx); |
| 1920 | state.mute_call_participant("8:orgid:a".into(), cx); |
| 1921 | state.mute_all_call(cx); |
| 1922 | state.remove_call_participant("8:orgid:a".into(), cx); |
| 1923 | state.toggle_call_spotlight("8:orgid:a".into(), cx); |
| 1924 | }) |
| 1925 | }); |
| 1926 | assert_eq!( |
| 1927 | control.try_recv_command(), |
| 1928 | Some(CallCommand::Admit { |
| 1929 | mri: "8:orgid:g".into() |
| 1930 | }) |
| 1931 | ); |
| 1932 | assert_eq!(control.try_recv_command(), Some(CallCommand::AdmitAll)); |
| 1933 | assert_eq!( |
| 1934 | control.try_recv_command(), |
| 1935 | Some(CallCommand::Deny { |
| 1936 | mri: "8:orgid:g".into() |
| 1937 | }) |
| 1938 | ); |
| 1939 | assert_eq!( |
| 1940 | control.try_recv_command(), |
| 1941 | Some(CallCommand::MuteParticipant { |
| 1942 | mri: "8:orgid:a".into() |
| 1943 | }) |
| 1944 | ); |
| 1945 | assert_eq!(control.try_recv_command(), Some(CallCommand::MuteAll)); |
| 1946 | assert_eq!( |
| 1947 | control.try_recv_command(), |
| 1948 | Some(CallCommand::RemoveParticipant { |
| 1949 | mri: "8:orgid:a".into() |
| 1950 | }) |
| 1951 | ); |
| 1952 | assert_eq!( |
| 1953 | control.try_recv_command(), |
| 1954 | Some(CallCommand::Spotlight { |
| 1955 | mri: "8:orgid:a".into() |
| 1956 | }) |
| 1957 | ); |
| 1958 | } |
| 1959 | |
| 1960 | #[gpui_kit::test] |
| 1961 | fn the_spotlight_button_stops_an_existing_spotlight_and_pinning_stays_local( |
| 1962 | cx: &mut TestAppContext, |
| 1963 | ) { |
| 1964 | let (app, mut control) = app_with_commands(cx, live_meeting(true), None); |
| 1965 | cx.update(|cx| { |
| 1966 | app.update(cx, |state, cx| { |
| 1967 | state.apply_call_update( |
| 1968 | 7, |
| 1969 | CallUpdate::Roster(vec![guest("8:orgid:a", "Ana", false, true)]), |
| 1970 | cx, |
| 1971 | ); |
| 1972 | state.toggle_call_spotlight("8:orgid:a".into(), cx); |
| 1973 | state.toggle_call_pin("8:orgid:a", cx); |
| 1974 | assert_eq!( |
| 1975 | state.call.as_ref().unwrap().model.pinned.as_deref(), |
| 1976 | Some("8:orgid:a") |
| 1977 | ); |
| 1978 | state.toggle_call_pin("8:orgid:a", cx); |
| 1979 | assert_eq!(state.call.as_ref().unwrap().model.pinned, None); |
| 1980 | }) |
| 1981 | }); |
| 1982 | assert_eq!( |
| 1983 | control.try_recv_command(), |
| 1984 | Some(CallCommand::StopSpotlight { |
| 1985 | mri: "8:orgid:a".into() |
| 1986 | }) |
| 1987 | ); |
| 1988 | assert_eq!(control.try_recv_command(), None); |
| 1989 | } |
| 1990 | |
| 1991 | #[gpui_kit::test] |
| 1992 | fn captions_toggle_between_on_and_off_and_only_in_meetings(cx: &mut TestAppContext) { |
| 1993 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 1994 | cx.update(|cx| { |
| 1995 | app.update(cx, |state, cx| { |
| 1996 | state.toggle_call_captions(cx); |
| 1997 | state.apply_call_update(7, CallUpdate::Captions(calling::CaptionState::On), cx); |
| 1998 | state.toggle_call_captions(cx); |
| 1999 | }) |
| 2000 | }); |
| 2001 | assert_eq!( |
| 2002 | control.try_recv_command(), |
| 2003 | Some(CallCommand::SetCaptions(true)) |
| 2004 | ); |
| 2005 | assert_eq!( |
| 2006 | control.try_recv_command(), |
| 2007 | Some(CallCommand::SetCaptions(false)) |
| 2008 | ); |
| 2009 | let app = app_with_call(cx); |
| 2010 | cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_captions(cx))); |
| 2011 | } |
| 2012 | |
| 2013 | #[gpui_kit::test] |
| 2014 | fn the_background_choice_is_remembered_and_sent_to_the_call(cx: &mut TestAppContext) { |
| 2015 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 2016 | let store = cx.update(|cx| app.read(cx).store.clone()); |
| 2017 | assert_eq!( |
| 2018 | cx.update(|cx| app.read(cx).call_background.clone()), |
| 2019 | BackgroundPick::None |
| 2020 | ); |
| 2021 | cx.update(|cx| { |
| 2022 | app.update(cx, |state, cx| { |
| 2023 | state.set_call_background(BackgroundPick::Blur, cx) |
| 2024 | }) |
| 2025 | }); |
| 2026 | assert_eq!( |
| 2027 | control.try_recv_command(), |
| 2028 | Some(CallCommand::SetBackground(BackgroundChoice::Blur)) |
| 2029 | ); |
| 2030 | assert_eq!( |
| 2031 | cx.update(|cx| app.read(cx).call.as_ref().unwrap().model.background.clone()), |
| 2032 | BackgroundPick::Blur |
| 2033 | ); |
| 2034 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 2035 | assert_eq!( |
| 2036 | cx.update(|cx| reopened.read(cx).call_background.clone()), |
| 2037 | BackgroundPick::Blur |
| 2038 | ); |
| 2039 | let picture = PathBuf::from("/pictures/wall.jpg"); |
| 2040 | cx.update(|cx| { |
| 2041 | app.update(cx, |state, cx| { |
| 2042 | state.set_call_background(BackgroundPick::Custom(picture.clone()), cx) |
| 2043 | }) |
| 2044 | }); |
| 2045 | assert_eq!( |
| 2046 | control.try_recv_command(), |
| 2047 | Some(CallCommand::SetBackground(BackgroundChoice::Image( |
| 2048 | picture.clone() |
| 2049 | ))) |
| 2050 | ); |
| 2051 | let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default()))); |
| 2052 | assert_eq!( |
| 2053 | cx.update(|cx| reopened.read(cx).call_background.clone()), |
| 2054 | BackgroundPick::Custom(picture) |
| 2055 | ); |
| 2056 | cx.update(|cx| { |
| 2057 | app.update(cx, |state, cx| { |
| 2058 | state.set_call_background(BackgroundPick::None, cx) |
| 2059 | }) |
| 2060 | }); |
| 2061 | assert_eq!( |
| 2062 | control.try_recv_command(), |
| 2063 | Some(CallCommand::SetBackground(BackgroundChoice::None)) |
| 2064 | ); |
| 2065 | } |
| 2066 | |
| 2067 | #[gpui_kit::test] |
| 2068 | fn a_default_image_that_is_not_downloaded_yet_sends_nothing(cx: &mut TestAppContext) { |
| 2069 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 2070 | cx.update(|cx| { |
| 2071 | app.update(cx, |state, cx| { |
| 2072 | state.set_call_background(BackgroundPick::Default("office_01".into()), cx) |
| 2073 | }) |
| 2074 | }); |
| 2075 | assert_eq!(control.try_recv_command(), None); |
| 2076 | } |
| 2077 | |
| 2078 | #[gpui_kit::test] |
| 2079 | fn attendees_cannot_start_a_recording_and_the_notice_needs_a_consent_request( |
| 2080 | cx: &mut TestAppContext, |
| 2081 | ) { |
| 2082 | let mut attendee_model = live_meeting(false); |
| 2083 | attendee_model.own_role = Some(calling::MeetingRole::Attendee); |
| 2084 | let (attendee, mut attendee_control) = app_with_commands(cx, attendee_model, None); |
| 2085 | cx.update(|cx| { |
| 2086 | attendee.update(cx, |state, cx| { |
| 2087 | state.run_call_record_action(RecordAction::Start(RecordingKind::Video), cx); |
| 2088 | state.accept_recording_notice(cx); |
| 2089 | }) |
| 2090 | }); |
| 2091 | assert_eq!(attendee_control.try_recv_command(), None); |
| 2092 | let (organizer, mut control) = app_with_commands(cx, live_meeting(true), None); |
| 2093 | cx.update(|cx| { |
| 2094 | organizer.update(cx, |state, cx| { |
| 2095 | state.run_call_record_action(RecordAction::Start(RecordingKind::Video), cx); |
| 2096 | state.apply_call_update(7, CallUpdate::Recording(true), cx); |
| 2097 | state.run_call_record_action(RecordAction::Stop(StopScope::Both), cx); |
| 2098 | state.accept_recording_notice(cx); |
| 2099 | state.apply_call_update(7, CallUpdate::ConsentRequired(true), cx); |
| 2100 | state.accept_recording_notice(cx); |
| 2101 | }) |
| 2102 | }); |
| 2103 | assert_eq!( |
| 2104 | control.try_recv_command(), |
| 2105 | Some(CallCommand::StartRecording { |
| 2106 | kind: RecordingKind::Video, |
| 2107 | title: "Standup".into() |
| 2108 | }) |
| 2109 | ); |
| 2110 | assert_eq!( |
| 2111 | control.try_recv_command(), |
| 2112 | Some(CallCommand::StopRecording { |
| 2113 | scope: StopScope::Both |
| 2114 | }) |
| 2115 | ); |
| 2116 | assert_eq!( |
| 2117 | control.try_recv_command(), |
| 2118 | Some(CallCommand::ConsentToRecording) |
| 2119 | ); |
| 2120 | assert_eq!(control.try_recv_command(), None); |
| 2121 | } |
| 2122 | |
| 2123 | #[gpui_kit::test] |
| 2124 | fn every_record_action_becomes_its_own_command_for_the_organizer(cx: &mut TestAppContext) { |
| 2125 | let (organizer, mut control) = app_with_commands(cx, live_meeting(true), None); |
| 2126 | cx.update(|cx| { |
| 2127 | organizer.update(cx, |state, cx| { |
| 2128 | state.run_call_record_action(RecordAction::Start(RecordingKind::AudioOnly), cx); |
| 2129 | state |
| 2130 | .run_call_record_action(RecordAction::Start(RecordingKind::TranscriptOnly), cx); |
| 2131 | state.run_call_record_action(RecordAction::Stop(StopScope::RecordingOnly), cx); |
| 2132 | state.run_call_record_action(RecordAction::Stop(StopScope::TranscriptionOnly), cx); |
| 2133 | }) |
| 2134 | }); |
| 2135 | let title = "Standup".to_owned(); |
| 2136 | assert_eq!( |
| 2137 | control.try_recv_command(), |
| 2138 | Some(CallCommand::StartRecording { |
| 2139 | kind: RecordingKind::AudioOnly, |
| 2140 | title: title.clone() |
| 2141 | }) |
| 2142 | ); |
| 2143 | assert_eq!( |
| 2144 | control.try_recv_command(), |
| 2145 | Some(CallCommand::StartRecording { |
| 2146 | kind: RecordingKind::TranscriptOnly, |
| 2147 | title |
| 2148 | }) |
| 2149 | ); |
| 2150 | assert_eq!( |
| 2151 | control.try_recv_command(), |
| 2152 | Some(CallCommand::StopRecording { |
| 2153 | scope: StopScope::RecordingOnly |
| 2154 | }) |
| 2155 | ); |
| 2156 | assert_eq!( |
| 2157 | control.try_recv_command(), |
| 2158 | Some(CallCommand::StopRecording { |
| 2159 | scope: StopScope::TranscriptionOnly |
| 2160 | }) |
| 2161 | ); |
| 2162 | assert_eq!(control.try_recv_command(), None); |
| 2163 | } |
| 2164 | |
| 2165 | #[gpui_kit::test] |
| 2166 | fn the_transcript_turns_captions_on_and_excludes_the_chat_panel(cx: &mut TestAppContext) { |
| 2167 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 2168 | cx.update(|cx| { |
| 2169 | app.update(cx, |state, cx| { |
| 2170 | state.apply_call_update( |
| 2171 | 7, |
| 2172 | CallUpdate::MeetingChat("19:meeting_x@thread.v2".into()), |
| 2173 | cx, |
| 2174 | ); |
| 2175 | state.toggle_call_chat(cx); |
| 2176 | state.show_call_transcript(cx); |
| 2177 | }) |
| 2178 | }); |
| 2179 | let (chat_open, transcript_open) = cx.update(|cx| { |
| 2180 | let call = app.read(cx).call.as_ref().unwrap(); |
| 2181 | (call.chat_open, call.transcript_open) |
| 2182 | }); |
| 2183 | assert!(!chat_open && transcript_open); |
| 2184 | assert_eq!( |
| 2185 | control.try_recv_command(), |
| 2186 | Some(CallCommand::SetCaptions(true)) |
| 2187 | ); |
| 2188 | cx.update(|cx| { |
| 2189 | app.update(cx, |state, cx| { |
| 2190 | state.apply_call_update(7, CallUpdate::Captions(calling::CaptionState::On), cx); |
| 2191 | state.show_call_transcript(cx); |
| 2192 | state.toggle_call_chat(cx); |
| 2193 | }) |
| 2194 | }); |
| 2195 | let (chat_open, transcript_open) = cx.update(|cx| { |
| 2196 | let call = app.read(cx).call.as_ref().unwrap(); |
| 2197 | (call.chat_open, call.transcript_open) |
| 2198 | }); |
| 2199 | assert!(chat_open && !transcript_open); |
| 2200 | assert_eq!(control.try_recv_command(), None); |
| 2201 | } |
| 2202 | |
| 2203 | #[gpui_kit::test] |
| 2204 | fn a_spoken_language_picked_while_captions_are_off_is_sent_once_they_are_on( |
| 2205 | cx: &mut TestAppContext, |
| 2206 | ) { |
| 2207 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 2208 | cx.update(|cx| { |
| 2209 | app.update(cx, |state, cx| { |
| 2210 | state.choose_call_caption_language("de-DE", cx); |
| 2211 | state.apply_call_update( |
| 2212 | 7, |
| 2213 | CallUpdate::Captions(calling::CaptionState::Starting), |
| 2214 | cx, |
| 2215 | ); |
| 2216 | }) |
| 2217 | }); |
| 2218 | assert_eq!( |
| 2219 | control.try_recv_command(), |
| 2220 | Some(CallCommand::SetCaptions(true)) |
| 2221 | ); |
| 2222 | assert_eq!(control.try_recv_command(), None); |
| 2223 | cx.update(|cx| { |
| 2224 | app.update(cx, |state, cx| { |
| 2225 | state.apply_call_update(7, CallUpdate::Captions(calling::CaptionState::On), cx); |
| 2226 | state.choose_call_caption_language("fr-FR", cx); |
| 2227 | }) |
| 2228 | }); |
| 2229 | assert_eq!( |
| 2230 | control.try_recv_command(), |
| 2231 | Some(CallCommand::SetCaptionLanguage("de-DE".into())) |
| 2232 | ); |
| 2233 | assert_eq!( |
| 2234 | control.try_recv_command(), |
| 2235 | Some(CallCommand::SetCaptionLanguage("fr-FR".into())) |
| 2236 | ); |
| 2237 | assert_eq!(control.try_recv_command(), None); |
| 2238 | } |
| 2239 | |
| 2240 | #[gpui_kit::test] |
| 2241 | fn caption_size_and_position_are_stored_and_reach_the_call_model(cx: &mut TestAppContext) { |
| 2242 | let (app, _control) = app_with_commands(cx, live_meeting(false), None); |
| 2243 | cx.update(|cx| { |
| 2244 | app.update(cx, |state, cx| { |
| 2245 | state.set_call_caption_size(CaptionSize::Large, cx); |
| 2246 | state.set_call_caption_position(CaptionPosition::Top, cx); |
| 2247 | }) |
| 2248 | }); |
| 2249 | let (view, stored) = cx.update(|cx| { |
| 2250 | let state = app.read(cx); |
| 2251 | ( |
| 2252 | state.call.as_ref().unwrap().model.view, |
| 2253 | ViewSettings::load(&state.store), |
| 2254 | ) |
| 2255 | }); |
| 2256 | assert_eq!( |
| 2257 | (view.caption_size, view.caption_position), |
| 2258 | (CaptionSize::Large, CaptionPosition::Top) |
| 2259 | ); |
| 2260 | assert_eq!( |
| 2261 | (stored.caption_size, stored.caption_position), |
| 2262 | (CaptionSize::Large, CaptionPosition::Top) |
| 2263 | ); |
| 2264 | } |
| 2265 | |
| 2266 | fn live_one_to_one() -> CallModel { |
| 2267 | let mut model = CallModel::people("Bea", &[("8:orgid:bea".into(), "Bea".into())]); |
| 2268 | model.apply(CallUpdate::State(CallState::Connected { |
| 2269 | since: Instant::now(), |
| 2270 | })); |
| 2271 | model |
| 2272 | } |
| 2273 | |
| 2274 | #[gpui_kit::test] |
| 2275 | fn hold_and_resume_alternate_and_stop_while_the_other_side_holds(cx: &mut TestAppContext) { |
| 2276 | let (app, mut control) = app_with_commands(cx, live_one_to_one(), None); |
| 2277 | cx.update(|cx| { |
| 2278 | app.update(cx, |state, cx| { |
| 2279 | state.toggle_call_hold(cx); |
| 2280 | state.apply_call_update(7, CallUpdate::Hold(HoldState::Local), cx); |
| 2281 | state.toggle_call_hold(cx); |
| 2282 | state.apply_call_update(7, CallUpdate::Hold(HoldState::Remote), cx); |
| 2283 | state.toggle_call_hold(cx); |
| 2284 | }) |
| 2285 | }); |
| 2286 | assert_eq!(control.try_recv_command(), Some(CallCommand::Hold(true))); |
| 2287 | assert_eq!(control.try_recv_command(), Some(CallCommand::Hold(false))); |
| 2288 | assert_eq!(control.try_recv_command(), None); |
| 2289 | let (meeting, mut meeting_control) = app_with_commands(cx, live_meeting(true), None); |
| 2290 | cx.update(|cx| meeting.update(cx, |state, cx| state.toggle_call_hold(cx))); |
| 2291 | assert_eq!(meeting_control.try_recv_command(), None); |
| 2292 | } |
| 2293 | |
| 2294 | fn candidate(name: &str) -> TransferCandidate { |
| 2295 | TransferCandidate { |
| 2296 | mri: format!("8:orgid:{name}"), |
| 2297 | name: name.into(), |
| 2298 | user_id: Some(name.into()), |
| 2299 | chat_id: format!("19:{name}"), |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | #[gpui_kit::test] |
| 2304 | fn a_blind_transfer_names_the_target(cx: &mut TestAppContext) { |
| 2305 | let (app, mut control) = app_with_commands(cx, live_one_to_one(), None); |
| 2306 | cx.update(|cx| { |
| 2307 | app.update(cx, |state, cx| { |
| 2308 | state.transfer_call_blind(candidate("ana"), cx) |
| 2309 | }) |
| 2310 | }); |
| 2311 | assert_eq!( |
| 2312 | control.try_recv_command(), |
| 2313 | Some(CallCommand::Transfer { |
| 2314 | target: calling::Callee { |
| 2315 | mri: "8:orgid:ana".into(), |
| 2316 | display_name: "ana".into() |
| 2317 | }, |
| 2318 | replaces: None |
| 2319 | }) |
| 2320 | ); |
| 2321 | } |
| 2322 | |
| 2323 | #[gpui_kit::test] |
| 2324 | fn the_transfer_list_holds_one_to_one_chats_without_the_person_on_the_call( |
| 2325 | cx: &mut TestAppContext, |
| 2326 | ) { |
| 2327 | let app = app_with_chats(cx); |
| 2328 | cx.update(|cx| { |
| 2329 | app.update(cx, |state, cx| { |
| 2330 | let names: Vec<String> = state |
| 2331 | .transfer_candidates() |
| 2332 | .into_iter() |
| 2333 | .map(|candidate| candidate.name) |
| 2334 | .collect(); |
| 2335 | assert_eq!(names, vec!["Bea".to_owned()]); |
| 2336 | let (handle, _control) = call_channel(); |
| 2337 | let mut on_the_call = |
| 2338 | CallModel::people("Bea", &[("8:orgid:bea-id".into(), "Bea".into())]); |
| 2339 | on_the_call.apply(CallUpdate::State(CallState::Connected { |
| 2340 | since: Instant::now(), |
| 2341 | })); |
| 2342 | state.call = Some(ActiveCall::new( |
| 2343 | 9, |
| 2344 | on_the_call, |
| 2345 | handle.commands, |
| 2346 | None, |
| 2347 | handle.video, |
| 2348 | )); |
| 2349 | assert!(state.transfer_candidates().is_empty()); |
| 2350 | cx.notify(); |
| 2351 | }) |
| 2352 | }); |
| 2353 | } |
| 2354 | |
| 2355 | fn room_move(returning: bool) -> calling::BreakoutMove { |
| 2356 | calling::BreakoutMove { |
| 2357 | room_name: "Room 1".into(), |
| 2358 | target: calling::MeetingTarget { |
| 2359 | thread_id: "19:meeting_room@thread.v2".into(), |
| 2360 | tenant_id: "t".into(), |
| 2361 | organizer_id: "o".into(), |
| 2362 | meeting_data: None, |
| 2363 | }, |
| 2364 | returning, |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | fn main_target() -> calling::MeetingTarget { |
| 2369 | calling::MeetingTarget { |
| 2370 | thread_id: "19:meeting_main@thread.v2".into(), |
| 2371 | tenant_id: "t".into(), |
| 2372 | organizer_id: "o".into(), |
| 2373 | meeting_data: None, |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | #[gpui_kit::test] |
| 2378 | fn being_moved_to_a_room_leaves_the_call_and_remembers_the_way_back(cx: &mut TestAppContext) { |
| 2379 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 2380 | cx.update(|cx| { |
| 2381 | app.update(cx, |state, cx| { |
| 2382 | state.call.as_mut().unwrap().meeting_target = Some(main_target()); |
| 2383 | state.apply_call_update(7, CallUpdate::BreakoutMove(room_move(false)), cx); |
| 2384 | let pending = state |
| 2385 | .call |
| 2386 | .as_ref() |
| 2387 | .unwrap() |
| 2388 | .pending_move |
| 2389 | .clone() |
| 2390 | .expect("a move is pending"); |
| 2391 | assert_eq!(pending.target.thread_id, "19:meeting_room@thread.v2"); |
| 2392 | assert_eq!(pending.title, "Room 1"); |
| 2393 | let room = pending.breakout.expect("a room"); |
| 2394 | assert_eq!(room.main, Some(main_target())); |
| 2395 | assert_eq!(room.main_title, "Standup"); |
| 2396 | assert_eq!( |
| 2397 | state.notice.as_ref().map(|notice| notice.text.as_str()), |
| 2398 | Some("Moving you to Room 1") |
| 2399 | ); |
| 2400 | state.apply_call_update(7, CallUpdate::BreakoutMove(room_move(false)), cx); |
| 2401 | }) |
| 2402 | }); |
| 2403 | assert_eq!(control.try_recv_command(), Some(CallCommand::Hangup)); |
| 2404 | assert_eq!(control.try_recv_command(), None); |
| 2405 | } |
| 2406 | |
| 2407 | #[gpui_kit::test] |
| 2408 | fn the_return_button_and_a_closed_room_both_lead_back_to_the_main_meeting( |
| 2409 | cx: &mut TestAppContext, |
| 2410 | ) { |
| 2411 | let mut room = live_meeting(false); |
| 2412 | room.breakout = Some(BreakoutState { |
| 2413 | room_name: "Room 1".into(), |
| 2414 | main: Some(main_target()), |
| 2415 | main_title: "Standup".into(), |
| 2416 | }); |
| 2417 | let (app, mut control) = app_with_commands(cx, room.clone(), None); |
| 2418 | cx.update(|cx| { |
| 2419 | app.update(cx, |state, cx| { |
| 2420 | state.return_to_main_meeting(cx); |
| 2421 | let pending = state |
| 2422 | .call |
| 2423 | .as_ref() |
| 2424 | .unwrap() |
| 2425 | .pending_move |
| 2426 | .clone() |
| 2427 | .expect("a move is pending"); |
| 2428 | assert_eq!(pending.target, main_target()); |
| 2429 | assert_eq!(pending.title, "Standup"); |
| 2430 | assert!(pending.breakout.is_none()); |
| 2431 | }) |
| 2432 | }); |
| 2433 | assert_eq!(control.try_recv_command(), Some(CallCommand::Hangup)); |
| 2434 | let (closed, _control) = app_with_commands(cx, room, None); |
| 2435 | cx.update(|cx| { |
| 2436 | closed.update(cx, |state, _| { |
| 2437 | let call = state.call.as_ref().unwrap(); |
| 2438 | let moved = room_closed_move(call, &EndReason::Remote(EndKind::RoomClosed)) |
| 2439 | .expect("goes back"); |
| 2440 | assert_eq!(moved.target, main_target()); |
| 2441 | assert!(room_closed_move(call, &EndReason::Remote(EndKind::Normal)).is_none()); |
| 2442 | assert!(room_closed_move(call, &EndReason::LocalHangup).is_none()); |
| 2443 | }) |
| 2444 | }); |
| 2445 | let (plain, _control) = app_with_commands(cx, live_meeting(false), None); |
| 2446 | cx.update(|cx| { |
| 2447 | plain.update(cx, |state, _| { |
| 2448 | assert!( |
| 2449 | room_closed_move( |
| 2450 | state.call.as_ref().unwrap(), |
| 2451 | &EndReason::Remote(EndKind::RoomClosed) |
| 2452 | ) |
| 2453 | .is_none() |
| 2454 | ) |
| 2455 | }) |
| 2456 | }); |
| 2457 | } |
| 2458 | |
| 2459 | #[gpui_kit::test] |
| 2460 | fn the_whiteboard_menu_asks_for_the_board_unless_someone_already_shares_one( |
| 2461 | cx: &mut TestAppContext, |
| 2462 | ) { |
| 2463 | let (app, mut control) = app_with_commands(cx, live_meeting(false), None); |
| 2464 | cx.update(|cx| { |
| 2465 | app.update(cx, |state, cx| { |
| 2466 | state.open_call_whiteboard(cx); |
| 2467 | state.apply_call_update( |
| 2468 | 7, |
| 2469 | CallUpdate::Whiteboard(Some(calling::ContentShare { |
| 2470 | session_id: "s".into(), |
| 2471 | presenter: Some("8:orgid:ana".into()), |
| 2472 | subject: "Whiteboard".into(), |
| 2473 | url: Some("https://app.whiteboard.microsoft.com/x".into()), |
| 2474 | whiteboard: true, |
| 2475 | })), |
| 2476 | cx, |
| 2477 | ); |
| 2478 | state.open_call_whiteboard(cx); |
| 2479 | }) |
| 2480 | }); |
| 2481 | assert_eq!( |
| 2482 | control.try_recv_command(), |
| 2483 | Some(CallCommand::OpenWhiteboard { |
| 2484 | title: "Standup".into() |
| 2485 | }) |
| 2486 | ); |
| 2487 | assert_eq!(control.try_recv_command(), None); |
| 2488 | } |
| 2489 | |
| 2490 | #[gpui_kit::test] |
| 2491 | fn the_chat_panel_opens_the_meeting_thread_and_the_badge_counts_only_while_closed( |
| 2492 | cx: &mut TestAppContext, |
| 2493 | ) { |
| 2494 | let (app, _control) = app_with_commands(cx, live_meeting(false), Some("19:list@thread.v2")); |
| 2495 | cx.update(|cx| { |
| 2496 | app.update(cx, |state, cx| { |
| 2497 | state |
| 2498 | .directory |
| 2499 | .unread_counts |
| 2500 | .insert("19:meeting_x@thread.v2".into(), 3); |
| 2501 | assert_eq!(state.call_chat_thread(), None); |
| 2502 | assert_eq!(state.call_chat_unread(), 0); |
| 2503 | state.apply_call_update( |
| 2504 | 7, |
| 2505 | CallUpdate::MeetingChat("19:meeting_x@thread.v2".into()), |
| 2506 | cx, |
| 2507 | ); |
| 2508 | assert_eq!(state.call_chat_unread(), 3); |
| 2509 | state.toggle_call_chat(cx); |
| 2510 | assert_eq!( |
| 2511 | state.call_chat_thread().as_deref(), |
| 2512 | Some("19:meeting_x@thread.v2") |
| 2513 | ); |
| 2514 | assert_eq!(state.call_chat_unread(), 0); |
| 2515 | state.toggle_call_chat(cx); |
| 2516 | assert_eq!(state.call_chat_thread(), None); |
| 2517 | }) |
| 2518 | }); |
| 2519 | } |
| 2520 | |
| 2521 | #[gpui_kit::test] |
| 2522 | fn a_call_without_a_chat_has_no_panel(cx: &mut TestAppContext) { |
| 2523 | let app = app_with_call(cx); |
| 2524 | cx.update(|cx| { |
| 2525 | app.update(cx, |state, cx| { |
| 2526 | state.toggle_call_chat(cx); |
| 2527 | assert!(!state.call.as_ref().unwrap().chat_open); |
| 2528 | }) |
| 2529 | }); |
| 2530 | } |
| 2531 | |
| 2532 | fn app_with_chats(cx: &mut TestAppContext) -> Entity<AppState> { |
| 2533 | cx.update(gpui_kit::init); |
| 2534 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 2535 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 2536 | cx.update(|cx| { |
| 2537 | app.update(cx, |state, _| { |
| 2538 | state.directory.me = Some(Person { |
| 2539 | user_id: "me-id".into(), |
| 2540 | display_name: "Me".into(), |
| 2541 | }); |
| 2542 | state.sidebar.chats = vec![ |
| 2543 | ChatRecord { |
| 2544 | id: "19:meeting@thread.v2".into(), |
| 2545 | kind: "meeting".into(), |
| 2546 | members: vec![MemberRecord { |
| 2547 | user_id: Some("bea-id".into()), |
| 2548 | display_name: "Bea".into(), |
| 2549 | }], |
| 2550 | ..Default::default() |
| 2551 | }, |
| 2552 | ChatRecord { |
| 2553 | id: "19:dm@unq.gbl.spaces".into(), |
| 2554 | kind: "oneOnOne".into(), |
| 2555 | members: vec![ |
| 2556 | MemberRecord { |
| 2557 | user_id: Some("me-id".into()), |
| 2558 | display_name: "Me".into(), |
| 2559 | }, |
| 2560 | MemberRecord { |
| 2561 | user_id: Some("bea-id".into()), |
| 2562 | display_name: "Bea".into(), |
| 2563 | }, |
| 2564 | ], |
| 2565 | ..Default::default() |
| 2566 | }, |
| 2567 | ]; |
| 2568 | }) |
| 2569 | }); |
| 2570 | app |
| 2571 | } |
| 2572 | |
| 2573 | #[gpui_kit::test] |
| 2574 | fn declining_by_shortcut_only_acts_while_a_ring_shows(cx: &mut TestAppContext) { |
| 2575 | let app = app_with_chats(cx); |
| 2576 | cx.update(|cx| { |
| 2577 | app.update(cx, |state, cx| { |
| 2578 | state.decline_ringing_call(cx); |
| 2579 | assert!(!state.rings.is_ringing()); |
| 2580 | state.demo_incoming_ring(cx); |
| 2581 | assert!(state.rings.is_ringing()); |
| 2582 | state.decline_ringing_call(cx); |
| 2583 | assert!(!state.rings.is_ringing()); |
| 2584 | }) |
| 2585 | }); |
| 2586 | } |
| 2587 | |
| 2588 | #[gpui_kit::test] |
| 2589 | fn picking_another_chat_keeps_the_call_window_showing_the_call(cx: &mut TestAppContext) { |
| 2590 | let app = app_with_call(cx); |
| 2591 | cx.update(|cx| { |
| 2592 | app.update(cx, |state, cx| { |
| 2593 | assert!(state.viewing_call()); |
| 2594 | state.select(Selection::Chat("c1".into()), cx); |
| 2595 | assert!(state.viewing_call()); |
| 2596 | }) |
| 2597 | }); |
| 2598 | } |
| 2599 | |
| 2600 | #[gpui_kit::test] |
| 2601 | fn an_ended_call_closes_the_view_and_raises_a_notice(cx: &mut TestAppContext) { |
| 2602 | let app = app_with_call(cx); |
| 2603 | let since = Instant::now() - Duration::from_secs(42); |
| 2604 | cx.update(|cx| { |
| 2605 | app.update(cx, |state, cx| { |
| 2606 | state.apply_call_update(7, CallUpdate::State(CallState::Connected { since }), cx); |
| 2607 | assert!(state.call.is_some()); |
| 2608 | state.apply_call_update( |
| 2609 | 7, |
| 2610 | CallUpdate::State(CallState::Ended { |
| 2611 | reason: EndReason::LocalHangup, |
| 2612 | }), |
| 2613 | cx, |
| 2614 | ); |
| 2615 | assert!(state.call.is_none()); |
| 2616 | let notice = state.notice.as_ref().expect("notice"); |
| 2617 | assert_eq!(notice.text, "Call ended 00:42"); |
| 2618 | }) |
| 2619 | }); |
| 2620 | } |
| 2621 | |
| 2622 | #[gpui_kit::test] |
| 2623 | fn a_dropped_call_shows_the_end_screen_and_plays_the_end_tone(cx: &mut TestAppContext) { |
| 2624 | let app = app_with_call(cx); |
| 2625 | let tones = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); |
| 2626 | let heard = tones.clone(); |
| 2627 | cx.update(|cx| { |
| 2628 | cx.subscribe(&app, move |_, event: &AppEvent, _| { |
| 2629 | if let AppEvent::CallTone(tone) = event { |
| 2630 | heard.borrow_mut().push(*tone); |
| 2631 | } |
| 2632 | }) |
| 2633 | .detach(); |
| 2634 | app.update(cx, |state, cx| { |
| 2635 | state.apply_call_update( |
| 2636 | 7, |
| 2637 | CallUpdate::State(CallState::Ended { |
| 2638 | reason: EndReason::Dropped, |
| 2639 | }), |
| 2640 | cx, |
| 2641 | ); |
| 2642 | assert!(state.call.is_none()); |
| 2643 | assert!(state.notice.is_none()); |
| 2644 | let screen = state.call_end.as_ref().expect("end screen"); |
| 2645 | assert_eq!(screen.heading(), "Call dropped"); |
| 2646 | assert_eq!(screen.retry_label(), "Call back"); |
| 2647 | state.dismiss_call_end(cx); |
| 2648 | assert!(state.call_end.is_none()); |
| 2649 | }) |
| 2650 | }); |
| 2651 | assert_eq!(*tones.borrow(), vec![CallTone::CallEnded]); |
| 2652 | } |
| 2653 | |
| 2654 | #[gpui_kit::test] |
| 2655 | fn a_normal_end_keeps_the_notice_and_no_end_screen(cx: &mut TestAppContext) { |
| 2656 | let app = app_with_call(cx); |
| 2657 | cx.update(|cx| { |
| 2658 | app.update(cx, |state, cx| { |
| 2659 | state.apply_call_update( |
| 2660 | 7, |
| 2661 | CallUpdate::State(CallState::Ended { |
| 2662 | reason: EndReason::Remote(EndKind::Removed), |
| 2663 | }), |
| 2664 | cx, |
| 2665 | ); |
| 2666 | assert!(state.call_end.is_none()); |
| 2667 | assert!(state.notice.is_some()); |
| 2668 | }) |
| 2669 | }); |
| 2670 | } |
| 2671 | |
| 2672 | #[gpui_kit::test] |
| 2673 | fn full_window_exists_only_while_someone_shares_and_escape_leaves_it(cx: &mut TestAppContext) { |
| 2674 | let app = app_with_call(cx); |
| 2675 | cx.update(|cx| { |
| 2676 | app.update(cx, |state, cx| { |
| 2677 | state.toggle_stage_fullscreen(cx); |
| 2678 | assert!(!state.stage_fullscreen()); |
| 2679 | state.apply_call_update(7, CallUpdate::ScreenShare(Some("8:orgid:a".into())), cx); |
| 2680 | state.toggle_stage_fullscreen(cx); |
| 2681 | assert!(state.stage_fullscreen()); |
| 2682 | state.leave_stage_fullscreen(cx); |
| 2683 | assert!(!state.stage_fullscreen()); |
| 2684 | state.toggle_stage_fullscreen(cx); |
| 2685 | state.apply_call_update(7, CallUpdate::ScreenShare(None), cx); |
| 2686 | assert!(!state.stage_fullscreen()); |
| 2687 | }) |
| 2688 | }); |
| 2689 | } |
| 2690 | |
| 2691 | #[gpui_kit::test] |
| 2692 | fn a_declined_call_names_who_declined(cx: &mut TestAppContext) { |
| 2693 | let app = app_with_call(cx); |
| 2694 | cx.update(|cx| { |
| 2695 | app.update(cx, |state, cx| { |
| 2696 | state.call.as_mut().unwrap().model.peer_name = "Bea".into(); |
| 2697 | state.apply_call_update( |
| 2698 | 7, |
| 2699 | CallUpdate::State(CallState::Ended { |
| 2700 | reason: EndReason::Remote(EndKind::Declined), |
| 2701 | }), |
| 2702 | cx, |
| 2703 | ); |
| 2704 | assert_eq!(state.notice.as_ref().unwrap().text, "Bea declined"); |
| 2705 | }) |
| 2706 | }); |
| 2707 | } |
| 2708 | |
| 2709 | #[gpui_kit::test] |
| 2710 | fn updates_of_an_older_call_are_ignored(cx: &mut TestAppContext) { |
| 2711 | let app = app_with_call(cx); |
| 2712 | cx.update(|cx| { |
| 2713 | app.update(cx, |state, cx| { |
| 2714 | state.apply_call_update( |
| 2715 | 6, |
| 2716 | CallUpdate::State(CallState::Ended { |
| 2717 | reason: EndReason::Dropped, |
| 2718 | }), |
| 2719 | cx, |
| 2720 | ); |
| 2721 | assert!(state.call.is_some()); |
| 2722 | assert!(state.notice.is_none()); |
| 2723 | }) |
| 2724 | }); |
| 2725 | } |
| 2726 | |
| 2727 | #[gpui_kit::test] |
| 2728 | fn a_ring_that_times_out_becomes_a_missed_call_event(cx: &mut TestAppContext) { |
| 2729 | cx.update(gpui_kit::init); |
| 2730 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 2731 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 2732 | cx.update(|cx| { |
| 2733 | app.update(cx, |state, cx| { |
| 2734 | state.on_engine_event(EngineEvent::Incoming(demo_ring()), cx); |
| 2735 | assert!(state.rings.is_ringing()); |
| 2736 | state.tick_rings(cx); |
| 2737 | assert!(state.rings.is_ringing()); |
| 2738 | state.on_engine_event( |
| 2739 | EngineEvent::RingEnded { |
| 2740 | ring_id: demo_ring().ring_id, |
| 2741 | kind: EndKind::Cancelled, |
| 2742 | answered_by: None, |
| 2743 | }, |
| 2744 | cx, |
| 2745 | ); |
| 2746 | assert!(!state.rings.is_ringing()); |
| 2747 | }) |
| 2748 | }); |
| 2749 | } |
| 2750 | |
| 2751 | #[gpui_kit::test] |
| 2752 | fn calls_are_refused_for_meeting_chats_and_missing_chats(cx: &mut TestAppContext) { |
| 2753 | let app = app_with_chats(cx); |
| 2754 | cx.update(|cx| { |
| 2755 | app.update(cx, |state, cx| { |
| 2756 | state.start_chat_call("19:meeting@thread.v2", cx); |
| 2757 | assert!(state.call.is_none()); |
| 2758 | assert_eq!( |
| 2759 | state.notice.as_ref().unwrap().text, |
| 2760 | "This chat cannot be called" |
| 2761 | ); |
| 2762 | state.start_chat_call("19:unknown", cx); |
| 2763 | assert!(state.call.is_none()); |
| 2764 | }) |
| 2765 | }); |
| 2766 | } |
| 2767 | |
| 2768 | #[gpui_kit::test] |
| 2769 | fn a_missed_call_finds_the_one_on_one_chat_of_the_caller(cx: &mut TestAppContext) { |
| 2770 | let app = app_with_chats(cx); |
| 2771 | cx.update(|cx| { |
| 2772 | app.update(cx, |state, _| { |
| 2773 | let missed = super::MissedCall { |
| 2774 | caller_mri: "8:orgid:bea-id".into(), |
| 2775 | caller_name: "Bea".into(), |
| 2776 | thread_id: None, |
| 2777 | at: chrono::Utc::now(), |
| 2778 | }; |
| 2779 | assert_eq!( |
| 2780 | state.conversation_of_missed_call(&missed).as_deref(), |
| 2781 | Some("19:dm@unq.gbl.spaces") |
| 2782 | ); |
| 2783 | let direct = super::MissedCall { |
| 2784 | thread_id: Some("19:dm@unq.gbl.spaces".into()), |
| 2785 | ..missed.clone() |
| 2786 | }; |
| 2787 | assert_eq!( |
| 2788 | state.conversation_of_missed_call(&direct).as_deref(), |
| 2789 | Some("19:dm@unq.gbl.spaces") |
| 2790 | ); |
| 2791 | }) |
| 2792 | }); |
| 2793 | } |
| 2794 | |
| 2795 | #[gpui_kit::test] |
| 2796 | fn an_accepted_one_on_one_ring_gets_the_chat_with_the_caller(cx: &mut TestAppContext) { |
| 2797 | let app = app_with_chats(cx); |
| 2798 | cx.update(|cx| { |
| 2799 | app.update(cx, |state, _| { |
| 2800 | let ring = calling::IncomingRing { |
| 2801 | caller: calling::Caller { |
| 2802 | mri: "8:orgid:bea-id".into(), |
| 2803 | display_name: "Bea".into(), |
| 2804 | }, |
| 2805 | thread_id: None, |
| 2806 | ..demo_ring() |
| 2807 | }; |
| 2808 | assert_eq!( |
| 2809 | state.conversation_of_ring(&ring).as_deref(), |
| 2810 | Some("19:dm@unq.gbl.spaces") |
| 2811 | ); |
| 2812 | let group = calling::IncomingRing { |
| 2813 | is_group: true, |
| 2814 | ..ring |
| 2815 | }; |
| 2816 | assert_eq!(state.conversation_of_ring(&group), None); |
| 2817 | }) |
| 2818 | }); |
| 2819 | } |
| 2820 | |
| 2821 | #[gpui_kit::test] |
| 2822 | fn only_meeting_chats_and_channels_watch_for_a_running_meeting(cx: &mut TestAppContext) { |
| 2823 | let app = app_with_chats(cx); |
| 2824 | cx.update(|cx| { |
| 2825 | app.update(cx, |state, _| { |
| 2826 | assert!(state.watches_live_meeting("19:meeting@thread.v2")); |
| 2827 | assert!(!state.watches_live_meeting("19:dm@unq.gbl.spaces")); |
| 2828 | assert!(!state.watches_live_meeting("19:unknown")); |
| 2829 | }) |
| 2830 | }); |
| 2831 | } |
| 2832 | |
| 2833 | #[gpui_kit::test] |
| 2834 | fn a_live_meeting_shows_until_it_ends(cx: &mut TestAppContext) { |
| 2835 | let app = app_with_chats(cx); |
| 2836 | let meeting = calling::meeting::LiveMeeting { |
| 2837 | thread_id: "19:meeting@thread.v2".into(), |
| 2838 | conversation_url: None, |
| 2839 | expiration: Some(chrono::Utc::now().timestamp() + 600), |
| 2840 | organizer_id: Some("org".into()), |
| 2841 | tenant_id: Some("tenant".into()), |
| 2842 | meeting_code: None, |
| 2843 | passcode: None, |
| 2844 | }; |
| 2845 | cx.update(|cx| { |
| 2846 | app.update(cx, |state, cx| { |
| 2847 | assert!(state.running_meeting("19:meeting@thread.v2").is_none()); |
| 2848 | state.apply_live_meeting("19:meeting@thread.v2".into(), Some(meeting), cx); |
| 2849 | assert!(state.running_meeting("19:meeting@thread.v2").is_some()); |
| 2850 | state.apply_live_meeting("19:meeting@thread.v2".into(), None, cx); |
| 2851 | assert!(state.running_meeting("19:meeting@thread.v2").is_none()); |
| 2852 | }) |
| 2853 | }); |
| 2854 | } |
| 2855 | } |