relay/src/nodeinfo.rs

99 lines
2.4 KiB
Rust
Raw Normal View History

2020-03-20 20:35:08 +00:00
use crate::{
config::{Config, UrlKind},
state::State,
};
2020-03-19 19:05:16 +00:00
use actix_web::{web, Responder};
use actix_webfinger::Link;
2020-03-20 20:35:08 +00:00
use serde_json::json;
2020-03-19 19:05:16 +00:00
pub async fn well_known(config: web::Data<Config>) -> impl Responder {
2020-03-20 20:35:08 +00:00
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,
}
]
}))
2020-03-19 19:05:16 +00:00
.with_header("Content-Type", "application/jrd+json")
}
2020-03-20 20:35:08 +00:00
pub async fn route(config: web::Data<Config>, state: web::Data<State>) -> web::Json<NodeInfo> {
2020-03-19 19:05:16 +00:00
web::Json(NodeInfo {
version: NodeInfoVersion,
software: Software {
name: config.software_name(),
version: config.software_version(),
2020-03-19 19:05:16 +00:00
},
protocols: vec![Protocol::ActivityPub],
services: vec![],
open_registrations: false,
usage: Usage {
local_posts: 0,
local_comments: 0,
},
2020-03-20 20:35:08 +00:00
metadata: Metadata {
peers: state
.listeners()
.await
.iter()
.filter_map(|listener| listener.as_url().domain())
.map(|s| s.to_owned())
.collect(),
},
2020-03-19 19:05:16 +00:00
})
}
#[derive(Clone, Debug, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeInfo {
version: NodeInfoVersion,
software: Software,
protocols: Vec<Protocol>,
services: Vec<Service>,
open_registrations: bool,
usage: Usage,
metadata: Metadata,
}
#[derive(Clone, Debug, Default)]
pub struct NodeInfoVersion;
#[derive(Clone, Debug, Default, serde::Serialize)]
pub struct Software {
name: String,
version: String,
}
#[derive(Clone, Debug, serde::Serialize)]
2020-03-20 18:44:34 +00:00
#[serde(rename_all = "lowercase")]
2020-03-19 19:05:16 +00:00
pub enum Protocol {
ActivityPub,
}
#[derive(Clone, Debug, serde::Serialize)]
pub enum Service {}
#[derive(Clone, Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Usage {
local_posts: u64,
local_comments: u64,
}
#[derive(Clone, Debug, Default, serde::Serialize)]
2020-03-20 20:35:08 +00:00
pub struct Metadata {
peers: Vec<String>,
}
2020-03-19 19:05:16 +00:00
impl serde::ser::Serialize for NodeInfoVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str("2.0")
}
}