add: allow free users to create 2 invites

This commit is contained in:
trisua 2025-06-22 13:50:12 -04:00
parent 626c6711ef
commit 2f83497f98
4 changed files with 66 additions and 19 deletions

View file

@ -1,4 +1,5 @@
use oiseau::{cache::Cache, query_rows};
use tetratto_shared::unix_epoch_timestamp;
use crate::model::{
Error, Result,
auth::{User, InviteCode},
@ -67,7 +68,9 @@ impl DataManager {
Ok(out)
}
const MAXIMUM_SUPPORTER_INVITE_CODES: usize = 15;
const MAXIMUM_FREE_INVITE_CODES: usize = 2;
const MAXIMUM_SUPPORTER_INVITE_CODES: usize = 24;
const MINIMUM_ACCOUNT_AGE_FOR_FREE_INVITE_CODES: usize = 2_629_800_000; // 1mo
/// Create a new invite_code in the database.
///
@ -75,8 +78,26 @@ impl DataManager {
/// * `data` - a mock [`InviteCode`] object to insert
pub async fn create_invite_code(&self, data: InviteCode, user: &User) -> Result<InviteCode> {
if !user.permissions.check(FinePermission::SUPPORTER) {
return Err(Error::RequiresSupporter);
} else {
// check account creation date
if unix_epoch_timestamp() - user.created
< Self::MINIMUM_ACCOUNT_AGE_FOR_FREE_INVITE_CODES
{
return Err(Error::MiscError(
"Your account is too young to do this".to_string(),
));
}
// our account is old enough, but we need to make sure we don't already have
// 2 invite codes
if self.get_invite_codes_by_owner(user.id).await?.len()
>= Self::MAXIMUM_FREE_INVITE_CODES
{
return Err(Error::MiscError(
"You already have the maximum number of invite codes you can create"
.to_string(),
));
}
} else if !user.permissions.check(FinePermission::MANAGE_INVITES) {
// check count
if self.get_invite_codes_by_owner(user.id).await?.len()
>= Self::MAXIMUM_SUPPORTER_INVITE_CODES