BlueGit

ict rusty-teams

Files

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

prejoin.rs

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