| 1 | use calling::{ |
| 2 | CallCommand, CallHandle, CallSpec, CallState, Callee, EndKind, EndReason, HoldState, |
| 3 | }; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | use super::background::BackgroundPick; |
| 7 | use super::demo::{DemoCall, start_demo_call}; |
| 8 | use super::health::HealthBanner; |
| 9 | use super::model::{ActiveCall, BreakoutState, CallKind, CallModel, ConsultCall, PendingMove}; |
| 10 | use super::recording_menu::RecordAction; |
| 11 | use super::target::plan_for_chat; |
| 12 | use crate::app_state::{AppEvent, AppState}; |
| 13 | use crate::data::is_one_on_one; |
| 14 | use crate::views::call_transcript::TranscriptPanel; |
| 15 | |
| 16 | const DEMO_ROOM_GUESTS: usize = 3; |
| 17 | |
| 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | pub struct TransferCandidate { |
| 20 | pub mri: String, |
| 21 | pub name: String, |
| 22 | pub user_id: Option<String>, |
| 23 | pub chat_id: String, |
| 24 | } |
| 25 | |
| 26 | pub fn room_closed_move(call: &ActiveCall, reason: &EndReason) -> Option<PendingMove> { |
| 27 | let room = call.model.breakout.as_ref()?; |
| 28 | if *reason != EndReason::Remote(EndKind::RoomClosed) { |
| 29 | return None; |
| 30 | } |
| 31 | Some(return_move(room)) |
| 32 | } |
| 33 | |
| 34 | fn return_move(room: &BreakoutState) -> PendingMove { |
| 35 | PendingMove { |
| 36 | target: room.main.clone().expect("a room with a way back"), |
| 37 | title: room.main_title.clone(), |
| 38 | breakout: None, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl AppState { |
| 43 | pub fn run_call_record_action(&mut self, action: RecordAction, _cx: &mut Context<Self>) { |
| 44 | let Some(call) = self.call.as_ref().filter(|call| call.model.can_record()) else { |
| 45 | return; |
| 46 | }; |
| 47 | let command = match action { |
| 48 | RecordAction::Start(kind) => CallCommand::StartRecording { |
| 49 | kind, |
| 50 | title: call.model.title.clone(), |
| 51 | }, |
| 52 | RecordAction::Stop(scope) => CallCommand::StopRecording { scope }, |
| 53 | }; |
| 54 | self.send_call_command(command); |
| 55 | } |
| 56 | |
| 57 | pub fn open_call_transcript( |
| 58 | &mut self, |
| 59 | panel: Entity<TranscriptPanel>, |
| 60 | cx: &mut Context<Self>, |
| 61 | ) { |
| 62 | self.transcript_panel = Some(panel); |
| 63 | self.show_call_transcript(cx); |
| 64 | } |
| 65 | |
| 66 | pub(super) fn show_call_transcript(&mut self, cx: &mut Context<Self>) { |
| 67 | let Some(call) = self |
| 68 | .call |
| 69 | .as_mut() |
| 70 | .filter(|call| call.model.kind == CallKind::Meeting) |
| 71 | else { |
| 72 | return; |
| 73 | }; |
| 74 | call.chat_open = false; |
| 75 | call.people_open = false; |
| 76 | call.transcript_open = true; |
| 77 | let captions_off = !call.model.captions_on(); |
| 78 | if captions_off { |
| 79 | self.send_call_command(CallCommand::SetCaptions(true)); |
| 80 | } |
| 81 | cx.emit(AppEvent::Call); |
| 82 | cx.notify(); |
| 83 | } |
| 84 | |
| 85 | pub fn close_call_transcript(&mut self, cx: &mut Context<Self>) { |
| 86 | if let Some(call) = self.call.as_mut() { |
| 87 | call.transcript_open = false; |
| 88 | } |
| 89 | self.transcript_panel = None; |
| 90 | cx.emit(AppEvent::Call); |
| 91 | cx.notify(); |
| 92 | } |
| 93 | |
| 94 | pub fn choose_call_caption_language(&mut self, tag: &str, cx: &mut Context<Self>) { |
| 95 | let Some(call) = self |
| 96 | .call |
| 97 | .as_mut() |
| 98 | .filter(|call| call.model.is_active() && call.model.kind == CallKind::Meeting) |
| 99 | else { |
| 100 | return; |
| 101 | }; |
| 102 | call.model.choose_caption_language(tag); |
| 103 | let captions_off = !call.model.captions_on(); |
| 104 | if captions_off { |
| 105 | self.send_call_command(CallCommand::SetCaptions(true)); |
| 106 | } |
| 107 | self.send_ready_caption_language(); |
| 108 | cx.notify(); |
| 109 | } |
| 110 | |
| 111 | fn send_ready_caption_language(&mut self) { |
| 112 | let language = self |
| 113 | .call |
| 114 | .as_mut() |
| 115 | .and_then(|call| call.model.take_ready_caption_language()); |
| 116 | if let Some(language) = language { |
| 117 | self.send_call_command(CallCommand::SetCaptionLanguage(language)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub fn accept_recording_notice(&mut self, _cx: &mut Context<Self>) { |
| 122 | if self |
| 123 | .call |
| 124 | .as_ref() |
| 125 | .is_some_and(|call| call.model.consent_required) |
| 126 | { |
| 127 | self.send_call_command(CallCommand::ConsentToRecording); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub fn toggle_call_hold(&mut self, _cx: &mut Context<Self>) { |
| 132 | let Some(call) = self.call.as_ref() else { |
| 133 | return; |
| 134 | }; |
| 135 | let hold = match call.model.hold { |
| 136 | HoldState::Local => false, |
| 137 | HoldState::Active if call.model.can_hold() => true, |
| 138 | _ => return, |
| 139 | }; |
| 140 | self.send_call_command(CallCommand::Hold(hold)); |
| 141 | } |
| 142 | |
| 143 | pub fn set_call_incoming_video(&mut self, enabled: bool, cx: &mut Context<Self>) { |
| 144 | self.send_call_command(CallCommand::SetIncomingVideo(enabled)); |
| 145 | cx.notify(); |
| 146 | } |
| 147 | |
| 148 | pub fn close_call_health_banner(&mut self, banner: HealthBanner, cx: &mut Context<Self>) { |
| 149 | if let Some(call) = self.call.as_mut() { |
| 150 | call.model.health.close_banner(banner); |
| 151 | cx.notify(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | pub fn turn_off_call_effects(&mut self, cx: &mut Context<Self>) { |
| 156 | self.set_call_background(BackgroundPick::None, cx); |
| 157 | let effects = calling::CameraEffects { |
| 158 | auto_framing: false, |
| 159 | adjust_brightness: false, |
| 160 | soft_focus: false, |
| 161 | ..self.call_effects |
| 162 | }; |
| 163 | self.set_call_effects(effects, cx); |
| 164 | } |
| 165 | |
| 166 | pub fn open_call_whiteboard(&mut self, cx: &mut Context<Self>) { |
| 167 | let Some(call) = self |
| 168 | .call |
| 169 | .as_ref() |
| 170 | .filter(|call| call.model.can_open_whiteboard()) |
| 171 | else { |
| 172 | return; |
| 173 | }; |
| 174 | if let Some(url) = call.model.whiteboard_url() { |
| 175 | cx.open_url(url); |
| 176 | return; |
| 177 | } |
| 178 | let title = call.model.title.clone(); |
| 179 | self.send_call_command(CallCommand::OpenWhiteboard { title }); |
| 180 | } |
| 181 | |
| 182 | pub fn transfer_candidates(&self) -> Vec<TransferCandidate> { |
| 183 | let peer = self |
| 184 | .call |
| 185 | .as_ref() |
| 186 | .and_then(|call| call.model.tiles.first().map(|tile| tile.mri.clone())); |
| 187 | self.sidebar |
| 188 | .chats |
| 189 | .iter() |
| 190 | .filter(|chat| is_one_on_one(chat)) |
| 191 | .filter_map(|chat| { |
| 192 | let plan = plan_for_chat(chat, self.directory.me.as_ref())?; |
| 193 | let (mri, name) = plan.callees.into_iter().next()?; |
| 194 | Some(TransferCandidate { |
| 195 | user_id: mri.strip_prefix("8:orgid:").map(str::to_owned), |
| 196 | mri, |
| 197 | name, |
| 198 | chat_id: chat.id.clone(), |
| 199 | }) |
| 200 | }) |
| 201 | .filter(|candidate| Some(&candidate.mri) != peer.as_ref()) |
| 202 | .collect() |
| 203 | } |
| 204 | |
| 205 | pub fn can_transfer_call(&self) -> bool { |
| 206 | self.call |
| 207 | .as_ref() |
| 208 | .is_some_and(|call| call.model.can_transfer() && call.consult.is_none()) |
| 209 | } |
| 210 | |
| 211 | pub fn open_transfer_picker( |
| 212 | &mut self, |
| 213 | picker: Entity<crate::views::transfer_picker::TransferPicker>, |
| 214 | cx: &mut Context<Self>, |
| 215 | ) { |
| 216 | if self.can_transfer_call() { |
| 217 | self.transfer_picker = Some(picker); |
| 218 | cx.notify(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | pub fn close_transfer_picker(&mut self, cx: &mut Context<Self>) { |
| 223 | if self.transfer_picker.take().is_some() { |
| 224 | cx.notify(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | pub fn transfer_call_blind(&mut self, target: TransferCandidate, cx: &mut Context<Self>) { |
| 229 | self.close_transfer_picker(cx); |
| 230 | if !self.can_transfer_call() { |
| 231 | return; |
| 232 | } |
| 233 | let callee = Callee { |
| 234 | mri: target.mri, |
| 235 | display_name: target.name, |
| 236 | }; |
| 237 | self.send_call_command(CallCommand::Transfer { |
| 238 | target: callee, |
| 239 | replaces: None, |
| 240 | }); |
| 241 | } |
| 242 | |
| 243 | pub fn start_consult(&mut self, target: TransferCandidate, cx: &mut Context<Self>) { |
| 244 | self.close_transfer_picker(cx); |
| 245 | if !self.can_transfer_call() { |
| 246 | return; |
| 247 | } |
| 248 | let spec = CallSpec::People { |
| 249 | callees: vec![Callee { |
| 250 | mri: target.mri.clone(), |
| 251 | display_name: target.name.clone(), |
| 252 | }], |
| 253 | thread_id: target.chat_id.clone(), |
| 254 | }; |
| 255 | let handle = if self.mode.demo { |
| 256 | Some(start_demo_call(DemoCall::People(vec![( |
| 257 | target.mri.clone(), |
| 258 | target.name.clone(), |
| 259 | )]))) |
| 260 | } else { |
| 261 | self.call_launcher |
| 262 | .clone() |
| 263 | .map(|launcher| launcher.start_consult(spec)) |
| 264 | }; |
| 265 | let Some(CallHandle { |
| 266 | commands, updates, .. |
| 267 | }) = handle |
| 268 | else { |
| 269 | self.raise_notice("Not connected to Teams yet".to_owned(), None, cx); |
| 270 | return; |
| 271 | }; |
| 272 | self.send_call_command(CallCommand::Hold(true)); |
| 273 | self.call_count += 1; |
| 274 | let consult_id = self.call_count; |
| 275 | let Some(call) = self.call.as_mut() else { |
| 276 | return; |
| 277 | }; |
| 278 | let call_id = call.id; |
| 279 | call.consult = Some(ConsultCall { |
| 280 | id: consult_id, |
| 281 | target: (target.mri, target.name), |
| 282 | commands, |
| 283 | state: CallState::Connecting, |
| 284 | replacement: None, |
| 285 | transferring: false, |
| 286 | }); |
| 287 | self.follow_consult(call_id, consult_id, updates, cx); |
| 288 | cx.notify(); |
| 289 | } |
| 290 | |
| 291 | fn follow_consult( |
| 292 | &mut self, |
| 293 | call_id: u64, |
| 294 | consult_id: u64, |
| 295 | mut updates: tokio::sync::mpsc::UnboundedReceiver<calling::CallUpdate>, |
| 296 | cx: &mut Context<Self>, |
| 297 | ) { |
| 298 | cx.spawn(async move |this, cx| { |
| 299 | while let Some(update) = updates.recv().await { |
| 300 | if this |
| 301 | .update(cx, |state, cx| { |
| 302 | state.apply_consult_update(call_id, consult_id, update, cx) |
| 303 | }) |
| 304 | .is_err() |
| 305 | { |
| 306 | return; |
| 307 | } |
| 308 | } |
| 309 | }) |
| 310 | .detach(); |
| 311 | } |
| 312 | |
| 313 | pub(super) fn apply_consult_update( |
| 314 | &mut self, |
| 315 | call_id: u64, |
| 316 | consult_id: u64, |
| 317 | update: calling::CallUpdate, |
| 318 | cx: &mut Context<Self>, |
| 319 | ) { |
| 320 | let Some(call) = self.call.as_mut().filter(|call| call.id == call_id) else { |
| 321 | return; |
| 322 | }; |
| 323 | let Some(consult) = call |
| 324 | .consult |
| 325 | .as_mut() |
| 326 | .filter(|consult| consult.id == consult_id) |
| 327 | else { |
| 328 | return; |
| 329 | }; |
| 330 | match update { |
| 331 | calling::CallUpdate::ReplacementLink(link) => consult.replacement = Some(link), |
| 332 | calling::CallUpdate::Notice(text) => self.raise_notice(text, None, cx), |
| 333 | calling::CallUpdate::State(state) => { |
| 334 | let ended = matches!(state, CallState::Ended { .. }); |
| 335 | consult.state = state; |
| 336 | if ended { |
| 337 | let transferring = consult.transferring; |
| 338 | call.consult = None; |
| 339 | if !transferring { |
| 340 | self.raise_notice("The consultation ended".to_owned(), None, cx); |
| 341 | self.send_call_command(CallCommand::Hold(false)); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | _ => {} |
| 346 | } |
| 347 | cx.notify(); |
| 348 | } |
| 349 | |
| 350 | pub fn transfer_consulted(&mut self, _cx: &mut Context<Self>) { |
| 351 | let Some(consult) = self |
| 352 | .call |
| 353 | .as_mut() |
| 354 | .and_then(|call| call.consult.as_mut()) |
| 355 | .filter(|consult| consult.ready_to_transfer()) |
| 356 | else { |
| 357 | return; |
| 358 | }; |
| 359 | consult.transferring = true; |
| 360 | let (mri, name) = consult.target.clone(); |
| 361 | let replaces = consult.replacement.clone(); |
| 362 | self.send_call_command(CallCommand::Transfer { |
| 363 | target: Callee { |
| 364 | mri, |
| 365 | display_name: name, |
| 366 | }, |
| 367 | replaces, |
| 368 | }); |
| 369 | } |
| 370 | |
| 371 | pub fn cancel_consult(&mut self, _cx: &mut Context<Self>) { |
| 372 | if let Some(consult) = self.call.as_ref().and_then(|call| call.consult.as_ref()) { |
| 373 | let _ = consult.commands.send(CallCommand::Hangup); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | pub(super) fn begin_breakout_move( |
| 378 | &mut self, |
| 379 | moved: calling::BreakoutMove, |
| 380 | cx: &mut Context<Self>, |
| 381 | ) { |
| 382 | let Some(call) = self |
| 383 | .call |
| 384 | .as_mut() |
| 385 | .filter(|call| call.pending_move.is_none()) |
| 386 | else { |
| 387 | return; |
| 388 | }; |
| 389 | let (main, main_title) = match &call.model.breakout { |
| 390 | Some(room) => ( |
| 391 | room.main |
| 392 | .clone() |
| 393 | .or_else(|| call.model.main_meeting.clone()), |
| 394 | room.main_title.clone(), |
| 395 | ), |
| 396 | None => (call.meeting_target.clone(), call.model.title.clone()), |
| 397 | }; |
| 398 | let pending = if moved.returning { |
| 399 | PendingMove { |
| 400 | target: moved.target, |
| 401 | title: main_title, |
| 402 | breakout: None, |
| 403 | } |
| 404 | } else { |
| 405 | let breakout = BreakoutState { |
| 406 | room_name: moved.room_name.clone(), |
| 407 | main, |
| 408 | main_title, |
| 409 | }; |
| 410 | PendingMove { |
| 411 | target: moved.target, |
| 412 | title: moved.room_name.clone(), |
| 413 | breakout: Some(breakout), |
| 414 | } |
| 415 | }; |
| 416 | let notice = if moved.returning { |
| 417 | "Moving you back to the main meeting".to_owned() |
| 418 | } else { |
| 419 | format!("Moving you to {}", moved.room_name) |
| 420 | }; |
| 421 | call.pending_move = Some(pending); |
| 422 | self.raise_notice(notice, None, cx); |
| 423 | self.send_call_command(CallCommand::Hangup); |
| 424 | } |
| 425 | |
| 426 | pub fn return_to_main_meeting(&mut self, _cx: &mut Context<Self>) { |
| 427 | let Some(call) = self |
| 428 | .call |
| 429 | .as_mut() |
| 430 | .filter(|call| call.pending_move.is_none()) |
| 431 | else { |
| 432 | return; |
| 433 | }; |
| 434 | let Some(room) = call |
| 435 | .model |
| 436 | .breakout |
| 437 | .as_ref() |
| 438 | .filter(|room| room.main.is_some()) |
| 439 | else { |
| 440 | return; |
| 441 | }; |
| 442 | call.pending_move = Some(return_move(room)); |
| 443 | self.send_call_command(CallCommand::Hangup); |
| 444 | } |
| 445 | |
| 446 | pub(super) fn join_moved_call(&mut self, moved: PendingMove, cx: &mut Context<Self>) { |
| 447 | let demo = DemoCall::Meeting { |
| 448 | guests: super::demo::demo_guests(DEMO_ROOM_GUESTS), |
| 449 | lobby: false, |
| 450 | organizer: false, |
| 451 | }; |
| 452 | let Some(handle) = |
| 453 | self.place_call(CallSpec::Meeting(moved.target.clone()), demo, false, cx) |
| 454 | else { |
| 455 | return; |
| 456 | }; |
| 457 | let mut model = CallModel::meeting(&moved.title, false); |
| 458 | model.breakout = moved.breakout; |
| 459 | self.begin_call(handle, model, Some(moved.target.thread_id.clone()), cx); |
| 460 | if let Some(call) = self.call.as_mut() { |
| 461 | call.meeting_target = Some(moved.target); |
| 462 | } |
| 463 | } |
| 464 | } |