Restrict accounts to min 30 days to be able to call and start working on masks

This commit is contained in:
2025-08-23 19:56:50 -03:00
parent 134d4e8879
commit 2a2ffd8894
14 changed files with 368 additions and 20 deletions
+132 -1
View File
@@ -21,7 +21,7 @@ use twilight_model::{
use crate::{
constants::DEFAULT_PREFIX,
groups::calls::DisplayProfile,
groups::calls::{DisplayProfile, masks::get_random_mask},
utils::{get_random_u64, i64_to_u64, u64_to_i64},
};
@@ -47,6 +47,9 @@ pub enum DatabaseError {
#[error("The resulting row could not be deserialized into an object")]
DeserializationFailed,
#[error("An unknown error occurred")]
Unknown(&'static str),
}
trait Object<'a> {
@@ -1744,3 +1747,131 @@ impl UserIncident {
incident.insert(session).await
}
}
#[derive(Debug, Clone)]
pub struct Participant {
pub call_id: u64,
pub user_id: u64,
pub channel_id: u64,
pub user_name: String,
pub user_avatar_url: String,
pub user_mask: String,
pub is_masked: bool,
}
impl<'a> Object<'a> for Participant {
type Row = (i64, i64, i64, String, String, String, bool);
fn from_row(row: Self::Row) -> Self {
Self {
call_id: i64_to_u64(row.0),
user_id: i64_to_u64(row.1),
channel_id: i64_to_u64(row.2),
user_name: row.3,
user_avatar_url: row.4,
user_mask: row.5,
is_masked: row.6,
}
}
fn to_row(value: Self) -> Self::Row {
(
u64_to_i64(value.call_id),
u64_to_i64(value.user_id),
u64_to_i64(value.channel_id),
value.user_name,
value.user_avatar_url,
value.user_mask,
value.is_masked,
)
}
}
impl Participant {
pub async fn get_for_call(call_id: u64, session: &Session) -> Result<Vec<Self>, DatabaseError> {
let call_id = u64_to_i64(call_id);
let rows = session
.query_unpaged(
r#"
SELECT * FROM payphone.participants
WHERE call_id = ?;
"#,
(call_id,),
)
.await
.map_err(DatabaseError::QueryError)?;
Ok(rows
.into_rows_result()
.map_err(|_| DatabaseError::ResultNotRows)?
.rows()
.map_err(|_| DatabaseError::ResultNotRows)?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| DatabaseError::DeserializationFailed)?
.into_iter()
.map(Self::from_row)
.collect::<Vec<_>>())
}
pub async fn create(
user_id: u64,
channel_id: u64,
call_id: u64,
profile: DisplayProfile,
is_masked: bool,
session: &Session,
) -> Result<(), DatabaseError> {
let masks_in_use = Self::get_for_call(call_id, session)
.await?
.iter()
.filter_map(|p| {
if p.channel_id == channel_id {
Some(p.user_mask.clone())
} else {
None
}
})
.collect::<Vec<_>>();
let (mask, _) = get_random_mask(&masks_in_use).ok_or(DatabaseError::Unknown(
"No remaining masks available for the channel",
))?;
let participant = Participant {
call_id,
user_id,
channel_id,
user_name: profile.name,
user_avatar_url: profile.avatar_url,
user_mask: mask,
is_masked,
};
participant.insert(session).await
}
pub async fn insert(&self, session: &Session) -> Result<(), DatabaseError> {
session
.query_unpaged(
r#"
INSERT INTO payphone.participants (
call_id,
user_id,
channel_id,
user_name,
user_avatar_url,
user_mask,
is_masked
) IF NOT EXISTS VALUES (?, ?, ?, ?, ?, ?, ?);
"#,
Self::to_row(self.clone()),
)
.await
.map_err(DatabaseError::QueryError)?;
Ok(())
}
}