From d00961ae861f17178f07ece7bd3edadd848d32e4 Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 21 Mar 2020 23:50:14 -0500 Subject: [PATCH] Add optional publish-blocks option --- README.md | 5 ++++- src/config.rs | 6 ++++++ src/nodeinfo.rs | 32 +++++++++++++++++++++----------- src/state.rs | 5 +++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b04b8b9..462b5b8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ $ ./relay -w asonix.dog blimps.xyz $ ./relay -uw asonix.dog blimps.xyz ``` -Whitelisted domains are only checked against incoming activities if `WHITELIST_MODE` is enabled +Whitelisted domains are only checked against incoming activities if `WHITELIST_MODE` is enabled. +Blocks can be published in the nodeinfo metadata by settings `PUBLISH_BLOCKS` to true ### Subscribing Mastodon admins can subscribe to this relay by adding the `/inbox` route to their relay settings. @@ -61,6 +62,7 @@ VALIDATE_SIGNATURES=false HTTPS=false DATABASE_URL= PRETTY_LOG=true +PUBLISH_BLOCKS=false ``` To run this server in production, you'll likely want to set most of them ```env @@ -73,6 +75,7 @@ VALIDATE_SIGNATURES=true HTTPS=true DATABASE_URL=postgres://pg_user:pg_pass@pg_host:pg_port/pg_database PRETTY_LOG=false +PUBLISH_BLOCKS=true ``` ### Contributing diff --git a/src/config.rs b/src/config.rs index 9b8b4ca..6483c1a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,7 @@ pub struct Config { https: bool, database_url: String, pretty_log: bool, + publish_blocks: bool, } pub enum UrlKind { @@ -42,6 +43,7 @@ impl Config { .set_default("validate_signatures", false)? .set_default("https", false)? .set_default("pretty_log", true)? + .set_default("publish_blocks", false)? .merge(Environment::new())?; Ok(config.try_into()?) @@ -79,6 +81,10 @@ impl Config { self.debug } + pub fn publish_blocks(&self) -> bool { + self.publish_blocks + } + pub fn whitelist_mode(&self) -> bool { self.whitelist_mode } diff --git a/src/nodeinfo.rs b/src/nodeinfo.rs index a47a6a9..c88b39b 100644 --- a/src/nodeinfo.rs +++ b/src/nodeinfo.rs @@ -4,22 +4,24 @@ use crate::{ }; use actix_web::{web, Responder}; use actix_webfinger::Link; -use serde_json::json; pub async fn well_known(config: web::Data) -> impl Responder { - web::Json(json!({ - "links": [ - Link { - rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(), - href: Some(config.generate_url(UrlKind::NodeInfo)), - template: None, - kind: None, - } - ] - })) + web::Json(Links { + links: vec![Link { + rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(), + href: Some(config.generate_url(UrlKind::NodeInfo)), + template: None, + kind: None, + }], + }) .with_header("Content-Type", "application/jrd+json") } +#[derive(serde::Serialize)] +struct Links { + links: Vec, +} + pub async fn route(config: web::Data, state: web::Data) -> web::Json { web::Json(NodeInfo { version: NodeInfoVersion, @@ -50,6 +52,11 @@ pub async fn route(config: web::Data, state: web::Data) -> web::J .filter_map(|listener| listener.as_url().domain()) .map(|s| s.to_owned()) .collect(), + blocks: if config.publish_blocks() { + Some(state.blocks().await) + } else { + None + }, }, }) } @@ -102,6 +109,9 @@ pub struct Usage { #[derive(Clone, Debug, Default, serde::Serialize)] pub struct Metadata { peers: Vec, + + #[serde(skip_serializing_if = "Option::is_none")] + blocks: Option>, } #[derive(Clone, Debug, Default, serde::Serialize)] diff --git a/src/state.rs b/src/state.rs index 55ec58d..40e2b0f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -65,6 +65,11 @@ impl State { read_guard.iter().cloned().collect() } + pub async fn blocks(&self) -> Vec { + let read_guard = self.blocks.read().await; + read_guard.iter().cloned().collect() + } + pub async fn listeners_without(&self, inbox: &XsdAnyUri, domain: &str) -> Vec { let read_guard = self.listeners.read().await;