Add automated warning and banning

This commit is contained in:
2025-08-14 23:18:43 -03:00
parent 8e0c591b21
commit 6dde25aa72
10 changed files with 587 additions and 36 deletions
+228 -2
View File
@@ -9,7 +9,7 @@ use scylla::{
},
};
use thiserror::Error;
use time::{OffsetDateTime, UtcDateTime};
use time::{Duration, OffsetDateTime, UtcDateTime, UtcOffset};
use twilight_http::Client;
use twilight_model::{
channel::Message as TwilightMessage,
@@ -134,8 +134,13 @@ impl<'a> Object<'a> for User {
}
}
pub struct NewUserIncident {
pub incident: UserIncident,
pub counting_incidents: usize,
pub is_first_incident: bool,
}
impl User {
#[allow(dead_code)]
pub async fn get_by_id(id: u64, session: &Session) -> Result<Option<Self>, DatabaseError> {
let id = u64_to_i64(id);
@@ -175,6 +180,77 @@ impl User {
},
}
}
pub async fn insert(&self, session: &Session) -> Result<(), DatabaseError> {
session
.query_unpaged(
r#"
INSERT INTO payphone.users (
user_id,
name,
email,
is_masked,
is_moderator,
joined_at,
last_seen_at,
supporter_since,
supporter_until
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
"#,
Self::to_row(self.clone()),
)
.await
.map_err(DatabaseError::QueryError)?;
Ok(())
}
pub async fn incident(
&self,
reason: String,
message: MessageData,
created_by_id: Option<u64>,
session: &Session,
) -> Result<NewUserIncident, DatabaseError> {
let incidents = UserIncident::get_for_user(self.id, session).await?;
let counting_incidents = incidents
.iter()
.filter(|incident| incident.created_at + Duration::weeks(12) > UtcDateTime::now())
.count();
let incident_type = if counting_incidents != 0 {
UserIncidentType::Ban
} else {
UserIncidentType::Warning
};
let duration = match counting_incidents {
0 => None, // Warning
1 => Some(Duration::days(1)), // First ban
_ => None, // Permanent ban
};
let incident = UserIncident {
user_id: self.id,
incident_id: get_random_u64(),
report_id: None,
r#type: incident_type,
created_at: UtcDateTime::now(),
expires_at: duration.map(|d| UtcDateTime::now() + d),
message,
reason,
created_by_id,
duration: duration.map(|_| "1 day".into()),
};
incident.insert(session).await?;
Ok(NewUserIncident {
incident,
counting_incidents,
is_first_incident: incidents.len() == 0,
})
}
}
impl From<TwilightUser> for User {
@@ -1008,3 +1084,153 @@ impl Message {
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UserIncidentType {
Warning,
Ban,
}
#[derive(Debug, Clone)]
pub struct UserIncident {
pub user_id: u64,
pub incident_id: u64,
pub report_id: Option<u64>,
pub r#type: UserIncidentType,
pub message: MessageData,
pub reason: String,
pub created_by_id: Option<u64>,
pub created_at: UtcDateTime,
pub expires_at: Option<UtcDateTime>,
pub duration: Option<String>,
}
impl<'a> Object<'a> for UserIncident {
type Row = (
i64,
i64,
Option<i64>,
String,
MessageData,
String,
Option<i64>,
OffsetDateTime,
Option<OffsetDateTime>,
Option<String>,
);
fn from_row(row: Self::Row) -> Self {
Self {
user_id: i64_to_u64(row.0),
incident_id: i64_to_u64(row.1),
report_id: row.2.map(i64_to_u64),
r#type: match row.3.as_str() {
"warning" => UserIncidentType::Warning,
"ban" => UserIncidentType::Ban,
_ => unreachable!(),
},
message: row.4,
reason: row.5,
created_by_id: row.6.map(i64_to_u64),
created_at: row.7.to_utc(),
expires_at: row.8.map(|i| i.to_utc()),
duration: row.9,
}
}
fn to_row(value: Self) -> Self::Row {
(
u64_to_i64(value.user_id),
u64_to_i64(value.incident_id),
value.report_id.map(u64_to_i64),
match value.r#type {
UserIncidentType::Warning => "warning".into(),
UserIncidentType::Ban => "ban".into(),
},
value.message,
value.reason,
value.created_by_id.map(u64_to_i64),
value.created_at.to_offset(UtcOffset::UTC),
value.expires_at.map(|i| i.to_offset(UtcOffset::UTC)),
value.duration,
)
}
}
impl UserIncident {
pub fn is_active(&self) -> bool {
self.expires_at
.map(|e| e > UtcDateTime::now())
.unwrap_or(true)
&& self.r#type == UserIncidentType::Ban
}
pub async fn get_for_user(user_id: u64, session: &Session) -> Result<Vec<Self>, DatabaseError> {
let user_id = u64_to_i64(user_id);
let result = session
.query_unpaged(
r#"
SELECT
user_id,
incident_id,
report_id,
type,
message,
reason,
created_by_id,
created_at,
expires_at,
duration
FROM
payphone.user_incidents
WHERE
user_id = ?;
"#,
(user_id,),
)
.await
.map_err(DatabaseError::QueryError)?
.into_rows_result()
.map_err(|_| DatabaseError::ResultNotRows)?;
Ok(result
.rows::<<Self as Object>::Row>()
.map_err(|_| DatabaseError::ResultNotRows)?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| DatabaseError::DeserializationFailed)?
.into_iter()
.map(Self::from_row)
.collect::<Vec<_>>())
}
pub async fn insert(&self, session: &Session) -> Result<(), DatabaseError> {
session
.query_unpaged(
r#"
INSERT INTO payphone.user_incidents (
user_id,
incident_id,
report_id,
type,
message,
reason,
created_by_id,
created_at,
expires_at,
duration
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
"#,
Self::to_row(self.clone()),
)
.await
.map_err(DatabaseError::QueryError)?;
Ok(())
}
}