BlueGit

ict rusty-teams

Files

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

actions.rs

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