From c51ca0744bbb36e378710dae5557c9608e802f8e Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 20 Mar 2024 07:55:03 +0100 Subject: [PATCH] chore: polish --- Cargo.toml | 1 + src/activitypub/mod.rs | 9 ++++++-- src/activitystream/macros.rs | 45 ++++++++++++++++++++++++++++++++++++ src/activitystream/mod.rs | 4 ++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7d7770ae..2c121917 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ thiserror = "1.0.58" tokio = { version = "1.35.1", features = ["full"] } tracing = "0.1.40" tracing-subscriber = "0.3.18" +uuid = { version = "1.8.0", features = ["v4"] } diff --git a/src/activitypub/mod.rs b/src/activitypub/mod.rs index 1f6e6c05..e9f96303 100644 --- a/src/activitypub/mod.rs +++ b/src/activitypub/mod.rs @@ -10,8 +10,13 @@ use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel}; use crate::{activitystream::{object::{ObjectType, activity::{Activity, ActivityType}}, Base, BaseType, Node}, model}; -pub fn uri_id(id: String) -> String { - if id.starts_with("http") { id } else { format!("http://localhost:3000/users/{id}") } +pub fn uri_id(entity: &str, id: String) -> String { + if id.starts_with("http") { id } else { format!("http://localhost:3000/{entity}/{id}") } +} + +pub fn id_uri(id: &str) -> &str { + id.split('/').last().unwrap_or("") +} } pub async fn inbox(State(db) : State>, Json(object): Json) -> Result, StatusCode> { diff --git a/src/activitystream/macros.rs b/src/activitystream/macros.rs index 93621eb1..8b22becf 100644 --- a/src/activitystream/macros.rs +++ b/src/activitystream/macros.rs @@ -124,6 +124,18 @@ macro_rules! getter { } }; + ($name:ident -> u64) => { + fn $name(&self) -> Option { + self.get(stringify!($name))?.as_u64() + } + }; + + ($name:ident::$rename:ident -> u64) => { + fn $name(&self) -> Option { + self.get(stringify!($rename))?.as_u64() + } + }; + ($name:ident -> chrono::DateTime) => { fn $name(&self) -> Option> { Some( @@ -184,6 +196,39 @@ macro_rules! setter { } }; + ($name:ident::$rename:ident -> &str) => { + paste::item! { + fn [< set_$name >](&mut self, val: Option<&str>) -> &mut Self { + $crate::activitystream::macros::set_maybe_value( + self, stringify!($rename), val.map(|x| serde_json::Value::String(x.to_string())) + ); + self + } + } + }; + + ($name:ident -> u64) => { + paste::item! { + fn [< set_$name >](&mut self, val: Option) -> &mut Self { + $crate::activitystream::macros::set_maybe_value( + self, stringify!($name), val.map(|x| serde_json::Value::Number(serde_json::Number::from(x))) + ); + self + } + } + }; + + ($name:ident::$rename:ident -> u64) => { + paste::item! { + fn [< set_$name >](&mut self, val: Option) -> &mut Self { + $crate::activitystream::macros::set_maybe_value( + self, stringify!($rename), val.map(|x| serde_json::Value::Number(serde_json::Number::from(x))) + ); + self + } + } + }; + ($name:ident -> chrono::DateTime) => { paste::item! { fn [< set_$name >](&mut self, val: Option>) -> &mut Self { diff --git a/src/activitystream/mod.rs b/src/activitystream/mod.rs index c76fc70f..f56a3650 100644 --- a/src/activitystream/mod.rs +++ b/src/activitystream/mod.rs @@ -29,6 +29,10 @@ pub trait Base { fn underlying_json_object(self) -> serde_json::Value; } +pub fn object() -> serde_json::Value { + serde_json::Value::Object(serde_json::Map::default()) +} + pub trait BaseMut { fn set_id(&mut self, val: Option<&str>) -> &mut Self; fn set_base_type(&mut self, val: Option) -> &mut Self;