From 9130f4d84884b99835e28c230fd6e2d537253b49 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 23 Apr 2018 11:54:37 +0100 Subject: [PATCH] Basic blog creation --- Cargo.lock | 16 +++++++++++++++ Cargo.toml | 1 + src/main.rs | 8 +++++++- src/routes/blogs.rs | 42 ++++++++++++++++++++++++++++++++++++++++ src/routes/mod.rs | 1 + src/utils.rs | 5 +++++ templates/blogs/new.tera | 16 +++++++++++++++ 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/routes/blogs.rs create mode 100644 src/utils.rs create mode 100644 templates/blogs/new.tera diff --git a/Cargo.lock b/Cargo.lock index 6c396f9..4f33cfc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -261,6 +261,14 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "heck" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "httparse" version = "1.2.4" @@ -461,6 +469,7 @@ dependencies = [ "bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "rocket_codegen 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "rocket_contrib 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -801,6 +810,11 @@ name = "unicode-normalization" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-segmentation" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -921,6 +935,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82" "checksum httparse 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f407128745b78abc95c0ffbe4e5d37427fdc0d45470710cfef8c44522a2e37" "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)" = "368cb56b2740ebf4230520e2b90ebb0461e69034d85d1945febd9b3971426db2" @@ -988,6 +1003,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f" +"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unidecode 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "402bb19d8e03f1d1a7450e2bd613980869438e0666331be3e073089124aa1adc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" diff --git a/Cargo.toml b/Cargo.toml index 2421726..babc56a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ version = "0.1.0" [dependencies] bcrypt = "0.2" dotenv = "*" +heck = "0.3.0" rocket = "*" rocket_codegen = "*" diff --git a/src/main.rs b/src/main.rs index 02335d7..f0963d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ extern crate dotenv; extern crate rocket; extern crate rocket_contrib; extern crate bcrypt; +extern crate heck; use diesel::pg::PgConnection; use diesel::r2d2::{ConnectionManager, Pool}; @@ -18,6 +19,7 @@ mod db_conn; mod models; mod schema; mod routes; +mod utils; use db_conn::DbConn; use models::instance::*; @@ -57,7 +59,11 @@ fn main() { routes::user::create, routes::session::new, - routes::session::create + routes::session::create, + + routes::blogs::details, + routes::blogs::new, + routes::blogs::create, ]) .manage(init_pool()) .attach(Template::fairing()) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs new file mode 100644 index 0000000..5b587cd --- /dev/null +++ b/src/routes/blogs.rs @@ -0,0 +1,42 @@ +use rocket::request::Form; +use rocket::response::Redirect; +use rocket_contrib::Template; +use std::collections::HashMap; + +use utils; +use db_conn::DbConn; +use models::blogs::*; +use models::instance::Instance; + +#[get("/~/")] +fn details(name: String) -> String { + format!("Welcome on ~{}", name) +} + +#[get("/blogs/new")] +fn new() -> Template { + Template::render("blogs/new", HashMap::::new()) +} + +#[derive(FromForm)] +struct NewBlogForm { + pub title: String +} + +#[post("/blogs/new", data = "")] +fn create(conn: DbConn, data: Form) -> Redirect { + let inst = Instance::get_local(&*conn).unwrap(); + let form = data.get(); + let slug = utils::make_actor_id(form.title.to_string()); + + Blog::insert(&*conn, NewBlog { + actor_id: slug.to_string(), + title: form.title.to_string(), + summary: String::from(""), + outbox_url: Blog::compute_outbox(slug.to_string(), inst.public_domain.to_string()), + inbox_url: Blog::compute_inbox(slug.to_string(), inst.public_domain.to_string()), + instance_id: inst.id + }); + + Redirect::to(format!("/~/{}", slug).as_str()) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index aa19563..d064d9f 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,3 +1,4 @@ +pub mod blogs; pub mod instance; pub mod session; pub mod user; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..0c5495a --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,5 @@ +use heck::CamelCase; + +pub fn make_actor_id(name: String) -> String { + name.as_str().to_camel_case() +} diff --git a/templates/blogs/new.tera b/templates/blogs/new.tera new file mode 100644 index 0000000..66cf3a3 --- /dev/null +++ b/templates/blogs/new.tera @@ -0,0 +1,16 @@ + + + + + New blog + + +

Create a blog

+
+ + + + +
+ +