BlueGit

ict rusty-teams

Files

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

consult.rs

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