1 use crate::apub::get_apub_protocol_string;
2 use crate::db::site_view::SiteView;
5 use actix_web::body::Body;
7 use actix_web::HttpResponse;
8 use diesel::r2d2::{ConnectionManager, Pool};
9 use diesel::PgConnection;
10 use serde::{Deserialize, Serialize};
13 pub fn config(cfg: &mut web::ServiceConfig) {
15 .route("/nodeinfo/2.0.json", web::get().to(node_info))
16 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
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(),
24 "{}://{}/nodeinfo/2.0.json",
25 get_apub_protocol_string(),
26 Settings::get().hostname
30 HttpResponse::Ok().json(node_info)
34 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
35 ) -> Result<HttpResponse, actix_web::Error> {
36 let res = web::block(move || {
38 let site_view = match SiteView::read(&conn) {
39 Ok(site_view) => site_view,
40 Err(_) => return Err(format_err!("not_found")),
42 let protocols = if Settings::get().federation.enabled {
43 vec!["activitypub".to_string()]
48 version: "2.0".to_string(),
49 software: NodeInfoSoftware {
50 name: "lemmy".to_string(),
51 version: version::VERSION.to_string(),
54 usage: NodeInfoUsage {
55 users: NodeInfoUsers {
56 total: site_view.number_of_users,
58 local_posts: site_view.number_of_posts,
59 local_comments: site_view.number_of_comments,
60 open_registrations: site_view.open_registration,
62 metadata: NodeInfoMetadata {
63 community_list_url: Some(format!(
64 "{}://{}/federation/communities",
65 get_apub_protocol_string(),
66 Settings::get().hostname
72 .map(|json| HttpResponse::Ok().json(json))
73 .map_err(|_| HttpResponse::InternalServerError())?;
77 #[derive(Serialize, Deserialize, Debug)]
78 pub struct NodeInfoWellKnown {
79 pub links: NodeInfoWellKnownLinks,
82 #[derive(Serialize, Deserialize, Debug)]
83 pub struct NodeInfoWellKnownLinks {
88 #[derive(Serialize, Deserialize, Debug)]
91 pub software: NodeInfoSoftware,
92 pub protocols: Vec<String>,
93 pub usage: NodeInfoUsage,
94 pub metadata: NodeInfoMetadata,
97 #[derive(Serialize, Deserialize, Debug)]
98 pub struct NodeInfoSoftware {
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,
112 #[derive(Serialize, Deserialize, Debug)]
113 pub struct NodeInfoUsers {
117 #[derive(Serialize, Deserialize, Debug)]
118 pub struct NodeInfoMetadata {
119 pub community_list_url: Option<String>,