BlueGit

ict rusty-teams

Files

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

remote_command.rs

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