]> Untitled Git - lemmy.git/blob - crates/routes/src/nodeinfo.rs
Adding instance software and version. Fixes #2222 (#2733)
[lemmy.git] / crates / routes / src / nodeinfo.rs
1 use actix_web::{error::ErrorBadRequest, web, Error, HttpResponse, Result};
2 use anyhow::anyhow;
3 use lemmy_api_common::context::LemmyContext;
4 use lemmy_db_schema::source::local_site::RegistrationMode;
5 use lemmy_db_views::structs::SiteView;
6 use lemmy_utils::{error::LemmyError, version};
7 use serde::{Deserialize, Serialize};
8 use url::Url;
9
10 pub fn config(cfg: &mut web::ServiceConfig) {
11   cfg
12     .route("/nodeinfo/2.0.json", web::get().to(node_info))
13     .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
14 }
15
16 async fn node_info_well_known(
17   context: web::Data<LemmyContext>,
18 ) -> Result<HttpResponse, LemmyError> {
19   let node_info = NodeInfoWellKnown {
20     links: vec![NodeInfoWellKnownLinks {
21       rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
22       href: Url::parse(&format!(
23         "{}/nodeinfo/2.0.json",
24         &context.settings().get_protocol_and_hostname(),
25       ))?,
26     }],
27   };
28   Ok(HttpResponse::Ok().json(node_info))
29 }
30
31 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
32   let site_view = SiteView::read_local(context.pool())
33     .await
34     .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
35
36   let protocols = if site_view.local_site.federation_enabled {
37     Some(vec!["activitypub".to_string()])
38   } else {
39     None
40   };
41   let open_registrations = Some(site_view.local_site.registration_mode == RegistrationMode::Open);
42   let json = NodeInfo {
43     version: Some("2.0".to_string()),
44     software: Some(NodeInfoSoftware {
45       name: Some("lemmy".to_string()),
46       version: Some(version::VERSION.to_string()),
47     }),
48     protocols,
49     usage: Some(NodeInfoUsage {
50       users: Some(NodeInfoUsers {
51         total: Some(site_view.counts.users),
52         active_halfyear: Some(site_view.counts.users_active_half_year),
53         active_month: Some(site_view.counts.users_active_month),
54       }),
55       local_posts: Some(site_view.counts.posts),
56       local_comments: Some(site_view.counts.comments),
57     }),
58     open_registrations,
59   };
60
61   Ok(HttpResponse::Ok().json(json))
62 }
63
64 #[derive(Serialize, Deserialize, Debug)]
65 struct NodeInfoWellKnown {
66   pub links: Vec<NodeInfoWellKnownLinks>,
67 }
68
69 #[derive(Serialize, Deserialize, Debug)]
70 struct NodeInfoWellKnownLinks {
71   pub rel: Url,
72   pub href: Url,
73 }
74
75 #[derive(Serialize, Deserialize, Debug, Default)]
76 #[serde(rename_all = "camelCase", default)]
77 pub struct NodeInfo {
78   pub version: Option<String>,
79   pub software: Option<NodeInfoSoftware>,
80   pub protocols: Option<Vec<String>>,
81   pub usage: Option<NodeInfoUsage>,
82   pub open_registrations: Option<bool>,
83 }
84
85 #[derive(Serialize, Deserialize, Debug, Default)]
86 #[serde(default)]
87 pub struct NodeInfoSoftware {
88   pub name: Option<String>,
89   pub version: Option<String>,
90 }
91
92 #[derive(Serialize, Deserialize, Debug, Default)]
93 #[serde(rename_all = "camelCase", default)]
94 pub struct NodeInfoUsage {
95   pub users: Option<NodeInfoUsers>,
96   pub local_posts: Option<i64>,
97   pub local_comments: Option<i64>,
98 }
99
100 #[derive(Serialize, Deserialize, Debug, Default)]
101 #[serde(rename_all = "camelCase", default)]
102 pub struct NodeInfoUsers {
103   pub total: Option<i64>,
104   pub active_halfyear: Option<i64>,
105   pub active_month: Option<i64>,
106 }