From fc43183ce1b4423f2a83098c06cbfe324b321d24 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 20 Nov 2024 18:02:54 +0100 Subject: [PATCH] feat(apb): add optional shortcuts --- apb/Cargo.toml | 3 ++- apb/src/lib.rs | 5 +++++ apb/src/shortcuts.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 apb/src/shortcuts.rs diff --git a/apb/Cargo.toml b/apb/Cargo.toml index 8db3f04..4f00a5c 100644 --- a/apb/Cargo.toml +++ b/apb/Cargo.toml @@ -23,7 +23,7 @@ sea-orm = { version = "1.0", optional = true, default-features = false } reqwest = { version = "0.12", features = ["json"], optional = true } [features] -default = ["activitypub-miscellaneous-terms", "send"] +default = ["activitypub-miscellaneous-terms", "send", "shortcuts"] # extensions activitypub-miscellaneous-terms = [] # https://swicg.github.io/miscellany/ activitypub-counters = [] # https://ns.alemi.dev/as/counters/# @@ -38,6 +38,7 @@ jsonld = [] send = [] orm = ["dep:sea-orm"] fetch = ["dep:reqwest"] +shortcuts = [] # providers unstructured = ["dep:serde_json"] #TODO eventually also make a structured base? diff --git a/apb/src/lib.rs b/apb/src/lib.rs index 849f2a1..5346b16 100644 --- a/apb/src/lib.rs +++ b/apb/src/lib.rs @@ -104,6 +104,11 @@ pub use key::{PublicKey, PublicKeyMut}; pub mod field; pub use field::{Field, FieldErr}; +#[cfg(feature = "shortcuts")] +pub mod shortcuts; +#[cfg(feature = "shortcuts")] +pub use shortcuts::Shortcuts; + #[cfg(feature = "jsonld")] mod jsonld; diff --git a/apb/src/shortcuts.rs b/apb/src/shortcuts.rs new file mode 100644 index 0000000..508a708 --- /dev/null +++ b/apb/src/shortcuts.rs @@ -0,0 +1,52 @@ +use crate::{Collection, Object}; + + +pub trait Shortcuts: crate::Object { + fn likes_count(&self) -> crate::Field { + let x = self + .likes() + .inner()? + .total_items()? + .min(i32::MAX as u64) + as i32; + Ok(x) + } + + fn shares_count(&self) -> crate::Field { + let x = self + .shares() + .inner()? + .total_items()? + .min(i32::MAX as u64) + as i32; + Ok(x) + } + + fn replies_count(&self) -> crate::Field { + let x = self + .replies() + .inner()? + .total_items()? + .min(i32::MAX as u64) + as i32; + Ok(x) + } + + fn image_url(&self) -> crate::Field { + let image_node = self.image(); + let image = image_node.inner()?; + let url = image.url(); + let id = url.id()?; + Ok(id.to_string()) + } + + fn icon_url(&self) -> crate::Field { + let icon_node = self.icon(); + let icon = icon_node.inner()?; + let url = icon.url(); + let id = url.id()?; + Ok(id.to_string()) + } +} + +impl Shortcuts for T {}