Add ActivityPub endpoint for actors

This commit is contained in:
Bat 2018-04-23 16:09:05 +01:00
parent 9eb1b987b2
commit 7b3a884ec6
6 changed files with 29 additions and 4 deletions

1
Cargo.lock generated
View File

@ -473,6 +473,7 @@ dependencies = [
"rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "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_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)", "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]] [[package]]

View File

@ -8,6 +8,7 @@ dotenv = "*"
heck = "0.3.0" heck = "0.3.0"
rocket = "*" rocket = "*"
rocket_codegen = "*" rocket_codegen = "*"
serde_json = "1.0"
[dependencies.diesel] [dependencies.diesel]
features = ["postgres", "r2d2"] features = ["postgres", "r2d2"]

View File

@ -1,5 +1,6 @@
use models::instance::Instance; use models::instance::Instance;
use diesel::PgConnection; use diesel::PgConnection;
use serde_json::Value;
pub trait Actor { pub trait Actor {
fn get_box_prefix() -> &'static str; fn get_box_prefix() -> &'static str;
@ -8,6 +9,10 @@ pub trait Actor {
fn get_instance(&self, conn: &PgConnection) -> Instance; fn get_instance(&self, conn: &PgConnection) -> Instance;
fn as_activity_pub (&self) -> Value {
json!({})
}
fn compute_outbox(&self, conn: &PgConnection) -> String { fn compute_outbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "outbox") self.compute_box(conn, "outbox")
} }

View File

@ -8,6 +8,8 @@ extern crate rocket;
extern crate rocket_contrib; extern crate rocket_contrib;
extern crate bcrypt; extern crate bcrypt;
extern crate heck; extern crate heck;
#[macro_use]
extern crate serde_json;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool};
@ -56,6 +58,7 @@ fn main() {
routes::user::me, routes::user::me,
routes::user::details, routes::user::details,
routes::user::activity,
routes::user::new, routes::user::new,
routes::user::create, routes::user::create,
@ -64,6 +67,7 @@ fn main() {
routes::session::delete, routes::session::delete,
routes::blogs::details, routes::blogs::details,
routes::blogs::activity,
routes::blogs::new, routes::blogs::new,
routes::blogs::create, routes::blogs::create,

View File

@ -1,6 +1,6 @@
use rocket::request::Form; use rocket::request::Form;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_contrib::Template; use rocket_contrib::{Json, Template};
use std::collections::HashMap; use std::collections::HashMap;
use utils; use utils;
@ -9,12 +9,19 @@ use models::blogs::*;
use models::blog_authors::*; use models::blog_authors::*;
use models::instance::Instance; use models::instance::Instance;
use models::user::User; use models::user::User;
use activity_pub::Actor;
#[get("/~/<name>")] #[get("/~/<name>", rank = 2)]
fn details(name: String) -> String { fn details(name: String) -> String {
format!("Welcome on ~{}", name) format!("Welcome on ~{}", name)
} }
#[get("/~/<name>", 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")] #[get("/blogs/new")]
fn new(_user: User) -> Template { fn new(_user: User) -> Template {
Template::render("blogs/new", HashMap::<String, i32>::new()) Template::render("blogs/new", HashMap::<String, i32>::new())

View File

@ -1,22 +1,29 @@
use rocket::request::Form; use rocket::request::Form;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_contrib::Template; use rocket_contrib::{Json, Template};
use std::collections::HashMap; use std::collections::HashMap;
use db_conn::DbConn; use db_conn::DbConn;
use models::user::*; use models::user::*;
use models::instance::Instance; use models::instance::Instance;
use activity_pub::Actor;
#[get("/me")] #[get("/me")]
fn me(user: User) -> String { fn me(user: User) -> String {
format!("Logged in as {}", user.username.to_string()) format!("Logged in as {}", user.username.to_string())
} }
#[get("/@/<name>")] #[get("/@/<name>", rank = 2)]
fn details(name: String) -> String { fn details(name: String) -> String {
format!("Hello, @{}", name) format!("Hello, @{}", name)
} }
#[get("/@/<name>", 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")] #[get("/users/new")]
fn new() -> Template { fn new() -> Template {
Template::render("users/new", HashMap::<String, i32>::new()) Template::render("users/new", HashMap::<String, i32>::new())