flabk/src/astreams/mod.rs

119 lines
3.0 KiB
Rust

use async_trait::async_trait;
use flabk_derive::IRI;
use serde::{Deserialize, Serialize};
use serde_ext::IriPartial;
use self::{
context::Context,
note::Note,
resolve::ResolveError,
serde_ext::{expand_partial, 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(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Activity {
// #[serde(rename = "@context")]
// #[serde(deserialize_with = "expand_partial")]
// pub ap_context: Context,
pub id: String,
#[serde(rename = "type")]
pub kind: ActivityKind,
#[serde(deserialize_with = "expand_partial")]
pub attributed_to: Actor,
pub content: Option<String>,
pub context: Option<String>,
#[serde(deserialize_with = "pull_single_into_vec")]
pub to: Vec<String>,
pub url: Option<String>,
}
#[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, Deserialize, IRI, Default)]
pub struct Actor {
id: String,
}
pub async fn test() {
let obj = r#"{
"@context": "https://www.w3.org/ns/activitystreams",
"attributedTo": "http://localhost:3001/u/test",
"content": "honk donk",
"context": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1",
"conversation": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1",
"id": "htts://localhost:3001/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::<Activity>(obj).unwrap();
println!();
println!("{:#?}", obj);
}