mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 21:47:02 +00:00
refactor: combine models and db crate together
This commit is contained in:
5
crates/core/database/src/models/bots/mod.rs
Normal file
5
crates/core/database/src/models/bots/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod model;
|
||||
// mod ops;
|
||||
|
||||
pub use model::*;
|
||||
// pub use ops::*;
|
||||
74
crates/core/database/src/models/bots/model.rs
Normal file
74
crates/core/database/src/models/bots/model.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
26
crates/core/database/src/models/bots/ops.rs
Normal file
26
crates/core/database/src/models/bots/ops.rs
Normal 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>;
|
||||
}
|
||||
9
crates/core/database/src/models/bots/ops/mongodb.rs
Normal file
9
crates/core/database/src/models/bots/ops/mongodb.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use revolt_database::MongoDb;
|
||||
|
||||
use super::AbstractBots;
|
||||
|
||||
mod init;
|
||||
mod scripts;
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractBots for MongoDb {}
|
||||
6
crates/core/database/src/models/bots/ops/reference.rs
Normal file
6
crates/core/database/src/models/bots/ops/reference.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use revolt_database::DummyDb;
|
||||
|
||||
use super::AbstractBots;
|
||||
|
||||
#[async_trait]
|
||||
impl AbstractBots for DummyDb {}
|
||||
Reference in New Issue
Block a user