refactor: combine models and db crate together

This commit is contained in:
Paul Makles
2023-04-22 12:12:02 +01:00
parent f2bb388b3b
commit e43833c0ea
27 changed files with 306 additions and 147 deletions

View File

@@ -0,0 +1,5 @@
mod model;
// mod ops;
pub use model::*;
// pub use ops::*;

View File

@@ -0,0 +1,74 @@
use crate::Database;
auto_derived_partial!(
/// Bot
pub struct Bot {
/// Bot Id
///
/// This equals the associated bot user's id.
#[serde(rename = "_id")]
pub id: String,
/// User Id of the bot owner
pub owner: String,
/// Token used to authenticate requests for this bot
pub token: String,
/// Whether the bot is public
/// (may be invited by anyone)
pub public: bool,
/// Whether to enable analytics
#[serde(skip_serializing_if = "crate::if_false", default)]
pub analytics: bool,
/// Whether this bot should be publicly discoverable
#[serde(skip_serializing_if = "crate::if_false", default)]
pub discoverable: bool,
/// Reserved; URL for handling interactions
#[serde(skip_serializing_if = "Option::is_none")]
pub interactions_url: Option<String>,
/// URL for terms of service
#[serde(skip_serializing_if = "Option::is_none")]
pub terms_of_service_url: Option<String>,
/// URL for privacy policy
#[serde(skip_serializing_if = "Option::is_none")]
pub privacy_policy_url: Option<String>,
/// Enum of bot flags
#[serde(skip_serializing_if = "Option::is_none")]
pub flags: Option<i32>,
},
"PartialBot"
);
auto_derived!(
/// Flags that may be attributed to a bot
#[repr(i32)]
pub enum BotFlags {
Verified = 1,
Official = 2,
}
/// Optional fields on bot object
pub enum FieldsBot {
Token,
InteractionsURL,
}
);
impl Bot {
/// Remove a field from this object
pub fn remove(&mut self, field: &FieldsBot) {
match field {
FieldsBot::Token => self.token = nanoid::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
Ok(())
}
}

View File

@@ -0,0 +1,26 @@
mod dummy;
mod mongodb;
#[async_trait]
pub trait AbstractBots: 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

@@ -0,0 +1,9 @@
use revolt_database::MongoDb;
use super::AbstractBots;
mod init;
mod scripts;
#[async_trait]
impl AbstractBots for MongoDb {}

View File

@@ -0,0 +1,6 @@
use revolt_database::DummyDb;
use super::AbstractBots;
#[async_trait]
impl AbstractBots for DummyDb {}