1 use actix_web::{body::Body, error::ErrorBadRequest, *};
3 use lemmy_api_common::blocking;
4 use lemmy_db_views::site_view::SiteView;
5 use lemmy_utils::{settings::structs::Settings, version, LemmyError};
6 use lemmy_websocket::LemmyContext;
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 Settings::get().get_protocol_and_hostname()
26 Ok(HttpResponse::Ok().json(node_info))
29 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
30 let site_view = blocking(context.pool(), SiteView::read)
32 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
34 let protocols = if Settings::get().federation.enabled {
35 vec!["activitypub".to_string()]
41 version: "2.0".to_string(),
42 software: NodeInfoSoftware {
43 name: "lemmy".to_string(),
44 version: version::VERSION.to_string(),
47 usage: NodeInfoUsage {
48 users: NodeInfoUsers {
49 total: site_view.counts.users,
50 active_halfyear: site_view.counts.users_active_half_year,
51 active_month: site_view.counts.users_active_month,
53 local_posts: site_view.counts.posts,
54 local_comments: site_view.counts.comments,
56 open_registrations: site_view.site.open_registration,
59 Ok(HttpResponse::Ok().json(json))
62 #[derive(Serialize, Deserialize, Debug)]
63 struct NodeInfoWellKnown {
64 pub links: NodeInfoWellKnownLinks,
67 #[derive(Serialize, Deserialize, Debug)]
68 struct NodeInfoWellKnownLinks {
73 #[derive(Serialize, Deserialize, Debug)]
74 #[serde(rename_all = "camelCase")]
77 pub software: NodeInfoSoftware,
78 pub protocols: Vec<String>,
79 pub usage: NodeInfoUsage,
80 pub open_registrations: bool,
83 #[derive(Serialize, Deserialize, Debug)]
84 struct NodeInfoSoftware {
89 #[derive(Serialize, Deserialize, Debug)]
90 #[serde(rename_all = "camelCase")]
91 struct NodeInfoUsage {
92 pub users: NodeInfoUsers,
94 pub local_comments: i64,
97 #[derive(Serialize, Deserialize, Debug)]
98 #[serde(rename_all = "camelCase")]
99 struct NodeInfoUsers {
101 pub active_halfyear: i64,
102 pub active_month: i64,