use serde::{Deserialize, Serialize};
pub const MAX_CALL_AUDIO_BYTES: usize = 4096;
pub const MAX_CALL_TEXT_BYTES: usize = 256;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct BridgedCall {
pub outside: String,
pub tilo: String,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutsideCall {
pub call: String,
#[serde(default)]
pub people: Vec<OutsidePerson>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutsidePerson {
pub account: String,
#[serde(default)]
pub name: String,
#[serde(default)]
pub avatar_url: Option<String>,
#[serde(default)]
pub avatar: Option<String>,
#[serde(default)]
pub muted: bool,
#[serde(default)]
pub deafened: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "link", rename_all = "kebab-case")]
pub enum CallLink {
#[default]
Idle,
Joining { call: String },
Joined { call: String },
Refused { call: String, reason: String },
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CallReport {
#[serde(default)]
pub calls: Vec<OutsideCall>,
#[serde(default)]
pub link: CallLink,
#[serde(default)]
pub status: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CallAudio {
pub speaker: String,
pub opus: Vec<u8>,
#[serde(default)]
pub skipped: u32,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CallStatus {
#[serde(default)]
pub call: Option<String>,
#[serde(default)]
pub status: Option<String>,
}
impl CallAudio {
#[must_use]
pub fn valid(&self) -> bool {
!self.speaker.is_empty()
&& self.speaker.len() <= MAX_CALL_TEXT_BYTES
&& !self.opus.is_empty()
&& self.opus.len() <= MAX_CALL_AUDIO_BYTES
}
}
impl CallReport {
#[must_use]
pub fn valid(&self) -> bool {
self.calls.iter().all(|call| {
!call.call.is_empty()
&& call.call.len() <= MAX_CALL_TEXT_BYTES
&& call.people.iter().all(|person| {
!person.account.is_empty()
&& person.account.len() <= MAX_CALL_TEXT_BYTES
&& !person.name.trim().is_empty()
&& person.name.chars().count() <= 80
&& person
.avatar_url
.as_deref()
.is_none_or(|url| url.len() <= 1024 && !url.trim().is_empty())
&& person.avatar.as_deref().is_none_or(|hash| {
hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit())
})
})
})
}
}
impl BridgedCall {
#[must_use]
pub fn valid(&self) -> bool {
!self.outside.is_empty()
&& self.outside.len() <= MAX_CALL_TEXT_BYTES
&& !self.tilo.is_empty()
&& self.tilo.len() <= MAX_CALL_TEXT_BYTES
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
reason = "in a test these are the assertion"
)]
use super::*;
#[test]
fn a_pair_names_two_calls_and_nothing_else() {
assert!(
BridgedCall {
outside: "458".into(),
tilo: "voice-tl".into()
}
.valid()
);
assert!(
!BridgedCall {
outside: String::new(),
tilo: "voice-tl".into()
}
.valid()
);
assert!(
!BridgedCall {
outside: "x".repeat(MAX_CALL_TEXT_BYTES + 1),
tilo: "voice-tl".into()
}
.valid()
);
}
#[test]
fn a_report_names_people_and_their_place() {
let report = CallReport {
calls: vec![OutsideCall {
call: "458".into(),
people: vec![OutsidePerson {
account: "7001".into(),
name: "Dana".into(),
avatar_url: Some("https://media.example/a.png".into()),
avatar: None,
muted: true,
deafened: false,
}],
}],
link: CallLink::Joined { call: "458".into() },
status: String::new(),
};
assert!(report.valid());
let mut wrong = report.clone();
wrong.calls[0].people[0].avatar = Some("not a hash".into());
assert!(!wrong.valid(), "a kept picture is named by its BLAKE3 hex");
let mut wrong = report;
wrong.calls[0].people[0].account = String::new();
assert!(!wrong.valid(), "a participant is named by an account");
}
#[test]
fn a_frame_is_bounded_and_named() {
let frame = CallAudio {
speaker: "7001".into(),
opus: vec![1, 2, 3],
skipped: 0,
};
assert!(frame.valid());
assert!(
!CallAudio {
speaker: String::new(),
opus: vec![1],
skipped: 0,
}
.valid()
);
assert!(
!CallAudio {
speaker: "7001".into(),
opus: vec![0; MAX_CALL_AUDIO_BYTES + 1],
skipped: 0,
}
.valid()
);
}
}