| 1 | use calling::{CallCommand, MuteCommand}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::join::JoinMode; |
| 5 | use crate::app_state::AppState; |
| 6 | |
| 7 | pub const JOIN_FLAG: &str = "--join"; |
| 8 | pub const HANG_UP_FLAG: &str = "--hang-up"; |
| 9 | pub const ADMIT_ALL_FLAG: &str = "--admit-all"; |
| 10 | pub const UNMUTE_FLAG: &str = "--unmute"; |
| 11 | const JOIN_COMMAND: &str = "join "; |
| 12 | const HANG_UP_COMMAND: &str = "hang-up"; |
| 13 | const ADMIT_ALL_COMMAND: &str = "admit-all"; |
| 14 | const UNMUTE_COMMAND: &str = "unmute"; |
| 15 | |
| 16 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 17 | pub enum RemoteCommand { |
| 18 | Join(String), |
| 19 | HangUp, |
| 20 | AdmitAll, |
| 21 | Unmute, |
| 22 | } |
| 23 | |
| 24 | impl RemoteCommand { |
| 25 | pub fn encode(&self) -> String { |
| 26 | match self { |
| 27 | RemoteCommand::Join(link) => format!("{JOIN_COMMAND}{link}"), |
| 28 | RemoteCommand::HangUp => HANG_UP_COMMAND.to_owned(), |
| 29 | RemoteCommand::AdmitAll => ADMIT_ALL_COMMAND.to_owned(), |
| 30 | RemoteCommand::Unmute => UNMUTE_COMMAND.to_owned(), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn decode(text: &str) -> Option<RemoteCommand> { |
| 35 | match text { |
| 36 | HANG_UP_COMMAND => return Some(RemoteCommand::HangUp), |
| 37 | ADMIT_ALL_COMMAND => return Some(RemoteCommand::AdmitAll), |
| 38 | UNMUTE_COMMAND => return Some(RemoteCommand::Unmute), |
| 39 | _ => {} |
| 40 | } |
| 41 | let link = text.strip_prefix(JOIN_COMMAND)?.trim(); |
| 42 | (!link.is_empty()).then(|| RemoteCommand::Join(link.to_owned())) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl AppState { |
| 47 | /// Commands a second `teams` process forwards, used by the call end-to-end test. |
| 48 | pub fn run_remote_command(&mut self, text: &str, cx: &mut Context<Self>) { |
| 49 | match RemoteCommand::decode(text) { |
| 50 | Some(RemoteCommand::Join(link)) => { |
| 51 | self.own_engine_next_call = true; |
| 52 | self.join_link(&link, JoinMode::Direct, cx); |
| 53 | self.own_engine_next_call = false; |
| 54 | } |
| 55 | Some(RemoteCommand::HangUp) => self.leave_call(cx), |
| 56 | Some(RemoteCommand::AdmitAll) => self.admit_all_call_guests(cx), |
| 57 | Some(RemoteCommand::Unmute) => self.send_call_command(CallCommand::Mute(MuteCommand::Unmute)), |
| 58 | None => {} |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[cfg(test)] |
| 64 | mod tests { |
| 65 | use super::RemoteCommand; |
| 66 | |
| 67 | #[test] |
| 68 | fn commands_survive_the_trip_through_the_pipe() { |
| 69 | for command in [ |
| 70 | RemoteCommand::Join("https://teams.microsoft.com/l/meetup-join/x".to_owned()), |
| 71 | RemoteCommand::HangUp, |
| 72 | RemoteCommand::AdmitAll, |
| 73 | RemoteCommand::Unmute, |
| 74 | ] { |
| 75 | assert_eq!(RemoteCommand::decode(&command.encode()), Some(command)); |
| 76 | } |
| 77 | assert_eq!(RemoteCommand::decode("join "), None); |
| 78 | assert_eq!(RemoteCommand::decode("reboot"), None); |
| 79 | } |
| 80 | } |