BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / record_rights.rs

record_rights.rs

8629da58 History
record_rights.rs 16.1 KiB Download Raw
1use calling::{MeetingRole, RecorderOption, RecordingKind};
2
3use super::model::{CallKind, CallModel};
4use super::recording_menu::RecordAction;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum RecordFeature {
8 Recording,
9 Transcription,
10}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum RecordBlock {
14 Role,
15 OrganizerOff,
16 OrganizerOnly,
17 Busy,
18 External,
19 CallPolicy,
20 MeetingPolicy,
21 Capability,
22}
23
24impl RecordBlock {
25 pub fn locked(self) -> bool {
26 self != RecordBlock::Busy
27 }
28
29 pub fn reason(self, feature: RecordFeature) -> &'static str {
30 match (self, feature) {
31 (RecordBlock::Role, RecordFeature::Recording) => {
32 "Only meeting organisers and presenters can control recording"
33 }
34 (RecordBlock::Role, RecordFeature::Transcription) => {
35 "Only meeting organisers and presenters can control transcription"
36 }
37 (RecordBlock::OrganizerOff, _) => {
38 "Unavailable because the meeting organiser has disabled recording for this meeting"
39 }
40 (RecordBlock::OrganizerOnly, _) => {
41 "Only meeting organiser and co-organiser can control recording for this meeting"
42 }
43 (RecordBlock::Busy, _) => "Starting...",
44 (RecordBlock::External, _) => {
45 "Unavailable to people in a different org from the organiser"
46 }
47 (RecordBlock::CallPolicy, RecordFeature::Recording) => {
48 "Unavailable for this call due to organisation policy. Reach out to your IT admin to get access."
49 }
50 (RecordBlock::MeetingPolicy, RecordFeature::Recording) => {
51 "Unavailable for this meeting due to organisation policy. Reach out to your IT admin to get access."
52 }
53 (
54 RecordBlock::CallPolicy | RecordBlock::MeetingPolicy,
55 RecordFeature::Transcription,
56 ) => "Unavailable due to org policy. Reach out to your IT admin to get access.",
57 (RecordBlock::Capability, _) => "Unavailable in this meeting",
58 }
59 }
60}
61
62impl CallModel {
63 pub fn record_hidden(&self) -> bool {
64 !self.is_live() || self.lobby || self.kind == CallKind::Test
65 }
66
67 pub fn record_block(&self) -> Option<RecordBlock> {
68 self.block(RecordFeature::Recording)
69 }
70
71 pub fn transcribe_block(&self) -> Option<RecordBlock> {
72 self.block(RecordFeature::Transcription)
73 }
74
75 pub fn can_record(&self) -> bool {
76 !self.record_hidden() && self.record_block().is_none()
77 }
78
79 pub fn action_allowed(&self, action: RecordAction) -> bool {
80 match action {
81 RecordAction::Start(RecordingKind::Video | RecordingKind::AudioOnly) => {
82 self.can_record()
83 }
84 _ => !self.record_hidden() && self.action_block(action).is_none(),
85 }
86 }
87
88 pub fn stop_block(&self) -> Option<RecordBlock> {
89 self.record_block()
90 .filter(|block| matches!(block, RecordBlock::Role | RecordBlock::OrganizerOnly))
91 }
92
93 pub fn action_block(&self, action: RecordAction) -> Option<(RecordBlock, RecordFeature)> {
94 match action {
95 RecordAction::Start(RecordingKind::TranscriptOnly) => self
96 .transcribe_block()
97 .map(|block| (block, RecordFeature::Transcription)),
98 RecordAction::Start(_) => self
99 .record_block()
100 .map(|block| (block, RecordFeature::Recording)),
101 RecordAction::Stop(_) => self
102 .stop_block()
103 .map(|block| (block, RecordFeature::Recording)),
104 }
105 }
106
107 pub fn record_menu_block(&self) -> Option<(RecordBlock, RecordFeature)> {
108 if self.recording || self.transcribing {
109 return None;
110 }
111 self.record_block()
112 .zip(self.transcribe_block())
113 .map(|(block, _)| (block, RecordFeature::Recording))
114 }
115
116 fn block(&self, feature: RecordFeature) -> Option<RecordBlock> {
117 let capability = self.capability;
118 let organizer = self.own_role == Some(MeetingRole::Organizer) || self.can_manage();
119 if self.own_role == Some(MeetingRole::Attendee) {
120 return Some(RecordBlock::Role);
121 }
122 match capability.recorder_option {
123 RecorderOption::Nobody => return Some(RecordBlock::OrganizerOff),
124 RecorderOption::OrganizersOnly if !organizer => {
125 return Some(RecordBlock::OrganizerOnly);
126 }
127 _ => {}
128 }
129 if self.record_pending {
130 return Some(RecordBlock::Busy);
131 }
132 let foreign = matches!(
133 (&self.meeting_tenant, &self.own_tenant),
134 (Some(meeting), Some(own)) if !meeting.eq_ignore_ascii_case(own)
135 );
136 if self.kind == CallKind::Meeting && foreign {
137 return Some(RecordBlock::External);
138 }
139 let policy = self.recording_policy;
140 let (call_allowed, meeting_allowed, capability_allowed) = match feature {
141 RecordFeature::Recording => (
142 policy.call_recording,
143 policy.meeting_recording,
144 capability.allow_cloud_recording,
145 ),
146 RecordFeature::Transcription => (
147 policy.call_transcription,
148 policy.meeting_transcription,
149 capability.allow_transcription,
150 ),
151 };
152 if self.kind == CallKind::Direct && !call_allowed {
153 return Some(RecordBlock::CallPolicy);
154 }
155 if self.kind != CallKind::Direct && !meeting_allowed {
156 return Some(RecordBlock::MeetingPolicy);
157 }
158 (capability_allowed == Some(false)).then_some(RecordBlock::Capability)
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use std::time::Instant;
165
166 use calling::{CallState, CallUpdate, MeetingCapability};
167 use teams_core::RecordingPolicy;
168
169 use super::*;
170
171 fn live(mut model: CallModel) -> CallModel {
172 model.apply(CallUpdate::State(CallState::Connected {
173 since: Instant::now(),
174 }));
175 model
176 }
177
178 fn meeting() -> CallModel {
179 live(CallModel::meeting("Standup", false))
180 }
181
182 fn direct() -> CallModel {
183 live(CallModel::people(
184 "Bea",
185 &[("8:orgid:bea".into(), "Bea".into())],
186 ))
187 }
188
189 fn group() -> CallModel {
190 live(CallModel::people(
191 "Team",
192 &[
193 ("8:orgid:bea".into(), "Bea".into()),
194 ("8:orgid:cy".into(), "Cy".into()),
195 ],
196 ))
197 }
198
199 fn capability(update: impl FnOnce(&mut MeetingCapability)) -> MeetingCapability {
200 let mut capability = MeetingCapability::default();
201 update(&mut capability);
202 capability
203 }
204
205 #[test]
206 fn everything_is_allowed_when_nothing_is_known() {
207 for model in [meeting(), direct(), group()] {
208 assert_eq!(model.record_block(), None);
209 assert_eq!(model.transcribe_block(), None);
210 assert!(model.can_record() && !model.record_hidden());
211 }
212 }
213
214 #[test]
215 fn hidden_until_live_and_while_in_the_lobby_or_the_echo_call() {
216 assert!(CallModel::meeting("Standup", true).record_hidden());
217 let mut lobby = meeting();
218 lobby.lobby = true;
219 assert!(lobby.record_hidden() && !lobby.can_record());
220 assert!(live(CallModel::test()).record_hidden());
221 assert!(!meeting().record_hidden());
222 }
223
224 #[test]
225 fn an_attendee_is_blocked_and_a_presenter_is_not() {
226 let mut model = meeting();
227 model.own_role = Some(MeetingRole::Attendee);
228 assert_eq!(model.record_block(), Some(RecordBlock::Role));
229 assert_eq!(model.transcribe_block(), Some(RecordBlock::Role));
230 model.own_role = Some(MeetingRole::Presenter);
231 assert_eq!(model.record_block(), None);
232 }
233
234 #[test]
235 fn the_recorder_option_off_blocks_everyone_even_the_organizer() {
236 let mut model = meeting();
237 model.own_role = Some(MeetingRole::Organizer);
238 model.capability =
239 capability(|capability| capability.recorder_option = RecorderOption::Nobody);
240 assert_eq!(model.record_block(), Some(RecordBlock::OrganizerOff));
241 assert_eq!(model.transcribe_block(), Some(RecordBlock::OrganizerOff));
242 }
243
244 #[test]
245 fn the_organizer_only_option_blocks_presenters() {
246 let mut model = meeting();
247 model.capability =
248 capability(|capability| capability.recorder_option = RecorderOption::OrganizersOnly);
249 model.own_role = Some(MeetingRole::Presenter);
250 assert_eq!(model.record_block(), Some(RecordBlock::OrganizerOnly));
251 model.own_role = Some(MeetingRole::Organizer);
252 assert_eq!(model.record_block(), None);
253 model.own_role = None;
254 model.organizer = true;
255 assert_eq!(model.record_block(), None);
256 }
257
258 #[test]
259 fn a_start_in_flight_blocks_with_a_reason_without_a_lock() {
260 let mut model = meeting();
261 model.apply(CallUpdate::RecordingPending(true));
262 let block = model.record_block().unwrap();
263 assert_eq!(block, RecordBlock::Busy);
264 assert!(!block.locked());
265 assert_eq!(model.transcribe_block(), Some(RecordBlock::Busy));
266 model.apply(CallUpdate::RecordingPending(false));
267 assert_eq!(model.record_block(), None);
268 }
269
270 #[test]
271 fn a_meeting_of_another_tenant_is_blocked_but_a_call_is_not() {
272 let mut model = meeting();
273 model.apply(CallUpdate::MeetingTenant("tenant-a".into()));
274 assert_eq!(model.record_block(), None);
275 model.own_tenant = Some("tenant-b".into());
276 assert_eq!(model.record_block(), Some(RecordBlock::External));
277 assert_eq!(model.transcribe_block(), Some(RecordBlock::External));
278 model.own_tenant = Some("TENANT-A".into());
279 assert_eq!(model.record_block(), None);
280 let mut call = direct();
281 call.meeting_tenant = Some("tenant-a".into());
282 call.own_tenant = Some("tenant-b".into());
283 assert_eq!(call.record_block(), None);
284 }
285
286 #[test]
287 fn direct_calls_use_the_calling_policy_and_the_rest_the_meeting_policy() {
288 let no_calls = RecordingPolicy {
289 call_recording: false,
290 call_transcription: false,
291 ..RecordingPolicy::default()
292 };
293 let no_meetings = RecordingPolicy {
294 meeting_recording: false,
295 meeting_transcription: false,
296 ..RecordingPolicy::default()
297 };
298 for (mut model, policy, blocked) in [
299 (direct(), no_calls, Some(RecordBlock::CallPolicy)),
300 (direct(), no_meetings, None),
301 (group(), no_calls, None),
302 (group(), no_meetings, Some(RecordBlock::MeetingPolicy)),
303 (meeting(), no_calls, None),
304 (meeting(), no_meetings, Some(RecordBlock::MeetingPolicy)),
305 ] {
306 model.recording_policy = policy;
307 assert_eq!(model.record_block(), blocked);
308 assert_eq!(model.transcribe_block(), blocked);
309 }
310 }
311
312 #[test]
313 fn recording_and_transcription_follow_their_own_policy_flags() {
314 let mut model = meeting();
315 model.recording_policy = RecordingPolicy {
316 meeting_transcription: false,
317 ..RecordingPolicy::default()
318 };
319 assert_eq!(model.record_block(), None);
320 assert_eq!(model.transcribe_block(), Some(RecordBlock::MeetingPolicy));
321 assert!(model.can_record());
322 }
323
324 #[test]
325 fn the_meeting_capability_blocks_only_when_it_says_false() {
326 let mut model = meeting();
327 model.capability = capability(|capability| capability.allow_cloud_recording = Some(true));
328 assert_eq!(model.record_block(), None);
329 model.capability = capability(|capability| capability.allow_cloud_recording = Some(false));
330 assert_eq!(model.record_block(), Some(RecordBlock::Capability));
331 assert_eq!(model.transcribe_block(), None);
332 model.capability = capability(|capability| capability.allow_transcription = Some(false));
333 assert_eq!(model.record_block(), None);
334 assert_eq!(model.transcribe_block(), Some(RecordBlock::Capability));
335 }
336
337 #[test]
338 fn the_checks_run_in_the_order_of_teams() {
339 let mut model = meeting();
340 model.own_role = Some(MeetingRole::Attendee);
341 model.capability = capability(|capability| {
342 capability.recorder_option = RecorderOption::Nobody;
343 capability.allow_cloud_recording = Some(false);
344 });
345 model.recording_policy.meeting_recording = false;
346 model.record_pending = true;
347 model.meeting_tenant = Some("a".into());
348 model.own_tenant = Some("b".into());
349 let expected = [
350 RecordBlock::Role,
351 RecordBlock::OrganizerOff,
352 RecordBlock::Busy,
353 RecordBlock::External,
354 RecordBlock::MeetingPolicy,
355 RecordBlock::Capability,
356 ];
357 assert_eq!(model.record_block(), Some(expected[0]));
358 model.own_role = None;
359 assert_eq!(model.record_block(), Some(expected[1]));
360 model.capability.recorder_option = RecorderOption::OrganizersAndPresenters;
361 assert_eq!(model.record_block(), Some(expected[2]));
362 model.record_pending = false;
363 assert_eq!(model.record_block(), Some(expected[3]));
364 model.own_tenant = None;
365 assert_eq!(model.record_block(), Some(expected[4]));
366 model.recording_policy.meeting_recording = true;
367 assert_eq!(model.record_block(), Some(expected[5]));
368 }
369
370 #[test]
371 fn the_reasons_use_the_teams_wording_per_feature() {
372 assert_eq!(
373 RecordBlock::Role.reason(RecordFeature::Transcription),
374 "Only meeting organisers and presenters can control transcription"
375 );
376 assert_eq!(
377 RecordBlock::CallPolicy.reason(RecordFeature::Recording),
378 "Unavailable for this call due to organisation policy. Reach out to your IT admin to get access."
379 );
380 assert_eq!(
381 RecordBlock::MeetingPolicy.reason(RecordFeature::Transcription),
382 "Unavailable due to org policy. Reach out to your IT admin to get access."
383 );
384 assert_eq!(
385 RecordBlock::Capability.reason(RecordFeature::Recording),
386 "Unavailable in this meeting"
387 );
388 }
389
390 #[test]
391 fn the_menu_locks_only_when_both_features_are_blocked_and_nothing_runs() {
392 let mut model = meeting();
393 model.own_role = Some(MeetingRole::Attendee);
394 assert_eq!(
395 model.record_menu_block(),
396 Some((RecordBlock::Role, RecordFeature::Recording))
397 );
398 model.recording = true;
399 assert_eq!(model.record_menu_block(), None);
400 model.recording = false;
401 model.own_role = None;
402 model.capability = capability(|capability| capability.allow_cloud_recording = Some(false));
403 assert_eq!(model.record_menu_block(), None);
404 }
405
406 #[test]
407 fn stopping_is_only_blocked_by_the_role_rules() {
408 let mut model = meeting();
409 model.recording_policy.meeting_recording = false;
410 assert_eq!(model.stop_block(), None);
411 model.own_role = Some(MeetingRole::Attendee);
412 assert_eq!(model.stop_block(), Some(RecordBlock::Role));
413 }
414
415 #[test]
416 fn each_action_asks_its_own_feature() {
417 let mut model = meeting();
418 model.recording_policy.meeting_transcription = false;
419 let start = |kind| RecordAction::Start(kind);
420 assert_eq!(model.action_block(start(RecordingKind::Video)), None);
421 assert_eq!(
422 model.action_block(start(RecordingKind::TranscriptOnly)),
423 Some((RecordBlock::MeetingPolicy, RecordFeature::Transcription))
424 );
425 model.recording_policy = RecordingPolicy {
426 meeting_recording: false,
427 ..RecordingPolicy::default()
428 };
429 assert_eq!(
430 model.action_block(start(RecordingKind::AudioOnly)),
431 Some((RecordBlock::MeetingPolicy, RecordFeature::Recording))
432 );
433 assert_eq!(
434 model.action_block(RecordAction::Stop(calling::StopScope::Both)),
435 None
436 );
437 }
438}