forked from jmug/stoatchat
feat: account strike CRUD API
This commit is contained in:
@@ -2,6 +2,11 @@ disallowed-methods = [
|
|||||||
# Shouldn't need to access these directly
|
# Shouldn't need to access these directly
|
||||||
"revolt_database::models::bots::model::Bot::remove_field",
|
"revolt_database::models::bots::model::Bot::remove_field",
|
||||||
|
|
||||||
# Prefer to use Object::delete()
|
# Prefer to use Object::update()
|
||||||
"revolt_database::models::bots::ops::AbstractBots::update_bot",
|
"revolt_database::models::bots::ops::AbstractBots::update_bot",
|
||||||
|
"revolt_database::models::safety_strikes::ops::AbstractAccountStrikes::update_account_strike",
|
||||||
|
|
||||||
|
# Prefer to use Object::delete()
|
||||||
|
"revolt_database::models::bots::ops::AbstractBots::delete_bot",
|
||||||
|
"revolt_database::models::safety_strikes::ops::AbstractAccountStrikes::delete_account_strike",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ auto_derived_partial!(
|
|||||||
"PartialAccountStrike"
|
"PartialAccountStrike"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[allow(clippy::disallowed_methods)]
|
||||||
impl AccountStrike {
|
impl AccountStrike {
|
||||||
/// Update this strike
|
/// Update this strike
|
||||||
pub async fn update(&mut self, db: &Database, partial: PartialAccountStrike) -> Result<()> {
|
pub async fn update(&mut self, db: &Database, partial: PartialAccountStrike) -> Result<()> {
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ pub trait AbstractAccountStrikes: Sync + Send {
|
|||||||
/// Insert new strike into the database
|
/// Insert new strike into the database
|
||||||
async fn insert_account_strike(&self, strike: &AccountStrike) -> Result<()>;
|
async fn insert_account_strike(&self, strike: &AccountStrike) -> Result<()>;
|
||||||
|
|
||||||
|
/// Fetch strike by id
|
||||||
|
async fn fetch_account_strike(&self, id: &str) -> Result<AccountStrike>;
|
||||||
|
|
||||||
/// Fetch strikes by user id
|
/// Fetch strikes by user id
|
||||||
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>>;
|
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>>;
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,11 @@ impl AbstractAccountStrikes for MongoDb {
|
|||||||
query!(self, insert_one, COL, &strike).map(|_| ())
|
query!(self, insert_one, COL, &strike).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch strike by id
|
||||||
|
async fn fetch_account_strike(&self, id: &str) -> Result<AccountStrike> {
|
||||||
|
query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound))
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch strikes by user id
|
/// Fetch strikes by user id
|
||||||
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>> {
|
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
|
|||||||
@@ -18,6 +18,15 @@ impl AbstractAccountStrikes for ReferenceDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch strike by id
|
||||||
|
async fn fetch_account_strike(&self, id: &str) -> Result<AccountStrike> {
|
||||||
|
let strikes = self.account_strikes.lock().await;
|
||||||
|
strikes
|
||||||
|
.get(id)
|
||||||
|
.cloned()
|
||||||
|
.ok_or_else(|| create_error!(NotFound))
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch strikes by user id
|
/// Fetch strikes by user id
|
||||||
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>> {
|
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>> {
|
||||||
let strikes = self.account_strikes.lock().await;
|
let strikes = self.account_strikes.lock().await;
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ auto_derived!(
|
|||||||
/// Attached reason
|
/// Attached reason
|
||||||
pub reason: String,
|
pub reason: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Strike Data
|
||||||
|
pub struct DataEditAccountStrike {
|
||||||
|
/// New attached reason
|
||||||
|
pub reason: String,
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg(feature = "from_database")]
|
#[cfg(feature = "from_database")]
|
||||||
|
|||||||
23
crates/delta/src/routes/safety/delete_strike.rs
Normal file
23
crates/delta/src/routes/safety/delete_strike.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use revolt_database::Database;
|
||||||
|
use revolt_quark::models::User;
|
||||||
|
use revolt_quark::{Error, Result};
|
||||||
|
use rocket::State;
|
||||||
|
|
||||||
|
/// # Delete Strike
|
||||||
|
///
|
||||||
|
/// Delete a strike by its ID
|
||||||
|
#[openapi(tag = "User Safety")]
|
||||||
|
#[delete("/strikes/<strike_id>")]
|
||||||
|
pub async fn delete_strike(db: &State<Database>, user: User, strike_id: String) -> Result<()> {
|
||||||
|
// Must be privileged for this route
|
||||||
|
if !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
let strike = db
|
||||||
|
.fetch_account_strike(&strike_id)
|
||||||
|
.await
|
||||||
|
.map_err(Error::from_core)?;
|
||||||
|
|
||||||
|
strike.delete(db).await.map_err(Error::from_core)
|
||||||
|
}
|
||||||
39
crates/delta/src/routes/safety/edit_strike.rs
Normal file
39
crates/delta/src/routes/safety/edit_strike.rs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
use revolt_database::{Database, PartialAccountStrike};
|
||||||
|
use revolt_models::v0::DataEditAccountStrike;
|
||||||
|
use revolt_quark::models::User;
|
||||||
|
use revolt_quark::{Error, Result};
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
use rocket::State;
|
||||||
|
|
||||||
|
/// # Edit Strike
|
||||||
|
///
|
||||||
|
/// Edit a strike by its ID
|
||||||
|
#[openapi(tag = "User Safety")]
|
||||||
|
#[post("/strikes/<strike_id>", data = "<data>")]
|
||||||
|
pub async fn edit_strike(
|
||||||
|
db: &State<Database>,
|
||||||
|
user: User,
|
||||||
|
strike_id: String,
|
||||||
|
data: Json<DataEditAccountStrike>,
|
||||||
|
) -> Result<()> {
|
||||||
|
// Must be privileged for this route
|
||||||
|
if !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut strike = db
|
||||||
|
.fetch_account_strike(&strike_id)
|
||||||
|
.await
|
||||||
|
.map_err(Error::from_core)?;
|
||||||
|
|
||||||
|
strike
|
||||||
|
.update(
|
||||||
|
db,
|
||||||
|
PartialAccountStrike {
|
||||||
|
reason: Some(data.0.reason),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(Error::from_core)
|
||||||
|
}
|
||||||
28
crates/delta/src/routes/safety/fetch_strikes.rs
Normal file
28
crates/delta/src/routes/safety/fetch_strikes.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
use revolt_database::Database;
|
||||||
|
use revolt_models::v0::AccountStrike;
|
||||||
|
use revolt_quark::models::User;
|
||||||
|
use revolt_quark::{Error, Result};
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
use rocket::State;
|
||||||
|
|
||||||
|
/// # Fetch Strikes
|
||||||
|
///
|
||||||
|
/// Fetch strikes for a user by their ID
|
||||||
|
#[openapi(tag = "User Safety")]
|
||||||
|
#[get("/strikes/<user_id>")]
|
||||||
|
pub async fn fetch_strikes(
|
||||||
|
db: &State<Database>,
|
||||||
|
user: User,
|
||||||
|
user_id: String,
|
||||||
|
) -> Result<Json<Vec<AccountStrike>>> {
|
||||||
|
// Must be privileged for this route
|
||||||
|
if !user.privileged {
|
||||||
|
return Err(Error::NotPrivileged);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.fetch_account_strikes_by_user(&user_id)
|
||||||
|
.await
|
||||||
|
.map(|v| v.into_iter().map(|e| e.into()).collect())
|
||||||
|
.map(Json)
|
||||||
|
.map_err(Error::from_core)
|
||||||
|
}
|
||||||
@@ -8,6 +8,10 @@ mod report_content;
|
|||||||
|
|
||||||
mod fetch_snapshots;
|
mod fetch_snapshots;
|
||||||
|
|
||||||
|
mod delete_strike;
|
||||||
|
mod edit_strike;
|
||||||
|
mod fetch_strikes;
|
||||||
|
|
||||||
pub fn routes() -> (Vec<Route>, OpenApi) {
|
pub fn routes() -> (Vec<Route>, OpenApi) {
|
||||||
openapi_get_routes_spec![
|
openapi_get_routes_spec![
|
||||||
// Reports
|
// Reports
|
||||||
@@ -16,6 +20,10 @@ pub fn routes() -> (Vec<Route>, OpenApi) {
|
|||||||
fetch_reports::fetch_reports,
|
fetch_reports::fetch_reports,
|
||||||
report_content::report_content,
|
report_content::report_content,
|
||||||
// Snapshots
|
// Snapshots
|
||||||
fetch_snapshots::fetch_snapshots
|
fetch_snapshots::fetch_snapshots,
|
||||||
|
// Strikes
|
||||||
|
fetch_strikes::fetch_strikes,
|
||||||
|
edit_strike::edit_strike,
|
||||||
|
delete_strike::delete_strike
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user