1 use crate::{version, LemmyContext};
2 use actix_web::{body::Body, error::ErrorBadRequest, *};
4 use lemmy_db::site_view::SiteView;
5 use lemmy_structs::blocking;
6 use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
7 use serde::{Deserialize, Serialize};
10 pub fn config(cfg: &mut web::ServiceConfig) {
12 .route("/nodeinfo/2.0.json", web::get().to(node_info))
13 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
16 async fn node_info_well_known() -> Result<HttpResponse<Body>, LemmyError> {
17 let node_info = NodeInfoWellKnown {
18 links: NodeInfoWellKnownLinks {
19 rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
20 href: Url::parse(&format!(
21 "{}://{}/nodeinfo/2.0.json",
22 get_apub_protocol_string(),
23 Settings::get().hostname
27 Ok(HttpResponse::Ok().json(node_info))
30 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
31 let site_view = blocking(context.pool(), SiteView::read)
33 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
35 let protocols = if Settings::get().federation.enabled {
36 vec!["activitypub".to_string()]
42 version: "2.0".to_string(),
43 software: NodeInfoSoftware {
44 name: "lemmy".to_string(),
45 version: version::VERSION.to_string(),
48 usage: NodeInfoUsage {
49 users: NodeInfoUsers {
50 total: site_view.number_of_users,
52 local_posts: site_view.number_of_posts,
53 local_comments: site_view.number_of_comments,
54 open_registrations: site_view.open_registration,
58 Ok(HttpResponse::Ok().json(json))
61 #[derive(Serialize, Deserialize, Debug)]
62 pub struct NodeInfoWellKnown {
63 pub links: NodeInfoWellKnownLinks,
66 #[derive(Serialize, Deserialize, Debug)]
67 pub struct NodeInfoWellKnownLinks {
72 #[derive(Serialize, Deserialize, Debug)]
75 pub software: NodeInfoSoftware,
76 pub protocols: Vec<String>,
77 pub usage: NodeInfoUsage,
80 #[derive(Serialize, Deserialize, Debug)]
81 pub struct NodeInfoSoftware {
86 #[derive(Serialize, Deserialize, Debug)]
87 #[serde(rename_all = "camelCase")]
88 pub struct NodeInfoUsage {
89 pub users: NodeInfoUsers,
91 pub local_comments: i64,
92 pub open_registrations: bool,
95 #[derive(Serialize, Deserialize, Debug)]
96 pub struct NodeInfoUsers {