use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::spaces::check_app_id;
use crate::error::{Validate, ValidationError};
use crate::external_id::ExternalId;
use crate::http::Patch;
use crate::id::Id;
use crate::policy::RECORDING_AUDIO_DAYS_LIMIT;
use crate::recording::{
AudioState, Consent, RecordingEventKind, RecordingRule, RecordingStartLevel, RecordingState,
RecordingTrigger, StopReason, TranscriptFailure, TranscriptState,
};
use crate::time::Timestamp;
pub const FEATURE_APP_RECORDING: &str = "app_recording";
pub const FEATURE_VOICE_RECORD_CAP: &str = "voice_record_cap";
pub const FEATURE_RECORDING_REQUIRED: &str = "recording_required";
pub const FEATURE_MGMT_RECORDING_READS: &str = "mgmt_recording_reads";
pub const FEATURE_RECORDING_CONTROLS: &str = "recording_controls";
pub const RECORDING_EVENTS_KEEP_DAYS: u32 = 7;
pub const RECORDING_CHANGES_WAIT_MAX_S: u32 = 30;
pub const RECORDING_CHANGES_LIMIT_DEFAULT: u32 = 100;
pub const RECORDING_CHANGES_LIMIT_MAX: u32 = 500;
pub const MGMT_RECORDINGS_LIMIT_DEFAULT: u32 = 50;
pub const MGMT_RECORDINGS_LIMIT_MAX: u32 = 100;
pub const MGMT_TRANSCRIPT_LIMIT_DEFAULT: u32 = 200;
pub const MGMT_TRANSCRIPT_LIMIT_MAX: u32 = 1_000;
fn check_opt_app_id(v: Option<&str>) -> Result<(), ValidationError> {
v.map_or(Ok(()), |a| check_app_id("app_id", a))
}
fn check_limit(v: Option<u32>, max: u32) -> Result<(), ValidationError> {
match v {
Some(l) if !(1..=max).contains(&l) => Err(ValidationError::invalid(
"limit",
format!("limit must be 1–{max}"),
)),
_ => Ok(()),
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtChannelRecordingRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub app_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub auto: Option<bool>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub required: Patch<bool>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub audio_days: Patch<u16>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub require_consent: Patch<bool>,
}
impl MgmtChannelRecordingRequest {
pub fn new() -> MgmtChannelRecordingRequest {
MgmtChannelRecordingRequest::default()
}
}
impl Validate for MgmtChannelRecordingRequest {
fn validate(&self) -> Result<(), ValidationError> {
check_opt_app_id(self.app_id.as_deref())?;
if let Patch::Set(d) = self.audio_days
&& d > RECORDING_AUDIO_DAYS_LIMIT
{
return Err(ValidationError::invalid(
"audio_days",
format!("audio_days must be at most {RECORDING_AUDIO_DAYS_LIMIT}"),
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtChannelRecording {
pub channel_id: Id,
pub auto: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub required: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub audio_days: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub require_consent: Option<bool>,
pub rule: RecordingRule,
}
impl MgmtChannelRecording {
pub fn new(channel_id: Id) -> MgmtChannelRecording {
MgmtChannelRecording {
channel_id,
auto: false,
required: None,
audio_days: None,
require_consent: None,
rule: RecordingRule::None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtRecordingStopRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub app_id: Option<String>,
}
impl MgmtRecordingStopRequest {
pub fn new(app_id: Option<String>) -> MgmtRecordingStopRequest {
MgmtRecordingStopRequest { app_id }
}
}
impl Validate for MgmtRecordingStopRequest {
fn validate(&self) -> Result<(), ValidationError> {
check_opt_app_id(self.app_id.as_deref())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtRecordingQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub app_id: Option<String>,
}
impl MgmtRecordingQuery {
pub fn new(app_id: Option<String>) -> MgmtRecordingQuery {
MgmtRecordingQuery { app_id }
}
}
impl Validate for MgmtRecordingQuery {
fn validate(&self) -> Result<(), ValidationError> {
check_opt_app_id(self.app_id.as_deref())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtRecordingParticipant {
pub external_id: ExternalId,
pub consent: Consent,
pub recorded: bool,
}
impl MgmtRecordingParticipant {
pub fn new(external_id: ExternalId, consent: Consent, recorded: bool) -> Self {
MgmtRecordingParticipant {
external_id,
consent,
recorded,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
#[allow(deprecated)]
pub struct MgmtRecordingSummary {
pub id: Id,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub space_id: Option<Id>,
pub channel_id: Id,
pub state: RecordingState,
pub trigger: RecordingTrigger,
pub started_by: ExternalId,
pub started_at: Timestamp,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stopped_at: Option<Timestamp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stop_reason: Option<StopReason>,
pub record_message_id: Id,
pub transcript_state: TranscriptState,
#[deprecated(
note = "moves into `recording.transcript.failure`; kept for at least one more release, then removed in a later breaking release"
)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub transcript_failure: Option<TranscriptFailure>,
pub audio_state: AudioState,
pub audio_keep_days: u16,
pub participants: Vec<MgmtRecordingParticipant>,
}
impl MgmtRecordingSummary {
#[allow(deprecated)]
pub fn new(
id: Id,
channel_id: Id,
started_by: ExternalId,
started_at: Timestamp,
record_message_id: Id,
) -> MgmtRecordingSummary {
MgmtRecordingSummary {
id,
space_id: None,
channel_id,
state: RecordingState::Starting,
trigger: RecordingTrigger::Manual,
started_by,
started_at,
stopped_at: None,
stop_reason: None,
record_message_id,
transcript_state: TranscriptState::Pending,
transcript_failure: None,
audio_state: AudioState::Available,
audio_keep_days: 0,
participants: Vec::new(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtRecordingsQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub app_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl MgmtRecordingsQuery {
pub fn limit(&self) -> u32 {
self.limit.unwrap_or(MGMT_RECORDINGS_LIMIT_DEFAULT)
}
}
impl Validate for MgmtRecordingsQuery {
fn validate(&self) -> Result<(), ValidationError> {
check_opt_app_id(self.app_id.as_deref())?;
check_limit(self.limit, MGMT_RECORDINGS_LIMIT_MAX)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtRecordingsPage {
pub items: Vec<MgmtRecordingSummary>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next: Option<Id>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtTranscriptQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub app_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<Id>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl MgmtTranscriptQuery {
pub fn limit(&self) -> u32 {
self.limit.unwrap_or(MGMT_TRANSCRIPT_LIMIT_DEFAULT)
}
}
impl Validate for MgmtTranscriptQuery {
fn validate(&self) -> Result<(), ValidationError> {
check_opt_app_id(self.app_id.as_deref())?;
check_limit(self.limit, MGMT_TRANSCRIPT_LIMIT_MAX)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtTranscriptTurn {
pub message_id: Id,
pub speaker_external_id: ExternalId,
pub start_ms: u64,
pub end_ms: u64,
pub text: String,
}
impl MgmtTranscriptTurn {
pub fn new(
message_id: Id,
speaker_external_id: ExternalId,
start_ms: u64,
end_ms: u64,
text: String,
) -> Self {
MgmtTranscriptTurn {
message_id,
speaker_external_id,
start_ms,
end_ms,
text,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtTranscriptPage {
pub recording_id: Id,
pub transcript_state: TranscriptState,
pub turns: Vec<MgmtTranscriptTurn>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next: Option<Id>,
}
impl MgmtTranscriptPage {
pub fn new(recording_id: Id, transcript_state: TranscriptState) -> Self {
MgmtTranscriptPage {
recording_id,
transcript_state,
turns: Vec::new(),
next: None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtRecordingChangesQuery {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wait: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl MgmtRecordingChangesQuery {
pub fn limit(&self) -> u32 {
self.limit.unwrap_or(RECORDING_CHANGES_LIMIT_DEFAULT)
}
pub fn wait_s(&self) -> u32 {
self.wait.unwrap_or(0)
}
}
impl Validate for MgmtRecordingChangesQuery {
fn validate(&self) -> Result<(), ValidationError> {
check_limit(self.limit, RECORDING_CHANGES_LIMIT_MAX)?;
if let Some(w) = self.wait
&& w > RECORDING_CHANGES_WAIT_MAX_S
{
return Err(ValidationError::invalid(
"wait",
format!("wait must be at most {RECORDING_CHANGES_WAIT_MAX_S} seconds"),
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtRecordingChange {
pub seq: u64,
pub kind: RecordingEventKind,
pub recording_id: Id,
pub space_id: Id,
pub channel_id: Id,
pub at: Timestamp,
}
impl MgmtRecordingChange {
pub fn new(
seq: u64,
kind: RecordingEventKind,
recording_id: Id,
space_id: Id,
channel_id: Id,
at: Timestamp,
) -> Self {
MgmtRecordingChange {
seq,
kind,
recording_id,
space_id,
channel_id,
at,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtRecordingChanges {
pub items: Vec<MgmtRecordingChange>,
pub next: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct MgmtAppRecordingRequest {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub start_level: Option<RecordingStartLevel>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub required: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cap_required: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub server_read: Option<bool>,
#[serde(default, skip_serializing_if = "Patch::is_keep")]
pub audio_max_days: Patch<u16>,
}
impl MgmtAppRecordingRequest {
pub fn new() -> MgmtAppRecordingRequest {
MgmtAppRecordingRequest::default()
}
pub fn is_empty(&self) -> bool {
self.start_level.is_none()
&& self.required.is_none()
&& self.cap_required.is_none()
&& self.server_read.is_none()
&& self.audio_max_days.is_keep()
}
}
impl Validate for MgmtAppRecordingRequest {
fn validate(&self) -> Result<(), ValidationError> {
if let Some(l) = self.start_level {
crate::error::check_known("recording.start_level", l)?;
}
if let Patch::Set(d) = self.audio_max_days
&& d > RECORDING_AUDIO_DAYS_LIMIT
{
return Err(ValidationError::invalid(
"recording.audio_max_days",
format!("audio_max_days must be at most {RECORDING_AUDIO_DAYS_LIMIT}"),
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtAppRecording {
#[serde(default = "default_start_level")]
pub start_level: RecordingStartLevel,
#[serde(default)]
pub required: bool,
#[serde(default)]
pub cap_required: bool,
#[serde(default)]
pub server_read: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub audio_max_days: Option<u16>,
}
const fn default_start_level() -> RecordingStartLevel {
RecordingStartLevel::DEFAULT
}
impl Default for MgmtAppRecording {
fn default() -> Self {
MgmtAppRecording {
start_level: RecordingStartLevel::DEFAULT,
required: false,
cap_required: false,
server_read: false,
audio_max_days: None,
}
}
}
impl MgmtAppRecording {
pub fn new() -> MgmtAppRecording {
MgmtAppRecording::default()
}
pub fn is_default(&self) -> bool {
*self == MgmtAppRecording::default()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn channel_request_patches_and_bounds() {
let r: MgmtChannelRecordingRequest =
serde_json::from_str(r#"{"app_id":"jobs","required":null,"audio_days":30}"#).unwrap();
assert_eq!(r.required, Patch::Clear);
assert_eq!(r.audio_days, Patch::Set(30));
assert_eq!(r.require_consent, Patch::Keep);
assert!(r.validate().is_ok());
let mut bad = MgmtChannelRecordingRequest::new();
bad.audio_days = Patch::Set(RECORDING_AUDIO_DAYS_LIMIT + 1);
assert!(bad.validate().is_err());
let mut bad = MgmtChannelRecordingRequest::new();
bad.app_id = Some("Jobs!".into());
assert!(bad.validate().is_err());
assert!(
serde_json::from_str::<MgmtChannelRecordingRequest>(r#"{"zz":1}"#).is_err(),
"unknown fields are refused"
);
}
#[test]
fn query_limits() {
let mut q = MgmtRecordingChangesQuery::default();
assert_eq!((q.limit(), q.wait_s()), (100, 0));
q.wait = Some(31);
assert!(q.validate().is_err());
let mut q = MgmtRecordingsQuery::default();
assert_eq!(q.limit(), 50);
q.limit = Some(101);
assert!(q.validate().is_err());
let mut q = MgmtTranscriptQuery::default();
assert_eq!(q.limit(), 200);
q.limit = Some(0);
assert!(q.validate().is_err());
}
}