Merge pull request 'Fix blog slug' (#1094) from fix-blog-slug into main

Reviewed-on: https://git.joinplu.me/Plume/Plume/pulls/1094
This commit is contained in:
KitaitiMakoto 2022-05-19 17:29:34 +00:00
commit f660220495
5 changed files with 15 additions and 21 deletions

7
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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;
@ -16,14 +15,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.
*

View file

@ -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> {
Instance::get(conn, self.instance_id)
}

View file

@ -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();
@ -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 {
@ -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()
@ -379,6 +379,7 @@ pub fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {
#[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());
}
}