From 65829094c94f4097c0b6d6d275e8f252f7b118c2 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:40:51 +0900 Subject: [PATCH 1/8] Add test for blog slug validation --- src/routes/blogs.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 7359779a..99c63bf3 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -379,6 +379,7 @@ pub fn atom_feed(name: String, conn: DbConn) -> Option> { #[cfg(test)] mod tests { + use super::valid_slug; use crate::init_rocket; use diesel::Connection; use plume_common::utils::random_hex; @@ -524,4 +525,10 @@ mod tests { .finish(), ); } + + #[test] + fn test_valid_slug() { + assert!(valid_slug("Blog Title").is_ok()); + assert!(valid_slug("ブログ タイトル").is_ok()); + } } From 4dfe300ee3405d07f8c06fa9951dc6fc724b0a1c Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:50:51 +0900 Subject: [PATCH 2/8] Define Blog::slug() --- plume-models/src/blogs.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index ac20ee86..fb20be48 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -95,6 +95,10 @@ impl Blog { find_by!(blogs, find_by_ap_url, ap_url as &str); find_by!(blogs, find_by_name, actor_id as &str, instance_id as i32); + pub fn slug(title: &str) -> &str { + title + } + pub fn get_instance(&self, conn: &Connection) -> Result { Instance::get(conn, self.instance_id) } From 393f8e5e0cc4e66138e342a131ac68a85e56d786 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:51:16 +0900 Subject: [PATCH 3/8] Use Blog::slug() to determine blog's slug --- src/routes/blogs.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 99c63bf3..1e2c9636 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -79,7 +79,7 @@ pub struct NewBlogForm { } fn valid_slug(title: &str) -> Result<(), ValidationError> { - let slug = utils::make_actor_id(title); + let slug = Blog::slug(title); if slug.is_empty() { Err(ValidationError::new("empty_slug")) } else { @@ -93,7 +93,7 @@ pub fn create( conn: DbConn, rockets: PlumeRocket, ) -> RespondOrRedirect { - let slug = utils::make_actor_id(&form.title); + let slug = Blog::slug(&form.title); let intl = &rockets.intl.catalog; let user = rockets.user.clone().unwrap(); @@ -122,7 +122,7 @@ pub fn create( let blog = Blog::insert( &conn, NewBlog::new_local( - slug.clone(), + slug.into(), form.title.to_string(), String::from(""), Instance::get_local() From abf352b957978b9d6576dec602eed0029438f888 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:51:53 +0900 Subject: [PATCH 4/8] Remove unused function --- plume-common/src/utils.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 17912a02..efce6f2b 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -16,14 +16,6 @@ pub fn random_hex() -> String { .fold(String::new(), |res, byte| format!("{}{:x}", res, byte)) } -/// Remove non alphanumeric characters and CamelCase a string -pub fn make_actor_id(name: &str) -> String { - name.to_upper_camel_case() - .chars() - .filter(|c| c.is_alphanumeric()) - .collect() -} - /** * Percent-encode characters which are not allowed in IRI path segments. * From a1f958ee7a2452977b9669c79be885bad3ac545c Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:52:26 +0900 Subject: [PATCH 5/8] Remove unused import --- plume-common/src/utils.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index efce6f2b..b4c3dce5 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -1,4 +1,3 @@ -use heck::ToUpperCamelCase; use openssl::rand::rand_bytes; use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag}; use regex_syntax::is_word_character; From 248ed265c4292c8615275bca499829ce75d9d6da Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:53:16 +0900 Subject: [PATCH 6/8] Remove unsed heck from dependencies --- plume-common/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/plume-common/Cargo.toml b/plume-common/Cargo.toml index 52dbefdd..2e12a013 100644 --- a/plume-common/Cargo.toml +++ b/plume-common/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] array_tool = "1.0" base64 = "0.13" -heck = "0.4.0" hex = "0.4" openssl = "0.10.40" rocket = "0.4.6" From d6ee49b8806eb3092fa9536c00eacaf3ae60822b Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 01:55:12 +0900 Subject: [PATCH 7/8] Update Cargo.lock --- Cargo.lock | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58073d52..cea7e8ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1703,12 +1703,6 @@ dependencies = [ "ahash 0.7.6", ] -[[package]] -name = "heck" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" - [[package]] name = "hermit-abi" version = "0.1.19" @@ -3265,7 +3259,6 @@ dependencies = [ "chrono", "flume", "futures 0.3.21", - "heck", "hex", "once_cell", "openssl", From 29055d195734dfe935da1ac2b84a27072fbca7d3 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 20 May 2022 02:13:23 +0900 Subject: [PATCH 8/8] Follow clippy --- src/routes/blogs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 1e2c9636..b8f28959 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -101,7 +101,7 @@ pub fn create( Ok(_) => ValidationErrors::new(), Err(e) => e, }; - if Blog::find_by_fqn(&conn, &slug).is_ok() { + if Blog::find_by_fqn(&conn, slug).is_ok() { errors.add( "title", ValidationError {