use super::{ConsentPrompt, DeclineMode};
use crate::id::Id;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConsentAsk {
Join,
InCall,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConsentChoice {
pub label: &'static str,
pub granted: Option<bool>,
}
const TITLE_ROOM: &str = "This call is being recorded";
const AUTO_START: &str = "A recording starts when someone joins this channel.";
const REQUIRED: &str = "This room records every call with more than one person.";
const AGENT_READERS: &str = "Agents in this channel can read the transcript.";
const NOT_E2EE: &str = "Transcripts are not end-to-end encrypted.";
const ADMIN_DOWNLOAD: &str = "Authorised admins can download the audio.";
const NOBODY_YET: &str = "Nobody is recorded yet.";
const DECLINE_JOINS: &str = "If you don't allow it, you join without being recorded.";
const DECLINE_NOT_JOIN: &str = "In this channel you can't join without being recorded.";
const DECLINE_STAYS: &str = "If you don't allow it, you stay in the call without being recorded.";
const DECLINE_LEAVES: &str = "In this channel, not allowing it means leaving the call.";
const ALLOW_JOIN: &str = "Join and allow recording";
const JOIN_UNRECORDED: &str = "Join without being recorded";
const DONT_JOIN: &str = "Don't join";
const CANCEL: &str = "Cancel";
const ALLOW: &str = "Allow";
const DONT_RECORD_ME: &str = "Don't record me";
const LEAVE: &str = "Leave";
fn readers(ids: &[String]) -> String {
let xs: Vec<&str> = ids
.iter()
.map(|x| match x.as_str() {
"channel_members" => "the channel's members",
"search" => "search",
"agents" => "agents",
other => other,
})
.collect();
match xs.as_slice() {
[] => "the channel's members".to_owned(),
[one] => (*one).to_owned(),
[head @ .., last] => format!("{} and {last}", head.join(", ")),
}
}
fn and_list(xs: &[String]) -> String {
match xs {
[] => String::new(),
[one] => one.clone(),
[head @ .., last] => format!("{} and {last}", head.join(", ")),
}
}
impl ConsentPrompt {
pub fn title_en(&self, ask: ConsentAsk) -> String {
match (ask, self.room.name.as_deref()) {
(ConsentAsk::InCall, _) | (ConsentAsk::Join, None) => TITLE_ROOM.to_owned(),
(ConsentAsk::Join, Some(room)) if self.started_by.is_some() => {
format!("{room} is being recorded")
}
(ConsentAsk::Join, Some(room)) => format!("Calls in {room} are recorded"),
}
}
pub fn feature_en(&self) -> String {
format!("{}, recorded by {}", self.feature_label, self.recorder_name)
}
pub fn lines_en(&self, ask: ConsentAsk, member_name: &dyn Fn(Id) -> String) -> Vec<String> {
let mut lines = Vec::new();
match &self.started_by {
Some(m) => {
let name = if m.display_name.is_empty() {
member_name(m.member_id)
} else {
m.display_name.clone()
};
lines.push(format!("{name} started recording this call."));
}
None if self.required => {}
None => lines.push(AUTO_START.to_owned()),
}
if self.required {
lines.push(REQUIRED.to_owned());
}
lines.push(format!(
"{} joins the call as a participant and records the voice of each person who agrees. \
Nobody is recorded without saying yes.",
self.recorder_name
));
let who = readers(&self.transcript.readable_by);
lines.push(if self.transcript.posted_to_channel {
format!(
"Each person's words are transcribed and posted into this channel as ordinary \
messages that {who} can read."
)
} else {
format!("Each person's words are transcribed; {who} can read the transcript.")
});
if !self.app_readers.is_empty() {
lines.push(format!(
"{} can read the transcript.",
and_list(&self.app_readers)
));
}
if self.agent_readers {
lines.push(AGENT_READERS.to_owned());
}
if !self.transcript.e2ee {
lines.push(NOT_E2EE.to_owned());
}
lines.push(match self.audio_keep_days {
0 => "Audio is deleted once the transcript is final.".to_owned(),
1 => "Audio is kept for 1 day.".to_owned(),
n => format!("Audio is kept for {n} days."),
});
if self.admin_download {
lines.push(ADMIN_DOWNLOAD.to_owned());
}
lines.push(if self.recorded_now.is_empty() {
NOBODY_YET.to_owned()
} else {
let names: Vec<String> = self.recorded_now.iter().map(|m| member_name(*m)).collect();
format!("Recorded now: {}.", names.join(", "))
});
let not_join = self.decline_mode == DeclineMode::NotJoin;
lines.push(
match (ask, not_join) {
(ConsentAsk::Join, false) => DECLINE_JOINS,
(ConsentAsk::Join, true) => DECLINE_NOT_JOIN,
(ConsentAsk::InCall, false) => DECLINE_STAYS,
(ConsentAsk::InCall, true) => DECLINE_LEAVES,
}
.to_owned(),
);
lines
}
pub fn text_en(&self, ask: ConsentAsk, member_name: &dyn Fn(Id) -> String) -> String {
let mut out = self.feature_en();
for l in self.lines_en(ask, member_name) {
out.push('\n');
out.push_str(&l);
}
out
}
pub fn choices_en(&self, ask: ConsentAsk) -> Vec<ConsentChoice> {
let c = |label, granted| ConsentChoice { label, granted };
let not_join = self.decline_mode == DeclineMode::NotJoin;
match (ask, not_join) {
(ConsentAsk::Join, false) => vec![
c(ALLOW_JOIN, Some(true)),
c(JOIN_UNRECORDED, Some(false)),
c(CANCEL, None),
],
(ConsentAsk::Join, true) => vec![c(ALLOW_JOIN, Some(true)), c(DONT_JOIN, None)],
(ConsentAsk::InCall, false) => {
vec![c(ALLOW, Some(true)), c(DONT_RECORD_ME, Some(false))]
}
(ConsentAsk::InCall, true) => vec![c(ALLOW, Some(true)), c(LEAVE, Some(false))],
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::super::{PromptMember, PromptRoom, PromptRoomKind, PromptTranscript, RecordingRef};
use super::*;
const BOB: u64 = 200;
fn kit_prompt() -> ConsentPrompt {
ConsentPrompt {
recording_id: RecordingRef::Id(Id::new(900).unwrap()),
notice_version: 3,
recorder_name: "Bobbin".into(),
feature_label: "Sew Notes".into(),
started_by: Some(PromptMember {
member_id: Id::new(BOB).unwrap(),
display_name: "Bob".into(),
}),
room: PromptRoom {
channel_id: Id::new(48_831_765_147_549_698).unwrap(),
kind: PromptRoomKind::Voice,
name: Some("Lounge".into()),
},
audio_keep_days: 14,
transcript: PromptTranscript {
posted_to_channel: true,
readable_by: vec!["channel_members".into(), "search".into(), "agents".into()],
e2ee: false,
},
admin_download: true,
decline_mode: DeclineMode::JoinUnrecorded,
recorded_now: vec![Id::new(BOB).unwrap()],
required: false,
app_readers: vec![],
agent_readers: false,
}
}
fn names(id: Id) -> String {
if id.get() == BOB {
"Bob".into()
} else {
id.to_string()
}
}
#[test]
fn join_notice_is_the_kits_modal() {
let p = kit_prompt();
assert_eq!(p.title_en(ConsentAsk::Join), "Lounge is being recorded");
assert_eq!(p.feature_en(), "Sew Notes, recorded by Bobbin");
let want = "Sew Notes, recorded by Bobbin\n\
Bob started recording this call.\n\
Bobbin joins the call as a participant and records the voice of each person who \
agrees. Nobody is recorded without saying yes.\n\
Each person's words are transcribed and posted into this channel as ordinary \
messages that the channel's members, search and agents can read.\n\
Transcripts are not end-to-end encrypted.\n\
Audio is kept for 14 days.\n\
Authorised admins can download the audio.\n\
Recorded now: Bob.\n\
If you don't allow it, you join without being recorded.";
assert_eq!(p.text_en(ConsentAsk::Join, &names), want);
let labels: Vec<_> = p
.choices_en(ConsentAsk::Join)
.iter()
.map(|c| (c.label, c.granted))
.collect();
assert_eq!(
labels,
vec![
("Join and allow recording", Some(true)),
("Join without being recorded", Some(false)),
("Cancel", None),
]
);
}
#[test]
fn declining_means_not_joining_or_leaving() {
let mut p = kit_prompt();
p.decline_mode = DeclineMode::NotJoin;
let join = p.lines_en(ConsentAsk::Join, &names);
assert_eq!(
join.last().unwrap(),
"In this channel you can't join without being recorded."
);
assert_eq!(
p.choices_en(ConsentAsk::Join),
vec![
ConsentChoice {
label: "Join and allow recording",
granted: Some(true)
},
ConsentChoice {
label: "Don't join",
granted: None
},
]
);
let in_call = p.lines_en(ConsentAsk::InCall, &names);
assert_eq!(
in_call.last().unwrap(),
"In this channel, not allowing it means leaving the call."
);
assert_eq!(
p.choices_en(ConsentAsk::InCall),
vec![
ConsentChoice {
label: "Allow",
granted: Some(true)
},
ConsentChoice {
label: "Leave",
granted: Some(false)
},
]
);
}
#[test]
fn in_call_notice() {
let p = kit_prompt();
assert_eq!(
p.title_en(ConsentAsk::InCall),
"This call is being recorded"
);
assert_eq!(
p.lines_en(ConsentAsk::InCall, &names).last().unwrap(),
"If you don't allow it, you stay in the call without being recorded."
);
assert_eq!(
p.choices_en(ConsentAsk::InCall),
vec![
ConsentChoice {
label: "Allow",
granted: Some(true)
},
ConsentChoice {
label: "Don't record me",
granted: Some(false)
},
]
);
}
#[test]
fn auto_channel_and_the_other_slot_values() {
let mut p = kit_prompt();
p.started_by = None;
p.recorded_now = vec![];
p.audio_keep_days = 0;
p.transcript = PromptTranscript {
posted_to_channel: false,
readable_by: vec!["channel_members".into()],
e2ee: true,
};
p.admin_download = false;
assert_eq!(p.title_en(ConsentAsk::Join), "Calls in Lounge are recorded");
assert_eq!(
p.lines_en(ConsentAsk::Join, &names),
vec![
"A recording starts when someone joins this channel.",
"Bobbin joins the call as a participant and records the voice of each person \
who agrees. Nobody is recorded without saying yes.",
"Each person's words are transcribed; the channel's members can read the \
transcript.",
"Audio is deleted once the transcript is final.",
"Nobody is recorded yet.",
"If you don't allow it, you join without being recorded.",
]
);
p.audio_keep_days = 1;
p.transcript.readable_by = vec!["search".into(), "agents".into()];
p.recorded_now = vec![Id::new(BOB).unwrap(), Id::new(7).unwrap()];
let l = p.lines_en(ConsentAsk::Join, &names);
assert!(l.contains(&"Audio is kept for 1 day.".to_owned()), "{l:?}");
assert!(
l.contains(
&"Each person's words are transcribed; search and agents can read the transcript."
.to_owned()
),
"{l:?}"
);
assert!(l.contains(&"Recorded now: Bob, 7.".to_owned()), "{l:?}");
p.started_by = Some(PromptMember {
member_id: Id::new(BOB).unwrap(),
display_name: String::new(),
});
assert_eq!(
p.lines_en(ConsentAsk::Join, &names)[0],
"Bob started recording this call."
);
p.room.name = None;
p.room.kind = PromptRoomKind::Dm;
assert_eq!(p.title_en(ConsentAsk::Join), "This call is being recorded");
}
#[test]
fn required_room_notice() {
let mut p = kit_prompt();
p.started_by = None;
p.recorded_now = vec![];
p.required = true;
let want = "Sew Notes, recorded by Bobbin\n\
This room records every call with more than one person.\n\
Bobbin joins the call as a participant and records the voice of each person who \
agrees. Nobody is recorded without saying yes.\n\
Each person's words are transcribed and posted into this channel as ordinary \
messages that the channel's members, search and agents can read.\n\
Transcripts are not end-to-end encrypted.\n\
Audio is kept for 14 days.\n\
Authorised admins can download the audio.\n\
Nobody is recorded yet.\n\
If you don't allow it, you join without being recorded.";
assert_eq!(p.text_en(ConsentAsk::Join, &names), want);
assert_eq!(p.title_en(ConsentAsk::Join), "Calls in Lounge are recorded");
let mut q = kit_prompt();
q.required = true;
let l = q.lines_en(ConsentAsk::InCall, &names);
assert_eq!(l[0], "Bob started recording this call.");
assert_eq!(
l[1],
"This room records every call with more than one person."
);
}
#[test]
fn app_readers_notice() {
let mut p = kit_prompt();
p.app_readers = vec!["Helpdesk".into()];
let l = p.lines_en(ConsentAsk::Join, &names);
assert_eq!(l[3], "Helpdesk can read the transcript.", "{l:?}");
assert_eq!(l[4], "Transcripts are not end-to-end encrypted.", "{l:?}");
p.app_readers = vec!["Helpdesk".into(), "Payroll".into(), "Desk".into()];
let l = p.lines_en(ConsentAsk::Join, &names);
assert_eq!(l[3], "Helpdesk, Payroll and Desk can read the transcript.");
assert_eq!(kit_prompt().lines_en(ConsentAsk::Join, &names).len(), 8);
}
#[test]
fn agent_readers_notice() {
let mut p = kit_prompt();
p.app_readers = vec!["Helpdesk".into()];
p.agent_readers = true;
let l = p.lines_en(ConsentAsk::Join, &names);
assert_eq!(l[3], "Helpdesk can read the transcript.", "{l:?}");
assert_eq!(
l[4], "Agents in this channel can read the transcript.",
"{l:?}"
);
assert_eq!(l[5], "Transcripts are not end-to-end encrypted.", "{l:?}");
}
}