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;
11 use serde::{Deserialize, Serialize};
15 pub fn config(cfg: &mut web::ServiceConfig) {
17 .route("/nodeinfo/2.0.json", web::get().to(node_info))
18 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
21 async fn node_info_well_known() -> Result<HttpResponse<Body>, Error> {
22 let node_info = NodeInfoWellKnown {
23 links: NodeInfoWellKnownLinks {
24 rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
25 href: Url::parse(&format!(
26 "{}://{}/nodeinfo/2.0.json",
27 get_apub_protocol_string(),
28 Settings::get().hostname
32 Ok(HttpResponse::Ok().json(node_info))
36 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
37 ) -> Result<HttpResponse, actix_web::Error> {
38 let res = web::block(move || {
40 let site_view = match SiteView::read(&conn) {
41 Ok(site_view) => site_view,
42 Err(_) => return Err(format_err!("not_found")),
44 let protocols = if Settings::get().federation.enabled {
45 vec!["activitypub".to_string()]
50 version: "2.0".to_string(),
51 software: NodeInfoSoftware {
52 name: "lemmy".to_string(),
53 version: version::VERSION.to_string(),
56 usage: NodeInfoUsage {
57 users: NodeInfoUsers {
58 total: site_view.number_of_users,
60 local_posts: site_view.number_of_posts,
61 local_comments: site_view.number_of_comments,
62 open_registrations: site_view.open_registration,
64 metadata: NodeInfoMetadata {
65 community_list_url: Some(Url::parse(&format!(
66 "{}://{}/federation/communities",
67 get_apub_protocol_string(),
68 Settings::get().hostname
74 .map(|json| HttpResponse::Ok().json(json))
75 .map_err(|_| HttpResponse::InternalServerError())?;
79 #[derive(Serialize, Deserialize, Debug)]
80 pub struct NodeInfoWellKnown {
81 pub links: NodeInfoWellKnownLinks,
84 #[derive(Serialize, Deserialize, Debug)]
85 pub struct NodeInfoWellKnownLinks {
90 #[derive(Serialize, Deserialize, Debug)]
93 pub software: NodeInfoSoftware,
94 pub protocols: Vec<String>,
95 pub usage: NodeInfoUsage,
96 pub metadata: NodeInfoMetadata,
99 #[derive(Serialize, Deserialize, Debug)]
100 pub struct NodeInfoSoftware {
105 #[derive(Serialize, Deserialize, Debug)]
106 #[serde(rename_all = "camelCase")]
107 pub struct NodeInfoUsage {
108 pub users: NodeInfoUsers,
109 pub local_posts: i64,
110 pub local_comments: i64,
111 pub open_registrations: bool,
114 #[derive(Serialize, Deserialize, Debug)]
115 pub struct NodeInfoUsers {
119 #[derive(Serialize, Deserialize, Debug)]
120 pub struct NodeInfoMetadata {
121 pub community_list_url: Option<Url>,