From fcf3252292a094b889dc07c470974e44fe6e7b00 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 12 Nov 2019 01:27:16 +0100 Subject: [PATCH 1/7] Implement nodeinfo support (fixes #144) --- server/src/main.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/server/src/main.rs b/server/src/main.rs index 10f6026bb..392b0bbb9 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -5,11 +5,13 @@ extern crate diesel_migrations; use actix::prelude::*; use actix_files::NamedFile; use actix_web::*; +use actix_web::web::Json; use actix_web_actors::ws; use lemmy_server::db::establish_connection; use lemmy_server::websocket::server::*; use std::env; use std::time::{Duration, Instant}; +use serde::Serialize; embed_migrations!(); @@ -197,6 +199,7 @@ fn main() { .service(web::resource("/").to(index)) // static resources .service(actix_files::Files::new("/static", front_end_dir())) + .route("/nodeinfo/2.0.json", web::get().to(node_info)) }) .bind("0.0.0.0:8536") .unwrap() @@ -206,6 +209,56 @@ fn main() { let _ = sys.run(); } +#[derive(Serialize)] +struct Software { + name: String, + version: String, +} + +#[derive(Serialize)] +struct Usage { + users: Users, + localPosts: i32, + localComments: i32, +} + +#[derive(Serialize)] +struct Users { + total: i32, +} + +#[derive(Serialize)] +struct NodeInfo { + version: String, + software: Software, + protocols: [String; 0], + usage: Usage, + openRegistrations: bool, +} + +fn node_info() -> Result> { + // TODO: get info from database + // TODO: need to get lemmy version from somewhere else + let conn = establish_connection(); + let userCount = User_::count(conn) + let json = Json(NodeInfo { + version: "2.0".to_string(), + software: Software { + name: "lemmy".to_string(), + version: "0.1".to_string() + }, + protocols: [], // TODO: activitypub once that is implemented + usage: Usage { + users: Users { + total: 123, + }, + localPosts: 123, + localComments: 123, + }, + openRegistrations: true }); + return Ok(json); +} + fn index() -> Result { Ok(NamedFile::open(front_end_dir() + "/index.html")?) } From 4f116dc758684c8fca735bd6b30d93ab0bd3bc25 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 15 Nov 2019 03:08:25 +0100 Subject: [PATCH 2/7] got it working --- .gitignore | 2 ++ docker/dev/deploy.sh | 9 ++++--- server/src/lib.rs | 2 ++ server/src/main.rs | 55 ++------------------------------------ server/src/nodeinfo.rs | 60 ++++++++++++++++++++++++++++++++++++++++++ server/src/version.rs | 1 + ui/set_version.js | 11 -------- 7 files changed, 72 insertions(+), 68 deletions(-) create mode 100644 server/src/nodeinfo.rs create mode 100644 server/src/version.rs delete mode 100755 ui/set_version.js diff --git a/.gitignore b/.gitignore index 2feec03c1..4cb8939f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ ansible/inventory ansible/passwords/ +build/ +.idea/ diff --git a/docker/dev/deploy.sh b/docker/dev/deploy.sh index 4784a7427..64d815ade 100755 --- a/docker/dev/deploy.sh +++ b/docker/dev/deploy.sh @@ -6,10 +6,11 @@ new_tag="$1" git tag $new_tag # Setting the version on the front end -pushd ../../ui/ -node set_version.js -git add src/version.ts -popd +echo "export let version: string = '$(git describe --tags --long)';" > "ui/src/version.ts" +git add "ui/src/version.ts" +# Setting the version on the backend +echo "pub const VERSION: &'static str = \"$(git describe --tags --long)\";" > "server/src/version.rs" +git add "server/src/version.rs" # Changing the docker-compose prod sed -i "s/dessalines\/lemmy:.*/dessalines\/lemmy:$new_tag/" ../prod/docker-compose.yml diff --git a/server/src/lib.rs b/server/src/lib.rs index 162d95786..b5c26762f 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -27,6 +27,8 @@ pub mod apub; pub mod db; pub mod schema; pub mod websocket; +pub mod nodeinfo; +pub mod version; use chrono::{DateTime, NaiveDateTime, Utc}; use dotenv::dotenv; diff --git a/server/src/main.rs b/server/src/main.rs index 392b0bbb9..f930f2eda 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -5,13 +5,12 @@ extern crate diesel_migrations; use actix::prelude::*; use actix_files::NamedFile; use actix_web::*; -use actix_web::web::Json; use actix_web_actors::ws; use lemmy_server::db::establish_connection; use lemmy_server::websocket::server::*; use std::env; use std::time::{Duration, Instant}; -use serde::Serialize; +use lemmy_server::nodeinfo; embed_migrations!(); @@ -199,7 +198,7 @@ fn main() { .service(web::resource("/").to(index)) // static resources .service(actix_files::Files::new("/static", front_end_dir())) - .route("/nodeinfo/2.0.json", web::get().to(node_info)) + .route("/nodeinfo/2.0.json", web::get().to(nodeinfo::node_info)) }) .bind("0.0.0.0:8536") .unwrap() @@ -209,56 +208,6 @@ fn main() { let _ = sys.run(); } -#[derive(Serialize)] -struct Software { - name: String, - version: String, -} - -#[derive(Serialize)] -struct Usage { - users: Users, - localPosts: i32, - localComments: i32, -} - -#[derive(Serialize)] -struct Users { - total: i32, -} - -#[derive(Serialize)] -struct NodeInfo { - version: String, - software: Software, - protocols: [String; 0], - usage: Usage, - openRegistrations: bool, -} - -fn node_info() -> Result> { - // TODO: get info from database - // TODO: need to get lemmy version from somewhere else - let conn = establish_connection(); - let userCount = User_::count(conn) - let json = Json(NodeInfo { - version: "2.0".to_string(), - software: Software { - name: "lemmy".to_string(), - version: "0.1".to_string() - }, - protocols: [], // TODO: activitypub once that is implemented - usage: Usage { - users: Users { - total: 123, - }, - localPosts: 123, - localComments: 123, - }, - openRegistrations: true }); - return Ok(json); -} - fn index() -> Result { Ok(NamedFile::open(front_end_dir() + "/index.html")?) } diff --git a/server/src/nodeinfo.rs b/server/src/nodeinfo.rs new file mode 100644 index 000000000..6790c0422 --- /dev/null +++ b/server/src/nodeinfo.rs @@ -0,0 +1,60 @@ +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::version; + +#[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, +} + +#[derive(Serialize)] +pub struct Users { + total: i64, +} + +#[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() -> Result, 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: "2.0".to_string(), + software: Software { + name: "lemmy".to_string(), + version: version::VERSION.to_string(), + }, + 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 diff --git a/server/src/version.rs b/server/src/version.rs new file mode 100644 index 000000000..fe1bacb56 --- /dev/null +++ b/server/src/version.rs @@ -0,0 +1 @@ +pub const VERSION: &'static str = "v0.4.0-6-gd767e94"; diff --git a/ui/set_version.js b/ui/set_version.js deleted file mode 100755 index 218930852..000000000 --- a/ui/set_version.js +++ /dev/null @@ -1,11 +0,0 @@ -const fs = require('fs'); - -exports.setVersion = function() { - let revision = require('child_process') - .execSync('git describe --tags --long') - .toString().trim(); - let line = `export let version: string = "${revision}";`; - fs.writeFileSync("./src/version.ts", line); -} - -this.setVersion() From 10976bd6564727a6f59b4587e451cc4afd057a76 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 15 Nov 2019 16:08:14 +0100 Subject: [PATCH 3/7] nicer string handling --- server/src/nodeinfo.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/nodeinfo.rs b/server/src/nodeinfo.rs index 6790c0422..a406e536b 100644 --- a/server/src/nodeinfo.rs +++ b/server/src/nodeinfo.rs @@ -42,10 +42,10 @@ pub fn node_info() -> Result, Error> { Err(_e) => return Err(_e)?, }; let json = Json(NodeInfo { - version: "2.0".to_string(), + version: String::from("2.0"), software: Software { - name: "lemmy".to_string(), - version: version::VERSION.to_string(), + name: String::from("lemmy"), + version: String::from(version::VERSION), }, protocols: [], // TODO: put 'activitypub' once that is implemented usage: Usage { From 2666ba6adfa93aa1592f50fe7b074b7bf007ea33 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 15 Nov 2019 18:10:56 +0100 Subject: [PATCH 4/7] rewrite --- server/src/main.rs | 5 ++- server/src/nodeinfo.rs | 95 ++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 55 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index f930f2eda..9afc12b1e 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -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() diff --git a/server/src/nodeinfo.rs b/server/src/nodeinfo.rs index a406e536b..7799d5e0b 100644 --- a/server/src/nodeinfo.rs +++ b/server/src/nodeinfo.rs @@ -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, +pub fn node_info_well_known() -> HttpResponse { + let json = json!({ + "links": { + "rel": "http://nodeinfo.diaspora.software/ns/schema/2.0", + "href": format!("https://{}/nodeinfo/2.0.json", Settings::get().hostname), + } + }); + + return HttpResponse::Ok() + .content_type("application/json") + .body(json.to_string()); } -#[derive(Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Usage { - users: Users, - local_posts: i64, - local_comments: i64, +pub fn node_info() -> HttpResponse { + 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()); } - -#[derive(Serialize)] -pub struct Users { - total: i64, -} - -#[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() -> Result, 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 From 20966e085aba521c30b16e56b4a08bc056e02630 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 15 Nov 2019 18:31:25 +0100 Subject: [PATCH 5/7] preserve json order --- server/Cargo.lock | 1 + server/Cargo.toml | 2 +- server/src/main.rs | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index 5f9d78384..c135bbe60 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1833,6 +1833,7 @@ name = "serde_json" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.97 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/server/Cargo.toml b/server/Cargo.toml index 3f555829b..9d20c643a 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -12,7 +12,7 @@ bcrypt = "0.5.0" activitypub = "0.1.5" chrono = { version = "0.4.7", features = ["serde"] } failure = "0.1.5" -serde_json = "1.0.40" +serde_json = { version = "1.0.40", features = ["preserve_order"]} serde = { version = "1.0.94", features = ["derive"] } actix = "0.8.3" actix-web = "1.0" diff --git a/server/src/main.rs b/server/src/main.rs index 9afc12b1e..8c77199fb 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -11,7 +11,6 @@ use lemmy_server::nodeinfo; use lemmy_server::websocket::server::*; use std::env; use std::time::{Duration, Instant}; -use actix_web::http::header::ContentType; embed_migrations!(); From 7b5884b66435a32687d034b32a93360148947c22 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 16 Nov 2019 01:30:35 +0100 Subject: [PATCH 6/7] change version numbering command --- docker/dev/deploy.sh | 4 ++-- server/src/version.rs | 2 +- ui/src/version.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docker/dev/deploy.sh b/docker/dev/deploy.sh index 64d815ade..a46b41c9d 100755 --- a/docker/dev/deploy.sh +++ b/docker/dev/deploy.sh @@ -6,10 +6,10 @@ new_tag="$1" git tag $new_tag # Setting the version on the front end -echo "export let version: string = '$(git describe --tags --long)';" > "ui/src/version.ts" +echo "export let version: string = '$(git describe --tags)';" > "ui/src/version.ts" git add "ui/src/version.ts" # Setting the version on the backend -echo "pub const VERSION: &'static str = \"$(git describe --tags --long)\";" > "server/src/version.rs" +echo "pub const VERSION: &'static str = \"$(git describe --tags)\";" > "server/src/version.rs" git add "server/src/version.rs" # Changing the docker-compose prod diff --git a/server/src/version.rs b/server/src/version.rs index fe1bacb56..dfb054ec5 100644 --- a/server/src/version.rs +++ b/server/src/version.rs @@ -1 +1 @@ -pub const VERSION: &'static str = "v0.4.0-6-gd767e94"; +pub const VERSION: &'static str = "v0.4.0-9-g409c0f9"; diff --git a/ui/src/version.ts b/ui/src/version.ts index aa8e1d67c..9d9ed8b3e 100644 --- a/ui/src/version.ts +++ b/ui/src/version.ts @@ -1 +1 @@ -export let version: string = 'v0.4.0.2-0-g062d7f2'; +export let version: string = 'v0.4.0.2-6-gf396c6c'; From 9ac1beae8270ee3fbf938790fefa404d8b420239 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 16 Nov 2019 01:32:45 +0100 Subject: [PATCH 7/7] rebase --- server/src/version.rs | 2 +- ui/src/version.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/version.rs b/server/src/version.rs index dfb054ec5..f45b2fa78 100644 --- a/server/src/version.rs +++ b/server/src/version.rs @@ -1 +1 @@ -pub const VERSION: &'static str = "v0.4.0-9-g409c0f9"; +pub const VERSION: &'static str = "v0.4.0.2-7-g7c052f5"; diff --git a/ui/src/version.ts b/ui/src/version.ts index 9d9ed8b3e..e151ae083 100644 --- a/ui/src/version.ts +++ b/ui/src/version.ts @@ -1 +1 @@ -export let version: string = 'v0.4.0.2-6-gf396c6c'; +export let version: string = 'v0.4.0.2-7-g7c052f5';