mirror of
https://git.asonix.dog/asonix/activitystreams.git
synced 2024-11-22 03:40:59 +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::{
|
||||
ext::Ext,
|
||||
object::{properties::ApObjectProperties, Video},
|
||||
context,
|
||||
object::{ApObject, Video},
|
||||
prelude::*,
|
||||
uri,
|
||||
};
|
||||
use chrono::Duration;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut v = Video::full();
|
||||
fn main() -> Result<(), anyhow::Error> {
|
||||
let mut video = ApObject::new(Video::new());
|
||||
|
||||
v.as_mut()
|
||||
.set_context_xsd_any_uri("https://www.w3.org/ns/activitystreams")?
|
||||
.set_id("https://example.com/@example/lions")?
|
||||
.set_url_xsd_any_uri("https://example.com/@example/lions/video.webm")?
|
||||
.set_name_xsd_string("My Cool Video")?
|
||||
.set_summary_xsd_string("A video about some cool lions")?
|
||||
.set_media_type("video/webm")?
|
||||
.set_duration("PT4M20S")?;
|
||||
video
|
||||
.set_context(context())
|
||||
.set_id(uri!("https://example.com/@example/lions"))
|
||||
.set_media_type("video/webm".parse()?)
|
||||
.set_url(uri!("https://example.com/@example/lions/video.webm"))
|
||||
.set_summary("A cool video".to_owned())
|
||||
.set_duration(Duration::minutes(4) + Duration::seconds(20))
|
||||
.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);
|
||||
|
||||
let v: Ext<Video, ApObjectProperties> = serde_json::from_str(&s)?;
|
||||
let v: ApObject<Video> = serde_json::from_str(&s)?;
|
||||
|
||||
println!("Video again, {:#?}", v);
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use activitystreams::{
|
||||
collection::{properties::CollectionProperties, OrderedCollection},
|
||||
ext::Ext,
|
||||
object::{properties::ApObjectProperties, ObjectBox, Page},
|
||||
collection::OrderedCollection,
|
||||
object::{ApObject, Page},
|
||||
prelude::*,
|
||||
};
|
||||
use anyhow::Error;
|
||||
|
||||
|
@ -41,28 +41,28 @@ fn main() -> Result<(), Error> {
|
|||
"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);
|
||||
let obox = ObjectBox::from_concrete(page)?;
|
||||
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)?;
|
||||
let mut collection: ApObject<OrderedCollection> = serde_json::from_str(collection_json)?;
|
||||
println!("{:#?}", collection);
|
||||
|
||||
let cprops: &CollectionProperties = collection.as_ref();
|
||||
let v: Vec<Ext<Page, ApObjectProperties>> = cprops
|
||||
.get_many_items_base_boxes()
|
||||
.unwrap()
|
||||
.map(|base_box| base_box.clone().into_concrete())
|
||||
.collect::<Result<Vec<_>, std::io::Error>>()?;
|
||||
|
||||
let cprops: &mut CollectionProperties = collection.as_mut();
|
||||
cprops.set_many_items_base_boxes(v.clone())?;
|
||||
let v: Vec<ApObject<Page>> = collection
|
||||
.items()
|
||||
.clone()
|
||||
.many()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|any_base| any_base.take_base())
|
||||
.map(|base| base.solidify().and_then(|o| o.extend()))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
println!("{:#?}", v);
|
||||
let v = v
|
||||
.into_iter()
|
||||
.map(|o| o.into_any_base())
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
collection.set_many_items(v);
|
||||
|
||||
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