From 721456de306a1159700a487a64274d8bfbe81ffa Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 24 Apr 2018 15:52:47 +0100 Subject: [PATCH] Actually start playing with ActivityPub And Rust --- Cargo.lock | 24 ++++++++++++++++++++++++ Cargo.toml | 2 ++ src/activity_pub/activity.rs | 16 ++++++++++++++++ src/activity_pub/actor.rs | 13 ++++++++++--- src/activity_pub/mod.rs | 8 ++++++-- src/activity_pub/object.rs | 7 +++++++ src/main.rs | 4 ++++ src/routes/blogs.rs | 4 ++-- src/routes/user.rs | 4 ++-- 9 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 src/activity_pub/activity.rs create mode 100644 src/activity_pub/object.rs diff --git a/Cargo.lock b/Cargo.lock index b3d2bb1c..9ca72b2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,6 +473,8 @@ dependencies = [ "rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "rocket_codegen 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "rocket_contrib 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -669,6 +671,26 @@ name = "serde" version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_derive" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive_internals" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_json" version = "1.0.16" @@ -986,6 +1008,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)" = "a73973861352c932ed1365ce22b32467ce260ac4c8db11cf750ce56334ff2dcf" +"checksum serde_derive 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)" = "aa113e5fc4b008a626ba2bbd41330b56c9987d667f79f7b243e5a2d03d91ed1c" +"checksum serde_derive_internals 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9d30c4596450fd7bbda79ef15559683f9a79ac0193ea819db90000d7e1cae794" "checksum serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "8c6c4e049dc657a99e394bd85c22acbf97356feeec6dbf44150f2dcf79fb3118" "checksum slug 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "797bcb4d24e91239a8615415814f4afb2d8ca400c472de3c73f803a5a7689e11" "checksum smallvec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4f357e8cd37bf8822e1b964e96fd39e2cb5a0424f8aaa284ccaccc2162411c" diff --git a/Cargo.toml b/Cargo.toml index 9cce9242..34b3bb3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,8 @@ dotenv = "*" heck = "0.3.0" rocket = "*" rocket_codegen = "*" +serde = "*" +serde_derive = "1.0" serde_json = "1.0" [dependencies.diesel] diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs new file mode 100644 index 00000000..790d6956 --- /dev/null +++ b/src/activity_pub/activity.rs @@ -0,0 +1,16 @@ +use activity_pub::actor::Actor; +use activity_pub::object::Object; + +pub struct Create<'a, T, U> where T: Actor + 'static, U: Object { + by: &'a T, + object: U +} + +impl<'a, T: Actor, U: Object> Create<'a, T, U> { + pub fn new(by: &T, obj: U) -> Create { + Create { + by: by, + object: obj + } + } +} diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs index c505840c..ec54dd50 100644 --- a/src/activity_pub/actor.rs +++ b/src/activity_pub/actor.rs @@ -1,6 +1,8 @@ use diesel::PgConnection; -use activity_pub::{activity, Activity, context}; +use activity_pub::{activity_pub, ActivityPub, context}; +use activity_pub::activity::Create; +use activity_pub::object::{Attribuable, Object}; use models::instance::Instance; pub enum ActorType { @@ -26,8 +28,8 @@ pub trait Actor { fn get_actor_type() -> ActorType; - fn as_activity_pub (&self, conn: &PgConnection) -> Activity { - activity(json!({ + fn as_activity_pub (&self, conn: &PgConnection) -> ActivityPub { + activity_pub(json!({ "@context": context(), "id": self.compute_id(conn), "type": Self::get_actor_type().to_string(), @@ -63,4 +65,9 @@ pub trait Actor { user = self.get_actor_id() ) } + + fn create(&self, obj: T) -> Create where T: Object + Attribuable, Self: Actor + Sized { + obj.set_attribution::(self); + Create::::new(self, obj) + } } diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index f22cc282..1fefb49e 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -3,10 +3,14 @@ use rocket::response::Content; use rocket_contrib::Json; use serde_json; +pub mod activity; pub mod actor; +pub mod object; pub mod webfinger; -pub type Activity = Content; +pub type ActivityPub = Content; + +pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams"; pub fn context() -> serde_json::Value { json!([ @@ -32,6 +36,6 @@ pub fn context() -> serde_json::Value { ]) } -pub fn activity(json: serde_json::Value) -> Activity { +pub fn activity_pub(json: serde_json::Value) -> ActivityPub { Content(ContentType::new("application", "activity+json"), Json(json)) } diff --git a/src/activity_pub/object.rs b/src/activity_pub/object.rs new file mode 100644 index 00000000..db2aaae7 --- /dev/null +++ b/src/activity_pub/object.rs @@ -0,0 +1,7 @@ +use activity_pub::actor::Actor; + +pub trait Object {} + +pub trait Attribuable { + fn set_attribution(&self, by: &T) where T: Actor; +} diff --git a/src/main.rs b/src/main.rs index 1cd228b7..acdf4fea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,10 @@ extern crate diesel; extern crate dotenv; extern crate rocket; extern crate rocket_contrib; +#[feature(custom_attribute)] +extern crate serde; +#[macro_use] +extern crate serde_derive; #[macro_use] extern crate serde_json; diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 2059939d..9a445298 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -3,7 +3,7 @@ use rocket::response::Redirect; use rocket_contrib::Template; use std::collections::HashMap; -use activity_pub::Activity; +use activity_pub::ActivityPub; use activity_pub::actor::Actor; use db_conn::DbConn; use models::blog_authors::*; @@ -18,7 +18,7 @@ fn details(name: String) -> String { } #[get("/~/", format = "application/activity+json", rank = 1)] -fn activity_details(name: String, conn: DbConn) -> Activity { +fn activity_details(name: String, conn: DbConn) -> ActivityPub { let blog = Blog::find_by_actor_id(&*conn, name).unwrap(); blog.as_activity_pub(&*conn) } diff --git a/src/routes/user.rs b/src/routes/user.rs index 8e1f192f..b3b4d987 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -3,7 +3,7 @@ use rocket::response::Redirect; use rocket_contrib::Template; use std::collections::HashMap; -use activity_pub::Activity; +use activity_pub::ActivityPub; use activity_pub::actor::Actor; use db_conn::DbConn; use models::instance::Instance; @@ -20,7 +20,7 @@ fn details(name: String) -> String { } #[get("/@/", format = "application/activity+json", rank = 1)] -fn activity_details(name: String, conn: DbConn) -> Activity { +fn activity_details(name: String, conn: DbConn) -> ActivityPub { let user = User::find_by_name(&*conn, name).unwrap(); user.as_activity_pub(&*conn) }