Make it possible to test the federation locally

And explain how to do it in the README
This commit is contained in:
Bat 2018-05-02 13:47:46 +01:00
parent 5f43f783b6
commit ac1a111d7b
7 changed files with 46 additions and 10 deletions

1
.env
View file

@ -1 +0,0 @@
DB_URL=postgres://plume:plume@localhost/plume

View file

@ -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
```

View file

@ -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) {

View file

@ -16,6 +16,17 @@ pub type ActivityPub = Content<Json>;
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,

View file

@ -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<String>;
@ -26,7 +28,7 @@ pub trait Webfinger {
pub fn resolve(acct: String) -> Result<String, String> {
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::<Mime>().unwrap())]))

View file

@ -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<ConnectionManager<PgConnection>>;

View file

@ -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#"
<?xml version="1.0"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Link rel="lrdd" type="application/xrd+xml" template="https://{domain}/.well-known/webfinger?resource={{uri}}"/>
<Link rel="lrdd" type="application/xrd+xml" template="{url}"/>
</XRD>
"#, domain = BASE_URL.as_str())
"#, url = ap_url(format!("{domain}/.well-known/webfinger?resource={{uri}}", domain = BASE_URL.as_str())))
}
#[derive(FromForm)]