use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::error::{Validate, ValidationError};
pub const QUOTA_SPACES_MAX: &str = "spaces_max";
pub const QUOTA_CHANNELS_PER_SPACE_MAX: &str = "channels_per_space_max";
pub const QUOTA_MEMBERS_PER_SPACE_MAX: &str = "members_per_space_max";
pub const QUOTA_FILE_BYTES_MAX: &str = "file_bytes_max";
pub const QUOTA_CALL_PARTICIPANTS_MAX: &str = "call_participants_max";
pub const QUOTA_MESSAGES_PER_MIN: &str = "messages_per_min";
pub const APP_QUOTAS: [&str; 6] = [
QUOTA_SPACES_MAX,
QUOTA_CHANNELS_PER_SPACE_MAX,
QUOTA_MEMBERS_PER_SPACE_MAX,
QUOTA_FILE_BYTES_MAX,
QUOTA_CALL_PARTICIPANTS_MAX,
QUOTA_MESSAGES_PER_MIN,
];
pub const APP_QUOTA_COUNT_MAX: u32 = 1_000_000_000;
pub const APP_QUOTA_MESSAGES_PER_MIN_MAX: u32 = 1_000_000;
pub const APP_QUOTA_FILE_BYTES_MAX: u64 = 1 << 62;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct AppQuotas {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub spaces_max: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub channels_per_space_max: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub members_per_space_max: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_bytes_max: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub call_participants_max: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub messages_per_min: Option<u32>,
}
impl AppQuotas {
pub fn is_empty(&self) -> bool {
*self == AppQuotas::default()
}
pub fn get(&self, name: &str) -> Option<u64> {
match name {
QUOTA_SPACES_MAX => self.spaces_max.map(u64::from),
QUOTA_CHANNELS_PER_SPACE_MAX => self.channels_per_space_max.map(u64::from),
QUOTA_MEMBERS_PER_SPACE_MAX => self.members_per_space_max.map(u64::from),
QUOTA_FILE_BYTES_MAX => self.file_bytes_max,
QUOTA_CALL_PARTICIPANTS_MAX => self.call_participants_max.map(u64::from),
QUOTA_MESSAGES_PER_MIN => self.messages_per_min.map(u64::from),
_ => None,
}
}
}
impl Validate for AppQuotas {
fn validate(&self) -> Result<(), ValidationError> {
for (field, v) in [
(QUOTA_SPACES_MAX, self.spaces_max),
(QUOTA_CHANNELS_PER_SPACE_MAX, self.channels_per_space_max),
(QUOTA_MEMBERS_PER_SPACE_MAX, self.members_per_space_max),
(QUOTA_CALL_PARTICIPANTS_MAX, self.call_participants_max),
] {
if v.is_some_and(|v| v > APP_QUOTA_COUNT_MAX) {
return Err(ValidationError::invalid(
field,
format!("at most {APP_QUOTA_COUNT_MAX}"),
));
}
}
if self
.file_bytes_max
.is_some_and(|v| v > APP_QUOTA_FILE_BYTES_MAX)
{
return Err(ValidationError::invalid(
QUOTA_FILE_BYTES_MAX,
"at most 2^62 bytes",
));
}
if let Some(m) = self.messages_per_min
&& !(1..=APP_QUOTA_MESSAGES_PER_MIN_MAX).contains(&m)
{
return Err(ValidationError::invalid(
QUOTA_MESSAGES_PER_MIN,
format!("1–{APP_QUOTA_MESSAGES_PER_MIN_MAX} posts a minute"),
));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
pub struct AppQuotaUsage {
pub spaces_max: u64,
pub channels_per_space_max: u64,
pub members_per_space_max: u64,
pub file_bytes_max: u64,
pub call_participants_max: u64,
pub messages_per_min: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct AppQuotasResponse {
pub app_id: String,
pub quotas: AppQuotas,
pub used: AppQuotaUsage,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub changed: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct AppQuotaDetails {
pub quota: String,
pub limit: u64,
pub used: u64,
pub app_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retry_after_ms: Option<u64>,
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn validate_bounds() {
assert!(AppQuotas::default().validate().is_ok());
let ok = AppQuotas {
spaces_max: Some(0),
messages_per_min: Some(1),
file_bytes_max: Some(APP_QUOTA_FILE_BYTES_MAX),
..Default::default()
};
assert!(ok.validate().is_ok());
let zero_rate = AppQuotas {
messages_per_min: Some(0),
..Default::default()
};
assert_eq!(
zero_rate.validate().unwrap_err().field,
QUOTA_MESSAGES_PER_MIN
);
let big = AppQuotas {
members_per_space_max: Some(APP_QUOTA_COUNT_MAX + 1),
..Default::default()
};
assert_eq!(
big.validate().unwrap_err().field,
QUOTA_MEMBERS_PER_SPACE_MAX
);
}
#[test]
fn get_by_name() {
let q = AppQuotas {
channels_per_space_max: Some(3),
file_bytes_max: Some(9),
..Default::default()
};
assert_eq!(q.get(QUOTA_CHANNELS_PER_SPACE_MAX), Some(3));
assert_eq!(q.get(QUOTA_FILE_BYTES_MAX), Some(9));
assert_eq!(q.get(QUOTA_SPACES_MAX), None);
assert_eq!(q.get("nope"), None);
}
}