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