BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / people_actions.rs

people_actions.rs

people_actions.rs 16.9 KiB Download Raw
1use calling::{CallCommand, Callee, MeetingOptions, MeetingRole, Modality};
2use gpui_kit::*;
3
4use super::actions::NOT_CONNECTED_NOTICE;
5use super::features::TransferCandidate;
6use super::model::{CallKind, Tile, TileState};
7use super::options_dialog::OptionsDialog;
8use crate::app_state::{AppEvent, AppState};
9use crate::runtime;
10
11const INVITE_FAILED_NOTICE: &str = "Could not add that person";
12const NO_PEER_NOTICE: &str = "This call cannot be extended";
13
14impl AppState {
15 pub fn toggle_call_people(&mut self, cx: &mut Context<Self>) {
16 let Some(call) = self.call.as_mut() else {
17 return;
18 };
19 call.people_open = !call.people_open;
20 if call.people_open {
21 call.chat_open = false;
22 call.transcript_open = false;
23 self.transcript_panel = None;
24 }
25 cx.emit(AppEvent::Call);
26 cx.notify();
27 }
28
29 pub fn set_call_lock(&mut self, locked: bool, _cx: &mut Context<Self>) {
30 if self
31 .call
32 .as_ref()
33 .is_some_and(|call| call.model.organizes_meeting())
34 {
35 self.send_call_command(CallCommand::SetLock(locked));
36 }
37 }
38
39 pub fn set_call_role(&mut self, mri: String, role: MeetingRole, _cx: &mut Context<Self>) {
40 let allowed = self.call.as_ref().is_some_and(|call| {
41 call.model.can_change_roles(&mri) && role != MeetingRole::Organizer
42 });
43 if allowed {
44 self.send_call_command(CallCommand::SetRole { mri, role });
45 }
46 }
47
48 pub fn set_call_attendee_modality(
49 &mut self,
50 mri: String,
51 modality: Modality,
52 allowed: bool,
53 _cx: &mut Context<Self>,
54 ) {
55 if self
56 .call
57 .as_ref()
58 .is_some_and(|call| call.model.can_change_roles(&mri))
59 {
60 self.send_call_command(CallCommand::SetAttendeeModality {
61 mri,
62 modality,
63 allowed,
64 });
65 }
66 }
67
68 pub fn set_call_attendee_restrictions(
69 &mut self,
70 mic_off: bool,
71 camera_off: bool,
72 _cx: &mut Context<Self>,
73 ) {
74 if self
75 .call
76 .as_ref()
77 .is_some_and(|call| call.model.organizes_meeting())
78 {
79 self.send_call_command(CallCommand::SetAttendeeRestrictions {
80 mic_off,
81 camera_off,
82 });
83 }
84 }
85
86 pub fn open_meeting_options(&mut self, cx: &mut Context<Self>) {
87 let Some(call) = self
88 .call
89 .as_mut()
90 .filter(|call| call.model.organizes_meeting())
91 else {
92 return;
93 };
94 call.options_dialog = Some(OptionsDialog::loading());
95 self.send_call_command(CallCommand::LoadMeetingOptions);
96 cx.notify();
97 }
98
99 pub fn close_meeting_options(&mut self, cx: &mut Context<Self>) {
100 if let Some(call) = self
101 .call
102 .as_mut()
103 .filter(|call| call.options_dialog.is_some())
104 {
105 call.options_dialog = None;
106 cx.notify();
107 }
108 }
109
110 pub fn edit_meeting_options(
111 &mut self,
112 edit: impl FnOnce(&mut MeetingOptions),
113 cx: &mut Context<Self>,
114 ) {
115 let Some(draft) = self
116 .call
117 .as_mut()
118 .and_then(|call| call.options_dialog.as_mut())
119 .and_then(|dialog| dialog.draft.as_mut())
120 else {
121 return;
122 };
123 edit(draft);
124 cx.notify();
125 }
126
127 pub fn save_meeting_options(&mut self, cx: &mut Context<Self>) {
128 let writes = self
129 .call
130 .as_mut()
131 .and_then(|call| call.options_dialog.as_mut())
132 .and_then(OptionsDialog::begin_save);
133 if let Some(writes) = writes {
134 self.send_call_command(CallCommand::SaveMeetingOptions(writes));
135 cx.notify();
136 }
137 }
138
139 pub fn invite_candidates(&self) -> Vec<TransferCandidate> {
140 let present: Vec<&str> = self
141 .call
142 .as_ref()
143 .map(|call| {
144 call.model
145 .tiles
146 .iter()
147 .map(|tile| tile.mri.as_str())
148 .collect()
149 })
150 .unwrap_or_default();
151 self.transfer_candidates()
152 .into_iter()
153 .filter(|candidate| !present.contains(&candidate.mri.as_str()))
154 .collect()
155 }
156
157 pub fn open_invite_picker(&mut self, picker: AnyView, cx: &mut Context<Self>) {
158 if self
159 .call
160 .as_ref()
161 .is_some_and(|call| call.model.can_invite())
162 {
163 self.transfer_picker = Some(picker);
164 cx.notify();
165 }
166 }
167
168 fn drop_invited_tile(&mut self, mri: &str) {
169 if let Some(call) = self.call.as_mut() {
170 call.model
171 .tiles
172 .retain(|tile| tile.mri != mri || tile.state != TileState::Invited);
173 }
174 }
175
176 pub fn invite_call_person(&mut self, target: TransferCandidate, cx: &mut Context<Self>) {
177 self.close_transfer_picker(cx);
178 let Some(call) = self.call.as_mut().filter(|call| call.model.can_invite()) else {
179 return;
180 };
181 let callee = Callee {
182 mri: target.mri.clone(),
183 display_name: target.name.clone(),
184 };
185 if !call.model.tiles.iter().any(|tile| tile.mri == callee.mri) {
186 call.model
187 .tiles
188 .push(Tile::invited(&callee.mri, &callee.display_name));
189 }
190 let one_to_one = call.model.kind == CallKind::Direct;
191 let peer_user_id = call
192 .model
193 .tiles
194 .iter()
195 .find(|tile| tile.mri != callee.mri)
196 .and_then(|tile| tile.user_id.clone());
197 let call_id = call.id;
198 cx.emit(AppEvent::Call);
199 cx.notify();
200 if !one_to_one || self.mode.demo {
201 self.send_call_command(CallCommand::Invite {
202 callees: vec![callee],
203 group_thread: None,
204 });
205 return;
206 }
207 let (Some(engine), Some(peer_user_id), Some(invited_user_id)) =
208 (self.engine.clone(), peer_user_id, target.user_id.clone())
209 else {
210 let notice = if self.engine.is_none() {
211 NOT_CONNECTED_NOTICE
212 } else {
213 NO_PEER_NOTICE
214 };
215 self.drop_invited_tile(&callee.mri);
216 self.raise_notice(notice.to_owned(), None, cx);
217 return;
218 };
219 let receiver = runtime::spawn(async move {
220 engine
221 .create_group(&[peer_user_id, invited_user_id], None)
222 .await
223 });
224 let invited_mri = callee.mri.clone();
225 cx.spawn(async move |this, cx| {
226 let created = receiver.await.ok().and_then(Result::ok);
227 this.update(cx, |state, cx| match created {
228 Some(thread_id) if state.call.as_ref().is_some_and(|call| call.id == call_id) => {
229 state.send_call_command(CallCommand::Invite {
230 callees: vec![callee],
231 group_thread: Some(thread_id),
232 });
233 }
234 _ => {
235 if state.call.as_ref().is_some_and(|call| call.id == call_id) {
236 state.drop_invited_tile(&invited_mri);
237 }
238 state.raise_notice(INVITE_FAILED_NOTICE.to_owned(), None, cx);
239 }
240 })
241 .ok();
242 })
243 .detach();
244 }
245}
246
247#[cfg(test)]
248mod tests {
249 use std::sync::Arc;
250 use std::time::Instant;
251
252 use calling::{
253 CallCommand, CallControl, CallState, CallUpdate, MeetingCapability, MeetingOptions,
254 MeetingRole, Modality, RosterEntry, call_channel,
255 };
256 use gpui_kit::{AppContext as _, Entity, TestAppContext};
257 use serde_json::json;
258 use store::Store;
259
260 use crate::app_state::{AppState, Mode};
261 use crate::call::{ActiveCall, CallModel, TransferCandidate};
262
263 fn app_with_meeting(
264 cx: &mut TestAppContext,
265 organizer: bool,
266 ) -> (Entity<AppState>, CallControl) {
267 cx.update(gpui_kit::init);
268 let store = Arc::new(Store::open_in_memory().unwrap());
269 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
270 let (handle, control) = call_channel();
271 let mut model = CallModel::meeting("Standup", organizer);
272 model.apply(CallUpdate::State(CallState::Connected {
273 since: Instant::now(),
274 }));
275 model.apply(CallUpdate::OwnIdentity {
276 mri: "8:orgid:me".into(),
277 });
278 model.apply(CallUpdate::Roster(vec![RosterEntry {
279 mri: "8:orgid:a".into(),
280 display_name: "Ana".into(),
281 role: Some(MeetingRole::Attendee),
282 ..RosterEntry::default()
283 }]));
284 cx.update(|cx| {
285 app.update(cx, |state, _| {
286 state.call = Some(ActiveCall::new(
287 7,
288 model,
289 handle.commands,
290 Some("19:meeting_x@thread.v2".into()),
291 handle.video,
292 ));
293 })
294 });
295 (app, control)
296 }
297
298 fn options() -> MeetingOptions {
299 let option =
300 |name: &str, value: serde_json::Value| json!({"name": name, "currentValue": value});
301 MeetingOptions::from_response(&json!({"options": [
302 option("AutoAdmittedUsers", json!("EveryoneInCompany")),
303 option("PresenterOption", json!("Everyone")),
304 option("AttendeeRestrictions", json!("UnRestricted")),
305 option("AllowMeetingChat", json!("Enabled")),
306 option("AllowTeamsMeetingReactions", json!(true)),
307 option("AutoRecordAndTranscribeMode", json!("Off")),
308 ]}))
309 .unwrap()
310 }
311
312 #[gpui_kit::test]
313 fn the_people_panel_and_the_chat_panel_exclude_each_other(cx: &mut TestAppContext) {
314 let (app, _control) = app_with_meeting(cx, true);
315 let open = |app: &Entity<AppState>, cx: &mut TestAppContext| {
316 cx.update(|cx| {
317 let call = app.read(cx).call.as_ref().unwrap();
318 (call.people_open, call.chat_open)
319 })
320 };
321 cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_chat(cx)));
322 assert_eq!(open(&app, cx), (false, true));
323 cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_people(cx)));
324 assert_eq!(open(&app, cx), (true, false));
325 cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_chat(cx)));
326 assert_eq!(open(&app, cx), (false, true));
327 cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_people(cx)));
328 cx.update(|cx| app.update(cx, |state, cx| state.toggle_call_people(cx)));
329 assert_eq!(open(&app, cx), (false, false));
330 }
331
332 #[gpui_kit::test]
333 fn organizer_actions_become_commands_and_attendees_send_nothing(cx: &mut TestAppContext) {
334 let (attendee, mut attendee_control) = app_with_meeting(cx, false);
335 cx.update(|cx| {
336 attendee.update(cx, |state, cx| {
337 state.set_call_lock(true, cx);
338 state.set_call_role("8:orgid:a".into(), MeetingRole::Presenter, cx);
339 state.set_call_attendee_modality("8:orgid:a".into(), Modality::Mic, false, cx);
340 state.set_call_attendee_restrictions(true, false, cx);
341 state.open_meeting_options(cx);
342 })
343 });
344 assert_eq!(attendee_control.try_recv_command(), None);
345 let (organizer, mut control) = app_with_meeting(cx, true);
346 cx.update(|cx| {
347 organizer.update(cx, |state, cx| {
348 state.set_call_lock(true, cx);
349 state.set_call_role("8:orgid:a".into(), MeetingRole::Presenter, cx);
350 state.set_call_role("8:orgid:a".into(), MeetingRole::Organizer, cx);
351 state.set_call_attendee_modality("8:orgid:a".into(), Modality::Camera, false, cx);
352 state.set_call_attendee_restrictions(true, true, cx);
353 })
354 });
355 assert_eq!(control.try_recv_command(), Some(CallCommand::SetLock(true)));
356 assert_eq!(
357 control.try_recv_command(),
358 Some(CallCommand::SetRole {
359 mri: "8:orgid:a".into(),
360 role: MeetingRole::Presenter
361 })
362 );
363 assert_eq!(
364 control.try_recv_command(),
365 Some(CallCommand::SetAttendeeModality {
366 mri: "8:orgid:a".into(),
367 modality: Modality::Camera,
368 allowed: false
369 })
370 );
371 assert_eq!(
372 control.try_recv_command(),
373 Some(CallCommand::SetAttendeeRestrictions {
374 mic_off: true,
375 camera_off: true
376 })
377 );
378 assert_eq!(control.try_recv_command(), None);
379 }
380
381 #[gpui_kit::test]
382 fn the_capability_update_marks_the_meeting_as_locked(cx: &mut TestAppContext) {
383 let (app, _control) = app_with_meeting(cx, true);
384 cx.update(|cx| {
385 app.update(cx, |state, cx| {
386 state.apply_call_update(
387 7,
388 CallUpdate::Capability(MeetingCapability {
389 locked: true,
390 ..MeetingCapability::default()
391 }),
392 cx,
393 )
394 })
395 });
396 assert!(cx.update(|cx| app.read(cx).call.as_ref().unwrap().model.capability.locked));
397 }
398
399 #[gpui_kit::test]
400 fn the_options_dialog_loads_saves_only_changes_and_keeps_errors_inline(
401 cx: &mut TestAppContext,
402 ) {
403 let (app, mut control) = app_with_meeting(cx, true);
404 cx.update(|cx| app.update(cx, |state, cx| state.open_meeting_options(cx)));
405 assert_eq!(
406 control.try_recv_command(),
407 Some(CallCommand::LoadMeetingOptions)
408 );
409 cx.update(|cx| {
410 app.update(cx, |state, cx| {
411 state.apply_call_update(
412 7,
413 CallUpdate::MeetingOptions(Err("The options could not be read".into())),
414 cx,
415 )
416 })
417 });
418 let error = cx.update(|cx| {
419 app.read(cx)
420 .call
421 .as_ref()
422 .unwrap()
423 .options_dialog
424 .as_ref()
425 .unwrap()
426 .error
427 .clone()
428 });
429 assert_eq!(error.as_deref(), Some("The options could not be read"));
430 cx.update(|cx| {
431 app.update(cx, |state, cx| {
432 state.apply_call_update(7, CallUpdate::MeetingOptions(Ok(options())), cx);
433 state.save_meeting_options(cx);
434 state.edit_meeting_options(|draft| draft.meeting_chat = "InMeeting".into(), cx);
435 state.save_meeting_options(cx);
436 })
437 });
438 let Some(CallCommand::SaveMeetingOptions(writes)) = control.try_recv_command() else {
439 panic!("a save command");
440 };
441 assert_eq!(writes.len(), 1);
442 assert_eq!(writes[0].name, "AllowMeetingChat");
443 assert_eq!(control.try_recv_command(), None);
444 cx.update(|cx| {
445 app.update(cx, |state, cx| {
446 state.apply_call_update(
447 7,
448 CallUpdate::MeetingOptionsSaved(Err(
449 "the service refused AllowMeetingChat".into()
450 )),
451 cx,
452 )
453 })
454 });
455 assert!(cx.update(|cx| app.read(cx).call.as_ref().unwrap().options_dialog.is_some()));
456 cx.update(|cx| {
457 app.update(cx, |state, cx| {
458 state.apply_call_update(7, CallUpdate::MeetingOptionsSaved(Ok(options())), cx)
459 })
460 });
461 assert!(cx.update(|cx| app.read(cx).call.as_ref().unwrap().options_dialog.is_none()));
462 }
463
464 #[gpui_kit::test]
465 fn inviting_into_a_meeting_sends_the_person_and_shows_them_as_ringing(cx: &mut TestAppContext) {
466 let (app, mut control) = app_with_meeting(cx, true);
467 let candidate = TransferCandidate {
468 mri: "8:orgid:bo".into(),
469 name: "Bo".into(),
470 user_id: Some("bo".into()),
471 chat_id: "19:bo".into(),
472 };
473 cx.update(|cx| app.update(cx, |state, cx| state.invite_call_person(candidate, cx)));
474 let Some(CallCommand::Invite {
475 callees,
476 group_thread,
477 }) = control.try_recv_command()
478 else {
479 panic!("an invite command");
480 };
481 assert_eq!(callees.len(), 1);
482 assert_eq!(callees[0].mri, "8:orgid:bo");
483 assert_eq!(group_thread, None);
484 let ringing = cx.update(|cx| {
485 let tiles = &app.read(cx).call.as_ref().unwrap().model.tiles;
486 tiles
487 .iter()
488 .any(|tile| tile.mri == "8:orgid:bo" && tile.invited)
489 });
490 assert!(ringing);
491 }
492}