Fix nodeinfo, expose peers

This commit is contained in:
asonix 2020-03-20 15:35:08 -05:00
parent 8efb00dc72
commit 43280f30fe
3 changed files with 30 additions and 14 deletions

View file

@ -118,7 +118,7 @@ impl Config {
UrlKind::Following => format!("{}://{}/following", scheme, self.hostname), UrlKind::Following => format!("{}://{}/following", scheme, self.hostname),
UrlKind::Inbox => format!("{}://{}/inbox", scheme, self.hostname), UrlKind::Inbox => format!("{}://{}/inbox", scheme, self.hostname),
UrlKind::MainKey => format!("{}://{}/actor#main-key", scheme, self.hostname), UrlKind::MainKey => format!("{}://{}/actor#main-key", scheme, self.hostname),
UrlKind::NodeInfo => format!("{}://{}/nodeinfo/2.0", scheme, self.hostname), UrlKind::NodeInfo => format!("{}://{}/nodeinfo/2.0.json", scheme, self.hostname),
UrlKind::Outbox => format!("{}://{}/outbox", scheme, self.hostname), UrlKind::Outbox => format!("{}://{}/outbox", scheme, self.hostname),
} }
} }

View file

@ -120,7 +120,7 @@ async fn main() -> Result<(), anyhow::Error> {
.route(web::post().to(inbox::inbox)), .route(web::post().to(inbox::inbox)),
) )
.service(web::resource("/actor").route(web::get().to(actor::route))) .service(web::resource("/actor").route(web::get().to(actor::route)))
.service(web::resource("/nodeinfo/2.0").route(web::get().to(nodeinfo::route))) .service(web::resource("/nodeinfo/2.0.json").route(web::get().to(nodeinfo::route)))
.service( .service(
web::scope("/.well-known") web::scope("/.well-known")
.service(actix_webfinger::scoped::<_, RelayResolver>()) .service(actix_webfinger::scoped::<_, RelayResolver>())

View file

@ -1,19 +1,26 @@
use crate::config::{Config, UrlKind}; use crate::{
config::{Config, UrlKind},
state::State,
};
use actix_web::{web, Responder}; use actix_web::{web, Responder};
use actix_webfinger::Link; use actix_webfinger::Link;
use std::collections::HashMap; use serde_json::json;
pub async fn well_known(config: web::Data<Config>) -> impl Responder { pub async fn well_known(config: web::Data<Config>) -> impl Responder {
web::Json(Link { web::Json(json!({
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(), "links": [
href: Some(config.generate_url(UrlKind::NodeInfo)), Link {
template: None, rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_owned(),
kind: None, href: Some(config.generate_url(UrlKind::NodeInfo)),
}) template: None,
kind: None,
}
]
}))
.with_header("Content-Type", "application/jrd+json") .with_header("Content-Type", "application/jrd+json")
} }
pub async fn route(config: web::Data<Config>) -> web::Json<NodeInfo> { pub async fn route(config: web::Data<Config>, state: web::Data<State>) -> web::Json<NodeInfo> {
web::Json(NodeInfo { web::Json(NodeInfo {
version: NodeInfoVersion, version: NodeInfoVersion,
software: Software { software: Software {
@ -27,7 +34,15 @@ pub async fn route(config: web::Data<Config>) -> web::Json<NodeInfo> {
local_posts: 0, local_posts: 0,
local_comments: 0, local_comments: 0,
}, },
metadata: Metadata::default(), metadata: Metadata {
peers: state
.listeners()
.await
.iter()
.filter_map(|listener| listener.as_url().domain())
.map(|s| s.to_owned())
.collect(),
},
}) })
} }
@ -69,8 +84,9 @@ pub struct Usage {
} }
#[derive(Clone, Debug, Default, serde::Serialize)] #[derive(Clone, Debug, Default, serde::Serialize)]
#[serde(transparent)] pub struct Metadata {
pub struct Metadata(pub HashMap<String, serde_json::Value>); peers: Vec<String>,
}
impl serde::ser::Serialize for NodeInfoVersion { impl serde::ser::Serialize for NodeInfoVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>