From 9a8472bdccd44268738a40711807fe0b5ab38aa1 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 21 Jun 2018 18:42:17 +0100 Subject: [PATCH] Move compute_box from Actor to Instance --- src/activity_pub/actor.rs | 29 ++++++++++++----------------- src/models/blogs.rs | 11 +++++++---- src/models/instance.rs | 12 +++++++++++- src/models/users.rs | 13 ++++++++----- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs index e5001286..b699652e 100644 --- a/src/activity_pub/actor.rs +++ b/src/activity_pub/actor.rs @@ -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() + // } } diff --git a/src/models/blogs.rs b/src/models/blogs.rs index e62fa08c..f2fdd9a9 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -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::(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::(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::(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"), diff --git a/src/models/instance.rs b/src/models/instance.rs index b627718b..b93a92ab 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -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 {} diff --git a/src/models/users.rs b/src/models/users.rs index 8ddf5a3b..f55edce5 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -83,6 +83,8 @@ pub struct NewUser { pub shared_inbox_url: Option } +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::(conn).expect("Couldn't update outbox URL"); + .set(users::outbox_url.eq(instance.compute_box(USER_PREFIX, self.username.clone(), "outbox"))) + .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))) + .set(users::inbox_url.eq(instance.compute_box(USER_PREFIX, self.username.clone(), "inbox"))) .get_result::(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::(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"),