diff --git a/migrations/2018-05-13-175144_users_add_shared_inbox/down.sql b/migrations/2018-05-13-175144_users_add_shared_inbox/down.sql new file mode 100644 index 00000000..cd33ad7b --- /dev/null +++ b/migrations/2018-05-13-175144_users_add_shared_inbox/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE users DROP COLUMN shared_inbox_url; diff --git a/migrations/2018-05-13-175144_users_add_shared_inbox/up.sql b/migrations/2018-05-13-175144_users_add_shared_inbox/up.sql new file mode 100644 index 00000000..a16875f5 --- /dev/null +++ b/migrations/2018-05-13-175144_users_add_shared_inbox/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE users ADD COLUMN shared_inbox_url VARCHAR; diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 928dddd1..2d26da35 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -27,7 +27,6 @@ pub fn ap_url(url: String) -> String { format!("https://{}", url) } - pub fn context() -> serde_json::Value { json!([ CONTEXT_URL, diff --git a/src/models/users.rs b/src/models/users.rs index a62d3c65..511cbc41 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -16,6 +16,7 @@ use std::sync::Arc; use url::Url; use BASE_URL; +use activity_pub::ap_url; use activity_pub::activity::{Create, Activity}; use activity_pub::actor::{ActorType, Actor}; use activity_pub::inbox::Inbox; @@ -48,7 +49,8 @@ pub struct User { pub creation_date: NaiveDateTime, pub ap_url: String, pub private_key: Option, - pub public_key: String + pub public_key: String, + pub shared_inbox_url: Option } #[derive(Insertable)] @@ -65,7 +67,8 @@ pub struct NewUser { pub instance_id: i32, pub ap_url: String, pub private_key: Option, - pub public_key: String + pub public_key: String, + pub shared_inbox_url: Option } impl User { @@ -182,7 +185,8 @@ impl User { instance_id: instance.id, ap_url: acct["id"].as_str().unwrap().to_string(), public_key: acct["publicKey"]["publicKeyPem"].as_str().unwrap().to_string(), - private_key: None + private_key: None, + shared_inbox_url: acct["endpoints"]["sharedInbox"].as_str().map(|s| s.to_string()) }) } @@ -212,6 +216,12 @@ impl User { .set(users::ap_url.eq(self.compute_id(conn))) .get_result::(conn).expect("Couldn't update AP URL"); } + + if self.shared_inbox_url.is_none() { + diesel::update(self) + .set(users::shared_inbox_url.eq(ap_url(format!("{}/inbox", Instance::get_local(conn).unwrap().public_domain)))) + .get_result::(conn).expect("Couldn't update shared inbox URL"); + } } pub fn outbox(&self, conn: &PgConnection) -> Outbox { @@ -305,7 +315,7 @@ impl Actor for User { } fn get_shared_inbox_url(&self) -> Option { - None + self.shared_inbox_url.clone() } fn custom_props(&self, conn: &PgConnection) -> serde_json::Map { @@ -450,7 +460,8 @@ impl NewUser { instance_id: instance_id, ap_url: String::from(""), public_key: String::from_utf8(pub_key).unwrap(), - private_key: Some(String::from_utf8(priv_key).unwrap()) + private_key: Some(String::from_utf8(priv_key).unwrap()), + shared_inbox_url: None } } } diff --git a/src/schema.rs b/src/schema.rs index 264a2b8d..0edf8f7b 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -114,6 +114,7 @@ table! { ap_url -> Text, private_key -> Nullable, public_key -> Text, + shared_inbox_url -> Nullable, } }