feat(apb): add optional shortcuts

This commit is contained in:
əlemi 2024-11-20 18:02:54 +01:00
parent f82e624fd9
commit fc43183ce1
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 59 additions and 1 deletions

View file

@ -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?

View file

@ -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;

52
apb/src/shortcuts.rs Normal file
View file

@ -0,0 +1,52 @@
use crate::{Collection, Object};
pub trait Shortcuts: crate::Object {
fn likes_count(&self) -> crate::Field<i32> {
let x = self
.likes()
.inner()?
.total_items()?
.min(i32::MAX as u64)
as i32;
Ok(x)
}
fn shares_count(&self) -> crate::Field<i32> {
let x = self
.shares()
.inner()?
.total_items()?
.min(i32::MAX as u64)
as i32;
Ok(x)
}
fn replies_count(&self) -> crate::Field<i32> {
let x = self
.replies()
.inner()?
.total_items()?
.min(i32::MAX as u64)
as i32;
Ok(x)
}
fn image_url(&self) -> crate::Field<String> {
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<String> {
let icon_node = self.icon();
let icon = icon_node.inner()?;
let url = icon.url();
let id = url.id()?;
Ok(id.to_string())
}
}
impl<T: crate::Object> Shortcuts for T {}