Compute outbox/inbox URLs from activity_pub::Actor

This commit is contained in:
Bat 2018-04-23 14:12:59 +01:00
parent 2c3d9a2309
commit 9240ca3a84
4 changed files with 87 additions and 35 deletions

View File

@ -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<Blog> {
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::<Blog>(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::<Blog>(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
}
}
}

View File

@ -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<User> {
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::<User>(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::<User>(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
}
}
}

View File

@ -30,14 +30,12 @@ fn create(conn: DbConn, data: Form<NewBlogForm>, _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())
}

View File

@ -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<NewUserForm>) -> 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())