BlueGit

ict rusty-teams

Files

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

features.rs

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