BlueGit

ict rusty-teams

Files

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

features.rs

features.rs 14.6 KiB Download Raw
1use calling::{
2 CallCommand, CallHandle, CallSpec, CallState, Callee, EndKind, EndReason, HoldState,
3};
4use gpui_kit::*;
5
6use super::background::BackgroundPick;
7use super::demo::{DemoCall, start_demo_call};
8use super::health::HealthBanner;
9use super::model::{ActiveCall, BreakoutState, CallKind, CallModel, ConsultCall, PendingMove};
10use super::recording_menu::RecordAction;
11use super::target::plan_for_chat;
12use crate::app_state::{AppEvent, AppState};
13use crate::data::is_one_on_one;
14use crate::views::call_transcript::TranscriptPanel;
15
16const DEMO_ROOM_GUESTS: usize = 3;
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct TransferCandidate {
20 pub mri: String,
21 pub name: String,
22 pub user_id: Option<String>,
23 pub chat_id: String,
24}
25
26pub 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
34fn 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
42impl 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.note_call_feature("consult", format!("start with {}", target.mri));
273 self.send_call_command(CallCommand::Hold(true));
274 self.call_count += 1;
275 let consult_id = self.call_count;
276 let Some(call) = self.call.as_mut() else {
277 return;
278 };
279 let call_id = call.id;
280 call.consult = Some(ConsultCall {
281 id: consult_id,
282 target: (target.mri, target.name),
283 commands,
284 state: CallState::Connecting,
285 replacement: None,
286 transferring: false,
287 });
288 self.follow_consult(call_id, consult_id, updates, cx);
289 cx.notify();
290 }
291
292 fn follow_consult(
293 &mut self,
294 call_id: u64,
295 consult_id: u64,
296 mut updates: tokio::sync::mpsc::UnboundedReceiver<calling::CallUpdate>,
297 cx: &mut Context<Self>,
298 ) {
299 cx.spawn(async move |this, cx| {
300 while let Some(update) = updates.recv().await {
301 if this
302 .update(cx, |state, cx| {
303 state.apply_consult_update(call_id, consult_id, update, cx)
304 })
305 .is_err()
306 {
307 return;
308 }
309 }
310 })
311 .detach();
312 }
313
314 pub(super) fn apply_consult_update(
315 &mut self,
316 call_id: u64,
317 consult_id: u64,
318 update: calling::CallUpdate,
319 cx: &mut Context<Self>,
320 ) {
321 let Some(call) = self.call.as_mut().filter(|call| call.id == call_id) else {
322 return;
323 };
324 let Some(consult) = call
325 .consult
326 .as_mut()
327 .filter(|consult| consult.id == consult_id)
328 else {
329 return;
330 };
331 match update {
332 calling::CallUpdate::ReplacementLink(link) => consult.replacement = Some(link),
333 calling::CallUpdate::Notice(text) => self.raise_notice(text, None, cx),
334 calling::CallUpdate::State(state) => {
335 let ended = matches!(state, CallState::Ended { .. });
336 consult.state = state;
337 if ended {
338 let transferring = consult.transferring;
339 call.consult = None;
340 if !transferring {
341 self.note_call_feature("consult", "ended without a transfer".to_owned());
342 self.raise_notice("The consultation ended".to_owned(), None, cx);
343 self.send_call_command(CallCommand::Hold(false));
344 }
345 }
346 }
347 _ => {}
348 }
349 cx.notify();
350 }
351
352 pub fn transfer_consulted(&mut self, _cx: &mut Context<Self>) {
353 let Some(consult) = self
354 .call
355 .as_mut()
356 .and_then(|call| call.consult.as_mut())
357 .filter(|consult| consult.ready_to_transfer())
358 else {
359 return;
360 };
361 consult.transferring = true;
362 let (mri, name) = consult.target.clone();
363 let replaces = consult.replacement.clone();
364 self.send_call_command(CallCommand::Transfer {
365 target: Callee {
366 mri,
367 display_name: name,
368 },
369 replaces,
370 });
371 }
372
373 pub fn cancel_consult(&mut self, _cx: &mut Context<Self>) {
374 if let Some(consult) = self.call.as_ref().and_then(|call| call.consult.as_ref()) {
375 let _ = consult.commands.send(CallCommand::Hangup);
376 }
377 }
378
379 pub(super) fn begin_breakout_move(
380 &mut self,
381 moved: calling::BreakoutMove,
382 cx: &mut Context<Self>,
383 ) {
384 let Some(call) = self
385 .call
386 .as_mut()
387 .filter(|call| call.pending_move.is_none())
388 else {
389 return;
390 };
391 let (main, main_title) = match &call.model.breakout {
392 Some(room) => (
393 room.main
394 .clone()
395 .or_else(|| call.model.main_meeting.clone()),
396 room.main_title.clone(),
397 ),
398 None => (call.meeting_target.clone(), call.model.title.clone()),
399 };
400 let pending = if moved.returning {
401 PendingMove {
402 target: moved.target,
403 title: main_title,
404 breakout: None,
405 }
406 } else {
407 let breakout = BreakoutState {
408 room_name: moved.room_name.clone(),
409 main,
410 main_title,
411 };
412 PendingMove {
413 target: moved.target,
414 title: moved.room_name.clone(),
415 breakout: Some(breakout),
416 }
417 };
418 let notice = if moved.returning {
419 "Moving you back to the main meeting".to_owned()
420 } else {
421 format!("Moving you to {}", moved.room_name)
422 };
423 call.pending_move = Some(pending);
424 self.raise_notice(notice, None, cx);
425 self.send_call_command(CallCommand::Hangup);
426 }
427
428 pub fn return_to_main_meeting(&mut self, _cx: &mut Context<Self>) {
429 let Some(call) = self
430 .call
431 .as_mut()
432 .filter(|call| call.pending_move.is_none())
433 else {
434 return;
435 };
436 let Some(room) = call
437 .model
438 .breakout
439 .as_ref()
440 .filter(|room| room.main.is_some())
441 else {
442 return;
443 };
444 call.pending_move = Some(return_move(room));
445 self.send_call_command(CallCommand::Hangup);
446 }
447
448 pub(super) fn join_moved_call(&mut self, moved: PendingMove, cx: &mut Context<Self>) {
449 let demo = DemoCall::Meeting {
450 guests: super::demo::demo_guests(DEMO_ROOM_GUESTS),
451 lobby: false,
452 organizer: false,
453 };
454 let Some(handle) =
455 self.place_call(CallSpec::Meeting(moved.target.clone()), demo, false, cx)
456 else {
457 return;
458 };
459 let mut model = CallModel::meeting(&moved.title, false);
460 model.breakout = moved.breakout;
461 self.begin_call(handle, model, Some(moved.target.thread_id.clone()), cx);
462 if let Some(call) = self.call.as_mut() {
463 call.meeting_target = Some(moved.target);
464 }
465 }
466}