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:
47
rc_chat_room/src/operations/add_modify_sanction.rs
Normal file
47
rc_chat_room/src/operations/add_modify_sanction.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl};
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
const CODE: u8 = 7;
|
||||
|
||||
const SANCTION_TY_PARAM_KEY: u8 = 9; // int; in
|
||||
const IS_ADDING_PARAM_KEY: u8 = 10; // bool; in
|
||||
const DURATION_PARAM_KEY: u8 = 11; // int; in
|
||||
const REASON_PARAM_KEY: u8 = 2; // str; in
|
||||
const USERNAME_PARAM_KEY: u8 = 7; // str; in
|
||||
|
||||
pub(super) struct AddSanctionProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl <C: Send + 'static> SimpleOperation<C> for AddSanctionProvider {
|
||||
type User = crate::UserTy;
|
||||
const CODE: u8 = CODE;
|
||||
|
||||
async fn handle(&self, params: ParameterTable<C>, user: &Self::User) -> Result<ParameterTable<C>, SimpleOpError> {
|
||||
let mut params = params.to_dict();
|
||||
if let Some(Typed::Int(sanction_ty)) = params.remove(&SANCTION_TY_PARAM_KEY) {
|
||||
if let Some(Typed::Bool(is_adding)) = params.remove(&IS_ADDING_PARAM_KEY) {
|
||||
if let Some(Typed::Int(duration)) = params.remove(&DURATION_PARAM_KEY) {
|
||||
if let Some(Typed::Str(reason)) = params.remove(&REASON_PARAM_KEY) {
|
||||
if let Some(Typed::Str(username)) = params.remove(&USERNAME_PARAM_KEY) {
|
||||
let user_info = user.user()?;
|
||||
let sanction = rc_core::persist::user::SetSanction {
|
||||
type_: rc_core::persist::user::SanctionType::from_i32(sanction_ty)?,
|
||||
is_adding,
|
||||
duration,
|
||||
reason: reason.string,
|
||||
username: username.string,
|
||||
};
|
||||
user_info.set_sanction(sanction).await?;
|
||||
return Ok(params.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err((rc_core::data::error_codes::ChatErrorCodes::UnexpectedError as i16).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn add_modify_sanction_provider<C: Send + 'static>() -> SimpleOpImpl<C, crate::UserTy, AddSanctionProvider> {
|
||||
SimpleOpImpl::new(AddSanctionProvider)
|
||||
}
|
||||
30
rc_chat_room/src/operations/list_sanctions.rs
Normal file
30
rc_chat_room/src/operations/list_sanctions.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl};
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
const CODE: u8 = 16;
|
||||
|
||||
const SANCTIONS_PARAM_KEY: u8 = 31; // arr of str; out
|
||||
const USERNAME_PARAM_KEY: u8 = 22; // str; in
|
||||
|
||||
pub(super) struct GetSanctionsProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl SimpleOperation<()> for GetSanctionsProvider {
|
||||
type User = crate::UserTy;
|
||||
const CODE: u8 = CODE;
|
||||
|
||||
async fn handle(&self, params: ParameterTable, user: &Self::User) -> Result<ParameterTable, SimpleOpError> {
|
||||
let mut params = params.to_dict();
|
||||
if let Some(Typed::Str(username)) = params.remove(&USERNAME_PARAM_KEY) {
|
||||
let user_info = user.user()?;
|
||||
let sanctions = user_info.get_sanctions(username.string).await?;
|
||||
params.insert(SANCTIONS_PARAM_KEY, sanctions);
|
||||
return Ok(params.into());
|
||||
}
|
||||
Err((rc_core::data::error_codes::ChatErrorCodes::UnexpectedError as i16).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn list_sanctions_provider() -> SimpleOpImpl<(), crate::UserTy, GetSanctionsProvider> {
|
||||
SimpleOpImpl::new(GetSanctionsProvider)
|
||||
}
|
||||
@@ -7,6 +7,8 @@ mod public_channels;
|
||||
mod join_channel;
|
||||
mod user_online;
|
||||
mod subscribed_channels;
|
||||
mod add_modify_sanction;
|
||||
mod list_sanctions;
|
||||
|
||||
use polariton_server::operations::OperationsHandler;
|
||||
|
||||
@@ -25,5 +27,7 @@ pub fn handler(chat_system: crate::state::chat::ChatImpl, conf: &rc_core::persis
|
||||
.add(send_message::send_private_message_handler(chat_system.clone()))
|
||||
.add(join_channel::leave_channel_provider(chat_system.clone()))
|
||||
.add(subscribed_channels::all_subbed_channels_provider())
|
||||
.add(add_modify_sanction::add_modify_sanction_provider())
|
||||
.add(list_sanctions::list_sanctions_provider())
|
||||
//.add(polariton_server::operations::Ack::<00000, _>::default())
|
||||
}
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
use polariton_server::operations::SimpleFunc;
|
||||
use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl};
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
const CODE: u8 = 15;
|
||||
|
||||
const PARAM_KEY: u8 = 28;
|
||||
|
||||
pub(super) fn pending_sanctions_checker() -> SimpleFunc<15, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
|
||||
SimpleFunc::new(|params, _| {
|
||||
pub(super) struct PendingSanctionsProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl <C: Send + 'static> SimpleOperation<C> for PendingSanctionsProvider {
|
||||
type User = crate::UserTy;
|
||||
const CODE: u8 = CODE;
|
||||
|
||||
async fn handle(&self, params: ParameterTable<C>, _user: &Self::User) -> Result<ParameterTable<C>, SimpleOpError> {
|
||||
let mut params = params.to_dict();
|
||||
params.insert(PARAM_KEY, Typed::Bool(false.into()));
|
||||
//let user_info = user.user()?;
|
||||
//params.insert(PARAM_KEY, Typed::Bool(user_info.has_pending_sanctions().await?));
|
||||
params.insert(PARAM_KEY, Typed::Bool(false));
|
||||
Ok(params.into())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn pending_sanctions_checker<C: Send + 'static>() -> SimpleOpImpl<C, crate::UserTy, PendingSanctionsProvider> {
|
||||
SimpleOpImpl::new(PendingSanctionsProvider)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user