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