From a5c51f00eac114d889f33f509eb78d135a314f14 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 21 Mar 2024 00:04:44 +0100 Subject: [PATCH] feat: more link helpers and node constructors --- src/activitypub/mod.rs | 13 ++++++++++++- src/activitystream/node.rs | 28 ++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/activitypub/mod.rs b/src/activitypub/mod.rs index 60b2a93..b25930a 100644 --- a/src/activitypub/mod.rs +++ b/src/activitypub/mod.rs @@ -2,12 +2,23 @@ pub mod user; pub mod object; pub mod activity; - use axum::{extract::State, http::StatusCode, Json}; use sea_orm::{EntityTrait, IntoActiveModel}; use crate::{activitystream::{self, object::{activity::{Activity, ActivityType}, actor::{ActorMut, ActorType}, ObjectMut, ObjectType}, Base, BaseMut, BaseType, Node}, model, server::Context, url}; +pub const PUBLIC_TARGET : &str = "https://www.w3.org/ns/activitystreams#Public"; + +pub fn split_id(id: &str) -> (String, String) { + let clean = id + .replace("http://", "") + .replace("https://", ""); + let mut splits = clean.split('/'); + let first = splits.next().unwrap_or(""); + let last = splits.last().unwrap_or(first); + (first.to_string(), last.to_string()) +} + #[derive(Debug, serde::Deserialize)] // TODO i don't really like how pleroma/mastodon do it actually, maybe change this? diff --git a/src/activitystream/node.rs b/src/activitystream/node.rs index f3e933e..880b888 100644 --- a/src/activitystream/node.rs +++ b/src/activitystream/node.rs @@ -85,8 +85,28 @@ impl Node { } impl Node{ - pub fn link(uri: &str) -> Self { - Node::Link(Box::new(uri.to_string())) + pub fn empty() -> Self { + Node::Empty + } + + pub fn link(uri: String) -> Self { + Node::Link(Box::new(uri)) + } + + pub fn links(uris: Vec) -> Self { + Node::Array( + uris + .into_iter() + .map(Node::link) + .collect() + ) + } + + pub fn maybe_link(uri: Option) -> Self { + match uri { + Some(uri) => Node::Link(Box::new(uri)), + None => Node::Empty, + } } pub fn object(x: serde_json::Value) -> Self { @@ -146,7 +166,3 @@ impl From for Node { } } - - - -