Actually start playing with ActivityPub

And Rust
This commit is contained in:
Bat 2018-04-24 15:52:47 +01:00
parent 0b00849a62
commit 721456de30
9 changed files with 73 additions and 9 deletions

24
Cargo.lock generated
View File

@ -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"

View File

@ -8,6 +8,8 @@ dotenv = "*"
heck = "0.3.0"
rocket = "*"
rocket_codegen = "*"
serde = "*"
serde_derive = "1.0"
serde_json = "1.0"
[dependencies.diesel]

View File

@ -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<T, U> {
Create {
by: by,
object: obj
}
}
}

View File

@ -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<T>(&self, obj: T) -> Create<Self, T> where T: Object + Attribuable, Self: Actor + Sized {
obj.set_attribution::<Self>(self);
Create::<Self, T>::new(self, obj)
}
}

View File

@ -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<Json>;
pub type ActivityPub = Content<Json>;
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))
}

View File

@ -0,0 +1,7 @@
use activity_pub::actor::Actor;
pub trait Object {}
pub trait Attribuable {
fn set_attribution<T>(&self, by: &T) where T: Actor;
}

View File

@ -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;

View File

@ -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("/~/<name>", 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)
}

View File

@ -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("/@/<name>", 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)
}