main
use crate::errors::IssueError;
use cache::AppRedis;
use config::quota::QuotaConfig;
use std::ops::Deref;
#[derive(Clone)]
pub struct Admission {
spaces: space::Access,
quotas: AppRedis,
limits: QuotaConfig,
}
impl Admission {
pub fn new(spaces: space::Access, quotas: AppRedis, limits: QuotaConfig) -> Self {
Self {
spaces,
quotas,
limits,
}
}
pub async fn allow_issue(&self, user: i64) -> Result<(), IssueError> {
if self
.quotas
.quota_allow(
&format!("quota:issue_create:{user}"),
self.limits.issue_create_per_day,
86_400,
)
.await
{
Ok(())
} else {
Err(IssueError::RateLimited(
"issue creation quota exceeded, retry tomorrow".to_owned(),
))
}
}
pub async fn allow_comment(&self, user: i64) -> Result<(), IssueError> {
if self
.quotas
.quota_allow(
&format!("quota:comment_create:{user}"),
self.limits.comment_create_per_day,
86_400,
)
.await
{
Ok(())
} else {
Err(IssueError::RateLimited(
"comment creation quota exceeded, retry tomorrow".to_owned(),
))
}
}
}
impl Deref for Admission {
type Target = space::Access;
fn deref(&self) -> &Self::Target {
&self.spaces
}
}