BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / actions.rs

actions.rs

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