| 1 | use calling::{CallCommand, CallHandle, CallSpec, CallState, Callee, EndKind, EndReason, HoldState}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::demo::{DemoCall, start_demo_call}; |
| 5 | use super::model::{ActiveCall, BreakoutState, CallModel, ConsultCall, PendingMove}; |
| 6 | use super::target::plan_for_chat; |
| 7 | use crate::app_state::AppState; |
| 8 | use crate::data::is_one_on_one; |
| 9 | |
| 10 | const DEMO_ROOM_GUESTS: usize = 3; |
| 11 | |
| 12 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 13 | pub struct TransferCandidate { |
| 14 | pub mri: String, |
| 15 | pub name: String, |
| 16 | pub user_id: Option<String>, |
| 17 | pub chat_id: String, |
| 18 | } |
| 19 | |
| 20 | pub fn room_closed_move(call: &ActiveCall, reason: &EndReason) -> Option<PendingMove> { |
| 21 | let room = call.model.breakout.as_ref()?; |
| 22 | if *reason != EndReason::Remote(EndKind::RoomClosed) { |
| 23 | return None; |
| 24 | } |
| 25 | Some(return_move(room)) |
| 26 | } |
| 27 | |
| 28 | fn return_move(room: &BreakoutState) -> PendingMove { |
| 29 | PendingMove { target: room.main.clone().expect("a room with a way back"), title: room.main_title.clone(), breakout: None } |
| 30 | } |
| 31 | |
| 32 | impl AppState { |
| 33 | pub fn set_call_recording(&mut self, on: bool, _cx: &mut Context<Self>) { |
| 34 | let Some(call) = self.call.as_ref().filter(|call| call.model.can_record()) else { |
| 35 | return; |
| 36 | }; |
| 37 | let title = call.model.title.clone(); |
| 38 | self.send_call_command(CallCommand::SetRecording { on, title }); |
| 39 | } |
| 40 | |
| 41 | pub fn accept_recording_notice(&mut self, _cx: &mut Context<Self>) { |
| 42 | if self.call.as_ref().is_some_and(|call| call.model.consent_required) { |
| 43 | self.send_call_command(CallCommand::ConsentToRecording); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub fn toggle_call_hold(&mut self, _cx: &mut Context<Self>) { |
| 48 | let Some(call) = self.call.as_ref() else { |
| 49 | return; |
| 50 | }; |
| 51 | let hold = match call.model.hold { |
| 52 | HoldState::Local => false, |
| 53 | HoldState::Active if call.model.can_hold() => true, |
| 54 | _ => return, |
| 55 | }; |
| 56 | self.send_call_command(CallCommand::Hold(hold)); |
| 57 | } |
| 58 | |
| 59 | pub fn open_call_whiteboard(&mut self, cx: &mut Context<Self>) { |
| 60 | let Some(call) = self.call.as_ref().filter(|call| call.model.can_open_whiteboard()) else { |
| 61 | return; |
| 62 | }; |
| 63 | if let Some(url) = call.model.whiteboard_url() { |
| 64 | cx.open_url(url); |
| 65 | return; |
| 66 | } |
| 67 | let title = call.model.title.clone(); |
| 68 | self.send_call_command(CallCommand::OpenWhiteboard { title }); |
| 69 | } |
| 70 | |
| 71 | pub fn transfer_candidates(&self) -> Vec<TransferCandidate> { |
| 72 | let peer = self.call.as_ref().and_then(|call| call.model.tiles.first().map(|tile| tile.mri.clone())); |
| 73 | self.sidebar |
| 74 | .chats |
| 75 | .iter() |
| 76 | .filter(|chat| is_one_on_one(chat)) |
| 77 | .filter_map(|chat| { |
| 78 | let plan = plan_for_chat(chat, self.directory.me.as_ref())?; |
| 79 | let (mri, name) = plan.callees.into_iter().next()?; |
| 80 | Some(TransferCandidate { user_id: mri.strip_prefix("8:orgid:").map(str::to_owned), mri, name, chat_id: chat.id.clone() }) |
| 81 | }) |
| 82 | .filter(|candidate| Some(&candidate.mri) != peer.as_ref()) |
| 83 | .collect() |
| 84 | } |
| 85 | |
| 86 | pub fn can_transfer_call(&self) -> bool { |
| 87 | self.call.as_ref().is_some_and(|call| call.model.can_transfer() && call.consult.is_none()) |
| 88 | } |
| 89 | |
| 90 | pub fn open_transfer_picker(&mut self, picker: Entity<crate::views::transfer_picker::TransferPicker>, cx: &mut Context<Self>) { |
| 91 | if self.can_transfer_call() { |
| 92 | self.transfer_picker = Some(picker); |
| 93 | cx.notify(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn close_transfer_picker(&mut self, cx: &mut Context<Self>) { |
| 98 | if self.transfer_picker.take().is_some() { |
| 99 | cx.notify(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | pub fn transfer_call_blind(&mut self, target: TransferCandidate, cx: &mut Context<Self>) { |
| 104 | self.close_transfer_picker(cx); |
| 105 | if !self.can_transfer_call() { |
| 106 | return; |
| 107 | } |
| 108 | let callee = Callee { mri: target.mri, display_name: target.name }; |
| 109 | self.send_call_command(CallCommand::Transfer { target: callee, replaces: None }); |
| 110 | } |
| 111 | |
| 112 | pub fn start_consult(&mut self, target: TransferCandidate, cx: &mut Context<Self>) { |
| 113 | self.close_transfer_picker(cx); |
| 114 | if !self.can_transfer_call() { |
| 115 | return; |
| 116 | } |
| 117 | let spec = CallSpec::People { |
| 118 | callees: vec![Callee { mri: target.mri.clone(), display_name: target.name.clone() }], |
| 119 | thread_id: target.chat_id.clone(), |
| 120 | }; |
| 121 | let handle = if self.mode.demo { |
| 122 | Some(start_demo_call(DemoCall::People(vec![(target.mri.clone(), target.name.clone())]))) |
| 123 | } else { |
| 124 | self.call_launcher.clone().map(|launcher| launcher.start_consult(spec)) |
| 125 | }; |
| 126 | let Some(CallHandle { commands, updates, .. }) = handle else { |
| 127 | self.raise_notice("Not connected to Teams yet".to_owned(), None, cx); |
| 128 | return; |
| 129 | }; |
| 130 | self.send_call_command(CallCommand::Hold(true)); |
| 131 | self.call_count += 1; |
| 132 | let consult_id = self.call_count; |
| 133 | let Some(call) = self.call.as_mut() else { |
| 134 | return; |
| 135 | }; |
| 136 | let call_id = call.id; |
| 137 | call.consult = Some(ConsultCall { |
| 138 | id: consult_id, |
| 139 | target: (target.mri, target.name), |
| 140 | commands, |
| 141 | state: CallState::Connecting, |
| 142 | replacement: None, |
| 143 | transferring: false, |
| 144 | }); |
| 145 | self.follow_consult(call_id, consult_id, updates, cx); |
| 146 | cx.notify(); |
| 147 | } |
| 148 | |
| 149 | fn follow_consult(&mut self, call_id: u64, consult_id: u64, mut updates: tokio::sync::mpsc::UnboundedReceiver<calling::CallUpdate>, cx: &mut Context<Self>) { |
| 150 | cx.spawn(async move |this, cx| { |
| 151 | while let Some(update) = updates.recv().await { |
| 152 | if this.update(cx, |state, cx| state.apply_consult_update(call_id, consult_id, update, cx)).is_err() { |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | }) |
| 157 | .detach(); |
| 158 | } |
| 159 | |
| 160 | pub(super) fn apply_consult_update(&mut self, call_id: u64, consult_id: u64, update: calling::CallUpdate, cx: &mut Context<Self>) { |
| 161 | let Some(call) = self.call.as_mut().filter(|call| call.id == call_id) else { |
| 162 | return; |
| 163 | }; |
| 164 | let Some(consult) = call.consult.as_mut().filter(|consult| consult.id == consult_id) else { |
| 165 | return; |
| 166 | }; |
| 167 | match update { |
| 168 | calling::CallUpdate::ReplacementLink(link) => consult.replacement = Some(link), |
| 169 | calling::CallUpdate::Notice(text) => self.raise_notice(text, None, cx), |
| 170 | calling::CallUpdate::State(state) => { |
| 171 | let ended = matches!(state, CallState::Ended { .. }); |
| 172 | consult.state = state; |
| 173 | if ended { |
| 174 | let transferring = consult.transferring; |
| 175 | call.consult = None; |
| 176 | if !transferring { |
| 177 | self.raise_notice("The consultation ended".to_owned(), None, cx); |
| 178 | self.send_call_command(CallCommand::Hold(false)); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | _ => {} |
| 183 | } |
| 184 | cx.notify(); |
| 185 | } |
| 186 | |
| 187 | pub fn transfer_consulted(&mut self, _cx: &mut Context<Self>) { |
| 188 | let Some(consult) = self.call.as_mut().and_then(|call| call.consult.as_mut()).filter(|consult| consult.ready_to_transfer()) else { |
| 189 | return; |
| 190 | }; |
| 191 | consult.transferring = true; |
| 192 | let (mri, name) = consult.target.clone(); |
| 193 | let replaces = consult.replacement.clone(); |
| 194 | self.send_call_command(CallCommand::Transfer { target: Callee { mri, display_name: name }, replaces }); |
| 195 | } |
| 196 | |
| 197 | pub fn cancel_consult(&mut self, _cx: &mut Context<Self>) { |
| 198 | if let Some(consult) = self.call.as_ref().and_then(|call| call.consult.as_ref()) { |
| 199 | let _ = consult.commands.send(CallCommand::Hangup); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | pub(super) fn begin_breakout_move(&mut self, moved: calling::BreakoutMove, cx: &mut Context<Self>) { |
| 204 | let Some(call) = self.call.as_mut().filter(|call| call.pending_move.is_none()) else { |
| 205 | return; |
| 206 | }; |
| 207 | let (main, main_title) = match &call.model.breakout { |
| 208 | Some(room) => (room.main.clone().or_else(|| call.model.main_meeting.clone()), room.main_title.clone()), |
| 209 | None => (call.meeting_target.clone(), call.model.title.clone()), |
| 210 | }; |
| 211 | let pending = if moved.returning { |
| 212 | PendingMove { target: moved.target, title: main_title, breakout: None } |
| 213 | } else { |
| 214 | let breakout = BreakoutState { room_name: moved.room_name.clone(), main, main_title }; |
| 215 | PendingMove { target: moved.target, title: moved.room_name.clone(), breakout: Some(breakout) } |
| 216 | }; |
| 217 | let notice = if moved.returning { "Moving you back to the main meeting".to_owned() } else { format!("Moving you to {}", moved.room_name) }; |
| 218 | call.pending_move = Some(pending); |
| 219 | self.raise_notice(notice, None, cx); |
| 220 | self.send_call_command(CallCommand::Hangup); |
| 221 | } |
| 222 | |
| 223 | pub fn return_to_main_meeting(&mut self, _cx: &mut Context<Self>) { |
| 224 | let Some(call) = self.call.as_mut().filter(|call| call.pending_move.is_none()) else { |
| 225 | return; |
| 226 | }; |
| 227 | let Some(room) = call.model.breakout.as_ref().filter(|room| room.main.is_some()) else { |
| 228 | return; |
| 229 | }; |
| 230 | call.pending_move = Some(return_move(room)); |
| 231 | self.send_call_command(CallCommand::Hangup); |
| 232 | } |
| 233 | |
| 234 | pub(super) fn join_moved_call(&mut self, moved: PendingMove, cx: &mut Context<Self>) { |
| 235 | let demo = DemoCall::Meeting { guests: super::demo::demo_guests(DEMO_ROOM_GUESTS), lobby: false, organizer: false }; |
| 236 | let Some(handle) = self.place_call(CallSpec::Meeting(moved.target.clone()), demo, cx) else { |
| 237 | return; |
| 238 | }; |
| 239 | let mut model = CallModel::meeting(&moved.title, false); |
| 240 | model.breakout = moved.breakout; |
| 241 | self.begin_call(handle, model, Some(moved.target.thread_id.clone()), cx); |
| 242 | if let Some(call) = self.call.as_mut() { |
| 243 | call.meeting_target = Some(moved.target); |
| 244 | } |
| 245 | } |
| 246 | } |