]> Untitled Git - lemmy.git/blob - src/routes/nodeinfo.rs
Report only local counts in site_view.
[lemmy.git] / src / routes / nodeinfo.rs
1 use actix_web::{body::Body, error::ErrorBadRequest, *};
2 use anyhow::anyhow;
3 use lemmy_api::version;
4 use lemmy_db_views::site_view::SiteView;
5 use lemmy_structs::blocking;
6 use lemmy_utils::{settings::Settings, LemmyError};
7 use lemmy_websocket::LemmyContext;
8 use serde::{Deserialize, Serialize};
9 use url::Url;
10
11 pub fn config(cfg: &mut web::ServiceConfig) {
12   cfg
13     .route("/nodeinfo/2.0.json", web::get().to(node_info))
14     .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
15 }
16
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         Settings::get().get_protocol_and_hostname()
24       ))?,
25     },
26   };
27   Ok(HttpResponse::Ok().json(node_info))
28 }
29
30 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
31   let site_view = blocking(context.pool(), SiteView::read)
32     .await?
33     .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
34
35   let protocols = if Settings::get().federation.enabled {
36     vec!["activitypub".to_string()]
37   } else {
38     vec![]
39   };
40
41   let json = NodeInfo {
42     version: "2.0".to_string(),
43     software: NodeInfoSoftware {
44       name: "lemmy".to_string(),
45       version: version::VERSION.to_string(),
46     },
47     protocols,
48     usage: NodeInfoUsage {
49       users: NodeInfoUsers {
50         total: site_view.counts.users,
51       },
52       local_posts: site_view.counts.posts,
53       local_comments: site_view.counts.comments,
54     },
55     open_registrations: site_view.site.open_registration,
56   };
57
58   Ok(HttpResponse::Ok().json(json))
59 }
60
61 #[derive(Serialize, Deserialize, Debug)]
62 struct NodeInfoWellKnown {
63   pub links: NodeInfoWellKnownLinks,
64 }
65
66 #[derive(Serialize, Deserialize, Debug)]
67 struct NodeInfoWellKnownLinks {
68   pub rel: Url,
69   pub href: Url,
70 }
71
72 #[derive(Serialize, Deserialize, Debug)]
73 #[serde(rename_all = "camelCase")]
74 struct NodeInfo {
75   pub version: String,
76   pub software: NodeInfoSoftware,
77   pub protocols: Vec<String>,
78   pub usage: NodeInfoUsage,
79   pub open_registrations: bool,
80 }
81
82 #[derive(Serialize, Deserialize, Debug)]
83 struct NodeInfoSoftware {
84   pub name: String,
85   pub version: String,
86 }
87
88 #[derive(Serialize, Deserialize, Debug)]
89 #[serde(rename_all = "camelCase")]
90 struct NodeInfoUsage {
91   pub users: NodeInfoUsers,
92   pub local_posts: i64,
93   pub local_comments: i64,
94 }
95
96 #[derive(Serialize, Deserialize, Debug)]
97 struct NodeInfoUsers {
98   pub total: i64,
99 }