]> Untitled Git - lemmy.git/commitdiff
rewrite
authorFelix Ableitner <me@nutomic.com>
Fri, 15 Nov 2019 17:10:56 +0000 (18:10 +0100)
committerFelix Ableitner <me@nutomic.com>
Sat, 16 Nov 2019 00:31:41 +0000 (01:31 +0100)
server/src/main.rs
server/src/nodeinfo.rs

index f930f2eda26dfb5bf4595dcbe621279c0189e032..9afc12b1e9031552fe93abb48d1a71883acfd515 100644 (file)
@@ -7,10 +7,11 @@ use actix_files::NamedFile;
 use actix_web::*;
 use actix_web_actors::ws;
 use lemmy_server::db::establish_connection;
+use lemmy_server::nodeinfo;
 use lemmy_server::websocket::server::*;
 use std::env;
 use std::time::{Duration, Instant};
-use lemmy_server::nodeinfo;
+use actix_web::http::header::ContentType;
 
 embed_migrations!();
 
@@ -190,6 +191,7 @@ fn main() {
   // Start chat server actor in separate thread
   let server = ChatServer::default().start();
   // Create Http server with websocket support
+
   HttpServer::new(move || {
     App::new()
       .data(server.clone())
@@ -199,6 +201,7 @@ fn main() {
       // static resources
       .service(actix_files::Files::new("/static", front_end_dir()))
       .route("/nodeinfo/2.0.json", web::get().to(nodeinfo::node_info))
+      .route("/.well-known/nodeinfo", web::get().to(nodeinfo::node_info_well_known))
   })
   .bind("0.0.0.0:8536")
   .unwrap()
index a406e536b39e18e0d7130a2e8e738d5792f7c941..7799d5e0bb3da017b001fed932a8f16faae4ad03 100644 (file)
@@ -1,60 +1,47 @@
-use actix_web::web::Json;
-use serde::Serialize;
-use crate::db::establish_connection;
 use crate::db::community_view::SiteView;
-use actix_web::*;
-use failure::Error;
+use crate::db::establish_connection;
 use crate::version;
+use crate::Settings;
+use actix_web::HttpResponse;
+use actix_web::body::Body;
+use serde_json::json;
 
-#[derive(Serialize)]
-pub struct Software {
-    name: String,
-    version: String,
-}
-
-#[derive(Serialize)]
-#[serde(rename_all = "camelCase")]
-pub struct Usage {
-    users: Users,
-    local_posts: i64,
-    local_comments: i64,
-}
+pub fn node_info_well_known() -> HttpResponse<Body> {
+  let json = json!({
+    "links": {
+      "rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
+      "href": format!("https://{}/nodeinfo/2.0.json", Settings::get().hostname),
+    }
+  });
 
-#[derive(Serialize)]
-pub struct Users {
-    total: i64,
+  return HttpResponse::Ok()
+    .content_type("application/json")
+    .body(json.to_string());
 }
 
-#[derive(Serialize)]
-#[serde(rename_all = "camelCase")]
-pub struct NodeInfo {
-    version: String,
-    software: Software,
-    protocols: [String; 0],
-    usage: Usage,
-    open_registrations: bool,
+pub fn node_info() -> HttpResponse<Body> {
+  let conn = establish_connection();
+  let site_view = match SiteView::read(&conn) {
+    Ok(site_view) => site_view,
+    Err(_e) => return HttpResponse::InternalServerError().finish(),
+  };
+  let json = json!({
+    "version": "2.0",
+    "software": {
+      "name": "lemmy",
+      "version": version::VERSION,
+    },
+    "protocols": [],
+    "usage": {
+      "users": {
+        "total": site_view.number_of_users
+      },
+      "local_posts": site_view.number_of_posts,
+      "local_comments": site_view.number_of_comments,
+      "open_registrations": true,
+      }
+  });
+  return HttpResponse::Ok()
+    .content_type("application/json")
+    .body(json.to_string());
 }
-
-pub fn node_info() -> Result<Json<NodeInfo>, Error> {
-    let conn = establish_connection();
-    let site_view = match SiteView::read(&conn) {
-        Ok(site_view) => site_view,
-        Err(_e) => return Err(_e)?,
-    };
-    let json = Json(NodeInfo {
-        version: String::from("2.0"),
-        software: Software {
-            name: String::from("lemmy"),
-            version: String::from(version::VERSION),
-        },
-        protocols: [], // TODO: put 'activitypub' once that is implemented
-        usage: Usage {
-            users: Users {
-                total: site_view.number_of_users,
-            },
-            local_posts: site_view.number_of_posts,
-            local_comments: site_view.number_of_comments,
-        },
-        open_registrations: true });
-    return Ok(json);
-}
\ No newline at end of file