BlueGit

ict rusty-teams

Files

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

actions.rs

actions.rs 74.7 KiB Download Raw
1use std::time::{Duration, Instant};
2
3use calling::meeting::LiveMeeting;
4use calling::{
5 AcceptMode, CallCommand, CallHandle, CallSpec, CallState, CallUpdate, Callee, DeviceChoice,
6 EndReason, EngineEvent, 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::room_closed_move;
18use super::media::CallMedia;
19use super::model::{ActiveCall, CallKind, CallModel, ended_notice};
20use super::ring::{MissedCall, RingOutcome};
21use super::target::{is_organizer, plan_for_chat};
22use crate::app_state::{AppEvent, AppState, Selection, chat_title};
23use crate::notify::selection_for;
24
25const TIMER_REFRESH: Duration = Duration::from_millis(500);
26const SHARE_SOURCES_EVERY_TICKS: u32 = 10;
27pub(super) const NOT_CONNECTED_NOTICE: &str = "Not connected to Teams yet";
28const READ_ONLY_NOTICE: &str = "Calls are off in read-only mode";
29const NOT_CALLABLE_NOTICE: &str = "This chat cannot be called";
30pub(super) const MEETING_UNAVAILABLE_NOTICE: &str = "This meeting cannot be joined";
31const ANSWER_FAILED_NOTICE: &str = "Could not answer the call";
32const MEETING_KIND: &str = "meeting";
33const DEMO_LOBBY_MEETING: &str = "demo-chat-meeting-retro";
34const DEMO_CROWD: usize = 11;
35const DEMO_SMALL_MEETING: usize = 4;
36const DEMO_LIVE_MINUTES: i64 = 60;
37const ORGID_PREFIX: &str = "8:orgid:";
38pub const SHARE_SOUND_META_KEY: &str = "call_share_sound";
39const CAMERA_META_KEY: &str = "call_camera";
40const DEFAULT_CAMERA_META: &str = "default";
41
42pub fn load_share_sound(store: &store::Store) -> bool {
43 store.meta(SHARE_SOUND_META_KEY).ok().flatten().as_deref() == Some("1")
44}
45
46pub fn load_camera(store: &store::Store) -> DeviceChoice {
47 match store.meta(CAMERA_META_KEY).ok().flatten() {
48 Some(key) if !key.is_empty() && key != DEFAULT_CAMERA_META => DeviceChoice::Device(key),
49 _ => DeviceChoice::SystemDefault,
50 }
51}
52
53fn camera_meta(choice: &DeviceChoice) -> &str {
54 match choice {
55 DeviceChoice::SystemDefault => DEFAULT_CAMERA_META,
56 DeviceChoice::Device(key) => key,
57 }
58}
59
60fn now_unix() -> i64 {
61 chrono::Utc::now().timestamp()
62}
63
64impl AppState {
65 pub fn start_test_call(&mut self, cx: &mut Context<Self>) {
66 if self.call.is_some() {
67 self.show_call(true, cx);
68 return;
69 }
70 let Some(handle) = self.place_call(CallSpec::Echo, DemoCall::Test, false, cx) else {
71 return;
72 };
73 self.begin_call(handle, CallModel::test(), None, cx);
74 }
75
76 pub fn start_chat_call(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
77 self.start_chat_call_with(conversation_id, false, cx);
78 }
79
80 pub fn start_chat_video_call(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
81 self.start_chat_call_with(conversation_id, true, cx);
82 }
83
84 fn start_chat_call_with(
85 &mut self,
86 conversation_id: &str,
87 with_video: bool,
88 cx: &mut Context<Self>,
89 ) {
90 if self.call.is_some() {
91 self.show_call(true, cx);
92 return;
93 }
94 let plan = self
95 .sidebar
96 .chats
97 .iter()
98 .find(|chat| chat.id == conversation_id)
99 .and_then(|chat| plan_for_chat(chat, self.directory.me.as_ref()));
100 let Some(plan) = plan else {
101 self.raise_notice(NOT_CALLABLE_NOTICE.to_owned(), None, cx);
102 return;
103 };
104 let spec = CallSpec::People {
105 callees: plan
106 .callees
107 .iter()
108 .map(|(mri, name)| Callee {
109 mri: mri.clone(),
110 display_name: name.clone(),
111 })
112 .collect(),
113 thread_id: conversation_id.to_owned(),
114 };
115 let Some(handle) =
116 self.place_call(spec, DemoCall::People(plan.callees.clone()), with_video, cx)
117 else {
118 return;
119 };
120 let user_ids = plan
121 .callees
122 .iter()
123 .filter_map(|(mri, _)| mri.strip_prefix(ORGID_PREFIX).map(str::to_owned))
124 .collect();
125 self.request_avatars(user_ids, cx);
126 let model = CallModel::people(&plan.title, &plan.callees);
127 self.begin_call(handle, model, Some(conversation_id.to_owned()), cx);
128 if with_video && self.mode.demo {
129 self.send_call_command(CallCommand::SetCamera(true));
130 }
131 }
132
133 pub fn join_meeting_call(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
134 if self.call.is_some() {
135 self.show_call(true, cx);
136 return;
137 }
138 let Some(meeting) = self.running_meeting(conversation_id).cloned() else {
139 self.raise_notice(MEETING_UNAVAILABLE_NOTICE.to_owned(), None, cx);
140 return;
141 };
142 let Some(target) = meeting.target() else {
143 self.raise_notice(MEETING_UNAVAILABLE_NOTICE.to_owned(), None, cx);
144 return;
145 };
146 let guests = if conversation_id == DEMO_LOBBY_MEETING {
147 DEMO_SMALL_MEETING
148 } else {
149 DEMO_CROWD
150 };
151 let demo = DemoCall::Meeting {
152 guests: demo_guests(guests),
153 lobby: conversation_id == DEMO_LOBBY_MEETING,
154 organizer: conversation_id != DEMO_LOBBY_MEETING,
155 };
156 let Some(handle) = self.place_call(CallSpec::Meeting(target.clone()), demo, false, cx)
157 else {
158 return;
159 };
160 let title = selection_for(&self.sidebar, conversation_id)
161 .map(|selection| crate::app_state::selection_title(&self.sidebar, &selection))
162 .unwrap_or_else(|| "Meeting".to_owned());
163 let model = CallModel::meeting(&title, is_organizer(&meeting, self.directory.me.as_ref()));
164 self.begin_call(handle, model, Some(conversation_id.to_owned()), cx);
165 if let Some(call) = self.call.as_mut() {
166 call.meeting_target = Some(target);
167 }
168 }
169
170 pub(super) fn place_call(
171 &mut self,
172 spec: CallSpec,
173 demo: DemoCall,
174 with_video: bool,
175 cx: &mut Context<Self>,
176 ) -> Option<CallHandle> {
177 if self.mode.demo {
178 return Some(start_demo_call(demo));
179 }
180 if self.mode.read_only {
181 self.raise_notice(READ_ONLY_NOTICE.to_owned(), None, cx);
182 return None;
183 }
184 match self.call_launcher.clone() {
185 Some(launcher) if with_video => Some(launcher.start_video_call(spec)),
186 Some(launcher) => Some(launcher.start_call(spec)),
187 None => {
188 self.raise_notice(NOT_CONNECTED_NOTICE.to_owned(), None, cx);
189 None
190 }
191 }
192 }
193
194 pub(super) fn begin_call(
195 &mut self,
196 handle: CallHandle,
197 model: CallModel,
198 conversation_id: Option<String>,
199 cx: &mut Context<Self>,
200 ) {
201 self.call_count += 1;
202 let call_id = self.call_count;
203 let CallHandle {
204 commands,
205 updates,
206 video,
207 } = handle;
208 self.call = Some(ActiveCall::new(
209 call_id,
210 model,
211 commands,
212 conversation_id,
213 video,
214 ));
215 if self.call_share_sound {
216 self.send_call_command(CallCommand::SetShareSound(true));
217 }
218 if let Some(call) = self.call.as_mut() {
219 call.model.background = self.call_background.clone();
220 call.model.camera = self.call_camera.clone();
221 }
222 if self.call_camera != DeviceChoice::SystemDefault {
223 self.send_call_command(CallCommand::SelectCamera(self.call_camera.clone()));
224 }
225 if self.call_background != BackgroundPick::None {
226 self.send_background_command(cx);
227 }
228 self.refresh_call_backgrounds(cx);
229 self.new_chat = false;
230 self.follow_call(call_id, updates, cx);
231 cx.emit(AppEvent::Call);
232 cx.notify();
233 }
234
235 fn follow_call(
236 &mut self,
237 call_id: u64,
238 mut updates: tokio::sync::mpsc::UnboundedReceiver<CallUpdate>,
239 cx: &mut Context<Self>,
240 ) {
241 cx.spawn(async move |this, cx| {
242 while let Some(update) = updates.recv().await {
243 let alive =
244 this.update(cx, |state, cx| state.apply_call_update(call_id, update, cx));
245 if alive.is_err() {
246 return;
247 }
248 }
249 let _ = this.update(cx, |state, cx| state.end_orphaned_call(call_id, cx));
250 })
251 .detach();
252 cx.spawn(async move |this, cx| {
253 let mut ticks = 0u32;
254 loop {
255 cx.background_executor().timer(TIMER_REFRESH).await;
256 ticks += 1;
257 let running = this.update(cx, |state, cx| {
258 let running = state.call.as_ref().is_some_and(|call| call.id == call_id);
259 if running {
260 if ticks.is_multiple_of(SHARE_SOURCES_EVERY_TICKS) {
261 state.refresh_share_sources();
262 }
263 CallMedia::changed(&state.call_media, cx);
264 }
265 running
266 });
267 if !running.unwrap_or(false) {
268 return;
269 }
270 }
271 })
272 .detach();
273 }
274
275 fn end_orphaned_call(&mut self, call_id: u64, cx: &mut Context<Self>) {
276 let still_open = self
277 .call
278 .as_ref()
279 .is_some_and(|call| call.id == call_id && call.model.is_active());
280 if still_open {
281 let reason = EndReason::Failed("The call stopped unexpectedly".to_owned());
282 self.apply_call_update(call_id, CallUpdate::State(CallState::Ended { reason }), cx);
283 }
284 }
285
286 pub(super) fn apply_call_update(
287 &mut self,
288 call_id: u64,
289 update: CallUpdate,
290 cx: &mut Context<Self>,
291 ) {
292 let media = self.call_media.clone();
293 let Some(call) = self.call.as_mut().filter(|call| call.id == call_id) else {
294 return;
295 };
296 match update {
297 CallUpdate::VideoReady => {
298 call.pictures.apply(&call.video, cx);
299 CallMedia::changed(&media, cx);
300 return;
301 }
302 CallUpdate::Stats { .. } => return,
303 CallUpdate::Levels { local, remote } => {
304 let before = (call.model.local_speaking, call.model.remote_speaking);
305 call.model.apply(CallUpdate::Levels { local, remote });
306 if before != (call.model.local_speaking, call.model.remote_speaking) {
307 CallMedia::changed(&media, cx);
308 }
309 return;
310 }
311 CallUpdate::Notice(text) => {
312 self.raise_notice(text, None, cx);
313 return;
314 }
315 CallUpdate::WhiteboardUrl(url) => {
316 cx.open_url(&url);
317 return;
318 }
319 CallUpdate::BreakoutMove(moved) => {
320 self.begin_breakout_move(moved, cx);
321 return;
322 }
323 _ => {}
324 }
325 if let (CallUpdate::Caption(entry), Some(thread)) = (&update, call.chat_thread()) {
326 let speaker = if entry.display_name.is_empty() {
327 "Someone"
328 } else {
329 &entry.display_name
330 };
331 crate::agent_api::note_caption(thread, &entry.id, speaker, &entry.text, cx);
332 }
333 call.model.apply(update);
334 if call.model.screen_sharer.is_none() {
335 call.stage_fullscreen = false;
336 call.pictures.forget(&VideoKey::Screen, cx);
337 }
338 if !call.model.camera_on {
339 call.pictures.forget(&VideoKey::LocalCamera, cx);
340 }
341 let Some(reason) = call.model.ended_reason().cloned() else {
342 cx.emit(AppEvent::Call);
343 cx.notify();
344 return;
345 };
346 let elapsed = call.model.elapsed(Instant::now());
347 let notice = ended_notice(&reason, elapsed, &call.model.peer_name);
348 let next_move = call
349 .pending_move
350 .take()
351 .or_else(|| room_closed_move(call, &reason));
352 let consult = call.consult.take();
353 call.pictures.clear(cx);
354 if let Some(thread) = call.chat_thread() {
355 crate::agent_api::clear_captions(thread, cx);
356 }
357 self.call = None;
358 if let Some(consult) = consult {
359 let _ = consult.commands.send(CallCommand::Hangup);
360 }
361 match next_move {
362 Some(next_move) => self.join_moved_call(next_move, cx),
363 None => self.raise_notice(notice, None, cx),
364 }
365 self.refresh_call_history_after_call(cx);
366 cx.emit(AppEvent::Call);
367 cx.notify();
368 }
369
370 pub(super) fn send_call_command(&self, command: CallCommand) {
371 if let Some(call) = &self.call {
372 let _ = call.commands.send(command);
373 }
374 }
375
376 pub fn toggle_call_mute(&mut self, _cx: &mut Context<Self>) {
377 let can_toggle = self
378 .call
379 .as_ref()
380 .is_some_and(|call| call.model.can_unmute());
381 if can_toggle {
382 self.send_call_command(CallCommand::Mute(MuteCommand::Toggle));
383 }
384 }
385
386 pub fn toggle_call_camera(&mut self, _cx: &mut Context<Self>) {
387 let Some(call) = self
388 .call
389 .as_ref()
390 .filter(|call| call.model.can_use_camera())
391 else {
392 return;
393 };
394 let on = !call.model.camera_on;
395 self.send_call_command(CallCommand::SetCamera(on));
396 }
397
398 pub fn select_call_camera(&mut self, choice: DeviceChoice, _cx: &mut Context<Self>) {
399 let _ = self.store.set_meta(CAMERA_META_KEY, camera_meta(&choice));
400 self.call_camera = choice.clone();
401 if let Some(launcher) = &self.call_launcher {
402 launcher.set_default_camera(choice.clone());
403 }
404 if let Some(call) = self.call.as_mut() {
405 call.model.camera = choice.clone();
406 }
407 self.send_call_command(CallCommand::SelectCamera(choice));
408 }
409
410 pub fn start_call_share(&mut self, source: ShareSource, _cx: &mut Context<Self>) {
411 self.send_call_command(CallCommand::StartShare(source));
412 }
413
414 pub fn toggle_call_share(&mut self, cx: &mut Context<Self>) {
415 let Some(call) = self.call.as_ref() else {
416 return;
417 };
418 if call.model.local_share.is_some() {
419 self.stop_call_share(cx);
420 return;
421 }
422 if !call.model.can_share() {
423 return;
424 }
425 if let Some(source) = call.model.screens().first().map(|source| (*source).clone()) {
426 self.start_call_share(source, cx);
427 }
428 }
429
430 pub fn stop_call_share(&mut self, _cx: &mut Context<Self>) {
431 self.send_call_command(CallCommand::StopShare);
432 }
433
434 pub fn refresh_share_sources(&self) {
435 let idle = self
436 .call
437 .as_ref()
438 .is_some_and(|call| call.model.local_share.is_none());
439 if idle {
440 self.send_call_command(CallCommand::RefreshShareSources);
441 }
442 }
443
444 pub fn leave_call(&mut self, _cx: &mut Context<Self>) {
445 self.send_call_command(CallCommand::Hangup);
446 }
447
448 pub fn end_meeting_for_all(&mut self, _cx: &mut Context<Self>) {
449 let allowed = self
450 .call
451 .as_ref()
452 .is_some_and(|call| call.model.can_end_meeting);
453 if allowed {
454 self.send_call_command(CallCommand::EndMeeting);
455 }
456 }
457
458 pub fn select_call_input(&mut self, choice: DeviceChoice, _cx: &mut Context<Self>) {
459 self.send_call_command(CallCommand::SelectInput(choice));
460 }
461
462 pub fn select_call_output(&mut self, choice: DeviceChoice, _cx: &mut Context<Self>) {
463 self.send_call_command(CallCommand::SelectOutput(choice));
464 }
465
466 pub fn show_call(&mut self, viewing: bool, cx: &mut Context<Self>) {
467 let Some(call) = self.call.as_mut().filter(|call| call.viewing != viewing) else {
468 return;
469 };
470 call.viewing = viewing;
471 if viewing {
472 self.new_chat = false;
473 }
474 cx.emit(AppEvent::Call);
475 cx.notify();
476 }
477
478 pub fn toggle_call_chat(&mut self, cx: &mut Context<Self>) {
479 let Some(call) = self
480 .call
481 .as_mut()
482 .filter(|call| call.chat_thread().is_some())
483 else {
484 return;
485 };
486 call.chat_open = !call.chat_open;
487 cx.emit(AppEvent::Call);
488 cx.notify();
489 }
490
491 pub fn call_chat_thread(&self) -> Option<String> {
492 let call = self.call.as_ref().filter(|call| call.chat_open)?;
493 call.chat_thread().map(str::to_owned)
494 }
495
496 pub fn call_chat_unread(&self) -> u32 {
497 let Some(thread) = self
498 .call
499 .as_ref()
500 .filter(|call| !call.chat_open)
501 .and_then(|call| call.chat_thread())
502 else {
503 return 0;
504 };
505 let counted = self.directory.unread_counts.get(thread).copied();
506 counted.unwrap_or_else(|| {
507 u32::from(
508 self.sidebar
509 .chats
510 .iter()
511 .any(|chat| chat.id == thread && chat.unread),
512 )
513 })
514 }
515
516 pub fn toggle_call_hand(&mut self, cx: &mut Context<Self>) {
517 let Some(call) = self
518 .call
519 .as_mut()
520 .filter(|call| call.model.is_active() && call.model.has_reaction_controls())
521 else {
522 return;
523 };
524 let raised = call.model.own_hand.is_none();
525 call.model.own_hand = raised.then_some(u64::MAX);
526 self.send_call_command(CallCommand::SetHand(raised));
527 if raised {
528 self.raise_notice("Your hand is raised".to_owned(), None, cx);
529 }
530 cx.emit(AppEvent::Call);
531 cx.notify();
532 }
533
534 pub fn lower_call_hand(&mut self, mri: String, _cx: &mut Context<Self>) {
535 let allowed = self
536 .call
537 .as_ref()
538 .is_some_and(|call| call.model.can_lower_hands());
539 if allowed {
540 self.send_call_command(CallCommand::LowerHand { mri });
541 }
542 }
543
544 pub fn lower_all_call_hands(&mut self, _cx: &mut Context<Self>) {
545 let allowed = self
546 .call
547 .as_ref()
548 .is_some_and(|call| call.model.can_lower_hands() && call.model.any_hand_raised());
549 if allowed {
550 self.send_call_command(CallCommand::LowerAllHands);
551 }
552 }
553
554 pub fn send_call_reaction(&mut self, reaction: Reaction, cx: &mut Context<Self>) {
555 let Some(call) = self
556 .call
557 .as_mut()
558 .filter(|call| call.model.is_active() && call.model.has_reaction_controls())
559 else {
560 return;
561 };
562 if let Some(own_mri) = call.model.own_mri.clone() {
563 call.model.note_reaction(own_mri, reaction, Instant::now());
564 }
565 self.send_call_command(CallCommand::SendReaction(reaction));
566 cx.emit(AppEvent::Call);
567 cx.notify();
568 }
569
570 pub fn set_call_share_sound(&mut self, on: bool, cx: &mut Context<Self>) {
571 self.call_share_sound = on;
572 let _ = self
573 .store
574 .set_meta(SHARE_SOUND_META_KEY, if on { "1" } else { "0" });
575 self.send_call_command(CallCommand::SetShareSound(on));
576 cx.notify();
577 }
578
579 pub fn admit_call_guest(&mut self, mri: String, _cx: &mut Context<Self>) {
580 if self
581 .call
582 .as_ref()
583 .is_some_and(|call| call.model.can_admit())
584 {
585 self.send_call_command(CallCommand::Admit { mri });
586 }
587 }
588
589 pub fn admit_all_call_guests(&mut self, _cx: &mut Context<Self>) {
590 if self
591 .call
592 .as_ref()
593 .is_some_and(|call| call.model.can_admit() && !call.model.lobby_guests().is_empty())
594 {
595 self.send_call_command(CallCommand::AdmitAll);
596 }
597 }
598
599 pub fn deny_call_guest(&mut self, mri: String, _cx: &mut Context<Self>) {
600 if self
601 .call
602 .as_ref()
603 .is_some_and(|call| call.model.can_admit())
604 {
605 self.send_call_command(CallCommand::Deny { mri });
606 }
607 }
608
609 pub fn mute_call_participant(&mut self, mri: String, _cx: &mut Context<Self>) {
610 if self
611 .call
612 .as_ref()
613 .is_some_and(|call| call.model.can_manage())
614 {
615 self.send_call_command(CallCommand::MuteParticipant { mri });
616 }
617 }
618
619 pub fn mute_all_call(&mut self, _cx: &mut Context<Self>) {
620 if self
621 .call
622 .as_ref()
623 .is_some_and(|call| call.model.can_manage())
624 {
625 self.send_call_command(CallCommand::MuteAll);
626 }
627 }
628
629 pub fn remove_call_participant(&mut self, mri: String, _cx: &mut Context<Self>) {
630 if self
631 .call
632 .as_ref()
633 .is_some_and(|call| call.model.can_manage())
634 {
635 self.send_call_command(CallCommand::RemoveParticipant { mri });
636 }
637 }
638
639 pub fn toggle_call_spotlight(&mut self, mri: String, _cx: &mut Context<Self>) {
640 let Some(call) = self
641 .call
642 .as_ref()
643 .filter(|call| call.model.can_manage() && call.model.is_active())
644 else {
645 return;
646 };
647 let command = if call.model.is_spotlighted(&mri) {
648 CallCommand::StopSpotlight { mri }
649 } else {
650 CallCommand::Spotlight { mri }
651 };
652 self.send_call_command(command);
653 }
654
655 pub fn toggle_call_pin(&mut self, mri: &str, cx: &mut Context<Self>) {
656 if let Some(call) = self.call.as_mut() {
657 call.model.toggle_pin(mri);
658 cx.notify();
659 }
660 }
661
662 pub fn toggle_call_captions(&mut self, _cx: &mut Context<Self>) {
663 let Some(call) = self
664 .call
665 .as_ref()
666 .filter(|call| call.model.is_active() && call.model.kind == CallKind::Meeting)
667 else {
668 return;
669 };
670 let on = !call.model.captions_on();
671 self.send_call_command(CallCommand::SetCaptions(on));
672 }
673
674 pub fn set_call_background(&mut self, pick: BackgroundPick, cx: &mut Context<Self>) {
675 let _ = self.store.set_meta(BACKGROUND_META_KEY, &pick.to_meta());
676 self.call_background = pick.clone();
677 if let Some(call) = self.call.as_mut() {
678 call.model.background = pick;
679 }
680 self.send_background_command(cx);
681 cx.notify();
682 }
683
684 pub(super) fn send_background_command(&mut self, cx: &mut Context<Self>) {
685 let pick = self.call_background.clone();
686 if let Some(choice) = self.call_backgrounds.choice_for(&pick) {
687 self.send_call_command(CallCommand::SetBackground(choice));
688 return;
689 }
690 let BackgroundPick::Default(id) = pick else {
691 return;
692 };
693 let Some(image) = self.call_backgrounds.find(&id).cloned() else {
694 return;
695 };
696 let Some(launcher) = self.call_launcher.clone() else {
697 return;
698 };
699 let cache = self.call_backgrounds.cache.clone();
700 let receiver =
701 crate::runtime::spawn(
702 async move { launcher.download_background(&cache, &image).await },
703 );
704 cx.spawn(async move |this, cx| {
705 let path = receiver.await.ok().flatten();
706 this.update(cx, |state, cx| match path {
707 Some(path) if state.call_background == BackgroundPick::Default(id) => {
708 state.send_call_command(CallCommand::SetBackground(
709 calling::BackgroundChoice::Image(path),
710 ));
711 }
712 Some(_) => {}
713 None => state.raise_notice(
714 "That background could not be downloaded".to_owned(),
715 None,
716 cx,
717 ),
718 })
719 .ok();
720 })
721 .detach();
722 }
723
724 pub fn refresh_call_backgrounds(&mut self, cx: &mut Context<Self>) {
725 if self.mode.demo || !self.call_backgrounds.needs_refresh() {
726 return;
727 }
728 let Some(launcher) = self.call_launcher.clone() else {
729 return;
730 };
731 self.call_backgrounds.refreshing = true;
732 let cache = self.call_backgrounds.cache.clone();
733 let wanted = SHOWN_IMAGES;
734 let receiver =
735 crate::runtime::spawn(
736 async move { launcher.refresh_backgrounds(&cache, wanted).await },
737 );
738 cx.spawn(async move |this, cx| {
739 let catalog = receiver.await.ok().flatten();
740 this.update(cx, |state, cx| {
741 state.call_backgrounds.refreshing = false;
742 if let Some(catalog) = catalog {
743 state.call_backgrounds.images = catalog;
744 }
745 cx.notify();
746 })
747 .ok();
748 })
749 .detach();
750 }
751
752 pub fn add_call_background(&mut self, cx: &mut Context<Self>) {
753 let receiver = cx.prompt_for_paths(PathPromptOptions {
754 files: true,
755 directories: false,
756 multiple: false,
757 prompt: None,
758 });
759 cx.spawn(async move |this, cx| {
760 let Some(path) = receiver
761 .await
762 .ok()
763 .and_then(Result::ok)
764 .flatten()
765 .and_then(|paths| paths.into_iter().next())
766 else {
767 return;
768 };
769 this.update(cx, |state, cx| {
770 if !is_image_file(&path) {
771 state.raise_notice("Pick a JPG or PNG picture".to_owned(), None, cx);
772 return;
773 }
774 state.call_backgrounds.add_custom(path.clone());
775 save_custom_backgrounds(&state.store, &state.call_backgrounds.customs);
776 state.set_call_background(BackgroundPick::Custom(path), cx);
777 })
778 .ok();
779 })
780 .detach();
781 }
782
783 pub fn toggle_stage_fullscreen(&mut self, cx: &mut Context<Self>) {
784 let Some(call) = self
785 .call
786 .as_mut()
787 .filter(|call| call.model.screen_sharer.is_some())
788 else {
789 return;
790 };
791 call.stage_fullscreen = !call.stage_fullscreen;
792 cx.emit(AppEvent::Call);
793 cx.notify();
794 }
795
796 pub fn leave_stage_fullscreen(&mut self, cx: &mut Context<Self>) {
797 let Some(call) = self.call.as_mut().filter(|call| call.stage_fullscreen) else {
798 return;
799 };
800 call.stage_fullscreen = false;
801 cx.emit(AppEvent::Call);
802 cx.notify();
803 }
804
805 pub fn stage_fullscreen(&self) -> bool {
806 self.call
807 .as_ref()
808 .is_some_and(|call| call.stage_fullscreen && call.model.screen_sharer.is_some())
809 }
810
811 pub fn viewing_call(&self) -> bool {
812 self.call.as_ref().is_some_and(|call| call.viewing)
813 }
814}
815
816impl AppState {
817 pub fn on_engine_event(&mut self, event: EngineEvent, cx: &mut Context<Self>) {
818 match event {
819 EngineEvent::Incoming(ring) => self.push_ring(ring, false, cx),
820 EngineEvent::RingEnded { ring_id, kind, .. } => {
821 self.signal_ring(ring_id, RingSignal::Remote(kind), cx);
822 }
823 }
824 }
825
826 fn push_ring(&mut self, ring: calling::IncomingRing, demo: bool, cx: &mut Context<Self>) {
827 let caller_user_id = ring
828 .caller
829 .mri
830 .strip_prefix(ORGID_PREFIX)
831 .map(str::to_owned);
832 self.rings.push(ring, demo, Instant::now());
833 if let Some(user_id) = caller_user_id {
834 self.request_avatars(vec![user_id], cx);
835 }
836 cx.emit(AppEvent::Ring);
837 cx.notify();
838 }
839
840 pub fn demo_incoming_ring(&mut self, cx: &mut Context<Self>) {
841 self.push_ring(demo_ring(), true, cx);
842 }
843
844 pub fn demo_incoming_video_ring(&mut self, cx: &mut Context<Self>) {
845 self.push_ring(demo_video_ring(), true, cx);
846 }
847
848 pub fn start_demo_scene(&mut self, scene: DemoScene, cx: &mut Context<Self>) {
849 if self.call.is_some() {
850 return;
851 }
852 let (script, model, conversation_id) = if scene.is_one_to_one() {
853 (
854 DemoCall::People(vec![demo_caller()]),
855 CallModel::people("Mara Lindqvist", &[demo_caller()]),
856 "demo-chat-mara",
857 )
858 } else {
859 let script = DemoCall::Meeting {
860 guests: demo_guests(DEMO_SMALL_MEETING),
861 lobby: false,
862 organizer: true,
863 };
864 (
865 script,
866 CallModel::meeting("Standup", true),
867 "demo-chat-meeting-standup",
868 )
869 };
870 let handle = start_demo_scene_call(script, scene);
871 self.begin_call(handle, model, Some(conversation_id.to_owned()), cx);
872 if let Some(call) = self.call.as_mut().filter(|_| !scene.is_one_to_one()) {
873 call.meeting_target = Some(calling::MeetingTarget {
874 thread_id: conversation_id.to_owned(),
875 tenant_id: "demo-tenant".to_owned(),
876 organizer_id: "demo-me".to_owned(),
877 meeting_data: None,
878 });
879 }
880 }
881
882 fn signal_ring(&mut self, ring_id: u64, signal: RingSignal, cx: &mut Context<Self>) {
883 if let Some(outcome) = self.rings.signal(ring_id, signal, Instant::now()) {
884 self.finish_ring(ring_id, outcome, cx);
885 }
886 }
887
888 fn finish_ring(&mut self, ring_id: u64, outcome: RingOutcome, cx: &mut Context<Self>) {
889 if let RingOutcome::Missed(missed) = outcome {
890 if let Some(launcher) = &self.call_launcher {
891 launcher.drop_ring(ring_id);
892 }
893 cx.emit(AppEvent::MissedCall(missed));
894 }
895 cx.emit(AppEvent::Ring);
896 cx.notify();
897 }
898
899 pub fn tick_rings(&mut self, cx: &mut Context<Self>) {
900 if self.rings.is_empty() {
901 return;
902 }
903 for (ring_id, outcome) in self.rings.tick(Instant::now()) {
904 self.finish_ring(ring_id, outcome, cx);
905 }
906 }
907
908 pub fn accept_ringing_call(&mut self, cx: &mut Context<Self>) {
909 if let Some(ring_id) = self.rings.first_ringing_id() {
910 self.accept_ring(ring_id, AcceptMode::Audio, cx);
911 }
912 }
913
914 pub fn decline_ringing_call(&mut self, cx: &mut Context<Self>) {
915 if let Some(ring_id) = self.rings.first_ringing_id() {
916 self.decline_ring(ring_id, cx);
917 }
918 }
919
920 pub fn accept_ring(&mut self, ring_id: u64, mode: AcceptMode, cx: &mut Context<Self>) {
921 let Some(entry) = self.rings.get(ring_id).cloned() else {
922 return;
923 };
924 if self.call.is_some() {
925 self.leave_call(cx);
926 }
927 self.signal_ring(ring_id, RingSignal::Accept, cx);
928 let conversation_id = entry.ring.thread_id.clone();
929 let title = conversation_id
930 .as_deref()
931 .and_then(|id| self.sidebar.chats.iter().find(|chat| chat.id == id))
932 .filter(|_| entry.ring.is_group)
933 .map(chat_title)
934 .unwrap_or_else(|| entry.caller_name().to_owned());
935 let model = CallModel::incoming(&title, &entry.ring.caller.mri, entry.caller_name());
936 if entry.demo {
937 let handle = start_demo_call(DemoCall::Incoming(demo_caller()));
938 self.begin_call(handle, model, conversation_id, cx);
939 if mode == AcceptMode::Video {
940 self.send_call_command(CallCommand::SetCamera(true));
941 }
942 return;
943 }
944 let Some(launcher) = self.call_launcher.clone() else {
945 self.raise_notice(ANSWER_FAILED_NOTICE.to_owned(), None, cx);
946 return;
947 };
948 let receiver =
949 crate::runtime::spawn(async move { launcher.accept_ring(ring_id, mode).await });
950 cx.spawn(async move |this, cx| {
951 let handle = receiver.await.ok().flatten();
952 this.update(cx, |state, cx| match handle {
953 Some(handle) => state.begin_call(handle, model, conversation_id, cx),
954 None => state.raise_notice(ANSWER_FAILED_NOTICE.to_owned(), None, cx),
955 })
956 .ok();
957 })
958 .detach();
959 }
960
961 pub fn decline_ring(&mut self, ring_id: u64, cx: &mut Context<Self>) {
962 let Some(entry) = self.rings.get(ring_id) else {
963 return;
964 };
965 let demo = entry.demo;
966 if !demo && let Some(launcher) = &self.call_launcher {
967 launcher.decline_ring(ring_id);
968 }
969 self.signal_ring(ring_id, RingSignal::Decline, cx);
970 }
971
972 pub fn conversation_of_missed_call(&self, missed: &MissedCall) -> Option<String> {
973 if let Some(thread_id) = missed
974 .thread_id
975 .as_ref()
976 .filter(|id| self.sidebar.chats.iter().any(|chat| &chat.id == *id))
977 {
978 return Some(thread_id.clone());
979 }
980 let user_id = missed.caller_mri.strip_prefix(ORGID_PREFIX)?;
981 self.sidebar
982 .chats
983 .iter()
984 .find(|chat| {
985 crate::data::is_one_on_one(chat)
986 && chat
987 .members
988 .iter()
989 .any(|member| member.user_id.as_deref() == Some(user_id))
990 })
991 .map(|chat| chat.id.clone())
992 }
993}
994
995impl AppState {
996 pub fn running_meeting(&self, conversation_id: &str) -> Option<&LiveMeeting> {
997 self.live_meetings
998 .get(conversation_id)
999 .filter(|meeting| meeting.is_running(now_unix()) && meeting.target().is_some())
1000 }
1001
1002 pub fn demo_live_meetings(&mut self) {
1003 for conversation_id in ["demo-chat-meeting-standup", DEMO_LOBBY_MEETING] {
1004 self.live_meetings.insert(
1005 conversation_id.to_owned(),
1006 LiveMeeting {
1007 thread_id: conversation_id.to_owned(),
1008 conversation_url: None,
1009 expiration: Some(now_unix() + DEMO_LIVE_MINUTES * 60),
1010 organizer_id: Some("demo-jonas".to_owned()),
1011 tenant_id: Some("demo-tenant".to_owned()),
1012 meeting_code: None,
1013 passcode: None,
1014 },
1015 );
1016 }
1017 }
1018
1019 pub fn watches_live_meeting(&self, conversation_id: &str) -> bool {
1020 let meeting_chat =
1021 self.sidebar.chats.iter().any(|chat| {
1022 chat.id == conversation_id && chat.kind.eq_ignore_ascii_case(MEETING_KIND)
1023 });
1024 meeting_chat
1025 || matches!(
1026 selection_for(&self.sidebar, conversation_id),
1027 Some(Selection::Channel(_))
1028 )
1029 }
1030
1031 pub fn refresh_live_meeting(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
1032 if self.mode.demo || !self.watches_live_meeting(conversation_id) {
1033 return;
1034 }
1035 let Some(launcher) = self.call_launcher.clone() else {
1036 return;
1037 };
1038 if !self.live_refreshing.insert(conversation_id.to_owned()) {
1039 return;
1040 }
1041 let thread_id = conversation_id.to_owned();
1042 let receiver =
1043 crate::runtime::spawn(async move { launcher.live_meeting(&thread_id).await });
1044 let conversation_id = conversation_id.to_owned();
1045 cx.spawn(async move |this, cx| {
1046 let meeting = receiver.await.ok().flatten();
1047 this.update(cx, |state, cx| {
1048 state.apply_live_meeting(conversation_id, meeting, cx)
1049 })
1050 .ok();
1051 })
1052 .detach();
1053 }
1054
1055 pub fn apply_live_meeting(
1056 &mut self,
1057 conversation_id: String,
1058 meeting: Option<LiveMeeting>,
1059 cx: &mut Context<Self>,
1060 ) {
1061 self.live_refreshing.remove(&conversation_id);
1062 let changed = match meeting {
1063 Some(meeting) => {
1064 self.live_meetings
1065 .insert(conversation_id.clone(), meeting.clone())
1066 != Some(meeting)
1067 }
1068 None => self.live_meetings.remove(&conversation_id).is_some(),
1069 };
1070 if changed {
1071 cx.emit(AppEvent::LiveMeeting);
1072 cx.notify();
1073 }
1074 }
1075
1076 pub fn on_thread_changed(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
1077 self.refresh_live_meeting(conversation_id, cx);
1078 }
1079}
1080
1081#[cfg(test)]
1082mod tests {
1083 use std::sync::Arc;
1084 use std::time::{Duration, Instant};
1085
1086 use std::path::PathBuf;
1087
1088 use calling::{
1089 BackgroundChoice, CallCommand, CallControl, CallState, CallUpdate, DeviceChoice, EndKind,
1090 EndReason, EngineEvent, HoldState, Reaction, call_channel,
1091 };
1092 use gpui_kit::{AppContext as _, Entity, TestAppContext};
1093 use store::{ChatRecord, MemberRecord, Store};
1094
1095 use super::{ActiveCall, CallModel, load_camera};
1096 use crate::app_state::{AppEvent, AppState, Mode, Selection};
1097 use crate::call::TransferCandidate;
1098 use crate::call::background::BackgroundPick;
1099 use crate::call::demo::demo_ring;
1100 use crate::call::features::room_closed_move;
1101 use crate::call::model::{BreakoutState, ConsultCall};
1102 use crate::data::Person;
1103
1104 fn app_with_call(cx: &mut TestAppContext) -> Entity<AppState> {
1105 cx.update(gpui_kit::init);
1106 let store = Arc::new(Store::open_in_memory().unwrap());
1107 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
1108 let (handle, _control) = call_channel();
1109 cx.update(|cx| {
1110 app.update(cx, |state, _| {
1111 state.call = Some(ActiveCall::new(
1112 7,
1113 CallModel::test(),
1114 handle.commands,
1115 None,
1116 handle.video,
1117 ));
1118 })
1119 });
1120 app
1121 }
1122
1123 fn app_with_commands(
1124 cx: &mut TestAppContext,
1125 model: CallModel,
1126 conversation_id: Option<&str>,
1127 ) -> (Entity<AppState>, CallControl) {
1128 cx.update(gpui_kit::init);
1129 let store = Arc::new(Store::open_in_memory().unwrap());
1130 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
1131 let (handle, control) = call_channel();
1132 let conversation_id = conversation_id.map(str::to_owned);
1133 cx.update(|cx| {
1134 app.update(cx, |state, _| {
1135 state.call = Some(ActiveCall::new(
1136 7,
1137 model,
1138 handle.commands,
1139 conversation_id,
1140 handle.video,
1141 ));
1142 })
1143 });
1144 (app, control)
1145 }
1146
1147 fn live_meeting(can_end: bool) -> CallModel {
1148 let mut model = CallModel::meeting("Standup", can_end);
1149 model.apply(CallUpdate::State(CallState::Connected {
1150 since: Instant::now(),
1151 }));
1152 model.apply(CallUpdate::OwnIdentity {
1153 mri: "8:orgid:me".into(),
1154 });
1155 model
1156 }
1157
1158 #[gpui_kit::test]
1159 fn high_rate_updates_do_not_emit_call_events(cx: &mut TestAppContext) {
1160 let app = app_with_call(cx);
1161 let emitted = Arc::new(std::sync::atomic::AtomicUsize::new(0));
1162 let counter = emitted.clone();
1163 cx.update(|cx| {
1164 cx.subscribe(&app, move |_, event: &AppEvent, _| {
1165 if matches!(event, AppEvent::Call) {
1166 counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
1167 }
1168 })
1169 .detach();
1170 app.update(cx, |state, cx| {
1171 state.apply_call_update(7, CallUpdate::VideoReady, cx);
1172 state.apply_call_update(
1173 7,
1174 CallUpdate::Levels {
1175 local: 0.5,
1176 remote: 0.5,
1177 },
1178 cx,
1179 );
1180 state.apply_call_update(
1181 7,
1182 CallUpdate::Stats {
1183 inbound_packets: 1,
1184 outbound_packets: 1,
1185 },
1186 cx,
1187 );
1188 });
1189 });
1190 assert_eq!(emitted.load(std::sync::atomic::Ordering::SeqCst), 0);
1191 assert!(cx.update(|cx| app.read(cx).call.as_ref().unwrap().model.remote_speaking));
1192 cx.update(|cx| {
1193 app.update(cx, |state, cx| {
1194 state.apply_call_update(7, CallUpdate::Muted(true), cx)
1195 })
1196 });
1197 assert_eq!(emitted.load(std::sync::atomic::Ordering::SeqCst), 1);
1198 }
1199
1200 #[gpui_kit::test]
1201 fn the_computer_sound_switch_is_remembered_and_sent_to_the_call(cx: &mut TestAppContext) {
1202 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1203 let store = cx.update(|cx| app.read(cx).store.clone());
1204 assert!(!cx.update(|cx| app.read(cx).call_share_sound));
1205 cx.update(|cx| app.update(cx, |state, cx| state.set_call_share_sound(true, cx)));
1206 assert_eq!(
1207 control.try_recv_command(),
1208 Some(CallCommand::SetShareSound(true))
1209 );
1210 let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
1211 assert!(cx.update(|cx| reopened.read(cx).call_share_sound));
1212 cx.update(|cx| app.update(cx, |state, cx| state.set_call_share_sound(false, cx)));
1213 let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
1214 assert!(!cx.update(|cx| reopened.read(cx).call_share_sound));
1215 }
1216
1217 #[gpui_kit::test]
1218 fn the_camera_pick_is_remembered_and_the_next_call_starts_with_it(cx: &mut TestAppContext) {
1219 let (app, _control) = app_with_commands(cx, live_meeting(false), None);
1220 let store = cx.update(|cx| app.read(cx).store.clone());
1221 assert_eq!(load_camera(&store), DeviceChoice::SystemDefault);
1222 cx.update(|cx| {
1223 app.update(cx, |state, cx| {
1224 state.select_call_camera(DeviceChoice::Device("cam-2".into()), cx)
1225 })
1226 });
1227 assert_eq!(load_camera(&store), DeviceChoice::Device("cam-2".into()));
1228 let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
1229 assert_eq!(
1230 cx.update(|cx| reopened.read(cx).call_camera.clone()),
1231 DeviceChoice::Device("cam-2".into())
1232 );
1233 cx.update(|cx| {
1234 app.update(cx, |state, cx| {
1235 state.select_call_camera(DeviceChoice::SystemDefault, cx)
1236 })
1237 });
1238 assert_eq!(load_camera(&store), DeviceChoice::SystemDefault);
1239 }
1240
1241 #[gpui_kit::test]
1242 fn hands_and_reactions_become_call_commands_and_lowering_needs_the_organizer(
1243 cx: &mut TestAppContext,
1244 ) {
1245 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1246 cx.update(|cx| {
1247 app.update(cx, |state, cx| {
1248 state.toggle_call_hand(cx);
1249 state.send_call_reaction(Reaction::Heart, cx);
1250 state.lower_call_hand("8:orgid:a".into(), cx);
1251 state.lower_all_call_hands(cx);
1252 })
1253 });
1254 assert_eq!(control.try_recv_command(), Some(CallCommand::SetHand(true)));
1255 assert_eq!(
1256 control.try_recv_command(),
1257 Some(CallCommand::SendReaction(Reaction::Heart))
1258 );
1259 assert_eq!(control.try_recv_command(), None);
1260 let (organizer, mut organizer_control) = app_with_commands(cx, live_meeting(true), None);
1261 cx.update(|cx| {
1262 organizer.update(cx, |state, cx| {
1263 state.apply_call_update(
1264 7,
1265 CallUpdate::Roster(vec![calling::RosterEntry {
1266 mri: "8:orgid:a".into(),
1267 display_name: "Ana".into(),
1268 muted: false,
1269 in_lobby: false,
1270 has_video: false,
1271 sharing: false,
1272 hand: Some(calling::RaisedHand {
1273 state_id: "s".into(),
1274 rank: 1,
1275 }),
1276 spotlight: None,
1277 organizer: false,
1278 }]),
1279 cx,
1280 );
1281 state.lower_call_hand("8:orgid:a".into(), cx);
1282 state.lower_all_call_hands(cx);
1283 })
1284 });
1285 assert_eq!(
1286 organizer_control.try_recv_command(),
1287 Some(CallCommand::LowerHand {
1288 mri: "8:orgid:a".into()
1289 })
1290 );
1291 assert_eq!(
1292 organizer_control.try_recv_command(),
1293 Some(CallCommand::LowerAllHands)
1294 );
1295 }
1296
1297 fn guest(mri: &str, name: &str, in_lobby: bool, spotlit: bool) -> calling::RosterEntry {
1298 calling::RosterEntry {
1299 mri: mri.into(),
1300 display_name: name.into(),
1301 muted: false,
1302 in_lobby,
1303 has_video: false,
1304 sharing: false,
1305 hand: None,
1306 spotlight: spotlit.then(|| calling::Spotlight {
1307 state_id: "sp".into(),
1308 rank: 1,
1309 }),
1310 organizer: false,
1311 }
1312 }
1313
1314 #[gpui_kit::test]
1315 fn organizer_controls_become_commands_and_attendees_send_nothing(cx: &mut TestAppContext) {
1316 let (attendee, mut attendee_control) = app_with_commands(cx, live_meeting(false), None);
1317 cx.update(|cx| {
1318 attendee.update(cx, |state, cx| {
1319 state.admit_call_guest("8:orgid:g".into(), cx);
1320 state.admit_all_call_guests(cx);
1321 state.deny_call_guest("8:orgid:g".into(), cx);
1322 state.mute_call_participant("8:orgid:a".into(), cx);
1323 state.mute_all_call(cx);
1324 state.remove_call_participant("8:orgid:a".into(), cx);
1325 state.toggle_call_spotlight("8:orgid:a".into(), cx);
1326 })
1327 });
1328 assert_eq!(attendee_control.try_recv_command(), None);
1329 let (organizer, mut control) = app_with_commands(cx, live_meeting(true), None);
1330 cx.update(|cx| {
1331 organizer.update(cx, |state, cx| {
1332 state.apply_call_update(
1333 7,
1334 CallUpdate::Roster(vec![
1335 guest("8:orgid:a", "Ana", false, false),
1336 guest("8:orgid:g", "Gast", true, false),
1337 ]),
1338 cx,
1339 );
1340 state.admit_call_guest("8:orgid:g".into(), cx);
1341 state.admit_all_call_guests(cx);
1342 state.deny_call_guest("8:orgid:g".into(), cx);
1343 state.mute_call_participant("8:orgid:a".into(), cx);
1344 state.mute_all_call(cx);
1345 state.remove_call_participant("8:orgid:a".into(), cx);
1346 state.toggle_call_spotlight("8:orgid:a".into(), cx);
1347 })
1348 });
1349 assert_eq!(
1350 control.try_recv_command(),
1351 Some(CallCommand::Admit {
1352 mri: "8:orgid:g".into()
1353 })
1354 );
1355 assert_eq!(control.try_recv_command(), Some(CallCommand::AdmitAll));
1356 assert_eq!(
1357 control.try_recv_command(),
1358 Some(CallCommand::Deny {
1359 mri: "8:orgid:g".into()
1360 })
1361 );
1362 assert_eq!(
1363 control.try_recv_command(),
1364 Some(CallCommand::MuteParticipant {
1365 mri: "8:orgid:a".into()
1366 })
1367 );
1368 assert_eq!(control.try_recv_command(), Some(CallCommand::MuteAll));
1369 assert_eq!(
1370 control.try_recv_command(),
1371 Some(CallCommand::RemoveParticipant {
1372 mri: "8:orgid:a".into()
1373 })
1374 );
1375 assert_eq!(
1376 control.try_recv_command(),
1377 Some(CallCommand::Spotlight {
1378 mri: "8:orgid:a".into()
1379 })
1380 );
1381 }
1382
1383 #[gpui_kit::test]
1384 fn the_spotlight_button_stops_an_existing_spotlight_and_pinning_stays_local(
1385 cx: &mut TestAppContext,
1386 ) {
1387 let (app, mut control) = app_with_commands(cx, live_meeting(true), None);
1388 cx.update(|cx| {
1389 app.update(cx, |state, cx| {
1390 state.apply_call_update(
1391 7,
1392 CallUpdate::Roster(vec![guest("8:orgid:a", "Ana", false, true)]),
1393 cx,
1394 );
1395 state.toggle_call_spotlight("8:orgid:a".into(), cx);
1396 state.toggle_call_pin("8:orgid:a", cx);
1397 assert_eq!(
1398 state.call.as_ref().unwrap().model.pinned.as_deref(),
1399 Some("8:orgid:a")
1400 );
1401 state.toggle_call_pin("8:orgid:a", cx);
1402 assert_eq!(state.call.as_ref().unwrap().model.pinned, None);
1403 })
1404 });
1405 assert_eq!(
1406 control.try_recv_command(),
1407 Some(CallCommand::StopSpotlight {
1408 mri: "8:orgid:a".into()
1409 })
1410 );
1411 assert_eq!(control.try_recv_command(), None);
1412 }
1413
1414 #[gpui_kit::test]
1415 fn captions_toggle_between_on_and_off_and_only_in_meetings(cx: &mut TestAppContext) {
1416 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1417 cx.update(|cx| {
1418 app.update(cx, |state, cx| {
1419 state.toggle_call_captions(cx);
1420 state.apply_call_update(7, CallUpdate::Captions(calling::CaptionState::On), cx);
1421 state.toggle_call_captions(cx);
1422 })
1423 });
1424 assert_eq!(
1425 control.try_recv_command(),
1426 Some(CallCommand::SetCaptions(true))
1427 );
1428 assert_eq!(
1429 control.try_recv_command(),
1430 Some(CallCommand::SetCaptions(false))
1431 );
1432 let app = app_with_call(cx);
1433 cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_captions(cx)));
1434 }
1435
1436 #[gpui_kit::test]
1437 fn the_background_choice_is_remembered_and_sent_to_the_call(cx: &mut TestAppContext) {
1438 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1439 let store = cx.update(|cx| app.read(cx).store.clone());
1440 assert_eq!(
1441 cx.update(|cx| app.read(cx).call_background.clone()),
1442 BackgroundPick::None
1443 );
1444 cx.update(|cx| {
1445 app.update(cx, |state, cx| {
1446 state.set_call_background(BackgroundPick::Blur, cx)
1447 })
1448 });
1449 assert_eq!(
1450 control.try_recv_command(),
1451 Some(CallCommand::SetBackground(BackgroundChoice::Blur))
1452 );
1453 assert_eq!(
1454 cx.update(|cx| app.read(cx).call.as_ref().unwrap().model.background.clone()),
1455 BackgroundPick::Blur
1456 );
1457 let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
1458 assert_eq!(
1459 cx.update(|cx| reopened.read(cx).call_background.clone()),
1460 BackgroundPick::Blur
1461 );
1462 let picture = PathBuf::from("/pictures/wall.jpg");
1463 cx.update(|cx| {
1464 app.update(cx, |state, cx| {
1465 state.set_call_background(BackgroundPick::Custom(picture.clone()), cx)
1466 })
1467 });
1468 assert_eq!(
1469 control.try_recv_command(),
1470 Some(CallCommand::SetBackground(BackgroundChoice::Image(
1471 picture.clone()
1472 )))
1473 );
1474 let reopened = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
1475 assert_eq!(
1476 cx.update(|cx| reopened.read(cx).call_background.clone()),
1477 BackgroundPick::Custom(picture)
1478 );
1479 cx.update(|cx| {
1480 app.update(cx, |state, cx| {
1481 state.set_call_background(BackgroundPick::None, cx)
1482 })
1483 });
1484 assert_eq!(
1485 control.try_recv_command(),
1486 Some(CallCommand::SetBackground(BackgroundChoice::None))
1487 );
1488 }
1489
1490 #[gpui_kit::test]
1491 fn a_default_image_that_is_not_downloaded_yet_sends_nothing(cx: &mut TestAppContext) {
1492 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1493 cx.update(|cx| {
1494 app.update(cx, |state, cx| {
1495 state.set_call_background(BackgroundPick::Default("office_01".into()), cx)
1496 })
1497 });
1498 assert_eq!(control.try_recv_command(), None);
1499 }
1500
1501 #[gpui_kit::test]
1502 fn only_the_organizer_starts_a_recording_and_the_notice_needs_a_consent_request(
1503 cx: &mut TestAppContext,
1504 ) {
1505 let (attendee, mut attendee_control) = app_with_commands(cx, live_meeting(false), None);
1506 cx.update(|cx| {
1507 attendee.update(cx, |state, cx| {
1508 state.set_call_recording(true, cx);
1509 state.accept_recording_notice(cx);
1510 })
1511 });
1512 assert_eq!(attendee_control.try_recv_command(), None);
1513 let (organizer, mut control) = app_with_commands(cx, live_meeting(true), None);
1514 cx.update(|cx| {
1515 organizer.update(cx, |state, cx| {
1516 state.set_call_recording(true, cx);
1517 state.apply_call_update(7, CallUpdate::Recording(true), cx);
1518 state.set_call_recording(false, cx);
1519 state.accept_recording_notice(cx);
1520 state.apply_call_update(7, CallUpdate::ConsentRequired(true), cx);
1521 state.accept_recording_notice(cx);
1522 })
1523 });
1524 assert_eq!(
1525 control.try_recv_command(),
1526 Some(CallCommand::SetRecording {
1527 on: true,
1528 title: "Standup".into()
1529 })
1530 );
1531 assert_eq!(
1532 control.try_recv_command(),
1533 Some(CallCommand::SetRecording {
1534 on: false,
1535 title: "Standup".into()
1536 })
1537 );
1538 assert_eq!(
1539 control.try_recv_command(),
1540 Some(CallCommand::ConsentToRecording)
1541 );
1542 assert_eq!(control.try_recv_command(), None);
1543 }
1544
1545 fn live_one_to_one() -> CallModel {
1546 let mut model = CallModel::people("Bea", &[("8:orgid:bea".into(), "Bea".into())]);
1547 model.apply(CallUpdate::State(CallState::Connected {
1548 since: Instant::now(),
1549 }));
1550 model
1551 }
1552
1553 #[gpui_kit::test]
1554 fn hold_and_resume_alternate_and_stop_while_the_other_side_holds(cx: &mut TestAppContext) {
1555 let (app, mut control) = app_with_commands(cx, live_one_to_one(), None);
1556 cx.update(|cx| {
1557 app.update(cx, |state, cx| {
1558 state.toggle_call_hold(cx);
1559 state.apply_call_update(7, CallUpdate::Hold(HoldState::Local), cx);
1560 state.toggle_call_hold(cx);
1561 state.apply_call_update(7, CallUpdate::Hold(HoldState::Remote), cx);
1562 state.toggle_call_hold(cx);
1563 })
1564 });
1565 assert_eq!(control.try_recv_command(), Some(CallCommand::Hold(true)));
1566 assert_eq!(control.try_recv_command(), Some(CallCommand::Hold(false)));
1567 assert_eq!(control.try_recv_command(), None);
1568 let (meeting, mut meeting_control) = app_with_commands(cx, live_meeting(true), None);
1569 cx.update(|cx| meeting.update(cx, |state, cx| state.toggle_call_hold(cx)));
1570 assert_eq!(meeting_control.try_recv_command(), None);
1571 }
1572
1573 fn candidate(name: &str) -> TransferCandidate {
1574 TransferCandidate {
1575 mri: format!("8:orgid:{name}"),
1576 name: name.into(),
1577 user_id: Some(name.into()),
1578 chat_id: format!("19:{name}"),
1579 }
1580 }
1581
1582 #[gpui_kit::test]
1583 fn a_blind_transfer_names_the_target_and_a_consulted_one_carries_the_replacement_link(
1584 cx: &mut TestAppContext,
1585 ) {
1586 let (app, mut control) = app_with_commands(cx, live_one_to_one(), None);
1587 cx.update(|cx| {
1588 app.update(cx, |state, cx| {
1589 state.transfer_call_blind(candidate("ana"), cx)
1590 })
1591 });
1592 assert_eq!(
1593 control.try_recv_command(),
1594 Some(CallCommand::Transfer {
1595 target: calling::Callee {
1596 mri: "8:orgid:ana".into(),
1597 display_name: "ana".into()
1598 },
1599 replaces: None
1600 })
1601 );
1602 let (consulting, mut consult_control) = app_with_commands(cx, live_one_to_one(), None);
1603 let (consult_handle, _consult_updates) = call_channel();
1604 cx.update(|cx| {
1605 consulting.update(cx, |state, cx| {
1606 state.call.as_mut().unwrap().consult = Some(ConsultCall {
1607 id: 8,
1608 target: ("8:orgid:ana".into(), "Ana".into()),
1609 commands: consult_handle.commands,
1610 state: CallState::Connecting,
1611 replacement: Some("https://cc.skype.com/replacement".into()),
1612 transferring: false,
1613 });
1614 state.transfer_consulted(cx);
1615 state.call.as_mut().unwrap().consult.as_mut().unwrap().state =
1616 CallState::Connected {
1617 since: Instant::now(),
1618 };
1619 state.transfer_consulted(cx);
1620 })
1621 });
1622 assert_eq!(
1623 consult_control.try_recv_command(),
1624 Some(CallCommand::Transfer {
1625 target: calling::Callee {
1626 mri: "8:orgid:ana".into(),
1627 display_name: "Ana".into()
1628 },
1629 replaces: Some("https://cc.skype.com/replacement".into()),
1630 })
1631 );
1632 assert_eq!(consult_control.try_recv_command(), None);
1633 }
1634
1635 #[gpui_kit::test]
1636 fn the_transfer_list_holds_one_to_one_chats_without_the_person_on_the_call(
1637 cx: &mut TestAppContext,
1638 ) {
1639 let app = app_with_chats(cx);
1640 cx.update(|cx| {
1641 app.update(cx, |state, cx| {
1642 let names: Vec<String> = state
1643 .transfer_candidates()
1644 .into_iter()
1645 .map(|candidate| candidate.name)
1646 .collect();
1647 assert_eq!(names, vec!["Bea".to_owned()]);
1648 let (handle, _control) = call_channel();
1649 let mut on_the_call =
1650 CallModel::people("Bea", &[("8:orgid:bea-id".into(), "Bea".into())]);
1651 on_the_call.apply(CallUpdate::State(CallState::Connected {
1652 since: Instant::now(),
1653 }));
1654 state.call = Some(ActiveCall::new(
1655 9,
1656 on_the_call,
1657 handle.commands,
1658 None,
1659 handle.video,
1660 ));
1661 assert!(state.transfer_candidates().is_empty());
1662 cx.notify();
1663 })
1664 });
1665 }
1666
1667 fn room_move(returning: bool) -> calling::BreakoutMove {
1668 calling::BreakoutMove {
1669 room_name: "Room 1".into(),
1670 target: calling::MeetingTarget {
1671 thread_id: "19:meeting_room@thread.v2".into(),
1672 tenant_id: "t".into(),
1673 organizer_id: "o".into(),
1674 meeting_data: None,
1675 },
1676 returning,
1677 }
1678 }
1679
1680 fn main_target() -> calling::MeetingTarget {
1681 calling::MeetingTarget {
1682 thread_id: "19:meeting_main@thread.v2".into(),
1683 tenant_id: "t".into(),
1684 organizer_id: "o".into(),
1685 meeting_data: None,
1686 }
1687 }
1688
1689 #[gpui_kit::test]
1690 fn being_moved_to_a_room_leaves_the_call_and_remembers_the_way_back(cx: &mut TestAppContext) {
1691 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1692 cx.update(|cx| {
1693 app.update(cx, |state, cx| {
1694 state.call.as_mut().unwrap().meeting_target = Some(main_target());
1695 state.apply_call_update(7, CallUpdate::BreakoutMove(room_move(false)), cx);
1696 let pending = state
1697 .call
1698 .as_ref()
1699 .unwrap()
1700 .pending_move
1701 .clone()
1702 .expect("a move is pending");
1703 assert_eq!(pending.target.thread_id, "19:meeting_room@thread.v2");
1704 assert_eq!(pending.title, "Room 1");
1705 let room = pending.breakout.expect("a room");
1706 assert_eq!(room.main, Some(main_target()));
1707 assert_eq!(room.main_title, "Standup");
1708 assert_eq!(
1709 state.notice.as_ref().map(|notice| notice.text.as_str()),
1710 Some("Moving you to Room 1")
1711 );
1712 state.apply_call_update(7, CallUpdate::BreakoutMove(room_move(false)), cx);
1713 })
1714 });
1715 assert_eq!(control.try_recv_command(), Some(CallCommand::Hangup));
1716 assert_eq!(control.try_recv_command(), None);
1717 }
1718
1719 #[gpui_kit::test]
1720 fn the_return_button_and_a_closed_room_both_lead_back_to_the_main_meeting(
1721 cx: &mut TestAppContext,
1722 ) {
1723 let mut room = live_meeting(false);
1724 room.breakout = Some(BreakoutState {
1725 room_name: "Room 1".into(),
1726 main: Some(main_target()),
1727 main_title: "Standup".into(),
1728 });
1729 let (app, mut control) = app_with_commands(cx, room.clone(), None);
1730 cx.update(|cx| {
1731 app.update(cx, |state, cx| {
1732 state.return_to_main_meeting(cx);
1733 let pending = state
1734 .call
1735 .as_ref()
1736 .unwrap()
1737 .pending_move
1738 .clone()
1739 .expect("a move is pending");
1740 assert_eq!(pending.target, main_target());
1741 assert_eq!(pending.title, "Standup");
1742 assert!(pending.breakout.is_none());
1743 })
1744 });
1745 assert_eq!(control.try_recv_command(), Some(CallCommand::Hangup));
1746 let (closed, _control) = app_with_commands(cx, room, None);
1747 cx.update(|cx| {
1748 closed.update(cx, |state, _| {
1749 let call = state.call.as_ref().unwrap();
1750 let moved = room_closed_move(call, &EndReason::Remote(EndKind::RoomClosed))
1751 .expect("goes back");
1752 assert_eq!(moved.target, main_target());
1753 assert!(room_closed_move(call, &EndReason::Remote(EndKind::Normal)).is_none());
1754 assert!(room_closed_move(call, &EndReason::LocalHangup).is_none());
1755 })
1756 });
1757 let (plain, _control) = app_with_commands(cx, live_meeting(false), None);
1758 cx.update(|cx| {
1759 plain.update(cx, |state, _| {
1760 assert!(
1761 room_closed_move(
1762 state.call.as_ref().unwrap(),
1763 &EndReason::Remote(EndKind::RoomClosed)
1764 )
1765 .is_none()
1766 )
1767 })
1768 });
1769 }
1770
1771 #[gpui_kit::test]
1772 fn the_whiteboard_menu_asks_for_the_board_unless_someone_already_shares_one(
1773 cx: &mut TestAppContext,
1774 ) {
1775 let (app, mut control) = app_with_commands(cx, live_meeting(false), None);
1776 cx.update(|cx| {
1777 app.update(cx, |state, cx| {
1778 state.open_call_whiteboard(cx);
1779 state.apply_call_update(
1780 7,
1781 CallUpdate::Whiteboard(Some(calling::ContentShare {
1782 session_id: "s".into(),
1783 presenter: Some("8:orgid:ana".into()),
1784 subject: "Whiteboard".into(),
1785 url: Some("https://app.whiteboard.microsoft.com/x".into()),
1786 whiteboard: true,
1787 })),
1788 cx,
1789 );
1790 state.open_call_whiteboard(cx);
1791 })
1792 });
1793 assert_eq!(
1794 control.try_recv_command(),
1795 Some(CallCommand::OpenWhiteboard {
1796 title: "Standup".into()
1797 })
1798 );
1799 assert_eq!(control.try_recv_command(), None);
1800 }
1801
1802 #[gpui_kit::test]
1803 fn the_chat_panel_opens_the_meeting_thread_and_the_badge_counts_only_while_closed(
1804 cx: &mut TestAppContext,
1805 ) {
1806 let (app, _control) = app_with_commands(cx, live_meeting(false), Some("19:list@thread.v2"));
1807 cx.update(|cx| {
1808 app.update(cx, |state, cx| {
1809 state
1810 .directory
1811 .unread_counts
1812 .insert("19:meeting_x@thread.v2".into(), 3);
1813 assert_eq!(state.call_chat_thread(), None);
1814 assert_eq!(state.call_chat_unread(), 0);
1815 state.apply_call_update(
1816 7,
1817 CallUpdate::MeetingChat("19:meeting_x@thread.v2".into()),
1818 cx,
1819 );
1820 assert_eq!(state.call_chat_unread(), 3);
1821 state.toggle_call_chat(cx);
1822 assert_eq!(
1823 state.call_chat_thread().as_deref(),
1824 Some("19:meeting_x@thread.v2")
1825 );
1826 assert_eq!(state.call_chat_unread(), 0);
1827 state.toggle_call_chat(cx);
1828 assert_eq!(state.call_chat_thread(), None);
1829 })
1830 });
1831 }
1832
1833 #[gpui_kit::test]
1834 fn a_call_without_a_chat_has_no_panel(cx: &mut TestAppContext) {
1835 let app = app_with_call(cx);
1836 cx.update(|cx| {
1837 app.update(cx, |state, cx| {
1838 state.toggle_call_chat(cx);
1839 assert!(!state.call.as_ref().unwrap().chat_open);
1840 })
1841 });
1842 }
1843
1844 fn app_with_chats(cx: &mut TestAppContext) -> Entity<AppState> {
1845 cx.update(gpui_kit::init);
1846 let store = Arc::new(Store::open_in_memory().unwrap());
1847 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
1848 cx.update(|cx| {
1849 app.update(cx, |state, _| {
1850 state.directory.me = Some(Person {
1851 user_id: "me-id".into(),
1852 display_name: "Me".into(),
1853 });
1854 state.sidebar.chats = vec![
1855 ChatRecord {
1856 id: "19:meeting@thread.v2".into(),
1857 kind: "meeting".into(),
1858 members: vec![MemberRecord {
1859 user_id: Some("bea-id".into()),
1860 display_name: "Bea".into(),
1861 }],
1862 ..Default::default()
1863 },
1864 ChatRecord {
1865 id: "19:dm@unq.gbl.spaces".into(),
1866 kind: "oneOnOne".into(),
1867 members: vec![
1868 MemberRecord {
1869 user_id: Some("me-id".into()),
1870 display_name: "Me".into(),
1871 },
1872 MemberRecord {
1873 user_id: Some("bea-id".into()),
1874 display_name: "Bea".into(),
1875 },
1876 ],
1877 ..Default::default()
1878 },
1879 ];
1880 })
1881 });
1882 app
1883 }
1884
1885 #[gpui_kit::test]
1886 fn declining_by_shortcut_only_acts_while_a_ring_shows(cx: &mut TestAppContext) {
1887 let app = app_with_chats(cx);
1888 cx.update(|cx| {
1889 app.update(cx, |state, cx| {
1890 state.decline_ringing_call(cx);
1891 assert!(!state.rings.is_ringing());
1892 state.demo_incoming_ring(cx);
1893 assert!(state.rings.is_ringing());
1894 state.decline_ringing_call(cx);
1895 assert!(!state.rings.is_ringing());
1896 })
1897 });
1898 }
1899
1900 #[gpui_kit::test]
1901 fn picking_another_chat_moves_the_call_to_the_mini_window(cx: &mut TestAppContext) {
1902 let app = app_with_call(cx);
1903 cx.update(|cx| {
1904 app.update(cx, |state, cx| {
1905 assert!(state.viewing_call());
1906 state.select(Selection::Chat("c1".into()), cx);
1907 assert!(!state.viewing_call());
1908 state.show_call(true, cx);
1909 assert!(state.viewing_call());
1910 })
1911 });
1912 }
1913
1914 #[gpui_kit::test]
1915 fn an_ended_call_closes_the_view_and_raises_a_notice(cx: &mut TestAppContext) {
1916 let app = app_with_call(cx);
1917 let since = Instant::now() - Duration::from_secs(42);
1918 cx.update(|cx| {
1919 app.update(cx, |state, cx| {
1920 state.apply_call_update(7, CallUpdate::State(CallState::Connected { since }), cx);
1921 assert!(state.call.is_some());
1922 state.apply_call_update(
1923 7,
1924 CallUpdate::State(CallState::Ended {
1925 reason: EndReason::LocalHangup,
1926 }),
1927 cx,
1928 );
1929 assert!(state.call.is_none());
1930 let notice = state.notice.as_ref().expect("notice");
1931 assert_eq!(notice.text, "Call ended 00:42");
1932 })
1933 });
1934 }
1935
1936 #[gpui_kit::test]
1937 fn full_window_exists_only_while_someone_shares_and_escape_leaves_it(cx: &mut TestAppContext) {
1938 let app = app_with_call(cx);
1939 cx.update(|cx| {
1940 app.update(cx, |state, cx| {
1941 state.toggle_stage_fullscreen(cx);
1942 assert!(!state.stage_fullscreen());
1943 state.apply_call_update(7, CallUpdate::ScreenShare(Some("8:orgid:a".into())), cx);
1944 state.toggle_stage_fullscreen(cx);
1945 assert!(state.stage_fullscreen());
1946 state.leave_stage_fullscreen(cx);
1947 assert!(!state.stage_fullscreen());
1948 state.toggle_stage_fullscreen(cx);
1949 state.apply_call_update(7, CallUpdate::ScreenShare(None), cx);
1950 assert!(!state.stage_fullscreen());
1951 })
1952 });
1953 }
1954
1955 #[gpui_kit::test]
1956 fn a_declined_call_names_who_declined(cx: &mut TestAppContext) {
1957 let app = app_with_call(cx);
1958 cx.update(|cx| {
1959 app.update(cx, |state, cx| {
1960 state.call.as_mut().unwrap().model.peer_name = "Bea".into();
1961 state.apply_call_update(
1962 7,
1963 CallUpdate::State(CallState::Ended {
1964 reason: EndReason::Remote(EndKind::Declined),
1965 }),
1966 cx,
1967 );
1968 assert_eq!(state.notice.as_ref().unwrap().text, "Bea declined");
1969 })
1970 });
1971 }
1972
1973 #[gpui_kit::test]
1974 fn updates_of_an_older_call_are_ignored(cx: &mut TestAppContext) {
1975 let app = app_with_call(cx);
1976 cx.update(|cx| {
1977 app.update(cx, |state, cx| {
1978 state.apply_call_update(
1979 6,
1980 CallUpdate::State(CallState::Ended {
1981 reason: EndReason::Dropped,
1982 }),
1983 cx,
1984 );
1985 assert!(state.call.is_some());
1986 assert!(state.notice.is_none());
1987 })
1988 });
1989 }
1990
1991 #[gpui_kit::test]
1992 fn a_ring_that_times_out_becomes_a_missed_call_event(cx: &mut TestAppContext) {
1993 cx.update(gpui_kit::init);
1994 let store = Arc::new(Store::open_in_memory().unwrap());
1995 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
1996 cx.update(|cx| {
1997 app.update(cx, |state, cx| {
1998 state.on_engine_event(EngineEvent::Incoming(demo_ring()), cx);
1999 assert!(state.rings.is_ringing());
2000 state.tick_rings(cx);
2001 assert!(state.rings.is_ringing());
2002 state.on_engine_event(
2003 EngineEvent::RingEnded {
2004 ring_id: demo_ring().ring_id,
2005 kind: EndKind::Cancelled,
2006 answered_by: None,
2007 },
2008 cx,
2009 );
2010 assert!(!state.rings.is_ringing());
2011 })
2012 });
2013 }
2014
2015 #[gpui_kit::test]
2016 fn calls_are_refused_for_meeting_chats_and_missing_chats(cx: &mut TestAppContext) {
2017 let app = app_with_chats(cx);
2018 cx.update(|cx| {
2019 app.update(cx, |state, cx| {
2020 state.start_chat_call("19:meeting@thread.v2", cx);
2021 assert!(state.call.is_none());
2022 assert_eq!(
2023 state.notice.as_ref().unwrap().text,
2024 "This chat cannot be called"
2025 );
2026 state.start_chat_call("19:unknown", cx);
2027 assert!(state.call.is_none());
2028 })
2029 });
2030 }
2031
2032 #[gpui_kit::test]
2033 fn a_missed_call_finds_the_one_on_one_chat_of_the_caller(cx: &mut TestAppContext) {
2034 let app = app_with_chats(cx);
2035 cx.update(|cx| {
2036 app.update(cx, |state, _| {
2037 let missed = super::MissedCall {
2038 caller_mri: "8:orgid:bea-id".into(),
2039 caller_name: "Bea".into(),
2040 thread_id: None,
2041 at: chrono::Utc::now(),
2042 };
2043 assert_eq!(
2044 state.conversation_of_missed_call(&missed).as_deref(),
2045 Some("19:dm@unq.gbl.spaces")
2046 );
2047 let direct = super::MissedCall {
2048 thread_id: Some("19:dm@unq.gbl.spaces".into()),
2049 ..missed.clone()
2050 };
2051 assert_eq!(
2052 state.conversation_of_missed_call(&direct).as_deref(),
2053 Some("19:dm@unq.gbl.spaces")
2054 );
2055 })
2056 });
2057 }
2058
2059 #[gpui_kit::test]
2060 fn only_meeting_chats_and_channels_watch_for_a_running_meeting(cx: &mut TestAppContext) {
2061 let app = app_with_chats(cx);
2062 cx.update(|cx| {
2063 app.update(cx, |state, _| {
2064 assert!(state.watches_live_meeting("19:meeting@thread.v2"));
2065 assert!(!state.watches_live_meeting("19:dm@unq.gbl.spaces"));
2066 assert!(!state.watches_live_meeting("19:unknown"));
2067 })
2068 });
2069 }
2070
2071 #[gpui_kit::test]
2072 fn a_live_meeting_shows_until_it_ends(cx: &mut TestAppContext) {
2073 let app = app_with_chats(cx);
2074 let meeting = calling::meeting::LiveMeeting {
2075 thread_id: "19:meeting@thread.v2".into(),
2076 conversation_url: None,
2077 expiration: Some(chrono::Utc::now().timestamp() + 600),
2078 organizer_id: Some("org".into()),
2079 tenant_id: Some("tenant".into()),
2080 meeting_code: None,
2081 passcode: None,
2082 };
2083 cx.update(|cx| {
2084 app.update(cx, |state, cx| {
2085 assert!(state.running_meeting("19:meeting@thread.v2").is_none());
2086 state.apply_live_meeting("19:meeting@thread.v2".into(), Some(meeting), cx);
2087 assert!(state.running_meeting("19:meeting@thread.v2").is_some());
2088 state.apply_live_meeting("19:meeting@thread.v2".into(), None, cx);
2089 assert!(state.running_meeting("19:meeting@thread.v2").is_none());
2090 })
2091 });
2092 }
2093}