mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-15 22:32:13 +00:00
character descriptions max length (#1302)
This commit is contained in:
1 parent
56c1b57cce
commit
e89229896f
4 files changed
+25
-7
No files matched your search
@@ -0,0 +1 @@
|
||||
pub const CHARACTER_MAX_DESCRIPTION_LENGTH: usize = 500;
|
||||
+10
-1
@@ -29,8 +29,9 @@ use crate::state::server_state::ServerState;
|
||||
use crate::util::http_download_url_to_bytes::http_download_url_to_bytes;
|
||||
use crate::util::lookup::lookup_media_file_urls_as_map::lookup_media_file_urls_as_map;
|
||||
|
||||
use super::common::CHARACTER_MAX_DESCRIPTION_LENGTH;
|
||||
|
||||
const MAX_NAME_LENGTH: usize = 255;
|
||||
const MAX_DESCRIPTION_LENGTH: usize = 512;
|
||||
|
||||
/// Create a new character.
|
||||
#[utoipa::path(
|
||||
@@ -86,6 +87,14 @@ pub async fn create_character_handler(
|
||||
let character_description = request.character_description.as_deref()
|
||||
.map(|desc| desc.trim());
|
||||
|
||||
if let Some(desc) = character_description {
|
||||
if desc.len() > CHARACTER_MAX_DESCRIPTION_LENGTH {
|
||||
return Err(AdvancedCommonWebError::BadInputWithSimpleMessage(
|
||||
format!("Description exceeds maximum length of {} characters.", CHARACTER_MAX_DESCRIPTION_LENGTH),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// --- Look up the image media file ---
|
||||
|
||||
let file_urls_by_token = lookup_media_file_urls_as_map(
|
||||
|
||||
+13
-6
@@ -71,7 +71,7 @@ pub async fn edit_character_handler(
|
||||
// --- Determine what to update ---
|
||||
|
||||
let new_name = resolve_name_update(&request.updated_name);
|
||||
let description_update = resolve_description_update(&request.updated_description);
|
||||
let description_update = resolve_description_update(&request.updated_description)?;
|
||||
|
||||
let has_name_change = new_name.is_some();
|
||||
let has_description_change = !matches!(description_update, DescriptionUpdateType::NoUpdate);
|
||||
@@ -138,8 +138,9 @@ pub async fn edit_character_handler(
|
||||
|
||||
// =============== Private helpers ===============
|
||||
|
||||
use super::common::CHARACTER_MAX_DESCRIPTION_LENGTH;
|
||||
|
||||
const MAX_NAME_LENGTH: usize = 50;
|
||||
const MAX_DESCRIPTION_LENGTH: usize = 500;
|
||||
|
||||
enum DescriptionUpdateType {
|
||||
NoUpdate,
|
||||
@@ -160,17 +161,23 @@ fn resolve_name_update(updated_name: &Option<String>) -> Option<String> {
|
||||
}
|
||||
|
||||
/// Determine the description update type.
|
||||
fn resolve_description_update(updated_description: &Option<String>) -> DescriptionUpdateType {
|
||||
fn resolve_description_update(
|
||||
updated_description: &Option<String>,
|
||||
) -> Result<DescriptionUpdateType, AdvancedCommonWebError> {
|
||||
let desc = match updated_description.as_ref() {
|
||||
None => return DescriptionUpdateType::NoUpdate,
|
||||
None => return Ok(DescriptionUpdateType::NoUpdate),
|
||||
Some(d) => d,
|
||||
};
|
||||
|
||||
let trimmed = desc.trim();
|
||||
if trimmed.is_empty() {
|
||||
DescriptionUpdateType::Nullify
|
||||
Ok(DescriptionUpdateType::Nullify)
|
||||
} else if trimmed.len() > CHARACTER_MAX_DESCRIPTION_LENGTH {
|
||||
Err(AdvancedCommonWebError::BadInputWithSimpleMessage(
|
||||
format!("Description exceeds maximum length of {} characters.", CHARACTER_MAX_DESCRIPTION_LENGTH),
|
||||
))
|
||||
} else {
|
||||
DescriptionUpdateType::Update(truncate(trimmed, MAX_DESCRIPTION_LENGTH))
|
||||
Ok(DescriptionUpdateType::Update(trimmed.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod common;
|
||||
pub mod create_character_handler;
|
||||
pub mod delete_character_handler;
|
||||
pub mod edit_character_handler;
|
||||
|
||||
Reference in new issue
Block a user