mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 21:47:02 +00:00
chore: strip legacy admin API
This commit is contained in:
@@ -2,16 +2,12 @@ use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use futures::lock::Mutex;
|
||||
|
||||
use crate::{
|
||||
AccountStrike, Bot, Channel, File, Member, MemberCompositeKey, Server, User, UserSettings,
|
||||
Webhook,
|
||||
};
|
||||
use crate::{Bot, Channel, File, Member, MemberCompositeKey, Server, User, UserSettings, Webhook};
|
||||
|
||||
database_derived!(
|
||||
/// Reference implementation
|
||||
#[derive(Default)]
|
||||
pub struct ReferenceDb {
|
||||
pub account_strikes: Arc<Mutex<HashMap<String, AccountStrike>>>,
|
||||
pub bots: Arc<Mutex<HashMap<String, Bot>>>,
|
||||
pub channel_webhooks: Arc<Mutex<HashMap<String, Webhook>>>,
|
||||
pub user_settings: Arc<Mutex<HashMap<String, UserSettings>>>,
|
||||
|
||||
@@ -697,7 +697,9 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
|
||||
}
|
||||
|
||||
if revision <= 19 {
|
||||
info!("Running migration [revision 19 / 27-02-2023]: Create report / snapshot collections, migrate to new model if applicable.");
|
||||
info!(
|
||||
"Running migration [revision 19 / 27-02-2023]: Create report / snapshot collections."
|
||||
);
|
||||
|
||||
db.db()
|
||||
.create_collection("safety_reports", None)
|
||||
@@ -708,19 +710,6 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
|
||||
.create_collection("safety_snapshots", None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
db.col::<Document>("safety_reports")
|
||||
.update_many(
|
||||
doc! {},
|
||||
doc! {
|
||||
"$set": {
|
||||
"status": "Created"
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if revision <= 20 {
|
||||
|
||||
@@ -4,7 +4,6 @@ mod channel_webhooks;
|
||||
mod channels;
|
||||
mod files;
|
||||
mod ratelimit_events;
|
||||
mod safety_strikes;
|
||||
mod server_members;
|
||||
mod servers;
|
||||
mod user_settings;
|
||||
@@ -16,7 +15,6 @@ pub use channel_webhooks::*;
|
||||
pub use channels::*;
|
||||
pub use files::*;
|
||||
pub use ratelimit_events::*;
|
||||
pub use safety_strikes::*;
|
||||
pub use server_members::*;
|
||||
pub use servers::*;
|
||||
pub use user_settings::*;
|
||||
@@ -33,7 +31,6 @@ pub trait AbstractDatabase:
|
||||
+ channel_webhooks::AbstractWebhooks
|
||||
+ files::AbstractAttachments
|
||||
+ ratelimit_events::AbstractRatelimitEvents
|
||||
+ safety_strikes::AbstractAccountStrikes
|
||||
+ server_members::AbstractServerMembers
|
||||
+ servers::AbstractServers
|
||||
+ user_settings::AbstractUserSettings
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
mod model;
|
||||
mod ops;
|
||||
|
||||
pub use model::*;
|
||||
pub use ops::*;
|
||||
@@ -1,124 +0,0 @@
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::Database;
|
||||
|
||||
auto_derived_partial!(
|
||||
/// Account Strike
|
||||
pub struct AccountStrike {
|
||||
/// Strike Id
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
/// Id of reported user
|
||||
pub user_id: String,
|
||||
/// Id of moderator
|
||||
pub moderator_id: String,
|
||||
|
||||
/// Attached reason
|
||||
pub reason: String,
|
||||
},
|
||||
"PartialAccountStrike"
|
||||
);
|
||||
|
||||
#[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?;
|
||||
self.apply_options(partial);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete this strike
|
||||
pub async fn delete(&self, db: &Database) -> Result<()> {
|
||||
db.delete_account_strike(&self.id).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::{AccountStrike, PartialAccountStrike};
|
||||
|
||||
#[async_std::test]
|
||||
async fn crud() {
|
||||
database_test!(|db| async move {
|
||||
let user_id = "user";
|
||||
|
||||
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
|
||||
.update(
|
||||
&db,
|
||||
PartialAccountStrike {
|
||||
reason: Some("new reason".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let strike2 = AccountStrike::create(
|
||||
&db,
|
||||
user_id.to_string(),
|
||||
"reason 2".to_string(),
|
||||
"moderator_id".to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let strikes = db.fetch_account_strikes_by_user(user_id).await.unwrap();
|
||||
|
||||
let ids = strikes
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|strike| strike.id)
|
||||
.collect::<HashSet<String>>();
|
||||
|
||||
assert!(ids.contains(&strike.id));
|
||||
assert!(ids.contains(&strike2.id));
|
||||
|
||||
let fetched_strike = strikes
|
||||
.into_iter()
|
||||
.find(|entry| entry.id == strike.id)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(fetched_strike, updated_strike);
|
||||
assert_ne!(fetched_strike, strike);
|
||||
|
||||
strike.delete(&db).await.unwrap();
|
||||
assert_eq!(
|
||||
1,
|
||||
db.fetch_account_strikes_by_user(user_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.len()
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::{AccountStrike, PartialAccountStrike};
|
||||
|
||||
mod mongodb;
|
||||
mod reference;
|
||||
|
||||
#[async_trait]
|
||||
pub trait AbstractAccountStrikes: Sync + Send {
|
||||
/// Insert new strike into the database
|
||||
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
|
||||
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>>;
|
||||
|
||||
/// Update strike with new information
|
||||
async fn update_account_strike(&self, id: &str, partial: &PartialAccountStrike) -> Result<()>;
|
||||
|
||||
/// Delete a strike from the database
|
||||
async fn delete_account_strike(&self, id: &str) -> Result<()>;
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
use futures::StreamExt;
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::MongoDb;
|
||||
use crate::{AccountStrike, PartialAccountStrike};
|
||||
|
||||
use super::AbstractAccountStrikes;
|
||||
|
||||
static COL: &str = "safety_strikes";
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractAccountStrikes for MongoDb {
|
||||
/// Insert new strike into the database
|
||||
async fn insert_account_strike(&self, strike: &AccountStrike) -> Result<()> {
|
||||
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
|
||||
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>> {
|
||||
Ok(self
|
||||
.col::<AccountStrike>(COL)
|
||||
.find(
|
||||
doc! {
|
||||
"user_id": user_id,
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| create_database_error!("find", COL))?
|
||||
.filter_map(|s| async {
|
||||
if cfg!(debug_assertions) {
|
||||
Some(s.unwrap())
|
||||
} else {
|
||||
s.ok()
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
.await)
|
||||
}
|
||||
|
||||
/// Update strike with new information
|
||||
async fn update_account_strike(&self, id: &str, partial: &PartialAccountStrike) -> Result<()> {
|
||||
query!(self, update_one_by_id, COL, id, partial, vec![], None).map(|_| ())
|
||||
}
|
||||
|
||||
/// Delete a strike from the database
|
||||
async fn delete_account_strike(&self, id: &str) -> Result<()> {
|
||||
query!(self, delete_one_by_id, COL, id).map(|_| ())
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::ReferenceDb;
|
||||
use crate::{AccountStrike, PartialAccountStrike};
|
||||
|
||||
use super::AbstractAccountStrikes;
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractAccountStrikes for ReferenceDb {
|
||||
/// Insert new strike into the database
|
||||
async fn insert_account_strike(&self, strike: &AccountStrike) -> Result<()> {
|
||||
let mut strikes = self.account_strikes.lock().await;
|
||||
if strikes.contains_key(&strike.id) {
|
||||
Err(create_database_error!("insert", "strike"))
|
||||
} else {
|
||||
strikes.insert(strike.id.to_string(), strike.clone());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
async fn fetch_account_strikes_by_user(&self, user_id: &str) -> Result<Vec<AccountStrike>> {
|
||||
let strikes = self.account_strikes.lock().await;
|
||||
Ok(strikes
|
||||
.values()
|
||||
.filter(|strike| strike.user_id == user_id)
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Update strike with new information
|
||||
async fn update_account_strike(&self, id: &str, partial: &PartialAccountStrike) -> Result<()> {
|
||||
let mut strikes = self.account_strikes.lock().await;
|
||||
if let Some(strike) = strikes.get_mut(id) {
|
||||
strike.apply_options(partial.clone());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a strike from the database
|
||||
async fn delete_account_strike(&self, id: &str) -> Result<()> {
|
||||
let mut strikes = self.account_strikes.lock().await;
|
||||
if strikes.remove(id).is_some() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(create_error!(NotFound))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,5 @@
|
||||
use revolt_models::v0::*;
|
||||
|
||||
impl From<crate::AccountStrike> for AccountStrike {
|
||||
fn from(value: crate::AccountStrike) -> Self {
|
||||
AccountStrike {
|
||||
id: value.id,
|
||||
user_id: value.user_id,
|
||||
reason: value.reason,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Bot {
|
||||
pub fn into_public_bot(self, user: crate::User) -> PublicBot {
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -52,7 +42,7 @@ impl From<crate::Webhook> for Webhook {
|
||||
avatar: value.avatar.map(|file| file.into()),
|
||||
channel_id: value.channel_id,
|
||||
token: value.token,
|
||||
permissions: value.permissions
|
||||
permissions: value.permissions,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,7 +55,7 @@ impl From<crate::PartialWebhook> for PartialWebhook {
|
||||
avatar: value.avatar.map(|file| file.into()),
|
||||
channel_id: value.channel_id,
|
||||
token: value.token,
|
||||
permissions: value.permissions
|
||||
permissions: value.permissions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user