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