BlueGit

ict rusty-teams

Files

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

prejoin.rs

prejoin.rs 5.8 KiB Download Raw
1use calling::{CallCommand, CallSpec, MeetingTarget, MuteCommand};
2use gpui_kit::Context;
3
4use super::demo::{DemoCall, demo_guests};
5use super::model::CallModel;
6use crate::app_state::AppState;
7
8const OPTIONS_META_KEY: &str = "prejoin_options";
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum AudioChoice {
12 #[default]
13 Computer,
14 NoAudio,
15}
16
17impl AudioChoice {
18 pub fn label(self) -> &'static str {
19 match self {
20 AudioChoice::Computer => "Computer audio",
21 AudioChoice::NoAudio => "Don't use audio",
22 }
23 }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct JoinOptions {
28 pub camera_on: bool,
29 pub mic_on: bool,
30 pub audio: AudioChoice,
31}
32
33impl Default for JoinOptions {
34 fn default() -> Self {
35 JoinOptions {
36 camera_on: false,
37 mic_on: true,
38 audio: AudioChoice::Computer,
39 }
40 }
41}
42
43impl JoinOptions {
44 pub fn to_meta(&self) -> String {
45 format!(
46 "camera={}\nmic={}",
47 u8::from(self.camera_on),
48 u8::from(self.mic_on)
49 )
50 }
51
52 pub fn from_meta(text: &str) -> Self {
53 let mut options = JoinOptions::default();
54 for (key, value) in text.lines().filter_map(|line| line.split_once('=')) {
55 match key {
56 "camera" => options.camera_on = value == "1",
57 "mic" => options.mic_on = value == "1",
58 _ => {}
59 }
60 }
61 options
62 }
63
64 pub fn commands_after_join(&self) -> Vec<CallCommand> {
65 if self.audio == AudioChoice::NoAudio {
66 return vec![CallCommand::DisableAudio];
67 }
68 if self.mic_on {
69 vec![CallCommand::Mute(MuteCommand::Unmute)]
70 } else {
71 Vec::new()
72 }
73 }
74}
75
76pub fn load_join_options(store: &store::Store) -> JoinOptions {
77 store
78 .meta(OPTIONS_META_KEY)
79 .ok()
80 .flatten()
81 .map(|text| JoinOptions::from_meta(&text))
82 .unwrap_or_default()
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub struct DemoMeeting {
87 pub guests: usize,
88 pub lobby: bool,
89 pub organizer: bool,
90}
91
92#[derive(Debug, Clone)]
93pub struct MeetingJoin {
94 pub target: MeetingTarget,
95 pub title: String,
96 pub organizer: bool,
97 pub conversation_id: Option<String>,
98 pub demo: DemoMeeting,
99}
100
101#[derive(Debug, Clone)]
102pub struct PendingJoin {
103 pub id: u64,
104 pub join: MeetingJoin,
105}
106
107impl AppState {
108 pub(super) fn stage_meeting_join(&mut self, join: MeetingJoin, cx: &mut Context<Self>) {
109 if self.call.is_some() {
110 self.show_call(true, cx);
111 return;
112 }
113 self.prejoin_count += 1;
114 self.call_end = None;
115 self.prejoin = Some(PendingJoin {
116 id: self.prejoin_count,
117 join,
118 });
119 self.new_chat = false;
120 cx.notify();
121 }
122
123 pub fn confirm_prejoin(&mut self, options: JoinOptions, cx: &mut Context<Self>) {
124 let Some(pending) = self.prejoin.take() else {
125 return;
126 };
127 let _ = self.store.set_meta(OPTIONS_META_KEY, &options.to_meta());
128 self.join_options = options.clone();
129 self.start_meeting(pending.join, Some(&options), cx);
130 }
131
132 pub fn cancel_prejoin(&mut self, cx: &mut Context<Self>) {
133 if self.prejoin.take().is_some() {
134 cx.notify();
135 }
136 }
137
138 pub(super) fn start_meeting(
139 &mut self,
140 join: MeetingJoin,
141 options: Option<&JoinOptions>,
142 cx: &mut Context<Self>,
143 ) {
144 if self.call.is_some() {
145 self.show_call(true, cx);
146 return;
147 }
148 let demo = DemoCall::Meeting {
149 guests: demo_guests(join.demo.guests),
150 lobby: join.demo.lobby,
151 organizer: join.demo.organizer,
152 };
153 let with_video = options.is_some_and(|options| options.camera_on);
154 let Some(handle) =
155 self.place_call(CallSpec::Meeting(join.target.clone()), demo, with_video, cx)
156 else {
157 return;
158 };
159 let model = CallModel::meeting(&join.title, join.organizer);
160 self.begin_call(handle, model, join.conversation_id, cx);
161 if let Some(call) = self.call.as_mut() {
162 call.meeting_target = Some(join.target);
163 call.model.audio_off =
164 options.is_some_and(|options| options.audio == AudioChoice::NoAudio);
165 }
166 for command in options
167 .map(JoinOptions::commands_after_join)
168 .unwrap_or_default()
169 {
170 self.send_call_command(command);
171 }
172 }
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178
179 #[test]
180 fn options_survive_a_round_trip_through_the_store_text() {
181 let options = JoinOptions {
182 camera_on: true,
183 mic_on: false,
184 audio: AudioChoice::Computer,
185 };
186 assert_eq!(JoinOptions::from_meta(&options.to_meta()), options);
187 }
188
189 #[test]
190 fn empty_or_garbage_text_gives_the_defaults() {
191 assert_eq!(JoinOptions::from_meta(""), JoinOptions::default());
192 assert_eq!(JoinOptions::from_meta("junk\nmic"), JoinOptions::default());
193 }
194
195 #[test]
196 fn computer_audio_unmutes_only_when_the_mic_is_on() {
197 let default = JoinOptions::default();
198 assert_eq!(
199 default.commands_after_join(),
200 vec![CallCommand::Mute(MuteCommand::Unmute)]
201 );
202 let muted = JoinOptions {
203 mic_on: false,
204 ..default
205 };
206 assert_eq!(muted.commands_after_join(), Vec::<CallCommand>::new());
207 }
208
209 #[test]
210 fn without_audio_only_the_audio_is_switched_off() {
211 let options = JoinOptions {
212 audio: AudioChoice::NoAudio,
213 ..JoinOptions::default()
214 };
215 assert_eq!(
216 options.commands_after_join(),
217 vec![CallCommand::DisableAudio]
218 );
219 }
220}