From bde432cb750a81b4b1444e509e4ab38d2f60aaf8 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Mon, 14 Aug 2023 10:14:17 +0100 Subject: [PATCH] chore: strip bot model from quark --- .../models/channel_unreads/ops/reference.rs | 2 +- crates/quark/src/impl/dummy/mod.rs | 1 - crates/quark/src/impl/dummy/users/bot.rs | 46 ------------- crates/quark/src/impl/generic/mod.rs | 1 - crates/quark/src/impl/generic/users/bot.rs | 24 ------- crates/quark/src/impl/generic/users/user.rs | 5 +- crates/quark/src/impl/mongo/mod.rs | 1 - crates/quark/src/impl/mongo/users/bot.rs | 68 ------------------- crates/quark/src/models/mod.rs | 2 - crates/quark/src/traits/mod.rs | 3 - crates/quark/src/traits/users/bot.rs | 26 ------- crates/quark/src/util/ref.rs | 7 +- 12 files changed, 6 insertions(+), 180 deletions(-) delete mode 100644 crates/quark/src/impl/dummy/users/bot.rs delete mode 100644 crates/quark/src/impl/generic/users/bot.rs delete mode 100644 crates/quark/src/impl/mongo/users/bot.rs delete mode 100644 crates/quark/src/traits/users/bot.rs diff --git a/crates/core/database/src/models/channel_unreads/ops/reference.rs b/crates/core/database/src/models/channel_unreads/ops/reference.rs index 4fb216ae..000025ab 100644 --- a/crates/core/database/src/models/channel_unreads/ops/reference.rs +++ b/crates/core/database/src/models/channel_unreads/ops/reference.rs @@ -20,7 +20,7 @@ impl AbstractChannelUnreads for ReferenceDb { user: user_id.to_string(), }; - if let Some(mut unread) = unreads.get_mut(&key) { + if let Some(unread) = unreads.get_mut(&key) { unread.mentions = None; unread.last_id.replace(message_id.to_string()); } else { diff --git a/crates/quark/src/impl/dummy/mod.rs b/crates/quark/src/impl/dummy/mod.rs index 194f2edb..cd72470d 100644 --- a/crates/quark/src/impl/dummy/mod.rs +++ b/crates/quark/src/impl/dummy/mod.rs @@ -23,7 +23,6 @@ pub mod servers { } pub mod users { - pub mod bot; pub mod user; pub mod user_settings; } diff --git a/crates/quark/src/impl/dummy/users/bot.rs b/crates/quark/src/impl/dummy/users/bot.rs deleted file mode 100644 index e2e36c7e..00000000 --- a/crates/quark/src/impl/dummy/users/bot.rs +++ /dev/null @@ -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 { - 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 { - 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) -> 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> { - Ok(vec![self.fetch_bot(user_id).await.unwrap()]) - } - - async fn get_number_of_bots_by_user(&self, _user_id: &str) -> Result { - Ok(1) - } -} diff --git a/crates/quark/src/impl/generic/mod.rs b/crates/quark/src/impl/generic/mod.rs index b050cae5..cd4d13b3 100644 --- a/crates/quark/src/impl/generic/mod.rs +++ b/crates/quark/src/impl/generic/mod.rs @@ -19,7 +19,6 @@ pub mod servers { } pub mod users { - pub mod bot; pub mod user; pub mod user_settings; } diff --git a/crates/quark/src/impl/generic/users/bot.rs b/crates/quark/src/impl/generic/users/bot.rs deleted file mode 100644 index 2a327735..00000000 --- a/crates/quark/src/impl/generic/users/bot.rs +++ /dev/null @@ -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 - } -} diff --git a/crates/quark/src/impl/generic/users/user.rs b/crates/quark/src/impl/generic/users/user.rs index 38609b0f..8fc5a4eb 100644 --- a/crates/quark/src/impl/generic/users/user.rs +++ b/crates/quark/src/impl/generic/users/user.rs @@ -469,7 +469,10 @@ impl User { #[async_recursion] pub async fn from_token(db: &Database, token: &str, hint: UserHint) -> Result { 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 { diff --git a/crates/quark/src/impl/mongo/mod.rs b/crates/quark/src/impl/mongo/mod.rs index 7711ab0f..b04cd9f2 100644 --- a/crates/quark/src/impl/mongo/mod.rs +++ b/crates/quark/src/impl/mongo/mod.rs @@ -34,7 +34,6 @@ pub mod servers { } pub mod users { - pub mod bot; pub mod user; pub mod user_settings; } diff --git a/crates/quark/src/impl/mongo/users/bot.rs b/crates/quark/src/impl/mongo/users/bot.rs deleted file mode 100644 index 7dc5ff85..00000000 --- a/crates/quark/src/impl/mongo/users/bot.rs +++ /dev/null @@ -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 { - self.find_one_by_id(COL, id).await - } - - async fn fetch_bot_by_token(&self, token: &str) -> Result { - 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) -> 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> { - self.find( - COL, - doc! { - "owner": user_id - }, - ) - .await - } - - async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result { - // ! 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, - } - } -} diff --git a/crates/quark/src/models/mod.rs b/crates/quark/src/models/mod.rs index 370c72a1..4bd26af1 100644 --- a/crates/quark/src/models/mod.rs +++ b/crates/quark/src/models/mod.rs @@ -22,7 +22,6 @@ mod servers { } mod users { - pub mod bot; pub mod user; pub mod user_settings; } @@ -40,7 +39,6 @@ pub use servers::*; pub use users::*; pub use attachment::File; -pub use bot::Bot; pub use channel::Channel; pub use channel_invite::Invite; pub use channel_unread::ChannelUnread; diff --git a/crates/quark/src/traits/mod.rs b/crates/quark/src/traits/mod.rs index 341565a1..bda97615 100644 --- a/crates/quark/src/traits/mod.rs +++ b/crates/quark/src/traits/mod.rs @@ -21,7 +21,6 @@ mod servers { } mod users { - pub mod bot; pub mod user; pub mod user_settings; } @@ -45,7 +44,6 @@ pub use servers::server::AbstractServer; pub use servers::server_ban::AbstractServerBan; pub use servers::server_member::AbstractServerMember; -pub use users::bot::AbstractBot; pub use users::user::AbstractUser; pub use users::user_settings::AbstractUserSettings; @@ -65,7 +63,6 @@ pub trait AbstractDatabase: + AbstractServer + AbstractServerBan + AbstractServerMember - + AbstractBot + AbstractUser + AbstractUserSettings + AbstractReport diff --git a/crates/quark/src/traits/users/bot.rs b/crates/quark/src/traits/users/bot.rs deleted file mode 100644 index e9421580..00000000 --- a/crates/quark/src/traits/users/bot.rs +++ /dev/null @@ -1,26 +0,0 @@ -use crate::models::bot::{Bot, FieldsBot, PartialBot}; -use crate::Result; - -#[async_trait] -pub trait AbstractBot: Sync + Send { - /// Fetch a bot by its id - async fn fetch_bot(&self, id: &str) -> Result; - - /// Fetch a bot by its token - async fn fetch_bot_by_token(&self, token: &str) -> Result; - - /// Insert new bot into the database - async fn insert_bot(&self, bot: &Bot) -> Result<()>; - - /// Update bot with new information - async fn update_bot(&self, id: &str, bot: &PartialBot, remove: Vec) -> Result<()>; - - /// Delete a bot from the database - async fn delete_bot(&self, id: &str) -> Result<()>; - - /// Fetch bots owned by a user - async fn fetch_bots_by_user(&self, user_id: &str) -> Result>; - - /// Get the number of bots owned by a user - async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result; -} diff --git a/crates/quark/src/util/ref.rs b/crates/quark/src/util/ref.rs index 1b3ff148..110ad501 100644 --- a/crates/quark/src/util/ref.rs +++ b/crates/quark/src/util/ref.rs @@ -6,7 +6,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::models::{ - Bot, Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User, + Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User, }; use crate::{Database, Error, Result}; @@ -56,11 +56,6 @@ impl Ref { Ok(message) } - /// Fetch bot from Ref - pub async fn as_bot(&self, db: &Database) -> Result { - db.fetch_bot(&self.id).await - } - /// Fetch invite from Ref pub async fn as_invite(&self, db: &Database) -> Result { Invite::find(db, &self.id).await