BlueGit

ict rusty-teams

Files

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

features.rs

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