1 use actix_web::{error::ErrorBadRequest, web, Error, HttpResponse, Result};
3 use lemmy_api_common::context::LemmyContext;
4 use lemmy_db_schema::RegistrationMode;
5 use lemmy_db_views::structs::SiteView;
6 use lemmy_utils::{error::LemmyError, version};
7 use serde::{Deserialize, Serialize};
10 pub fn config(cfg: &mut web::ServiceConfig) {
12 .route("/nodeinfo/2.0.json", web::get().to(node_info))
13 .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
16 async fn node_info_well_known(
17 context: web::Data<LemmyContext>,
18 ) -> Result<HttpResponse, LemmyError> {
19 let node_info = NodeInfoWellKnown {
20 links: vec![NodeInfoWellKnownLinks {
21 rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
22 href: Url::parse(&format!(
23 "{}/nodeinfo/2.0.json",
24 &context.settings().get_protocol_and_hostname(),
28 Ok(HttpResponse::Ok().json(node_info))
31 async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
32 let site_view = SiteView::read_local(context.pool())
34 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
36 let protocols = if site_view.local_site.federation_enabled {
37 Some(vec!["activitypub".to_string()])
41 let open_registrations = Some(site_view.local_site.registration_mode == RegistrationMode::Open);
43 version: Some("2.0".to_string()),
44 software: Some(NodeInfoSoftware {
45 name: Some("lemmy".to_string()),
46 version: Some(version::VERSION.to_string()),
49 usage: Some(NodeInfoUsage {
50 users: Some(NodeInfoUsers {
51 total: Some(site_view.counts.users),
52 active_halfyear: Some(site_view.counts.users_active_half_year),
53 active_month: Some(site_view.counts.users_active_month),
55 local_posts: Some(site_view.counts.posts),
56 local_comments: Some(site_view.counts.comments),
61 Ok(HttpResponse::Ok().json(json))
64 #[derive(Serialize, Deserialize, Debug)]
65 struct NodeInfoWellKnown {
66 pub links: Vec<NodeInfoWellKnownLinks>,
69 #[derive(Serialize, Deserialize, Debug)]
70 struct NodeInfoWellKnownLinks {
75 #[derive(Serialize, Deserialize, Debug, Default)]
76 #[serde(rename_all = "camelCase", default)]
78 pub version: Option<String>,
79 pub software: Option<NodeInfoSoftware>,
80 pub protocols: Option<Vec<String>>,
81 pub usage: Option<NodeInfoUsage>,
82 pub open_registrations: Option<bool>,
85 #[derive(Serialize, Deserialize, Debug, Default)]
87 pub struct NodeInfoSoftware {
88 pub name: Option<String>,
89 pub version: Option<String>,
92 #[derive(Serialize, Deserialize, Debug, Default)]
93 #[serde(rename_all = "camelCase", default)]
94 pub struct NodeInfoUsage {
95 pub users: Option<NodeInfoUsers>,
96 pub local_posts: Option<i64>,
97 pub local_comments: Option<i64>,
100 #[derive(Serialize, Deserialize, Debug, Default)]
101 #[serde(rename_all = "camelCase", default)]
102 pub struct NodeInfoUsers {
103 pub total: Option<i64>,
104 pub active_halfyear: Option<i64>,
105 pub active_month: Option<i64>,