flabk/src/astreams/mod.rs

96 lines
2.5 KiB
Rust

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use self::{context::Context, note::Note, resolve::ResolveError, serde_ext::pull_single_into_vec};
pub mod context;
pub mod note;
pub mod resolve;
mod serde_ext;
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub enum ActivityKind {
Create,
Like,
Note,
}
#[derive(Debug, Clone)]
pub enum ExpandError {
InvalidKind(Option<ActivityKind>),
NoAttribution,
ResolveIRI(String),
Other(String),
}
impl From<ResolveError> for ExpandError {
fn from(err: ResolveError) -> Self {
Self::ResolveIRI(err.0)
}
}
#[async_trait]
pub trait ExpandLD<T> {
async fn expand(obj: &ObjectLD) -> Result<T, ExpandError>;
}
#[derive(Clone, Serialize, Deserialize)]
pub struct ActivityLD {
#[serde(rename = "@context")]
pub context_uri: String,
pub kind: ActivityKind,
#[serde(rename = "actor")]
pub actor_uri: String,
#[serde(rename = "to")]
pub to_uris: Vec<String>,
pub object: Option<ObjectLD>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ObjectLD {
#[serde(rename = "@context")]
pub context_uri: Option<String>,
pub id: String,
#[serde(rename = "type")]
pub kind: Option<ActivityKind>,
#[serde(rename = "attributedTo")]
pub attributed_to: Option<String>,
pub published: Option<String>,
pub content: Option<String>,
pub context: Option<String>,
pub conversation: Option<String>,
pub url: Option<String>,
#[serde(rename = "to")]
#[serde(deserialize_with = "pull_single_into_vec")]
pub to_uris: Vec<String>,
}
pub enum ActorType {}
#[derive(Debug, Clone)]
pub struct Actor {
id: String,
}
pub async fn test() {
let obj = r#"{
"@context": "https://www.w3.org/ns/activitystreams",
"attributedTo": "https://localhost/u/test",
"content": "honk donk",
"context": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1",
"conversation": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1",
"id": "https://localhost/u/test/h/6Q8BFF8W6PZT2ddngZ",
"published": "2022-09-30T19:04:45Z",
"summary": "",
"to": "https://www.w3.org/ns/activitystreams#Public",
"type": "Note",
"url": "https://localhost/u/test/h/6Q8BFF8W6PZT2ddngZ"
}"#;
let obj = serde_json::from_str::<ObjectLD>(obj).unwrap();
let unwrapped = Note::expand(&obj).await.unwrap();
println!("{:#?}", &unwrapped);
println!("to: {:#?}", obj.to_uris);
println!("{}", serde_json::to_string_pretty(&obj).unwrap());
}