chore: strip bot model from quark

This commit is contained in:
Paul Makles
2023-08-14 10:14:17 +01:00
parent 2fb9e7a802
commit bde432cb75
12 changed files with 6 additions and 180 deletions

View File

@@ -23,7 +23,6 @@ pub mod servers {
}
pub mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}

View File

@@ -1,46 +0,0 @@
use crate::models::bot::{Bot, FieldsBot, PartialBot};
use crate::{AbstractBot, Result};
use super::super::DummyDb;
#[async_trait]
impl AbstractBot for DummyDb {
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
Ok(Bot {
id: id.into(),
owner: "user".into(),
token: "token".into(),
public: true,
analytics: true,
discoverable: true,
..Default::default()
})
}
async fn fetch_bot_by_token(&self, _token: &str) -> Result<Bot> {
self.fetch_bot("bot").await
}
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
info!("Insert {bot:?}");
Ok(())
}
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()> {
info!("Update {id} with {bot:?} and remove {remove:?}");
Ok(())
}
async fn delete_bot(&self, id: &str) -> Result<()> {
info!("Delete {id}");
Ok(())
}
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
Ok(vec![self.fetch_bot(user_id).await.unwrap()])
}
async fn get_number_of_bots_by_user(&self, _user_id: &str) -> Result<usize> {
Ok(1)
}
}

View File

@@ -19,7 +19,6 @@ pub mod servers {
}
pub mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}

View File

@@ -1,24 +0,0 @@
use nanoid::nanoid;
use crate::{
models::{bot::FieldsBot, Bot},
Database, Result,
};
impl Bot {
/// Remove a field from this object
pub fn remove(&mut self, field: &FieldsBot) {
match field {
FieldsBot::Token => self.token = nanoid!(64),
FieldsBot::InteractionsURL => {
self.interactions_url.take();
}
}
}
/// Delete this bot
pub async fn delete(&self, db: &Database) -> Result<()> {
db.fetch_user(&self.id).await?.mark_deleted(db).await?;
db.delete_bot(&self.id).await
}
}

View File

@@ -469,7 +469,10 @@ impl User {
#[async_recursion]
pub async fn from_token(db: &Database, token: &str, hint: UserHint) -> Result<User> {
match hint {
UserHint::Bot => db.fetch_user(&db.fetch_bot_by_token(token).await?.id).await,
UserHint::Bot => {
let rvdb: revolt_database::Database = db.clone().into();
db.fetch_user(&rvdb.fetch_bot_by_token(token).await.map_err(|_| Error::InternalError)?.id).await
},
UserHint::User => db.fetch_user_by_token(token).await,
UserHint::Any => {
if let Ok(user) = User::from_token(db, token, UserHint::User).await {

View File

@@ -34,7 +34,6 @@ pub mod servers {
}
pub mod users {
pub mod bot;
pub mod user;
pub mod user_settings;
}

View File

@@ -1,68 +0,0 @@
use crate::models::bot::{Bot, FieldsBot, PartialBot};
use crate::r#impl::mongo::IntoDocumentPath;
use crate::{AbstractBot, Result};
use super::super::MongoDb;
static COL: &str = "bots";
#[async_trait]
impl AbstractBot for MongoDb {
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
self.find_one_by_id(COL, id).await
}
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot> {
self.find_one(
COL,
doc! {
"token": token
},
)
.await
}
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
self.insert_one(COL, &bot).await.map(|_| ())
}
async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec<FieldsBot>) -> Result<()> {
self.update_one_by_id(
COL,
id,
bot,
remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
None,
)
.await
.map(|_| ())
}
async fn delete_bot(&self, id: &str) -> Result<()> {
self.delete_one_by_id(COL, id).await.map(|_| ())
}
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
self.find(
COL,
doc! {
"owner": user_id
},
)
.await
}
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize> {
// ! FIXME: move this to generic?
self.fetch_bots_by_user(user_id).await.map(|x| x.len())
}
}
impl IntoDocumentPath for FieldsBot {
fn as_path(&self) -> Option<&'static str> {
match self {
FieldsBot::InteractionsURL => Some("interactions_url"),
FieldsBot::Token => None,
}
}
}