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