Move compute_box from Actor to Instance

This commit is contained in:
Bat 2018-06-21 18:42:17 +01:00
parent 606a3d12c7
commit 9a8472bdcc
4 changed files with 38 additions and 27 deletions

View file

@ -10,24 +10,19 @@ pub trait Actor: Sized {
fn get_instance(&self, conn: &PgConnection) -> Instance;
fn compute_outbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "outbox")
}
// fn compute_outbox(&self, conn: &PgConnection) -> String {
// self.compute_box(conn, "outbox")
// }
fn compute_inbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "inbox")
}
// fn compute_inbox(&self, conn: &PgConnection) -> String {
// self.compute_box(conn, "inbox")
// }
fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String {
format!("{id}/{name}", id = self.compute_id(conn), name = box_name)
}
// fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String {
// format!("{id}/{name}", id = self.compute_id(conn), name = box_name)
// }
fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!(
"{instance}/{prefix}/{user}",
instance = self.get_instance(conn).public_domain,
prefix = Self::get_box_prefix(),
user = self.get_actor_id()
))
}
// fn compute_id(&self, conn: &PgConnection) -> String {
// String::new()
// }
}

View file

@ -55,6 +55,8 @@ pub struct NewBlog {
pub public_key: String
}
const BLOG_PREFIX: &'static str = "~";
impl Blog {
insert!(blogs, NewBlog);
get!(blogs);
@ -142,21 +144,22 @@ impl Blog {
}
pub fn update_boxes(&self, conn: &PgConnection) {
let instance = self.get_instance(conn);
if self.outbox_url.len() == 0 {
diesel::update(self)
.set(blogs::outbox_url.eq(self.compute_outbox(conn)))
.set(blogs::outbox_url.eq(instance.compute_box(BLOG_PREFIX, self.actor_id.clone(), "outbox")))
.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)))
.set(blogs::inbox_url.eq(instance.compute_box(BLOG_PREFIX, self.actor_id.clone(), "inbox")))
.get_result::<Blog>(conn).expect("Couldn't update inbox URL");
}
if self.ap_url.len() == 0 {
diesel::update(self)
.set(blogs::ap_url.eq(self.compute_id(conn)))
.set(blogs::ap_url.eq(instance.compute_box(BLOG_PREFIX, self.actor_id.clone(), "")))
.get_result::<Blog>(conn).expect("Couldn't update AP URL");
}
}
@ -189,7 +192,7 @@ impl Blog {
Link {
rel: String::from("http://schemas.google.com/g/2010#updates-from"),
mime_type: Some(String::from("application/atom+xml")),
href: self.compute_box(conn, "feed.atom")
href: self.get_instance(conn).compute_box(BLOG_PREFIX, self.actor_id.clone(), "feed.atom")
},
Link {
rel: String::from("self"),

View file

@ -2,7 +2,7 @@ use chrono::NaiveDateTime;
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use std::iter::Iterator;
use activity_pub::inbox::Inbox;
use activity_pub::{ap_url, inbox::Inbox};
use models::users::User;
use schema::{instances, users};
@ -58,6 +58,16 @@ impl Instance {
.expect("Couldn't load admins")
.len() > 0
}
pub fn compute_box(&self, prefix: &'static str, name: String, box_name: &'static str) -> String {
ap_url(format!(
"{instance}/{prefix}/{name}/{box_name}",
instance = self.public_domain,
prefix = prefix,
name = name,
box_name = box_name
))
}
}
impl Inbox for Instance {}

View file

@ -83,6 +83,8 @@ pub struct NewUser {
pub shared_inbox_url: Option<String>
}
const USER_PREFIX: &'static str = "@";
impl User {
insert!(users, NewUser);
get!(users);
@ -196,21 +198,22 @@ impl User {
}
pub fn update_boxes(&self, conn: &PgConnection) {
let instance = self.get_instance(conn);
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");
.set(users::outbox_url.eq(instance.compute_box(USER_PREFIX, self.username.clone(), "outbox")))
.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)))
.set(users::inbox_url.eq(instance.compute_box(USER_PREFIX, self.username.clone(), "inbox")))
.get_result::<User>(conn).expect("Couldn't update inbox URL");
}
if self.ap_url.len() == 0 {
diesel::update(self)
.set(users::ap_url.eq(self.compute_id(conn)))
.set(users::ap_url.eq(instance.compute_box(USER_PREFIX, self.username.clone(), "")))
.get_result::<User>(conn).expect("Couldn't update AP URL");
}
@ -340,7 +343,7 @@ impl User {
Link {
rel: String::from("http://schemas.google.com/g/2010#updates-from"),
mime_type: Some(String::from("application/atom+xml")),
href: self.compute_box(conn, "feed.atom")
href: self.get_instance(conn).compute_box(USER_PREFIX, self.username.clone(), "feed.atom")
},
Link {
rel: String::from("self"),