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

@@ -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 {

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,
}
}
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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<Bot>;
/// Fetch a bot by its token
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot>;
/// 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<FieldsBot>) -> 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<Vec<Bot>>;
/// Get the number of bots owned by a user
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize>;
}

View File

@@ -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<Bot> {
db.fetch_bot(&self.id).await
}
/// Fetch invite from Ref
pub async fn as_invite(&self, db: &Database) -> Result<Invite> {
Invite::find(db, &self.id).await