1 use actix_web::{error::ErrorBadRequest, web, Error, HttpResponse, Result};
3 use lemmy_api_common::context::LemmyContext;
4 use lemmy_db_views::structs::SiteView;
5 use lemmy_utils::{error::LemmyError, version};
6 use serde::{Deserialize, Serialize};
9 pub fn config(cfg: &mut web::ServiceConfig) {
11 .route("/nodeinfo/2.0.json", web::get().to(node_info))
12 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
15 async fn node_info_well_known(
16 context: web::Data<LemmyContext>,
17 ) -> Result<HttpResponse, LemmyError> {
18 let node_info = NodeInfoWellKnown {
19 links: vec![NodeInfoWellKnownLinks {
20 rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
21 href: Url::parse(&format!(
22 "{}/nodeinfo/2.0.json",
23 &context.settings().get_protocol_and_hostname(),
27 Ok(HttpResponse::Ok().json(node_info))
30 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
31 let site_view = SiteView::read_local(context.pool())
33 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
35 let protocols = if site_view.local_site.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.counts.users,
51 active_halfyear: site_view.counts.users_active_half_year,
52 active_month: site_view.counts.users_active_month,
54 local_posts: site_view.counts.posts,
55 local_comments: site_view.counts.comments,
57 open_registrations: site_view.local_site.open_registration,
60 Ok(HttpResponse::Ok().json(json))
63 #[derive(Serialize, Deserialize, Debug)]
64 struct NodeInfoWellKnown {
65 pub links: Vec<NodeInfoWellKnownLinks>,
68 #[derive(Serialize, Deserialize, Debug)]
69 struct NodeInfoWellKnownLinks {
74 #[derive(Serialize, Deserialize, Debug)]
75 #[serde(rename_all = "camelCase")]
78 pub software: NodeInfoSoftware,
79 pub protocols: Vec<String>,
80 pub usage: NodeInfoUsage,
81 pub open_registrations: bool,
84 #[derive(Serialize, Deserialize, Debug)]
85 struct NodeInfoSoftware {
90 #[derive(Serialize, Deserialize, Debug)]
91 #[serde(rename_all = "camelCase")]
92 struct NodeInfoUsage {
93 pub users: NodeInfoUsers,
95 pub local_comments: i64,
98 #[derive(Serialize, Deserialize, Debug)]
99 #[serde(rename_all = "camelCase")]
100 struct NodeInfoUsers {
102 pub active_halfyear: i64,
103 pub active_month: i64,