1 use actix_web::{body::Body, error::ErrorBadRequest, *};
3 use lemmy_api::version;
4 use lemmy_db_views::site_view::SiteView;
5 use lemmy_structs::blocking;
6 use lemmy_utils::{settings::Settings, LemmyError};
7 use lemmy_websocket::LemmyContext;
8 use serde::{Deserialize, Serialize};
11 pub fn config(cfg: &mut web::ServiceConfig) {
13 .route("/nodeinfo/2.0.json", web::get().to(node_info))
14 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
17 async fn node_info_well_known() -> Result<HttpResponse<Body>, LemmyError> {
18 let node_info = NodeInfoWellKnown {
19 links: NodeInfoWellKnownLinks {
20 rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
21 href: Url::parse(&format!(
22 "{}/nodeinfo/2.0.json",
23 Settings::get().get_protocol_and_hostname()
27 Ok(HttpResponse::Ok().json(node_info))
30 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
31 let site_view = blocking(context.pool(), SiteView::read)
33 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
35 let protocols = if Settings::get().federation.enabled {
36 vec!["activitypub".to_string()]
42 version: "2.0".to_string(),
43 software: NodeInfoSoftware {
44 name: "lemmy".to_string(),
45 version: version::VERSION.to_string(),
48 usage: NodeInfoUsage {
49 // TODO get these again
50 users: NodeInfoUsers { total: 0 },
53 open_registrations: site_view.site.open_registration,
57 Ok(HttpResponse::Ok().json(json))
60 #[derive(Serialize, Deserialize, Debug)]
61 struct NodeInfoWellKnown {
62 pub links: NodeInfoWellKnownLinks,
65 #[derive(Serialize, Deserialize, Debug)]
66 struct NodeInfoWellKnownLinks {
71 #[derive(Serialize, Deserialize, Debug)]
74 pub software: NodeInfoSoftware,
75 pub protocols: Vec<String>,
76 pub usage: NodeInfoUsage,
79 #[derive(Serialize, Deserialize, Debug)]
80 struct NodeInfoSoftware {
85 #[derive(Serialize, Deserialize, Debug)]
86 #[serde(rename_all = "camelCase")]
87 struct NodeInfoUsage {
88 pub users: NodeInfoUsers,
90 pub local_comments: i64,
91 pub open_registrations: bool,
94 #[derive(Serialize, Deserialize, Debug)]
95 struct NodeInfoUsers {