mirror of
https://git.asonix.dog/asonix/activitystreams.git
synced 2024-11-25 21:31:00 +00:00
Examples
This commit is contained in:
parent
4c72ace1ce
commit
5701eb78bd
3 changed files with 78 additions and 35 deletions
|
@ -1,27 +1,30 @@
|
||||||
use activitystreams::{
|
use activitystreams::{
|
||||||
ext::Ext,
|
context,
|
||||||
object::{properties::ApObjectProperties, Video},
|
object::{ApObject, Video},
|
||||||
|
prelude::*,
|
||||||
|
uri,
|
||||||
};
|
};
|
||||||
|
use chrono::Duration;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), anyhow::Error> {
|
||||||
let mut v = Video::full();
|
let mut video = ApObject::new(Video::new());
|
||||||
|
|
||||||
v.as_mut()
|
video
|
||||||
.set_context_xsd_any_uri("https://www.w3.org/ns/activitystreams")?
|
.set_context(context())
|
||||||
.set_id("https://example.com/@example/lions")?
|
.set_id(uri!("https://example.com/@example/lions"))
|
||||||
.set_url_xsd_any_uri("https://example.com/@example/lions/video.webm")?
|
.set_media_type("video/webm".parse()?)
|
||||||
.set_name_xsd_string("My Cool Video")?
|
.set_url(uri!("https://example.com/@example/lions/video.webm"))
|
||||||
.set_summary_xsd_string("A video about some cool lions")?
|
.set_summary("A cool video".to_owned())
|
||||||
.set_media_type("video/webm")?
|
.set_duration(Duration::minutes(4) + Duration::seconds(20))
|
||||||
.set_duration("PT4M20S")?;
|
.set_shares(uri!("https://example.com/@example/lions/video.webm#shares"));
|
||||||
|
|
||||||
println!("Video, {:#?}", v);
|
println!("Video, {:#?}", video);
|
||||||
|
|
||||||
let s = serde_json::to_string(&v)?;
|
let s = serde_json::to_string(&video)?;
|
||||||
|
|
||||||
println!("json, {}", s);
|
println!("json, {}", s);
|
||||||
|
|
||||||
let v: Ext<Video, ApObjectProperties> = serde_json::from_str(&s)?;
|
let v: ApObject<Video> = serde_json::from_str(&s)?;
|
||||||
|
|
||||||
println!("Video again, {:#?}", v);
|
println!("Video again, {:#?}", v);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use activitystreams::{
|
use activitystreams::{
|
||||||
collection::{properties::CollectionProperties, OrderedCollection},
|
collection::OrderedCollection,
|
||||||
ext::Ext,
|
object::{ApObject, Page},
|
||||||
object::{properties::ApObjectProperties, ObjectBox, Page},
|
prelude::*,
|
||||||
};
|
};
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
|
|
||||||
|
@ -41,28 +41,28 @@ fn main() -> Result<(), Error> {
|
||||||
"published": "2020-03-13T00:14:41.188634+00:00"
|
"published": "2020-03-13T00:14:41.188634+00:00"
|
||||||
}"#;
|
}"#;
|
||||||
|
|
||||||
let page: Ext<Page, ApObjectProperties> = serde_json::from_str(page_json)?;
|
let page: ApObject<Page> = serde_json::from_str(page_json)?;
|
||||||
println!("{:#?}", page);
|
println!("{:#?}", page);
|
||||||
let obox = ObjectBox::from_concrete(page)?;
|
let mut collection: ApObject<OrderedCollection> = serde_json::from_str(collection_json)?;
|
||||||
println!("{:#?}", obox);
|
|
||||||
let obox_string = serde_json::to_string(&obox)?;
|
|
||||||
println!("{}", obox_string);
|
|
||||||
let obox: ObjectBox = serde_json::from_str(&obox_string)?;
|
|
||||||
println!("{:#?}", obox);
|
|
||||||
let mut collection: OrderedCollection = serde_json::from_str(collection_json)?;
|
|
||||||
println!("{:#?}", collection);
|
println!("{:#?}", collection);
|
||||||
|
|
||||||
let cprops: &CollectionProperties = collection.as_ref();
|
let v: Vec<ApObject<Page>> = collection
|
||||||
let v: Vec<Ext<Page, ApObjectProperties>> = cprops
|
.items()
|
||||||
.get_many_items_base_boxes()
|
.clone()
|
||||||
.unwrap()
|
.many()
|
||||||
.map(|base_box| base_box.clone().into_concrete())
|
.into_iter()
|
||||||
.collect::<Result<Vec<_>, std::io::Error>>()?;
|
.flatten()
|
||||||
|
.filter_map(|any_base| any_base.take_base())
|
||||||
let cprops: &mut CollectionProperties = collection.as_mut();
|
.map(|base| base.solidify().and_then(|o| o.extend()))
|
||||||
cprops.set_many_items_base_boxes(v.clone())?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
println!("{:#?}", v);
|
println!("{:#?}", v);
|
||||||
|
let v = v
|
||||||
|
.into_iter()
|
||||||
|
.map(|o| o.into_any_base())
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
|
collection.set_many_items(v);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
40
examples/handle_incoming.rs
Normal file
40
examples/handle_incoming.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
use activitystreams::{activity::ActorAndObject, prelude::*};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub enum AcceptedTypes {
|
||||||
|
Accept,
|
||||||
|
Announce,
|
||||||
|
Create,
|
||||||
|
Delete,
|
||||||
|
Follow,
|
||||||
|
Reject,
|
||||||
|
Update,
|
||||||
|
Undo,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type AcceptedActivity = ActorAndObject<AcceptedTypes>;
|
||||||
|
|
||||||
|
pub fn handle_activity(activity: AcceptedActivity) -> Result<(), anyhow::Error> {
|
||||||
|
println!("Actor: {:?}", activity.actor());
|
||||||
|
println!("Object: {:?}", activity.object());
|
||||||
|
|
||||||
|
match activity.kind() {
|
||||||
|
Some(AcceptedTypes::Accept) => println!("Accept"),
|
||||||
|
Some(AcceptedTypes::Announce) => println!("Announce"),
|
||||||
|
Some(AcceptedTypes::Create) => println!("Create"),
|
||||||
|
Some(AcceptedTypes::Delete) => println!("Delete"),
|
||||||
|
Some(AcceptedTypes::Follow) => println!("Follow"),
|
||||||
|
Some(AcceptedTypes::Reject) => println!("Reject"),
|
||||||
|
Some(AcceptedTypes::Update) => println!("Update"),
|
||||||
|
Some(AcceptedTypes::Undo) => println!("Undo"),
|
||||||
|
None => return Err(anyhow::Error::msg("No activity type provided")),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
static EXAMPLE_JSON: &str = r#"{"actor":"https://asonix.dog/users/asonix","object":"https://asonix.dog/users/asonix/posts/1","type":"Announce"}"#;
|
||||||
|
|
||||||
|
fn main() -> Result<(), anyhow::Error> {
|
||||||
|
handle_activity(serde_json::from_str(EXAMPLE_JSON)?)
|
||||||
|
}
|
Loading…
Reference in a new issue