From ac1a111d7b5870d5eac6c259d7b3af4a7ce1fccc Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 2 May 2018 13:47:46 +0100 Subject: [PATCH] Make it possible to test the federation locally And explain how to do it in the README --- .env | 1 - README.md | 22 ++++++++++++++++++++++ src/activity_pub/actor.rs | 11 ++++++----- src/activity_pub/mod.rs | 11 +++++++++++ src/activity_pub/webfinger.rs | 4 +++- src/main.rs | 2 +- src/routes/well_known.rs | 5 +++-- 7 files changed, 46 insertions(+), 10 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index ca0310d2..00000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -DB_URL=postgres://plume:plume@localhost/plume diff --git a/README.md b/README.md index 064614e3..4b0cd119 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,25 @@ cargo run You'll need Rust nightly. Once the app started, try to visit [localhost:8000](http://localhost:8000). + +To configure the instance (needed before you can do anything else), +go on [/configire](http://localhost:8000/configure). + +## Testing the federation + +To test the federation, you'll need to setup another database (see "Setup the database"), +also owned by the "plume" user, but with a different name. Then, you'll need to run the +migrations for this database too. + +``` +diesel migration run --database-url postgres://plume:plume@localhost/my_other_plume_db +``` + +To run this other instance, you'll need to give two environment variables: + +- `ROCKET_PORT`, the port on which your app will run +- `DB_NAME`, the name of the database you just created + +``` +ROCKET_PORT=3033 DB_NAME=my_other_plume_db cargo run +``` diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs index de520ba9..192d190d 100644 --- a/src/activity_pub/actor.rs +++ b/src/activity_pub/actor.rs @@ -1,7 +1,8 @@ use diesel::PgConnection; use reqwest::Client; -use activity_pub::{activity_pub, ActivityPub, context}; +use BASE_URL; +use activity_pub::{activity_pub, ActivityPub, context, ap_url}; use activity_pub::activity::Activity; use models::instance::Instance; @@ -40,7 +41,7 @@ pub trait Actor: Sized { "summary": "", "url": self.compute_id(conn), "endpoints": { - "sharedInbox": "https://plu.me/inbox" + "sharedInbox": ap_url(format!("{}/inbox", BASE_URL.as_str())) } })) } @@ -58,12 +59,12 @@ pub trait Actor: Sized { } fn compute_id(&self, conn: &PgConnection) -> String { - format!( - "https://{instance}/{prefix}/{user}", + ap_url(format!( + "{instance}/{prefix}/{user}", instance = self.get_instance(conn).public_domain, prefix = Self::get_box_prefix(), user = self.get_actor_id() - ) + )) } fn send_to_inbox(&self, conn: &PgConnection, act: Activity) { diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 03bcf958..2e68c021 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -16,6 +16,17 @@ pub type ActivityPub = Content; pub const CONTEXT_URL: &'static str = "https://www.w3.org/ns/activitystreams"; pub const PUBLIC_VISIBILTY: &'static str = "https://www.w3.org/ns/activitystreams#Public"; +#[cfg(debug_assertions)] +pub fn ap_url(url: String) -> String { + format!("http://{}", url) +} + +#[cfg(not(debug_assertions))] +pub fn ap_url(url: String) -> String { + format!("https://{}", url) +} + + pub fn context() -> serde_json::Value { json!([ CONTEXT_URL, diff --git a/src/activity_pub/webfinger.rs b/src/activity_pub/webfinger.rs index d9c28038..acc4f1ff 100644 --- a/src/activity_pub/webfinger.rs +++ b/src/activity_pub/webfinger.rs @@ -4,6 +4,8 @@ use reqwest::header::{Accept, qitem}; use reqwest::mime::Mime; use serde_json; +use activity_pub::ap_url; + pub trait Webfinger { fn webfinger_subject(&self, conn: &PgConnection) -> String; fn webfinger_aliases(&self, conn: &PgConnection) -> Vec; @@ -26,7 +28,7 @@ pub trait Webfinger { pub fn resolve(acct: String) -> Result { let instance = acct.split("@").last().unwrap(); - let url = format!("https://{}/.well-known/webfinger?resource=acct:{}", instance, acct); + let url = ap_url(format!("{}/.well-known/webfinger?resource=acct:{}", instance, acct)); Client::new() .get(&url[..]) .header(Accept(vec![qitem("application/jrd+json".parse::().unwrap())])) diff --git a/src/main.rs b/src/main.rs index 7dbf19cd..232eb499 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,7 @@ lazy_static! { .unwrap_or(format!("127.0.0.1:{}", env::var("ROCKET_PORT").unwrap_or(String::from("8000")))); pub static ref DB_URL: String = env::var("DB_URL") - .unwrap_or(format!("DATABASE_URL=postgres://plume:plume@localhost/{}", env::var("DB_TABLE").unwrap_or(String::from("plume")))); + .unwrap_or(format!("postgres://plume:plume@localhost/{}", env::var("DB_NAME").unwrap_or(String::from("plume")))); } type PgPool = Pool>; diff --git a/src/routes/well_known.rs b/src/routes/well_known.rs index c4977478..7a7a1ec0 100644 --- a/src/routes/well_known.rs +++ b/src/routes/well_known.rs @@ -2,6 +2,7 @@ use rocket::http::ContentType; use rocket::response::Content; use BASE_URL; +use activity_pub::ap_url; use activity_pub::webfinger::Webfinger; use db_conn::DbConn; use models::blogs::Blog; @@ -12,9 +13,9 @@ fn host_meta() -> String { format!(r#" - + - "#, domain = BASE_URL.as_str()) + "#, url = ap_url(format!("{domain}/.well-known/webfinger?resource={{uri}}", domain = BASE_URL.as_str()))) } #[derive(FromForm)]