forked from jmug/stoatchat
feat: implement creating strikes
feat: add moderator_id to strikes fix: use correct collection for strikes
This commit is contained in:
@@ -16,7 +16,7 @@ struct MigrationInfo {
|
||||
revision: i32,
|
||||
}
|
||||
|
||||
pub const LATEST_REVISION: i32 = 22;
|
||||
pub const LATEST_REVISION: i32 = 23;
|
||||
|
||||
pub async fn migrate_database(db: &MongoDb) {
|
||||
let migrations = db.col::<Document>("migrations");
|
||||
@@ -751,6 +751,23 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if revision <= 22 {
|
||||
info!("Running migration [revision 22 / 31-05-2023]: Add moderator_id to account strikes.");
|
||||
|
||||
db.col::<Document>("safety_strikes")
|
||||
.update_many(
|
||||
doc! {},
|
||||
doc! {
|
||||
"$set": {
|
||||
"moderator_id": "01EX2NCWQ0CHS3QJF0FEQS1GR4"
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to update server members.");
|
||||
}
|
||||
|
||||
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
|
||||
|
||||
// Reminder to update LATEST_REVISION when adding new migrations.
|
||||
|
||||
@@ -8,8 +8,10 @@ auto_derived_partial!(
|
||||
/// Strike Id
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
/// User Id of reported user
|
||||
/// Id of reported user
|
||||
pub user_id: String,
|
||||
/// Id of moderator
|
||||
pub moderator_id: String,
|
||||
|
||||
/// Attached reason
|
||||
pub reason: String,
|
||||
@@ -19,6 +21,23 @@ auto_derived_partial!(
|
||||
|
||||
#[allow(clippy::disallowed_methods)]
|
||||
impl AccountStrike {
|
||||
pub async fn create(
|
||||
db: &Database,
|
||||
user_id: String,
|
||||
reason: String,
|
||||
moderator_id: String,
|
||||
) -> Result<AccountStrike> {
|
||||
let strike = AccountStrike {
|
||||
id: ulid::Ulid::new().to_string(),
|
||||
user_id,
|
||||
moderator_id,
|
||||
reason,
|
||||
};
|
||||
|
||||
db.insert_account_strike(&strike).await?;
|
||||
Ok(strike)
|
||||
}
|
||||
|
||||
/// Update this strike
|
||||
pub async fn update(&mut self, db: &Database, partial: PartialAccountStrike) -> Result<()> {
|
||||
db.update_account_strike(&self.id, &partial).await?;
|
||||
@@ -42,16 +61,15 @@ mod tests {
|
||||
async fn crud() {
|
||||
database_test!(|db| async move {
|
||||
let user_id = "user";
|
||||
let strike_a = "a";
|
||||
let strike_b = "b";
|
||||
|
||||
let strike = AccountStrike {
|
||||
id: strike_a.to_string(),
|
||||
user_id: user_id.to_string(),
|
||||
reason: "reason 1".to_string(),
|
||||
};
|
||||
|
||||
db.insert_account_strike(&strike).await.unwrap();
|
||||
let strike = AccountStrike::create(
|
||||
&db,
|
||||
user_id.to_string(),
|
||||
"reason 1".to_string(),
|
||||
"moderator_id".to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut updated_strike = strike.clone();
|
||||
updated_strike
|
||||
@@ -65,11 +83,12 @@ mod tests {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
db.insert_account_strike(&AccountStrike {
|
||||
id: strike_b.to_string(),
|
||||
user_id: user_id.to_string(),
|
||||
reason: "reason 2".to_string(),
|
||||
})
|
||||
let strike2 = AccountStrike::create(
|
||||
&db,
|
||||
user_id.to_string(),
|
||||
"reason 2".to_string(),
|
||||
"moderator_id".to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -81,12 +100,12 @@ mod tests {
|
||||
.map(|strike| strike.id)
|
||||
.collect::<HashSet<String>>();
|
||||
|
||||
assert!(ids.contains(strike_a));
|
||||
assert!(ids.contains(strike_b));
|
||||
assert!(ids.contains(&strike.id));
|
||||
assert!(ids.contains(&strike2.id));
|
||||
|
||||
let fetched_strike = strikes
|
||||
.into_iter()
|
||||
.find(|strike| strike.id == strike_a)
|
||||
.find(|strike| strike.id == strike.id)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(fetched_strike, updated_strike);
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{AccountStrike, PartialAccountStrike};
|
||||
|
||||
use super::AbstractAccountStrikes;
|
||||
|
||||
static COL: &str = "bots";
|
||||
static COL: &str = "safety_strikes";
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractAccountStrikes for MongoDb {
|
||||
|
||||
Reference in New Issue
Block a user