mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add minimum chat moderation to enable bans for #22
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20250529_000001_create_sanction_table"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
// Define how to apply this migration: Create the Sanctions table.
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(crate::schema::sanction::Entity)
|
||||
.col(
|
||||
ColumnDef::new(crate::schema::sanction::Column::Id)
|
||||
.unsigned()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::UserId).unsigned().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-sanction-user_id")
|
||||
.from(crate::schema::sanction::Entity, crate::schema::sanction::Column::UserId)
|
||||
.to(crate::schema::user::Entity, crate::schema::user::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::CreationTime).big_integer().not_null())
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::IssuerId).unsigned().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-sanction-issuer_id")
|
||||
.from(crate::schema::sanction::Entity, crate::schema::sanction::Column::IssuerId)
|
||||
.to(crate::schema::user::Entity, crate::schema::user::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::IssuerName).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::Descriptor).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::Reason).string().not_null())
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::Duration).big_integer())
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::Acknowledged).big_integer())
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::AppealerId).unsigned())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-sanction-appealer_id")
|
||||
.from(crate::schema::sanction::Entity, crate::schema::sanction::Column::AppealerId)
|
||||
.to(crate::schema::user::Entity, crate::schema::user::Column::Id),
|
||||
)
|
||||
.col(ColumnDef::new(crate::schema::sanction::Column::AppealTime).big_integer())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
// Define how to rollback this migration: Drop the Sanctions table.
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(crate::schema::sanction::Entity).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod m20250424_000003_create_garage_table;
|
||||
mod m20250424_000004_create_user_aux_table;
|
||||
mod m20250424_000005_create_campaign_tables;
|
||||
mod m20250526_000001_add_garage_customisation;
|
||||
mod m20250529_000001_create_sanction_table;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
@@ -19,6 +20,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20250424_000004_create_user_aux_table::Migration),
|
||||
Box::new(m20250424_000005_create_campaign_tables::Migration),
|
||||
Box::new(m20250526_000001_add_garage_customisation::Migration),
|
||||
Box::new(m20250529_000001_create_sanction_table::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ pub mod garage;
|
||||
pub mod campaign;
|
||||
pub mod campaign_difficulty_completion;
|
||||
pub mod common_query;
|
||||
pub mod sanction;
|
||||
|
||||
pub fn parse_int_csv(s: &str) -> Vec<u32> {
|
||||
s.split(',').filter_map(|i_as_s| {
|
||||
|
||||
46
rc_database/src/schema/sanction.rs
Normal file
46
rc_database/src/schema/sanction.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "sanctions")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: u32,
|
||||
pub user_id: u32,
|
||||
pub creation_time: i64, // seconds since unix epoch
|
||||
pub issuer_id: u32,
|
||||
pub issuer_name: String,
|
||||
pub descriptor: Descriptor,
|
||||
pub reason: String,
|
||||
pub duration: Option<i64>, // seconds after creation_time (null means permanent or irrelevant)
|
||||
pub acknowledged: Option<i64>, // seconds since unix epoch
|
||||
pub appealer_id: Option<u32>, // moderator who approved appeal
|
||||
pub appeal_time: Option<i64>, // seconds since unix epoch
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::user::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::user::Column::Id"
|
||||
)]
|
||||
User,
|
||||
}
|
||||
|
||||
impl Related<super::user::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::User.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "PascalCase")]
|
||||
pub enum Descriptor {
|
||||
Warn,
|
||||
Mute,
|
||||
Ban,
|
||||
Note,
|
||||
Kick,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, QueryOrder, QuerySelect, TransactionTrait};
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect, TransactionTrait};
|
||||
|
||||
pub struct Database {
|
||||
orm: sea_orm::DatabaseConnection,
|
||||
@@ -105,6 +105,24 @@ impl Database {
|
||||
entity.insert(&self.orm).await
|
||||
}
|
||||
|
||||
pub async fn update_perms_by_user_id(&self, mut entity: crate::schema::permissions::ActiveModel, user_id: u32) -> Result<Option<crate::schema::permissions::Model>, sea_orm::DbErr> {
|
||||
let id_opt = crate::schema::permissions::Entity::find()
|
||||
.select_only()
|
||||
.column(crate::schema::permissions::Column::Id)
|
||||
.filter(crate::schema::permissions::Column::UserId.eq(user_id))
|
||||
.into_model::<crate::schema::common_query::Id>()
|
||||
.one(&self.orm)
|
||||
.await?;
|
||||
if let Some(id) = id_opt {
|
||||
entity.id = sea_orm::ActiveValue::Set(id.id);
|
||||
Ok(Some(crate::schema::permissions::Entity::update(entity)
|
||||
.exec(&self.orm)
|
||||
.await?))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn garage_max_slot_by_user_id(&self, user_id: u32) -> Result<u32, sea_orm::DbErr> {
|
||||
let result = crate::schema::garage::Entity::find()
|
||||
.select_only()
|
||||
@@ -227,4 +245,26 @@ impl Database {
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn count_sanctions_to_ack_by_user_id_and_descriptor(&self, user_id: u32, desc: crate::schema::sanction::Descriptor) -> Result<u64, sea_orm::DbErr> {
|
||||
crate::schema::sanction::Entity::find()
|
||||
.filter(crate::schema::sanction::Column::UserId.eq(user_id))
|
||||
.filter(crate::schema::sanction::Column::Descriptor.eq(desc))
|
||||
.filter(crate::schema::sanction::Column::Acknowledged.is_null())
|
||||
.count(&self.orm)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn sanctions_by_user_id(&self, user_id: u32) -> Result<Vec<crate::schema::sanction::Model>, sea_orm::DbErr> {
|
||||
crate::schema::sanction::Entity::find()
|
||||
.filter(crate::schema::sanction::Column::UserId.eq(user_id))
|
||||
.filter(crate::schema::sanction::Column::Acknowledged.is_null())
|
||||
.order_by_asc(crate::schema::sanction::Column::CreationTime)
|
||||
.all(&self.orm)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_sanction(&self, entity: crate::schema::sanction::ActiveModel) -> Result<crate::schema::sanction::Model, sea_orm::DbErr> {
|
||||
entity.insert(&self.orm).await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user