BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / call / bots.rs

bots.rs

bots.rs 9.5 KiB Download Raw
1use super::*;
2
3pub(super) fn is_own_meeting(own: Option<&MeetingTarget>, target: &MeetingTarget) -> bool {
4 own.is_some_and(|own| own.thread_id == target.thread_id)
5}
6
7pub(super) async fn save_meeting_options(
8 session: &session::Session,
9 target: &MeetingTarget,
10 writes: &[OptionWrite],
11) -> std::result::Result<meeting_options::MeetingOptions, String> {
12 for change in writes {
13 meeting_options::write(session, target, change)
14 .await
15 .map_err(|error| error.to_string())?;
16 }
17 meeting_options::fetch(session, target)
18 .await
19 .map_err(|error| error.to_string())
20}
21
22pub(super) fn target_of<'a>(roster: &'a Roster, mri: &'a str) -> Target<'a> {
23 let display_name = roster
24 .members()
25 .iter()
26 .find(|member| member.mri == mri)
27 .map_or("", |member| member.display_name.as_str());
28 Target { mri, display_name }
29}
30
31pub(super) const RECORDING_STOP_RETRIES: usize = 6;
32
33pub(super) const RECORDING_STOP_RETRY_WAIT: Duration = Duration::from_secs(5);
34
35/// The recorder answers 409 to a stop sent while its start is still settling.
36pub(super) fn recorder_busy(error: &Error) -> bool {
37 matches!(error, Error::Signaling(reason) if reason.contains("HTTP 409"))
38}
39
40pub(super) fn report_organizer_result(
41 result: Result<u16>,
42 label: &str,
43 notice: &'static str,
44 control: &CallControl,
45 timeline: &Timeline,
46) {
47 if let Err(error) = &result {
48 timeline.record(format!("{label} failed"), error.to_string());
49 control.send(CallUpdate::Notice(notice.to_owned()));
50 }
51 control.send(CallUpdate::Organizer {
52 action: label.to_owned(),
53 outcome: result.map_err(|error| error.to_string()),
54 });
55}
56
57pub(super) fn named_caption(mut entry: CaptionEntry, roster: &Roster) -> CaptionEntry {
58 if entry.display_name.is_empty() {
59 entry.display_name = roster
60 .name_of(&entry.user_id)
61 .unwrap_or_default()
62 .to_owned();
63 }
64 entry
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub(super) enum BotJob {
69 Captions,
70 Recording,
71}
72
73#[allow(clippy::too_many_arguments)]
74pub(super) async fn run_bot_steps(
75 job: BotJob,
76 flow: &mut CaptionFlow,
77 roster: &Roster,
78 remote: &RemoteCall,
79 links: &CallbackLinks,
80 meeting_chat: Option<&str>,
81 plan: &RecordingPlan,
82 bot_pending: &mut bool,
83 control: &CallControl,
84 timeline: &Timeline,
85) {
86 let is_active: fn(&crate::roster::CaptionBot) -> bool = match job {
87 BotJob::Captions => |bot| bot.active,
88 BotJob::Recording => |bot| bot.recording,
89 };
90 let fail = |flow: &mut CaptionFlow, reason: &str| {
91 flow.failed();
92 control.send(match job {
93 BotJob::Captions => CallUpdate::Captions(CaptionState::Failed(reason.to_owned())),
94 BotJob::Recording => CallUpdate::Notice(reason.to_owned()),
95 });
96 if job == BotJob::Recording {
97 control.send(CallUpdate::RecordingPending(false));
98 }
99 };
100 let (join_failure, start_failure) = match job {
101 BotJob::Captions => (CAPTIONS_JOIN_FAILED_NOTICE, CAPTIONS_START_FAILED_NOTICE),
102 BotJob::Recording => (RECORDING_JOIN_FAILED_NOTICE, RECORDING_START_FAILED_NOTICE),
103 };
104 loop {
105 match flow.next_for(roster.caption_bot(), is_active) {
106 CaptionStep::Wait => return,
107 CaptionStep::AddBot => {
108 if *bot_pending {
109 continue;
110 }
111 let Some(thread) = meeting_chat else {
112 return fail(
113 flow,
114 "This call has no chat for the captions and the recording",
115 );
116 };
117 *bot_pending = true;
118 let added = remote
119 .signaling
120 .add_caption_bot(
121 &remote.conversation,
122 &remote.participant,
123 thread,
124 links,
125 remote.threadless,
126 )
127 .await;
128 if job == BotJob::Recording {
129 report_organizer_result(
130 added
131 .as_ref()
132 .map(|status| *status)
133 .map_err(|error| Error::Signaling(error.to_string())),
134 "recording add bot",
135 join_failure,
136 control,
137 timeline,
138 );
139 }
140 if let Err(error) = added {
141 timeline.record("bot invitation failed", error.to_string());
142 *bot_pending = false;
143 return fail(flow, join_failure);
144 }
145 }
146 CaptionStep::Start => {
147 let Some(url) = roster.caption_bot().and_then(|bot| bot.command_url.clone()) else {
148 return;
149 };
150 match job {
151 BotJob::Captions => {
152 if let Err(error) = remote
153 .signaling
154 .caption_command(&url, BotAction::Start, &remote.participant)
155 .await
156 {
157 timeline.record("caption start failed", error.to_string());
158 return fail(flow, start_failure);
159 }
160 if let Err(error) = remote
161 .signaling
162 .set_caption_preference(&remote.conversation, &remote.participant, true)
163 .await
164 {
165 timeline.record("caption preference failed", error.to_string());
166 }
167 }
168 BotJob::Recording => {
169 let started = remote
170 .signaling
171 .recording_command(&url, plan.start(), &remote.participant, &plan.title)
172 .await;
173 let failed = started.is_err();
174 report_organizer_result(
175 started,
176 "recording start",
177 start_failure,
178 control,
179 timeline,
180 );
181 if failed {
182 return fail(flow, start_failure);
183 }
184 }
185 }
186 }
187 CaptionStep::Stop => {
188 let Some(url) = roster.caption_bot().and_then(|bot| bot.command_url.clone()) else {
189 continue;
190 };
191 match job {
192 BotJob::Captions => {
193 if let Err(error) = remote
194 .signaling
195 .caption_command(&url, BotAction::Stop, &remote.participant)
196 .await
197 {
198 timeline.record("caption stop failed", error.to_string());
199 }
200 if let Err(error) = remote
201 .signaling
202 .set_caption_preference(
203 &remote.conversation,
204 &remote.participant,
205 false,
206 )
207 .await
208 {
209 timeline.record("caption preference failed", error.to_string());
210 }
211 }
212 BotJob::Recording => {
213 let remote = remote.clone();
214 let updates = control.updates();
215 let (command, title) = (plan.stop(), plan.title.clone());
216 tokio::spawn(async move {
217 let mut stopped = remote
218 .signaling
219 .recording_command(&url, command, &remote.participant, &title)
220 .await;
221 for _ in 0..RECORDING_STOP_RETRIES {
222 if !stopped.as_ref().is_err_and(recorder_busy) {
223 break;
224 }
225 tokio::time::sleep(RECORDING_STOP_RETRY_WAIT).await;
226 stopped = remote
227 .signaling
228 .recording_command(&url, command, &remote.participant, &title)
229 .await;
230 }
231 if stopped.is_err() {
232 let _ = updates
233 .send(CallUpdate::Notice(RECORDING_STOP_FAILED_NOTICE.into()));
234 }
235 let _ = updates.send(CallUpdate::Organizer {
236 action: "recording stop".into(),
237 outcome: stopped.map_err(|error| error.to_string()),
238 });
239 });
240 }
241 }
242 }
243 CaptionStep::ReportOn => {
244 if job == BotJob::Captions {
245 control.send(CallUpdate::Captions(CaptionState::On));
246 }
247 }
248 }
249 }
250}