From 11a87263be99fc785654aad810f15b2de0f162b5 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sat, 22 Apr 2023 17:55:30 +0100 Subject: [PATCH] feat(core/models): implement Bot and PublicBot --- crates/core/models/Cargo.toml | 15 +++++ crates/core/models/src/bots.rs | 103 +++++++++++++++++++++++++++++++++ crates/core/models/src/lib.rs | 26 +++++++++ 3 files changed, 144 insertions(+) create mode 100644 crates/core/models/src/bots.rs diff --git a/crates/core/models/Cargo.toml b/crates/core/models/Cargo.toml index 812e083f..5f6f64e3 100644 --- a/crates/core/models/Cargo.toml +++ b/crates/core/models/Cargo.toml @@ -5,4 +5,19 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +serde = [ "dep:serde" ] +schemas = [ "dep:schemars" ] +from_database = [ "revolt-database" ] + +default = [ "serde", "from_database" ] + [dependencies] +# Repo +revolt-database = { path = "../database", optional = true } + +# Serialisation +serde = { version = "1", features = ["derive"], optional = true } + +# Spec Generation +schemars = { version = "0.8.8", optional = true } diff --git a/crates/core/models/src/bots.rs b/crates/core/models/src/bots.rs new file mode 100644 index 00000000..7c201e68 --- /dev/null +++ b/crates/core/models/src/bots.rs @@ -0,0 +1,103 @@ +auto_derived!( + /// # Bot + pub struct Bot { + /// Bot Id + #[serde(rename = "_id")] + pub id: String, + + /// User Id of the bot owner + #[serde(rename = "owner")] + pub owner_id: 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 + #[cfg_attr( + feature = "serde", + serde(skip_serializing_if = "crate::if_false", default) + )] + pub analytics: bool, + /// Whether this bot should be publicly discoverable + #[cfg_attr( + feature = "serde", + serde(skip_serializing_if = "crate::if_false", default) + )] + pub discoverable: bool, + /// Reserved; URL for handling interactions + #[cfg_attr( + feature = "serde", + serde(skip_serializing_if = "String::is_empty", default) + )] + pub interactions_url: String, + /// URL for terms of service + #[cfg_attr( + feature = "serde", + serde(skip_serializing_if = "String::is_empty", default) + )] + pub terms_of_service_url: String, + /// URL for privacy policy + #[cfg_attr( + feature = "serde", + serde(skip_serializing_if = "String::is_empty", default) + )] + pub privacy_policy_url: String, + + /// Enum of bot flags + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] + pub flags: Option, + } + + /// # Public Bot + pub struct PublicBot { + /// Bot Id + #[serde(rename = "_id")] + id: String, + + /// Bot Username + username: String, + /// Profile Avatar + #[serde(skip_serializing_if = "String::is_empty")] + avatar: String, + /// Profile Description + #[serde(skip_serializing_if = "String::is_empty")] + description: String, + } +); + +#[cfg(feature = "from_database")] +impl PublicBot { + pub fn from( + bot: revolt_database::Bot, + username: String, + avatar: Option, + description: Option, + ) -> Self { + PublicBot { + id: bot.id, + username, + avatar: avatar.unwrap_or_default(), + description: description.unwrap_or_default(), + } + } +} + +#[cfg(feature = "from_database")] +impl From for Bot { + fn from(value: revolt_database::Bot) -> Self { + Bot { + id: value.id, + owner_id: value.owner, + token: value.token, + public: value.public, + analytics: value.analytics, + discoverable: value.discoverable, + interactions_url: value.interactions_url, + terms_of_service_url: value.terms_of_service_url, + privacy_policy_url: value.privacy_policy_url, + flags: value.flags, + } + } +} diff --git a/crates/core/models/src/lib.rs b/crates/core/models/src/lib.rs index 8b137891..21f199c8 100644 --- a/crates/core/models/src/lib.rs +++ b/crates/core/models/src/lib.rs @@ -1 +1,27 @@ +#[cfg(feature = "serde")] +#[macro_use] +extern crate serde; +#[cfg(feature = "schemas")] +#[macro_use] +extern crate schemars; + +macro_rules! auto_derived { + ( $( $item:item )+ ) => { + $( + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] + #[cfg_attr(feature = "schemas", derive(JsonSchema))] + #[derive(Debug, Clone, Eq, PartialEq)] + $item + )+ + }; +} + +mod bots; + +pub use bots::*; + +/// Utility function to check if a boolean value is false +pub fn if_false(t: &bool) -> bool { + !t +}