1use serde::{Deserialize, Serialize};
12
13pub const MAX_CALL_AUDIO_BYTES: usize = 4096;
15pub const MAX_CALL_TEXT_BYTES: usize = 256;
17
18#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
20pub struct BridgedCall {
21 pub outside: String,
23 pub tilo: String,
25}
26
27#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
29pub struct OutsideCall {
30 pub call: String,
32 #[serde(default)]
34 pub people: Vec<OutsidePerson>,
35}
36
37#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
39pub struct OutsidePerson {
40 pub account: String,
42 #[serde(default)]
44 pub name: String,
45 #[serde(default)]
48 pub avatar_url: Option<String>,
49 #[serde(default)]
51 pub avatar: Option<String>,
52 #[serde(default)]
54 pub muted: bool,
55 #[serde(default)]
58 pub deafened: bool,
59}
60
61#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
63#[serde(tag = "link", rename_all = "kebab-case")]
64pub enum CallLink {
65 #[default]
67 Idle,
68 Joining { call: String },
70 Joined { call: String },
72 Refused { call: String, reason: String },
75}
76
77#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
80pub struct CallReport {
81 #[serde(default)]
82 pub calls: Vec<OutsideCall>,
83 #[serde(default)]
84 pub link: CallLink,
85 #[serde(default)]
88 pub status: String,
89}
90
91#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
93pub struct CallAudio {
94 pub speaker: String,
96 pub opus: Vec<u8>,
98 #[serde(default)]
101 pub skipped: u32,
102}
103
104#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
106pub struct CallStatus {
107 #[serde(default)]
109 pub call: Option<String>,
110 #[serde(default)]
112 pub status: Option<String>,
113}
114
115impl CallAudio {
116 #[must_use]
119 pub fn valid(&self) -> bool {
120 !self.speaker.is_empty()
121 && self.speaker.len() <= MAX_CALL_TEXT_BYTES
122 && !self.opus.is_empty()
123 && self.opus.len() <= MAX_CALL_AUDIO_BYTES
124 }
125}
126
127impl CallReport {
128 #[must_use]
130 pub fn valid(&self) -> bool {
131 self.calls.iter().all(|call| {
132 !call.call.is_empty()
133 && call.call.len() <= MAX_CALL_TEXT_BYTES
134 && call.people.iter().all(|person| {
135 !person.account.is_empty()
136 && person.account.len() <= MAX_CALL_TEXT_BYTES
137 && !person.name.trim().is_empty()
138 && person.name.chars().count() <= 80
139 && person
140 .avatar_url
141 .as_deref()
142 .is_none_or(|url| url.len() <= 1024 && !url.trim().is_empty())
143 && person.avatar.as_deref().is_none_or(|hash| {
144 hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit())
145 })
146 })
147 })
148 }
149}
150
151impl BridgedCall {
152 #[must_use]
154 pub fn valid(&self) -> bool {
155 !self.outside.is_empty()
156 && self.outside.len() <= MAX_CALL_TEXT_BYTES
157 && !self.tilo.is_empty()
158 && self.tilo.len() <= MAX_CALL_TEXT_BYTES
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 #![allow(
165 clippy::unwrap_used,
166 clippy::expect_used,
167 clippy::panic,
168 reason = "in a test these are the assertion"
169 )]
170
171 use super::*;
172
173 #[test]
174 fn a_pair_names_two_calls_and_nothing_else() {
175 assert!(
176 BridgedCall {
177 outside: "458".into(),
178 tilo: "voice-tl".into()
179 }
180 .valid()
181 );
182 assert!(
183 !BridgedCall {
184 outside: String::new(),
185 tilo: "voice-tl".into()
186 }
187 .valid()
188 );
189 assert!(
190 !BridgedCall {
191 outside: "x".repeat(MAX_CALL_TEXT_BYTES + 1),
192 tilo: "voice-tl".into()
193 }
194 .valid()
195 );
196 }
197
198 #[test]
199 fn a_report_names_people_and_their_place() {
200 let report = CallReport {
201 calls: vec![OutsideCall {
202 call: "458".into(),
203 people: vec![OutsidePerson {
204 account: "7001".into(),
205 name: "Dana".into(),
206 avatar_url: Some("https://media.example/a.png".into()),
207 avatar: None,
208 muted: true,
209 deafened: false,
210 }],
211 }],
212 link: CallLink::Joined { call: "458".into() },
213 status: String::new(),
214 };
215 assert!(report.valid());
216 let mut wrong = report.clone();
217 wrong.calls[0].people[0].avatar = Some("not a hash".into());
218 assert!(!wrong.valid(), "a kept picture is named by its BLAKE3 hex");
219 let mut wrong = report;
220 wrong.calls[0].people[0].account = String::new();
221 assert!(!wrong.valid(), "a participant is named by an account");
222 }
223
224 #[test]
225 fn a_frame_is_bounded_and_named() {
226 let frame = CallAudio {
227 speaker: "7001".into(),
228 opus: vec![1, 2, 3],
229 skipped: 0,
230 };
231 assert!(frame.valid());
232 assert!(
233 !CallAudio {
234 speaker: String::new(),
235 opus: vec![1],
236 skipped: 0,
237 }
238 .valid()
239 );
240 assert!(
241 !CallAudio {
242 speaker: "7001".into(),
243 opus: vec![0; MAX_CALL_AUDIO_BYTES + 1],
244 skipped: 0,
245 }
246 .valid()
247 );
248 }
249}