1 use actix_web::{body::Body, error::ErrorBadRequest, *};
3 use lemmy_api::version;
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 lemmy_websocket::LemmyContext;
8 use serde::{Deserialize, Serialize};
11 pub fn config(cfg: &mut web::ServiceConfig) {
13 .route("/nodeinfo/2.0.json", web::get().to(node_info))
14 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
17 async fn node_info_well_known() -> Result<HttpResponse<Body>, LemmyError> {
18 let node_info = NodeInfoWellKnown {
19 links: NodeInfoWellKnownLinks {
20 rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
21 href: Url::parse(&format!(
22 "{}://{}/nodeinfo/2.0.json",
23 get_apub_protocol_string(),
24 Settings::get().hostname
28 Ok(HttpResponse::Ok().json(node_info))
31 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
32 let site_view = blocking(context.pool(), SiteView::read)
34 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
36 let protocols = if Settings::get().federation.enabled {
37 vec!["activitypub".to_string()]
43 version: "2.0".to_string(),
44 software: NodeInfoSoftware {
45 name: "lemmy".to_string(),
46 version: version::VERSION.to_string(),
49 usage: NodeInfoUsage {
50 users: NodeInfoUsers {
51 total: site_view.number_of_users,
53 local_posts: site_view.number_of_posts,
54 local_comments: site_view.number_of_comments,
55 open_registrations: site_view.open_registration,
59 Ok(HttpResponse::Ok().json(json))
62 #[derive(Serialize, Deserialize, Debug)]
63 pub struct NodeInfoWellKnown {
64 pub links: NodeInfoWellKnownLinks,
67 #[derive(Serialize, Deserialize, Debug)]
68 pub struct NodeInfoWellKnownLinks {
73 #[derive(Serialize, Deserialize, Debug)]
76 pub software: NodeInfoSoftware,
77 pub protocols: Vec<String>,
78 pub usage: NodeInfoUsage,
81 #[derive(Serialize, Deserialize, Debug)]
82 pub struct NodeInfoSoftware {
87 #[derive(Serialize, Deserialize, Debug)]
88 #[serde(rename_all = "camelCase")]
89 pub struct NodeInfoUsage {
90 pub users: NodeInfoUsers,
92 pub local_comments: i64,
93 pub open_registrations: bool,
96 #[derive(Serialize, Deserialize, Debug)]
97 pub struct NodeInfoUsers {