relay/src/routes/nodeinfo.rs

133 lines
3.2 KiB
Rust
Raw Normal View History

2020-03-20 20:35:08 +00:00
use crate::{
config::{Config, UrlKind},
2020-03-23 22:17:53 +00:00
data::State,
2020-03-20 20:35:08 +00:00
};
2020-03-19 19:05:16 +00:00
use actix_web::{web, Responder};
use actix_webfinger::Link;
pub async fn well_known(config: web::Data<Config>) -> impl Responder {
2020-03-22 04:50:14 +00:00
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,
}],
})
2020-03-19 19:05:16 +00:00
.with_header("Content-Type", "application/jrd+json")
}
2020-03-22 04:50:14 +00:00
#[derive(serde::Serialize)]
struct Links {
links: Vec<Link>,
}
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],
2020-03-20 21:19:14 +00:00
services: Services {
inbound: vec![],
outbound: vec![],
},
2020-03-19 19:05:16 +00:00
open_registrations: false,
usage: Usage {
2020-03-20 21:19:14 +00:00
users: Users {
total: 1,
active_halfyear: 1,
active_month: 1,
},
2020-03-19 19:05:16 +00:00
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-22 04:50:14 +00:00
blocks: if config.publish_blocks() {
Some(state.blocks().await)
} else {
None
},
2020-03-20 20:35:08 +00:00
},
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>,
2020-03-20 21:19:14 +00:00
services: Services,
2020-03-19 19:05:16 +00:00
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)]
2020-03-20 21:19:14 +00:00
pub struct Services {
inbound: Vec<Service>,
outbound: Vec<Service>,
}
#[derive(Clone, Debug, serde::Serialize)]
#[serde(rename_all = "lowercase")]
2020-03-19 19:05:16 +00:00
pub enum Service {}
#[derive(Clone, Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Usage {
2020-03-20 21:19:14 +00:00
users: Users,
2020-03-19 19:05:16 +00:00
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-22 04:50:14 +00:00
#[serde(skip_serializing_if = "Option::is_none")]
blocks: Option<Vec<String>>,
2020-03-20 20:35:08 +00:00
}
2020-03-19 19:05:16 +00:00
2020-03-20 21:19:14 +00:00
#[derive(Clone, Debug, Default, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Users {
total: u64,
active_halfyear: u64,
active_month: u64,
}
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")
}
}