From cd2557c1c33e5b7eb5f84a8c56293ea083c1a0d9 Mon Sep 17 00:00:00 2001 From: Timur Gordon <31495328+timurgordon@users.noreply.github.com> Date: Tue, 24 Jun 2025 19:23:27 +0200 Subject: [PATCH] move dsl's to rhailib wip --- .../src/models/library => _archive}/rhai.rs | 2 +- heromodels/Cargo.toml | 2 +- heromodels/src/models/access/access.rs | 19 +++-- heromodels/src/models/library/items.rs | 78 +++++++------------ heromodels/src/models/library/mod.rs | 2 - heromodels/src/models/log/README.md | 3 + heromodels/src/models/log/log.rs | 60 ++++++++++++++ heromodels/src/models/log/mod.rs | 5 ++ heromodels/src/models/mod.rs | 3 +- heromodels/src/models/object/README.md | 3 + heromodels/src/models/object/mod.rs | 5 ++ heromodels/src/models/object/object.rs | 44 +++++++++++ 12 files changed, 161 insertions(+), 65 deletions(-) rename {heromodels/src/models/library => _archive}/rhai.rs (99%) create mode 100644 heromodels/src/models/log/README.md create mode 100644 heromodels/src/models/log/log.rs create mode 100644 heromodels/src/models/log/mod.rs create mode 100644 heromodels/src/models/object/README.md create mode 100644 heromodels/src/models/object/mod.rs create mode 100644 heromodels/src/models/object/object.rs diff --git a/heromodels/src/models/library/rhai.rs b/_archive/rhai.rs similarity index 99% rename from heromodels/src/models/library/rhai.rs rename to _archive/rhai.rs index 57a606a..e33d8a4 100644 --- a/heromodels/src/models/library/rhai.rs +++ b/_archive/rhai.rs @@ -655,7 +655,7 @@ mod rhai_library_module { } } -pub fn register_library_rhai_module(engine: &mut Engine, db: Arc) { +pub fn register_library_rhai_module(engine: &mut Engine) { let module = exported_module!(rhai_library_module); engine.register_global_module(module.into()); diff --git a/heromodels/Cargo.toml b/heromodels/Cargo.toml index 87b0ced..4cd485a 100644 --- a/heromodels/Cargo.toml +++ b/heromodels/Cargo.toml @@ -86,4 +86,4 @@ required-features = ["rhai"] [[example]] name = "rhai_auth_example" path = "examples/rhai_auth_example.rs" -required-features = ["rhai", "authorization"] +required-features = ["rhai", "macros"] diff --git a/heromodels/src/models/access/access.rs b/heromodels/src/models/access/access.rs index b5b8050..5aede48 100644 --- a/heromodels/src/models/access/access.rs +++ b/heromodels/src/models/access/access.rs @@ -87,16 +87,16 @@ impl Access { pub fn can_access_resource( db: Arc, public_key: &str, - _resource_id_to_check: u32, - _resource_type_to_check: &str, + object_id: u32, + _object_type: &str, ) -> bool { - // Query for Access records matching the public key. - // Note: This fetches all access records for the user. For performance with many records, - // consider a more specific query if your DB supports it, or caching. + println!("Checking access for public key: {}", public_key); + + // get all access records for object let access_records = match db .collection::() .expect("Failed to get Access collection") - .get::(public_key) + .get::(&object_id) { Ok(records) => records, Err(_e) => { @@ -107,9 +107,8 @@ pub fn can_access_resource( } }; - if !access_records.is_empty() { - return true; - } + println!("Access records: {:#?}", access_records); - false // Default to deny if no grant is found + // if circle_pk is in access records true + return access_records.iter().any(|record| record.circle_pk == public_key) } diff --git a/heromodels/src/models/library/items.rs b/heromodels/src/models/library/items.rs index aacd182..02056b1 100644 --- a/heromodels/src/models/library/items.rs +++ b/heromodels/src/models/library/items.rs @@ -41,6 +41,11 @@ impl Image { Self::default() } + /// Gets the ID of the image. + pub fn id(&self) -> u32 { + self.base_data.id + } + /// Sets the title of the image. pub fn title(mut self, title: impl Into) -> Self { self.title = title.into(); @@ -107,6 +112,11 @@ impl Pdf { Self::default() } + /// Gets the ID of the image. + pub fn id(&self) -> u32 { + self.base_data.id + } + /// Sets the title of the PDF. pub fn title(mut self, title: impl Into) -> Self { self.title = title.into(); @@ -134,7 +144,7 @@ impl Pdf { /// Represents a Markdown document library item. #[model] -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)] pub struct Markdown { /// Base model data pub base_data: BaseModelData, @@ -147,23 +157,17 @@ pub struct Markdown { pub content: String, } -impl Default for Markdown { - fn default() -> Self { - Self { - base_data: BaseModelData::new(), - title: String::new(), - description: None, - content: String::new(), - } - } -} - impl Markdown { /// Creates a new `Markdown` document with default values. pub fn new() -> Self { Self::default() } + /// Gets the ID of the image. + pub fn id(&self) -> u32 { + self.base_data.id + } + /// Sets the title of the document. pub fn title(mut self, title: impl Into) -> Self { self.title = title.into(); @@ -184,7 +188,7 @@ impl Markdown { } /// Represents a table of contents entry for a book. -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)] pub struct TocEntry { /// Title of the chapter/section pub title: String, @@ -194,16 +198,6 @@ pub struct TocEntry { pub subsections: Vec, } -impl Default for TocEntry { - fn default() -> Self { - Self { - title: String::new(), - page: 0, - subsections: Vec::new(), - } - } -} - impl TocEntry { /// Creates a new `TocEntry` with default values. pub fn new() -> Self { @@ -231,7 +225,7 @@ impl TocEntry { /// Represents a Book library item (collection of markdown pages with TOC). #[model] -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)] pub struct Book { /// Base model data pub base_data: BaseModelData, @@ -246,24 +240,17 @@ pub struct Book { pub pages: Vec, } -impl Default for Book { - fn default() -> Self { - Self { - base_data: BaseModelData::new(), - title: String::new(), - description: None, - table_of_contents: Vec::new(), - pages: Vec::new(), - } - } -} - impl Book { /// Creates a new `Book` with default values. pub fn new() -> Self { Self::default() } + /// Gets the ID of the book. + pub fn id(&self) -> u32 { + self.base_data.id + } + /// Sets the title of the book. pub fn title(mut self, title: impl Into) -> Self { self.title = title.into(); @@ -303,7 +290,7 @@ impl Book { /// Represents a Slides library item (collection of images for slideshow). #[model] -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)] pub struct Slides { /// Base model data pub base_data: BaseModelData, @@ -318,24 +305,17 @@ pub struct Slides { pub slide_titles: Vec>, } -impl Default for Slides { - fn default() -> Self { - Self { - base_data: BaseModelData::new(), - title: String::new(), - description: None, - slide_urls: Vec::new(), - slide_titles: Vec::new(), - } - } -} - impl Slides { /// Creates a new `Slides` with default values. pub fn new() -> Self { Self::default() } + /// Gets the ID of the slideshow. + pub fn id(&self) -> u32 { + self.base_data.id + } + /// Sets the title of the slideshow. pub fn title(mut self, title: impl Into) -> Self { self.title = title.into(); diff --git a/heromodels/src/models/library/mod.rs b/heromodels/src/models/library/mod.rs index a9bcd44..f13c08e 100644 --- a/heromodels/src/models/library/mod.rs +++ b/heromodels/src/models/library/mod.rs @@ -1,4 +1,2 @@ pub mod collection; pub mod items; -pub mod rhai; -pub use rhai::register_library_rhai_module; diff --git a/heromodels/src/models/log/README.md b/heromodels/src/models/log/README.md new file mode 100644 index 0000000..472a8e7 --- /dev/null +++ b/heromodels/src/models/log/README.md @@ -0,0 +1,3 @@ +## Object Model + +This is a generic object model mostly used for testing purposes. \ No newline at end of file diff --git a/heromodels/src/models/log/log.rs b/heromodels/src/models/log/log.rs new file mode 100644 index 0000000..1dc2c3b --- /dev/null +++ b/heromodels/src/models/log/log.rs @@ -0,0 +1,60 @@ +use std::sync::Arc; + +use crate::db::{hero::OurDB, Collection, Db}; +use heromodels_core::BaseModelData; +use heromodels_derive::model; +// Temporarily removed to fix compilation issues +// use rhai_autobind_macros::rhai_model_export; +use rhai::{CustomType, TypeBuilder}; +use serde::{Deserialize, Serialize}; + +/// Represents an event in a contact +#[model] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)] +pub struct Log { + /// Base model data + pub base_data: BaseModelData, + #[index] + pub title: String, + pub description: String, + #[index] + pub subject_pk: String, + #[index] + pub object_id: u32, +} + +impl Log { + pub fn new() -> Self { + Log { + title: String::new(), + base_data: BaseModelData::new(), + description: String::new(), + subject_pk: String::new(), + object_id: 0, + } + } + + pub fn id(&self) -> u32 { + self.base_data.id + } + + pub fn title(mut self, title: String) -> Self { + self.title = title; + self + } + + pub fn description(mut self, description: String) -> Self { + self.description = description; + self + } + + pub fn subject_pk(mut self, subject_pk: String) -> Self { + self.subject_pk = subject_pk; + self + } + + pub fn object_id(mut self, object_id: u32) -> Self { + self.object_id = object_id; + self + } +} \ No newline at end of file diff --git a/heromodels/src/models/log/mod.rs b/heromodels/src/models/log/mod.rs new file mode 100644 index 0000000..77f230e --- /dev/null +++ b/heromodels/src/models/log/mod.rs @@ -0,0 +1,5 @@ +// Export contact module +pub mod log; + +// Re-export contact, Group from the inner contact module (contact.rs) within src/models/contact/mod.rs +pub use self::log::Log; diff --git a/heromodels/src/models/mod.rs b/heromodels/src/models/mod.rs index 7763482..4689378 100644 --- a/heromodels/src/models/mod.rs +++ b/heromodels/src/models/mod.rs @@ -12,6 +12,7 @@ pub mod flow; pub mod governance; pub mod legal; pub mod library; +pub mod object; pub mod projects; // Re-export key types for convenience @@ -38,6 +39,4 @@ pub use circle::register_circle_rhai_module; pub use flow::register_flow_rhai_module; pub use legal::register_legal_rhai_module; #[cfg(feature = "rhai")] -pub use library::register_library_rhai_module; -#[cfg(feature = "rhai")] pub use projects::register_projects_rhai_module; diff --git a/heromodels/src/models/object/README.md b/heromodels/src/models/object/README.md new file mode 100644 index 0000000..472a8e7 --- /dev/null +++ b/heromodels/src/models/object/README.md @@ -0,0 +1,3 @@ +## Object Model + +This is a generic object model mostly used for testing purposes. \ No newline at end of file diff --git a/heromodels/src/models/object/mod.rs b/heromodels/src/models/object/mod.rs new file mode 100644 index 0000000..16a1ca7 --- /dev/null +++ b/heromodels/src/models/object/mod.rs @@ -0,0 +1,5 @@ +// Export contact module +pub mod object; + +// Re-export contact, Group from the inner contact module (contact.rs) within src/models/contact/mod.rs +pub use self::object::Object; diff --git a/heromodels/src/models/object/object.rs b/heromodels/src/models/object/object.rs new file mode 100644 index 0000000..8c5bcd5 --- /dev/null +++ b/heromodels/src/models/object/object.rs @@ -0,0 +1,44 @@ +use std::sync::Arc; + +use crate::db::{hero::OurDB, Collection, Db}; +use heromodels_core::BaseModelData; +use heromodels_derive::model; +// Temporarily removed to fix compilation issues +// use rhai_autobind_macros::rhai_model_export; +use rhai::{CustomType, TypeBuilder}; +use serde::{Deserialize, Serialize}; + +/// Represents an event in a contact +#[model] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)] +pub struct Object { + /// Base model data + pub base_data: BaseModelData, + #[index] + pub title: String, + pub description: String +} + +impl Object { + pub fn new() -> Self { + Object { + title: String::new(), + base_data: BaseModelData::new(), + description: String::new(), + } + } + + pub fn id(&self) -> u32 { + self.base_data.id + } + + pub fn title(mut self, title: String) -> Self { + self.title = title; + self + } + + pub fn description(mut self, description: String) -> Self { + self.description = description; + self + } +} \ No newline at end of file