]> Untitled Git - lemmy.git/blob - server/src/routes/nodeinfo.rs
Merge branch 'federation_add_fed_columns' of https://yerbamate.dev/dessalines/lemmy...
[lemmy.git] / server / src / routes / nodeinfo.rs
1 use crate::apub::get_apub_protocol_string;
2 use crate::db::site_view::SiteView;
3 use crate::version;
4 use crate::Settings;
5 use actix_web::body::Body;
6 use actix_web::web;
7 use actix_web::HttpResponse;
8 use diesel::r2d2::{ConnectionManager, Pool};
9 use diesel::PgConnection;
10 use serde::{Deserialize, Serialize};
11 use std::fmt::Debug;
12
13 pub fn config(cfg: &mut web::ServiceConfig) {
14   cfg
15     .route("/nodeinfo/2.0.json", web::get().to(node_info))
16     .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
17 }
18
19 async fn node_info_well_known() -> HttpResponse<Body> {
20   let node_info = NodeInfoWellKnown {
21     links: NodeInfoWellKnownLinks {
22       rel: "http://nodeinfo.diaspora.software/ns/schema/2.0".to_string(),
23       href: format!(
24         "{}://{}/nodeinfo/2.0.json",
25         get_apub_protocol_string(),
26         Settings::get().hostname
27       ),
28     },
29   };
30   HttpResponse::Ok().json(node_info)
31 }
32
33 async fn node_info(
34   db: web::Data<Pool<ConnectionManager<PgConnection>>>,
35 ) -> Result<HttpResponse, actix_web::Error> {
36   let res = web::block(move || {
37     let conn = db.get()?;
38     let site_view = match SiteView::read(&conn) {
39       Ok(site_view) => site_view,
40       Err(_) => return Err(format_err!("not_found")),
41     };
42     let protocols = if Settings::get().federation.enabled {
43       vec!["activitypub".to_string()]
44     } else {
45       vec![]
46     };
47     Ok(NodeInfo {
48       version: "2.0".to_string(),
49       software: NodeInfoSoftware {
50         name: "lemmy".to_string(),
51         version: version::VERSION.to_string(),
52       },
53       protocols,
54       usage: NodeInfoUsage {
55         users: NodeInfoUsers {
56           total: site_view.number_of_users,
57         },
58         local_posts: site_view.number_of_posts,
59         local_comments: site_view.number_of_comments,
60         open_registrations: site_view.open_registration,
61       },
62       metadata: NodeInfoMetadata {
63         community_list_url: Some(format!(
64           "{}://{}/federation/communities",
65           get_apub_protocol_string(),
66           Settings::get().hostname
67         )),
68       },
69     })
70   })
71   .await
72   .map(|json| HttpResponse::Ok().json(json))
73   .map_err(|_| HttpResponse::InternalServerError())?;
74   Ok(res)
75 }
76
77 #[derive(Serialize, Deserialize, Debug)]
78 pub struct NodeInfoWellKnown {
79   pub links: NodeInfoWellKnownLinks,
80 }
81
82 #[derive(Serialize, Deserialize, Debug)]
83 pub struct NodeInfoWellKnownLinks {
84   pub rel: String,
85   pub href: String,
86 }
87
88 #[derive(Serialize, Deserialize, Debug)]
89 pub struct NodeInfo {
90   pub version: String,
91   pub software: NodeInfoSoftware,
92   pub protocols: Vec<String>,
93   pub usage: NodeInfoUsage,
94   pub metadata: NodeInfoMetadata,
95 }
96
97 #[derive(Serialize, Deserialize, Debug)]
98 pub struct NodeInfoSoftware {
99   pub name: String,
100   pub version: String,
101 }
102
103 #[derive(Serialize, Deserialize, Debug)]
104 #[serde(rename_all = "camelCase")]
105 pub struct NodeInfoUsage {
106   pub users: NodeInfoUsers,
107   pub local_posts: i64,
108   pub local_comments: i64,
109   pub open_registrations: bool,
110 }
111
112 #[derive(Serialize, Deserialize, Debug)]
113 pub struct NodeInfoUsers {
114   pub total: i64,
115 }
116
117 #[derive(Serialize, Deserialize, Debug)]
118 pub struct NodeInfoMetadata {
119   pub community_list_url: Option<String>,
120 }