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 get_instance(&self, conn: &PgConnection) -> Instance;
fn compute_outbox(&self, conn: &PgConnection) -> String { // fn compute_outbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "outbox") // self.compute_box(conn, "outbox")
} // }
fn compute_inbox(&self, conn: &PgConnection) -> String { // fn compute_inbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "inbox") // self.compute_box(conn, "inbox")
} // }
fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String { // fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String {
format!("{id}/{name}", id = self.compute_id(conn), name = box_name) // format!("{id}/{name}", id = self.compute_id(conn), name = box_name)
} // }
fn compute_id(&self, conn: &PgConnection) -> String { // fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!( // String::new()
"{instance}/{prefix}/{user}", // }
instance = self.get_instance(conn).public_domain,
prefix = Self::get_box_prefix(),
user = self.get_actor_id()
))
}
} }

View File

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

View File

@ -2,7 +2,7 @@ use chrono::NaiveDateTime;
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use std::iter::Iterator; use std::iter::Iterator;
use activity_pub::inbox::Inbox; use activity_pub::{ap_url, inbox::Inbox};
use models::users::User; use models::users::User;
use schema::{instances, users}; use schema::{instances, users};
@ -58,6 +58,16 @@ impl Instance {
.expect("Couldn't load admins") .expect("Couldn't load admins")
.len() > 0 .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 {} impl Inbox for Instance {}

View File

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