use std::net::IpAddr;
use laravel::rate_limit::{self, Limit};
use crate::author::{Author, Commenter};
use crate::ops;
pub async fn throttle<U: Commenter>(author: Author<'_, U>, ip: IpAddr) -> laravel::Result<()> {
let per_minute = ops::config().rate_limit;
if per_minute == 0 {
return Ok(());
}
let key = match author {
Author::User(user) => format!("comments:user:{}", user.commenter_id()),
Author::Guest { .. } => format!("comments:ip:{ip}"),
};
rate_limit::attempt(&key, Limit::per_minute(per_minute)).await??;
Ok(())
}