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