From 7b3a884ec6b72f19d88f9f75759fddf72bd7b5cb Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 23 Apr 2018 16:09:05 +0100 Subject: [PATCH] Add ActivityPub endpoint for actors --- Cargo.lock | 1 + Cargo.toml | 1 + src/activity_pub/mod.rs | 5 +++++ src/main.rs | 4 ++++ src/routes/blogs.rs | 11 +++++++++-- src/routes/user.rs | 11 +++++++++-- 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f33cfc..b3d2bb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,6 +473,7 @@ 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_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index babc56a..9cce924 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ dotenv = "*" heck = "0.3.0" rocket = "*" rocket_codegen = "*" +serde_json = "1.0" [dependencies.diesel] features = ["postgres", "r2d2"] diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 21c73fe..c93f36c 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -1,5 +1,6 @@ use models::instance::Instance; use diesel::PgConnection; +use serde_json::Value; pub trait Actor { fn get_box_prefix() -> &'static str; @@ -8,6 +9,10 @@ pub trait Actor { fn get_instance(&self, conn: &PgConnection) -> Instance; + fn as_activity_pub (&self) -> Value { + json!({}) + } + fn compute_outbox(&self, conn: &PgConnection) -> String { self.compute_box(conn, "outbox") } diff --git a/src/main.rs b/src/main.rs index 904c6fa..b779342 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ extern crate rocket; extern crate rocket_contrib; extern crate bcrypt; extern crate heck; +#[macro_use] +extern crate serde_json; use diesel::pg::PgConnection; use diesel::r2d2::{ConnectionManager, Pool}; @@ -56,6 +58,7 @@ fn main() { routes::user::me, routes::user::details, + routes::user::activity, routes::user::new, routes::user::create, @@ -64,6 +67,7 @@ fn main() { routes::session::delete, routes::blogs::details, + routes::blogs::activity, routes::blogs::new, routes::blogs::create, diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 14a0b44..d1d06fd 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -1,6 +1,6 @@ use rocket::request::Form; use rocket::response::Redirect; -use rocket_contrib::Template; +use rocket_contrib::{Json, Template}; use std::collections::HashMap; use utils; @@ -9,12 +9,19 @@ use models::blogs::*; use models::blog_authors::*; use models::instance::Instance; use models::user::User; +use activity_pub::Actor; -#[get("/~/")] +#[get("/~/", rank = 2)] fn details(name: String) -> String { format!("Welcome on ~{}", name) } +#[get("/~/", format = "application/activity+json", rank = 1)] +fn activity(name: String, conn: DbConn) -> Json { + let blog = Blog::find_by_actor_id(&*conn, name).unwrap(); + Json(blog.as_activity_pub()) +} + #[get("/blogs/new")] fn new(_user: User) -> Template { Template::render("blogs/new", HashMap::::new()) diff --git a/src/routes/user.rs b/src/routes/user.rs index c875029..f5857f8 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -1,22 +1,29 @@ use rocket::request::Form; use rocket::response::Redirect; -use rocket_contrib::Template; +use rocket_contrib::{Json, Template}; use std::collections::HashMap; use db_conn::DbConn; use models::user::*; use models::instance::Instance; +use activity_pub::Actor; #[get("/me")] fn me(user: User) -> String { format!("Logged in as {}", user.username.to_string()) } -#[get("/@/")] +#[get("/@/", rank = 2)] fn details(name: String) -> String { format!("Hello, @{}", name) } +#[get("/@/", format = "application/activity+json", rank = 1)] +fn activity(name: String, conn: DbConn) -> Json { + let user = User::find_by_name(&*conn, name).unwrap(); + Json(user.as_activity_pub()) +} + #[get("/users/new")] fn new() -> Template { Template::render("users/new", HashMap::::new())