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") .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> { pub fn get(conn: &PgConnection, id: i32) -> Option<Blog> {
blogs::table.filter(blogs::id.eq(id)) blogs::table.filter(blogs::id.eq(id))
.limit(1) .limit(1)
@ -57,6 +49,20 @@ impl Blog {
.expect("Error loading blog by email") .expect("Error loading blog by email")
.into_iter().nth(0) .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 { impl Actor for Blog {
@ -72,3 +78,21 @@ impl Actor for Blog {
Instance::get(conn, self.instance_id).unwrap() 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") .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> { pub fn get(conn: &PgConnection, id: i32) -> Option<User> {
users::table.filter(users::id.eq(id)) users::table.filter(users::id.eq(id))
.limit(1) .limit(1)
@ -88,6 +80,20 @@ impl User {
pub fn auth(&self, pass: String) -> bool { pub fn auth(&self, pass: String) -> bool {
bcrypt::verify(pass.as_str(), self.hashed_password.clone().unwrap().as_str()).is_ok() 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 { impl<'a, 'r> FromRequest<'a, 'r> for User {
@ -116,3 +122,28 @@ impl Actor for User {
Instance::get(conn, self.instance_id).unwrap() 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 form = data.get();
let slug = utils::make_actor_id(form.title.to_string()); let slug = utils::make_actor_id(form.title.to_string());
Blog::insert(&*conn, NewBlog { Blog::insert(&*conn, NewBlog::new_local(
actor_id: slug.to_string(), slug.to_string(),
title: form.title.to_string(), form.title.to_string(),
summary: String::from(""), String::from(""),
outbox_url: Blog::compute_outbox(slug.to_string(), inst.public_domain.to_string()), inst.id
inbox_url: Blog::compute_inbox(slug.to_string(), inst.public_domain.to_string()), )).update_boxes(&*conn);
instance_id: inst.id
});
Redirect::to(format!("/~/{}", slug).as_str()) Redirect::to(format!("/~/{}", slug).as_str())
} }

View file

@ -6,6 +6,7 @@ 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 {
@ -36,17 +37,15 @@ fn create(conn: DbConn, data: Form<NewUserForm>) -> Redirect {
let form = data.get(); let form = data.get();
if form.password == form.password_confirmation { if form.password == form.password_confirmation {
User::insert(&*conn, NewUser { User::insert(&*conn, NewUser::new_local(
username: form.username.to_string(), form.username.to_string(),
display_name: form.username.to_string(), form.username.to_string(),
outbox_url: User::compute_outbox(form.username.to_string(), inst.public_domain.to_string()), !inst.has_admin(&*conn),
inbox_url: User::compute_inbox(form.username.to_string(), inst.public_domain.to_string()), String::from(""),
is_admin: !inst.has_admin(&*conn), form.email.to_string(),
summary: String::from(""), User::hash_pass(form.password.to_string()),
email: Some(form.email.to_string()), inst.id
hashed_password: Some(User::hash_pass(form.password.to_string())), )).update_boxes(&*conn);
instance_id: inst.id
});
} }
Redirect::to(format!("/@/{}", data.get().username).as_str()) Redirect::to(format!("/@/{}", data.get().username).as_str())