feat: work on activitystream types

This commit is contained in:
əlemi 2024-03-14 05:27:08 +01:00
parent 6bae33f0a6
commit ac3f07f804
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,4 @@
pub trait Activity : super::Object {
fn actor(&self) -> Option<&super::ObjectOrLink> { None }
fn object(&self) -> Option<&super::ObjectOrLink> { None }
}

View file

@ -0,0 +1,7 @@
pub mod object;
pub use object::{Object, Link, ObjectOrLink};
pub mod activity;
pub use activity::Activity;

View file

@ -0,0 +1,82 @@
pub enum ObjectOrLink {
Object(Box<dyn Object>),
Link(Box<dyn Link>),
}
impl From<serde_json::Value> for ObjectOrLink {
fn from(value: serde_json::Value) -> Self {
if value.get("href").is_some() {
Self::Link(Box::new(value))
} else {
Self::Object(Box::new(value))
}
}
}
pub trait Link {
fn href(&self) -> Option<&str> { None }
fn rel(&self) -> Option<&str> { None }
fn media_type(&self) -> Option<&str> { None } // also in obj
fn name(&self) -> Option<&str> { None } // also in obj
fn hreflang(&self) -> Option<&str> { None }
fn height(&self) -> Option<&str> { None }
fn width(&self) -> Option<&str> { None }
fn preview(&self) -> Option<&str> { None } // also in obj
}
pub trait Object {
fn id(&self) -> Option<&str> { None }
fn object_type(&self) -> Option<&str> { None }
fn attachment (&self) -> Option<&str> { None }
fn attributed_to (&self) -> Option<&str> { None }
fn audience (&self) -> Option<&str> { None }
fn content (&self) -> Option<&str> { None }
fn context (&self) -> Option<&str> { None }
fn name (&self) -> Option<&str> { None }
fn end_time (&self) -> Option<&str> { None }
fn generator (&self) -> Option<&str> { None }
fn icon (&self) -> Option<&str> { None }
fn image (&self) -> Option<&str> { None }
fn in_reply_to (&self) -> Option<&str> { None }
fn location (&self) -> Option<&str> { None }
fn preview (&self) -> Option<&str> { None }
fn published (&self) -> Option<&str> { None }
fn replies (&self) -> Option<&str> { None }
fn start_time (&self) -> Option<&str> { None }
fn summary (&self) -> Option<&str> { None }
fn tag (&self) -> Option<&str> { None }
fn updated (&self) -> Option<&str> { None }
fn url (&self) -> Option<&str> { None }
fn to (&self) -> Option<&str> { None }
fn bto (&self) -> Option<&str> { None }
fn cc (&self) -> Option<&str> { None }
fn bcc (&self) -> Option<&str> { None }
fn media_type (&self) -> Option<&str> { None }
fn duration (&self) -> Option<&str> { None }
}
/// impl for empty object
impl Object for () {}
// TODO only Value::Object is a valid Object, but rn "asd" behaves like {} (both are valid...)
/// impl for any json value
impl Object for serde_json::Value {
fn id(&self) -> Option<&str> {
self.get("id")?.as_str()
}
fn object_type(&self) -> Option<&str> {
self.get("type")?.as_str()
}
// ...
}
impl Link for serde_json::Value {
fn href(&self) -> Option<&str> {
self.get("href")?.as_str()
}
// ...
}

View file

@ -1,4 +1,5 @@
pub mod model;
pub mod activitystream;
use axum::{
routing::get,