From 4666cd3ee3a1f826a242e42e2c3cd353a7fc7ac6 Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 29 Apr 2018 18:49:56 +0100 Subject: [PATCH] Blog outbox --- src/activity_pub/activity.rs | 10 ++++++++++ src/activity_pub/mod.rs | 4 +++- src/activity_pub/outbox.rs | 38 ++++++++++++++++++++++++++++++++++++ src/activity_pub/sign.rs | 2 +- src/main.rs | 1 + src/models/blogs.rs | 10 ++++++++++ src/routes/blogs.rs | 7 +++++++ 7 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/activity_pub/outbox.rs diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs index b3e6a60f..098c4bc0 100644 --- a/src/activity_pub/activity.rs +++ b/src/activity_pub/activity.rs @@ -1,6 +1,16 @@ +use serde_json; + use activity_pub::actor::Actor; use activity_pub::object::Object; +#[derive(Clone)] +pub struct Activity {} +impl Activity { + pub fn serialize(&self) -> serde_json::Value { + json!({}) + } +} + pub struct Create<'a, T, U> where T: Actor + 'static, U: Object { by: &'a T, object: U diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 1fefb49e..a3a40c79 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -6,6 +6,8 @@ use serde_json; pub mod activity; pub mod actor; pub mod object; +pub mod outbox; +pub mod sign; pub mod webfinger; pub type ActivityPub = Content; @@ -14,7 +16,7 @@ pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams"; pub fn context() -> serde_json::Value { json!([ - "https://www.w3.org/ns/activitystreams", + CONTEXT_URL, "https://w3id.org/security/v1", { "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", diff --git a/src/activity_pub/outbox.rs b/src/activity_pub/outbox.rs new file mode 100644 index 00000000..d8134ef4 --- /dev/null +++ b/src/activity_pub/outbox.rs @@ -0,0 +1,38 @@ +use rocket::http::Status; +use rocket::response::{Response, Responder}; +use rocket::request::Request; +use serde_json; + +use activity_pub::{activity_pub, ActivityPub, context}; +use activity_pub::activity::Activity; + +pub struct Outbox { + id: String, + items: Vec +} + +impl Outbox { + pub fn new(id: String, items: Vec) -> Outbox { + Outbox { + id: id, + items: items + } + } + + fn serialize(&self) -> ActivityPub { + let items = self.items.clone(); + activity_pub(json!({ + "@context": context(), + "type": "OrderedCollection", + "id": self.id, + "totalItems": items.len(), + "orderedItems": items.into_iter().map(|i| i.serialize()).collect::>() + })) + } +} + +impl<'r> Responder<'r> for Outbox { + fn respond_to(self, request: &Request) -> Result, Status> { + self.serialize().respond_to(request) + } +} diff --git a/src/activity_pub/sign.rs b/src/activity_pub/sign.rs index 000c9930..c1df72f0 100644 --- a/src/activity_pub/sign.rs +++ b/src/activity_pub/sign.rs @@ -4,7 +4,7 @@ use chrono::Utc; use openssl::sha::{sha256, sha512}; use serde_json; -// Comments are from the Mastodon source code, to knremow what to do. +// (Comments are from the Mastodon source code, to remember what to do.) pub trait Signer { fn get_key_id(&self) -> String; diff --git a/src/main.rs b/src/main.rs index e5876119..8c331b5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,6 +79,7 @@ fn main() { routes::blogs::details, routes::blogs::activity_details, + routes::blogs::outbox, routes::blogs::new, routes::blogs::create, diff --git a/src/models/blogs.rs b/src/models/blogs.rs index f459641a..24ae6e00 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -1,6 +1,8 @@ use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use activity_pub::activity::Activity; use activity_pub::actor::{Actor, ActorType}; +use activity_pub::outbox::Outbox; use activity_pub::webfinger::*; use models::instance::Instance; use schema::blogs; @@ -65,6 +67,14 @@ impl Blog { .get_result::(conn).expect("Couldn't update inbox URL"); } } + + pub fn outbox(&self, conn: &PgConnection) -> Outbox { + Outbox::new(self.compute_outbox(conn), self.get_activities(conn)) + } + + fn get_activities(&self, conn: &PgConnection) -> Vec { + vec![] + } } impl Actor for Blog { diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 9a445298..60d20430 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use activity_pub::ActivityPub; use activity_pub::actor::Actor; +use activity_pub::outbox::Outbox; use db_conn::DbConn; use models::blog_authors::*; use models::blogs::*; @@ -55,3 +56,9 @@ fn create(conn: DbConn, data: Form, user: User) -> Redirect { Redirect::to(format!("/~/{}", slug).as_str()) } + +#[get("/~//outbox")] +fn outbox(name: String, conn: DbConn) -> Outbox { + let blog = Blog::find_by_actor_id(&*conn, name).unwrap(); + blog.outbox(&*conn) +}