From 9240ca3a84cebd3968d487505f0f56e371913ad2 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 23 Apr 2018 14:12:59 +0100 Subject: [PATCH] Compute outbox/inbox URLs from activity_pub::Actor --- src/models/blogs.rs | 40 ++++++++++++++++++++++++++++++-------- src/models/user.rs | 47 +++++++++++++++++++++++++++++++++++++-------- src/routes/blogs.rs | 14 ++++++-------- src/routes/user.rs | 21 ++++++++++---------- 4 files changed, 87 insertions(+), 35 deletions(-) diff --git a/src/models/blogs.rs b/src/models/blogs.rs index 8bf31d65..7dfe5705 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -34,14 +34,6 @@ impl Blog { .expect("Error saving new blog") } - pub fn compute_outbox(blog: String, hostname: String) -> String { - format!("https://{}/~/{}/outbox", hostname, blog) - } - - pub fn compute_inbox(blog: String, hostname: String) -> String { - format!("https://{}/~/{}/inbox", hostname, blog) - } - pub fn get(conn: &PgConnection, id: i32) -> Option { blogs::table.filter(blogs::id.eq(id)) .limit(1) @@ -57,6 +49,20 @@ impl Blog { .expect("Error loading blog by email") .into_iter().nth(0) } + + pub fn update_boxes(&self, conn: &PgConnection) { + if self.outbox_url.len() == 0 { + diesel::update(self) + .set(blogs::outbox_url.eq(self.compute_outbox(conn))) + .get_result::(conn).expect("Couldn't update outbox URL"); + } + + if self.inbox_url.len() == 0 { + diesel::update(self) + .set(blogs::inbox_url.eq(self.compute_inbox(conn))) + .get_result::(conn).expect("Couldn't update inbox URL"); + } + } } impl Actor for Blog { @@ -72,3 +78,21 @@ impl Actor for Blog { Instance::get(conn, self.instance_id).unwrap() } } + +impl NewBlog { + pub fn new_local( + actor_id: String, + title: String, + summary: String, + instance_id: i32 + ) -> NewBlog { + NewBlog { + actor_id: actor_id, + title: title, + summary: summary, + outbox_url: String::from(""), + inbox_url: String::from(""), + instance_id: instance_id + } + } +} diff --git a/src/models/user.rs b/src/models/user.rs index 4d9afe1c..681dae36 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -49,14 +49,6 @@ impl User { .expect("Error saving new user") } - pub fn compute_outbox(user: String, hostname: String) -> String { - format!("https://{}/@/{}/outbox", hostname, user) - } - - pub fn compute_inbox(user: String, hostname: String) -> String { - format!("https://{}/@/{}/inbox", hostname, user) - } - pub fn get(conn: &PgConnection, id: i32) -> Option { users::table.filter(users::id.eq(id)) .limit(1) @@ -88,6 +80,20 @@ impl User { pub fn auth(&self, pass: String) -> bool { bcrypt::verify(pass.as_str(), self.hashed_password.clone().unwrap().as_str()).is_ok() } + + pub fn update_boxes(&self, conn: &PgConnection) { + if self.outbox_url.len() == 0 { + diesel::update(self) + .set(users::outbox_url.eq(self.compute_outbox(conn))) + .get_result::(conn).expect("Couldn't update outbox URL"); + } + + if self.inbox_url.len() == 0 { + diesel::update(self) + .set(users::inbox_url.eq(self.compute_inbox(conn))) + .get_result::(conn).expect("Couldn't update outbox URL"); + } + } } impl<'a, 'r> FromRequest<'a, 'r> for User { @@ -116,3 +122,28 @@ impl Actor for User { Instance::get(conn, self.instance_id).unwrap() } } + +impl NewUser { + /// Creates a new local user + pub fn new_local( + username: String, + display_name: String, + is_admin: bool, + summary: String, + email: String, + password: String, + instance_id: i32 + ) -> NewUser { + NewUser { + username: username, + display_name: display_name, + outbox_url: String::from(""), + inbox_url: String::from(""), + is_admin: is_admin, + summary: summary, + email: Some(email), + hashed_password: Some(password), + instance_id: instance_id + } + } +} diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 9ddc6221..a6d42e7c 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -30,14 +30,12 @@ fn create(conn: DbConn, data: Form, _user: User) -> Redirect { let form = data.get(); let slug = utils::make_actor_id(form.title.to_string()); - Blog::insert(&*conn, NewBlog { - actor_id: slug.to_string(), - title: form.title.to_string(), - summary: String::from(""), - outbox_url: Blog::compute_outbox(slug.to_string(), inst.public_domain.to_string()), - inbox_url: Blog::compute_inbox(slug.to_string(), inst.public_domain.to_string()), - instance_id: inst.id - }); + Blog::insert(&*conn, NewBlog::new_local( + slug.to_string(), + form.title.to_string(), + String::from(""), + inst.id + )).update_boxes(&*conn); Redirect::to(format!("/~/{}", slug).as_str()) } diff --git a/src/routes/user.rs b/src/routes/user.rs index a328fdea..1cc3a097 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -6,6 +6,7 @@ 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 { @@ -36,17 +37,15 @@ fn create(conn: DbConn, data: Form) -> Redirect { let form = data.get(); if form.password == form.password_confirmation { - User::insert(&*conn, NewUser { - username: form.username.to_string(), - display_name: form.username.to_string(), - outbox_url: User::compute_outbox(form.username.to_string(), inst.public_domain.to_string()), - inbox_url: User::compute_inbox(form.username.to_string(), inst.public_domain.to_string()), - is_admin: !inst.has_admin(&*conn), - summary: String::from(""), - email: Some(form.email.to_string()), - hashed_password: Some(User::hash_pass(form.password.to_string())), - instance_id: inst.id - }); + User::insert(&*conn, NewUser::new_local( + form.username.to_string(), + form.username.to_string(), + !inst.has_admin(&*conn), + String::from(""), + form.email.to_string(), + User::hash_pass(form.password.to_string()), + inst.id + )).update_boxes(&*conn); } Redirect::to(format!("/@/{}", data.get().username).as_str())