From 0b00849a6250597aa18ee335ffb31726aa7769eb Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 24 Apr 2018 13:31:02 +0100 Subject: [PATCH] Refactor ActivityPub --- src/activity_pub/actor.rs | 30 +++++------------------------- src/activity_pub/mod.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 4 ++-- src/routes/blogs.rs | 7 ++++--- src/routes/user.rs | 7 ++++--- 5 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs index e7beb8ee..c505840c 100644 --- a/src/activity_pub/actor.rs +++ b/src/activity_pub/actor.rs @@ -1,6 +1,6 @@ use diesel::PgConnection; -use serde_json::Value; +use activity_pub::{activity, Activity, context}; use models::instance::Instance; pub enum ActorType { @@ -26,29 +26,9 @@ pub trait Actor { fn get_actor_type() -> ActorType; - fn as_activity_pub (&self, conn: &PgConnection) -> Value { - json!({ - "@context": [ - "https://www.w3.org/ns/activitystreams", - "https://w3id.org/security/v1", - { - "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", - "sensitive": "as:sensitive", - "movedTo": "as:movedTo", - "Hashtag": "as:Hashtag", - "ostatus":"http://ostatus.org#", - "atomUri":"ostatus:atomUri", - "inReplyToAtomUri":"ostatus:inReplyToAtomUri", - "conversation":"ostatus:conversation", - "toot":"http://joinmastodon.org/ns#", - "Emoji":"toot:Emoji", - "focalPoint": { - "@container":"@list", - "@id":"toot:focalPoint" - }, - "featured":"toot:featured" - } - ], + fn as_activity_pub (&self, conn: &PgConnection) -> Activity { + activity(json!({ + "@context": context(), "id": self.compute_id(conn), "type": Self::get_actor_type().to_string(), "inbox": self.compute_inbox(conn), @@ -60,7 +40,7 @@ pub trait Actor { "endpoints": { "sharedInbox": "https://plu.me/inbox" } - }) + })) } fn compute_outbox(&self, conn: &PgConnection) -> String { diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index f5f42555..f22cc282 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -1,2 +1,37 @@ +use rocket::http::ContentType; +use rocket::response::Content; +use rocket_contrib::Json; +use serde_json; + pub mod actor; pub mod webfinger; + +pub type Activity = Content; + +pub fn context() -> serde_json::Value { + json!([ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "sensitive": "as:sensitive", + "movedTo": "as:movedTo", + "Hashtag": "as:Hashtag", + "ostatus":"http://ostatus.org#", + "atomUri":"ostatus:atomUri", + "inReplyToAtomUri":"ostatus:inReplyToAtomUri", + "conversation":"ostatus:conversation", + "toot":"http://joinmastodon.org/ns#", + "Emoji":"toot:Emoji", + "focalPoint": { + "@container":"@list", + "@id":"toot:focalPoint" + }, + "featured":"toot:featured" + } + ]) +} + +pub fn activity(json: serde_json::Value) -> Activity { + Content(ContentType::new("application", "activity+json"), Json(json)) +} diff --git a/src/main.rs b/src/main.rs index e7bdb638..1cd228b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ fn main() { routes::user::me, routes::user::details, - routes::user::activity, + routes::user::activity_details, routes::user::new, routes::user::create, @@ -70,7 +70,7 @@ fn main() { routes::session::delete, routes::blogs::details, - routes::blogs::activity, + routes::blogs::activity_details, routes::blogs::new, routes::blogs::create, diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 4c78be74..2059939d 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -1,8 +1,9 @@ use rocket::request::Form; use rocket::response::Redirect; -use rocket_contrib::{Json, Template}; +use rocket_contrib::Template; use std::collections::HashMap; +use activity_pub::Activity; use activity_pub::actor::Actor; use db_conn::DbConn; use models::blog_authors::*; @@ -17,9 +18,9 @@ fn details(name: String) -> String { } #[get("/~/", format = "application/activity+json", rank = 1)] -fn activity(name: String, conn: DbConn) -> Json { +fn activity_details(name: String, conn: DbConn) -> Activity { let blog = Blog::find_by_actor_id(&*conn, name).unwrap(); - Json(blog.as_activity_pub(&*conn)) + blog.as_activity_pub(&*conn) } #[get("/blogs/new")] diff --git a/src/routes/user.rs b/src/routes/user.rs index dc01cb7f..8e1f192f 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -1,8 +1,9 @@ use rocket::request::Form; use rocket::response::Redirect; -use rocket_contrib::{Json, Template}; +use rocket_contrib::Template; use std::collections::HashMap; +use activity_pub::Activity; use activity_pub::actor::Actor; use db_conn::DbConn; use models::instance::Instance; @@ -19,9 +20,9 @@ fn details(name: String) -> String { } #[get("/@/", format = "application/activity+json", rank = 1)] -fn activity(name: String, conn: DbConn) -> Json { +fn activity_details(name: String, conn: DbConn) -> Activity { let user = User::find_by_name(&*conn, name).unwrap(); - Json(user.as_activity_pub(&*conn)) + user.as_activity_pub(&*conn) } #[get("/users/new")]