| 1 | use calling::{CallCommand, CallHandle, CallSpec, CallUpdate, Callee}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::demo::{DemoCall, start_demo_call}; |
| 5 | use super::features::TransferCandidate; |
| 6 | use super::model::{ActiveCall, CallModel, ConsultCall, ConsultTransfer}; |
| 7 | use crate::app_state::{AppEvent, AppState}; |
| 8 | |
| 9 | const CONSULT_REPORT_NOTE: &str = "consult call, the first call is on hold"; |
| 10 | |
| 11 | fn send_to(call: &ActiveCall, command: CallCommand) { |
| 12 | if let Some(report) = &call.report { |
| 13 | report.borrow_mut().command(&command); |
| 14 | } |
| 15 | let _ = call.commands.send(command); |
| 16 | } |
| 17 | |
| 18 | impl AppState { |
| 19 | pub fn start_consult(&mut self, target: TransferCandidate, cx: &mut Context<Self>) { |
| 20 | self.close_transfer_picker(cx); |
| 21 | if !self.can_transfer_call() { |
| 22 | return; |
| 23 | } |
| 24 | let handle = if self.mode.demo { |
| 25 | Some(start_demo_call(DemoCall::People(vec![( |
| 26 | target.mri.clone(), |
| 27 | target.name.clone(), |
| 28 | )]))) |
| 29 | } else { |
| 30 | self.call_launcher.clone().map(|launcher| { |
| 31 | launcher.start_consult(CallSpec::People { |
| 32 | callees: vec![Callee { |
| 33 | mri: target.mri.clone(), |
| 34 | display_name: target.name.clone(), |
| 35 | }], |
| 36 | thread_id: target.chat_id.clone(), |
| 37 | }) |
| 38 | }) |
| 39 | }; |
| 40 | let Some(handle) = handle else { |
| 41 | self.raise_notice("Not connected to Teams yet".to_owned(), None, cx); |
| 42 | return; |
| 43 | }; |
| 44 | self.begin_consult(target, handle, cx); |
| 45 | } |
| 46 | |
| 47 | fn begin_consult( |
| 48 | &mut self, |
| 49 | target: TransferCandidate, |
| 50 | handle: CallHandle, |
| 51 | cx: &mut Context<Self>, |
| 52 | ) { |
| 53 | let callee = (target.mri.clone(), target.name.clone()); |
| 54 | self.note_call_feature("consult", format!("start with {}", target.mri)); |
| 55 | self.send_call_command(CallCommand::Hold(true)); |
| 56 | let Some(held) = self.call.take() else { |
| 57 | return; |
| 58 | }; |
| 59 | let held_name = held.model.peer_name.clone(); |
| 60 | self.held_call = Some(held); |
| 61 | if let Some(user_id) = target.user_id.clone() { |
| 62 | self.request_avatars(vec![user_id], cx); |
| 63 | } |
| 64 | let model = CallModel::people(&target.name, std::slice::from_ref(&callee)); |
| 65 | self.begin_call(handle, model, Some(target.chat_id), cx); |
| 66 | if let Some(call) = self.call.as_mut() { |
| 67 | call.consult = Some(ConsultCall::new(callee, held_name)); |
| 68 | } |
| 69 | self.note_call_feature("consult", CONSULT_REPORT_NOTE.to_owned()); |
| 70 | cx.notify(); |
| 71 | } |
| 72 | |
| 73 | pub fn transfer_consulted(&mut self, cx: &mut Context<Self>) { |
| 74 | let (Some(call), Some(held)) = (self.call.as_mut(), self.held_call.as_ref()) else { |
| 75 | return; |
| 76 | }; |
| 77 | let model = &call.model; |
| 78 | let Some(consult) = call |
| 79 | .consult |
| 80 | .as_mut() |
| 81 | .filter(|consult| consult.ready_to_transfer(model)) |
| 82 | else { |
| 83 | return; |
| 84 | }; |
| 85 | consult.transfer = ConsultTransfer::Pending; |
| 86 | let (mri, display_name) = consult.target.clone(); |
| 87 | send_to( |
| 88 | held, |
| 89 | CallCommand::Transfer { |
| 90 | target: Callee { mri, display_name }, |
| 91 | replaces: consult.replacement.clone(), |
| 92 | }, |
| 93 | ); |
| 94 | cx.notify(); |
| 95 | } |
| 96 | |
| 97 | pub fn resume_held_call(&mut self, _cx: &mut Context<Self>) { |
| 98 | let (Some(call), Some(held)) = (self.call.as_ref(), self.held_call.as_ref()) else { |
| 99 | return; |
| 100 | }; |
| 101 | if call.consult.is_none() { |
| 102 | return; |
| 103 | } |
| 104 | send_to(held, CallCommand::Hold(false)); |
| 105 | send_to(call, CallCommand::Hangup); |
| 106 | } |
| 107 | |
| 108 | pub(super) fn is_held_call(&self, call_id: u64) -> bool { |
| 109 | self.held_call |
| 110 | .as_ref() |
| 111 | .is_some_and(|held| held.id == call_id) |
| 112 | } |
| 113 | |
| 114 | pub(super) fn apply_held_call_update(&mut self, update: CallUpdate, cx: &mut Context<Self>) { |
| 115 | let Some(held) = self.held_call.as_mut() else { |
| 116 | return; |
| 117 | }; |
| 118 | match update { |
| 119 | CallUpdate::Notice(text) => { |
| 120 | self.raise_notice(app_base::ui_locale::localize_notice(&text), None, cx); |
| 121 | return; |
| 122 | } |
| 123 | CallUpdate::TransferResult { succeeded } => { |
| 124 | self.settle_consult_transfer(succeeded); |
| 125 | cx.notify(); |
| 126 | return; |
| 127 | } |
| 128 | CallUpdate::VideoReady |
| 129 | | CallUpdate::Levels { .. } |
| 130 | | CallUpdate::Stats { .. } |
| 131 | | CallUpdate::Sample(_) |
| 132 | | CallUpdate::Health(_) => return, |
| 133 | other => held.model.apply(other), |
| 134 | } |
| 135 | if held.model.ended_reason().is_none() { |
| 136 | return; |
| 137 | } |
| 138 | let Some(mut ended) = self.held_call.take() else { |
| 139 | return; |
| 140 | }; |
| 141 | ended.pictures.clear(cx); |
| 142 | let consult = self.call.as_mut().and_then(|call| { |
| 143 | let transferred = call |
| 144 | .consult |
| 145 | .as_ref() |
| 146 | .is_some_and(|consult| consult.transfer == ConsultTransfer::Done); |
| 147 | if transferred { |
| 148 | None |
| 149 | } else { |
| 150 | call.consult.take() |
| 151 | } |
| 152 | }); |
| 153 | if let Some(consult) = consult { |
| 154 | self.raise_notice(format!("{} hung up", consult.held_name), None, cx); |
| 155 | } |
| 156 | self.refresh_call_history_after_call(cx); |
| 157 | cx.emit(AppEvent::Call); |
| 158 | cx.notify(); |
| 159 | } |
| 160 | |
| 161 | fn settle_consult_transfer(&mut self, succeeded: bool) { |
| 162 | let Some(call) = self.call.as_mut() else { |
| 163 | return; |
| 164 | }; |
| 165 | let Some(consult) = call.consult.as_mut() else { |
| 166 | return; |
| 167 | }; |
| 168 | consult.transfer = if succeeded { |
| 169 | ConsultTransfer::Done |
| 170 | } else { |
| 171 | ConsultTransfer::Idle |
| 172 | }; |
| 173 | if succeeded { |
| 174 | send_to(call, CallCommand::Hangup); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | #[cfg(test)] |
| 180 | mod tests { |
| 181 | use std::sync::Arc; |
| 182 | use std::time::Instant; |
| 183 | |
| 184 | use calling::{ |
| 185 | CallCommand, CallControl, CallState, CallUpdate, Callee, EndReason, HoldState, Replacement, |
| 186 | call_channel, |
| 187 | }; |
| 188 | use gpui_kit::{AppContext as _, Entity, TestAppContext}; |
| 189 | use store::Store; |
| 190 | |
| 191 | use super::{ActiveCall, CallModel, ConsultCall, TransferCandidate}; |
| 192 | use crate::app_state::{AppState, Mode}; |
| 193 | |
| 194 | const HELD_ID: u64 = 7; |
| 195 | const CONSULT_ID: u64 = 8; |
| 196 | |
| 197 | fn connected(name: &str, mri: &str) -> CallModel { |
| 198 | let mut model = CallModel::people(name, &[(mri.into(), name.into())]); |
| 199 | model.apply(CallUpdate::State(CallState::Connected { |
| 200 | since: Instant::now(), |
| 201 | })); |
| 202 | model |
| 203 | } |
| 204 | |
| 205 | fn consulting(cx: &mut TestAppContext) -> (Entity<AppState>, CallControl, CallControl) { |
| 206 | cx.update(gpui_kit::init); |
| 207 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 208 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 209 | let (held_handle, held_control) = call_channel(); |
| 210 | let (consult_handle, consult_control) = call_channel(); |
| 211 | cx.update(|cx| { |
| 212 | app.update(cx, |state, _| { |
| 213 | let mut held_model = connected("Chris", "8:orgid:chris"); |
| 214 | held_model.apply(CallUpdate::Hold(HoldState::Local)); |
| 215 | state.held_call = Some(ActiveCall::new( |
| 216 | HELD_ID, |
| 217 | held_model, |
| 218 | held_handle.commands, |
| 219 | None, |
| 220 | held_handle.video, |
| 221 | )); |
| 222 | let mut consult = ActiveCall::new( |
| 223 | CONSULT_ID, |
| 224 | connected("Ana", "8:orgid:ana"), |
| 225 | consult_handle.commands, |
| 226 | None, |
| 227 | consult_handle.video, |
| 228 | ); |
| 229 | consult.consult = Some(ConsultCall::new( |
| 230 | ("8:orgid:ana".into(), "Ana".into()), |
| 231 | "Chris".into(), |
| 232 | )); |
| 233 | state.call = Some(consult); |
| 234 | }) |
| 235 | }); |
| 236 | (app, held_control, consult_control) |
| 237 | } |
| 238 | |
| 239 | fn ended() -> CallUpdate { |
| 240 | CallUpdate::State(CallState::Ended { |
| 241 | reason: EndReason::LocalHangup, |
| 242 | }) |
| 243 | } |
| 244 | |
| 245 | fn leg() -> Replacement { |
| 246 | Replacement { |
| 247 | link: "https://cc/ana/replacement".into(), |
| 248 | participant_id: Some("leg-ana".into()), |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #[gpui_kit::test] |
| 253 | fn consulting_holds_the_first_call_and_shows_the_consult_call(cx: &mut TestAppContext) { |
| 254 | cx.update(gpui_kit::init); |
| 255 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 256 | let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default()))); |
| 257 | let (held_handle, mut held_control) = call_channel(); |
| 258 | let (consult_handle, _consult_control) = call_channel(); |
| 259 | let target = TransferCandidate { |
| 260 | mri: "8:orgid:ana".into(), |
| 261 | name: "Ana".into(), |
| 262 | user_id: Some("ana".into()), |
| 263 | chat_id: "19:ana".into(), |
| 264 | }; |
| 265 | cx.update(|cx| { |
| 266 | app.update(cx, |state, cx| { |
| 267 | state.call = Some(ActiveCall::new( |
| 268 | HELD_ID, |
| 269 | connected("Chris", "8:orgid:chris"), |
| 270 | held_handle.commands, |
| 271 | None, |
| 272 | held_handle.video, |
| 273 | )); |
| 274 | state.begin_consult(target, consult_handle, cx); |
| 275 | assert_eq!(state.held_call.as_ref().unwrap().id, HELD_ID); |
| 276 | let call = state.call.as_ref().unwrap(); |
| 277 | assert_ne!(call.id, HELD_ID); |
| 278 | assert_eq!(call.model.peer_name, "Ana"); |
| 279 | assert_eq!( |
| 280 | call.consult.as_ref().unwrap().banner_text(), |
| 281 | "Chris is on hold" |
| 282 | ); |
| 283 | }) |
| 284 | }); |
| 285 | assert_eq!( |
| 286 | held_control.try_recv_command(), |
| 287 | Some(CallCommand::Hold(true)) |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | #[gpui_kit::test] |
| 292 | fn the_transfer_goes_to_the_held_call_with_the_consulted_persons_leg(cx: &mut TestAppContext) { |
| 293 | let (app, mut held_control, mut consult_control) = consulting(cx); |
| 294 | cx.update(|cx| { |
| 295 | app.update(cx, |state, cx| { |
| 296 | state.transfer_consulted(cx); |
| 297 | state.apply_call_update(CONSULT_ID, CallUpdate::ReplacementLink(leg()), cx); |
| 298 | state.transfer_consulted(cx); |
| 299 | state.transfer_consulted(cx); |
| 300 | }) |
| 301 | }); |
| 302 | assert_eq!( |
| 303 | held_control.try_recv_command(), |
| 304 | Some(CallCommand::Transfer { |
| 305 | target: Callee { |
| 306 | mri: "8:orgid:ana".into(), |
| 307 | display_name: "Ana".into() |
| 308 | }, |
| 309 | replaces: Some(leg()), |
| 310 | }) |
| 311 | ); |
| 312 | assert_eq!(held_control.try_recv_command(), None); |
| 313 | assert_eq!(consult_control.try_recv_command(), None); |
| 314 | } |
| 315 | |
| 316 | #[gpui_kit::test] |
| 317 | fn a_failed_transfer_can_be_tried_again_and_a_completed_one_ends_both_calls( |
| 318 | cx: &mut TestAppContext, |
| 319 | ) { |
| 320 | let (app, mut held_control, mut consult_control) = consulting(cx); |
| 321 | cx.update(|cx| { |
| 322 | app.update(cx, |state, cx| { |
| 323 | state.apply_call_update(CONSULT_ID, CallUpdate::ReplacementLink(leg()), cx); |
| 324 | state.transfer_consulted(cx); |
| 325 | state.apply_call_update( |
| 326 | HELD_ID, |
| 327 | CallUpdate::TransferResult { succeeded: false }, |
| 328 | cx, |
| 329 | ); |
| 330 | state.transfer_consulted(cx); |
| 331 | state.apply_call_update( |
| 332 | HELD_ID, |
| 333 | CallUpdate::TransferResult { succeeded: true }, |
| 334 | cx, |
| 335 | ); |
| 336 | state.apply_call_update(HELD_ID, ended(), cx); |
| 337 | assert!(state.held_call.is_none()); |
| 338 | assert!(state.call.as_ref().unwrap().consult.is_some()); |
| 339 | state.apply_call_update(CONSULT_ID, ended(), cx); |
| 340 | assert!(state.call.is_none()); |
| 341 | assert_eq!( |
| 342 | state.notice.as_ref().unwrap().text, |
| 343 | "Call transferred to Ana" |
| 344 | ); |
| 345 | }) |
| 346 | }); |
| 347 | assert!(matches!( |
| 348 | held_control.try_recv_command(), |
| 349 | Some(CallCommand::Transfer { .. }) |
| 350 | )); |
| 351 | assert!(matches!( |
| 352 | held_control.try_recv_command(), |
| 353 | Some(CallCommand::Transfer { .. }) |
| 354 | )); |
| 355 | assert_eq!( |
| 356 | consult_control.try_recv_command(), |
| 357 | Some(CallCommand::Hangup) |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | #[gpui_kit::test] |
| 362 | fn the_consult_call_keeps_going_when_the_held_caller_hangs_up(cx: &mut TestAppContext) { |
| 363 | let (app, _held_control, mut consult_control) = consulting(cx); |
| 364 | cx.update(|cx| { |
| 365 | app.update(cx, |state, cx| { |
| 366 | state.apply_call_update(HELD_ID, ended(), cx); |
| 367 | assert!(state.held_call.is_none()); |
| 368 | let call = state.call.as_ref().expect("consult call stays"); |
| 369 | assert_eq!(call.id, CONSULT_ID); |
| 370 | assert!(call.consult.is_none()); |
| 371 | assert!(call.model.is_active()); |
| 372 | assert_eq!(state.notice.as_ref().unwrap().text, "Chris hung up"); |
| 373 | }) |
| 374 | }); |
| 375 | assert_eq!(consult_control.try_recv_command(), None); |
| 376 | } |
| 377 | |
| 378 | #[gpui_kit::test] |
| 379 | fn the_held_call_comes_back_when_the_consult_ends_and_resume_ends_the_consult( |
| 380 | cx: &mut TestAppContext, |
| 381 | ) { |
| 382 | let (app, mut held_control, mut consult_control) = consulting(cx); |
| 383 | cx.update(|cx| { |
| 384 | app.update(cx, |state, cx| { |
| 385 | state.resume_held_call(cx); |
| 386 | state.apply_call_update(CONSULT_ID, ended(), cx); |
| 387 | let call = state.call.as_ref().expect("held call is back"); |
| 388 | assert_eq!(call.id, HELD_ID); |
| 389 | assert!(state.held_call.is_none()); |
| 390 | assert_eq!( |
| 391 | state.notice.as_ref().unwrap().text, |
| 392 | "The consultation ended" |
| 393 | ); |
| 394 | }) |
| 395 | }); |
| 396 | assert_eq!( |
| 397 | held_control.try_recv_command(), |
| 398 | Some(CallCommand::Hold(false)) |
| 399 | ); |
| 400 | assert_eq!( |
| 401 | consult_control.try_recv_command(), |
| 402 | Some(CallCommand::Hangup) |
| 403 | ); |
| 404 | } |
| 405 | |
| 406 | #[gpui_kit::test] |
| 407 | fn held_call_updates_never_reach_the_visible_consult_call(cx: &mut TestAppContext) { |
| 408 | let (app, _held_control, _consult_control) = consulting(cx); |
| 409 | cx.update(|cx| { |
| 410 | app.update(cx, |state, cx| { |
| 411 | state.apply_call_update(HELD_ID, CallUpdate::Hold(HoldState::Active), cx); |
| 412 | assert_eq!( |
| 413 | state.held_call.as_ref().unwrap().model.hold, |
| 414 | HoldState::Active |
| 415 | ); |
| 416 | assert_eq!(state.call.as_ref().unwrap().model.hold, HoldState::Active); |
| 417 | assert_eq!(state.call.as_ref().unwrap().model.peer_name, "Ana"); |
| 418 | assert!(!state.can_transfer_call()); |
| 419 | }) |
| 420 | }); |
| 421 | } |
| 422 | } |