Remove character limit check
This commit is contained in:
parent
a3723e2e6d
commit
a67a7b9b17
5 changed files with 19 additions and 19 deletions
|
@ -21,11 +21,14 @@ use crate::activitypub::{
|
|||
use crate::config::Instance;
|
||||
use crate::errors::{ConversionError, DatabaseError, ValidationError};
|
||||
use crate::models::attachments::queries::create_attachment;
|
||||
use crate::models::posts::hashtags::normalize_hashtag;
|
||||
use crate::models::posts::helpers::get_post_by_object_id;
|
||||
use crate::models::posts::mentions::mention_to_address;
|
||||
use crate::models::posts::queries::create_post;
|
||||
use crate::models::posts::types::{Post, PostCreateData, Visibility};
|
||||
use crate::models::posts::{
|
||||
hashtags::normalize_hashtag,
|
||||
helpers::get_post_by_object_id,
|
||||
mentions::mention_to_address,
|
||||
queries::create_post,
|
||||
types::{Post, PostCreateData, Visibility},
|
||||
validators::CONTENT_MAX_SIZE,
|
||||
};
|
||||
use crate::models::profiles::queries::get_profile_by_acct;
|
||||
use crate::models::profiles::types::DbActorProfile;
|
||||
use crate::models::users::queries::get_user_by_name;
|
||||
|
@ -42,7 +45,6 @@ fn get_note_author_id(object: &Object) -> Result<String, ValidationError> {
|
|||
Ok(author_id)
|
||||
}
|
||||
|
||||
const CONTENT_MAX_SIZE: usize = 100000;
|
||||
const ATTACHMENTS_MAX_NUM: usize = 15;
|
||||
|
||||
fn parse_object_url(value: &JsonValue) -> Result<String, ConversionError> {
|
||||
|
|
|
@ -58,7 +58,7 @@ async fn create_status(
|
|||
let current_user = get_current_user(db_client, auth.token()).await?;
|
||||
let instance = config.instance();
|
||||
let mut post_data = PostCreateData::try_from(status_data.into_inner())?;
|
||||
post_data.clean(config.post_character_limit)?;
|
||||
post_data.clean()?;
|
||||
// Mentions
|
||||
let mention_map = find_mentioned_profiles(
|
||||
db_client,
|
||||
|
|
|
@ -4,4 +4,4 @@ pub mod links;
|
|||
pub mod mentions;
|
||||
pub mod queries;
|
||||
pub mod types;
|
||||
mod validators;
|
||||
pub mod validators;
|
||||
|
|
|
@ -261,9 +261,9 @@ impl PostCreateData {
|
|||
}
|
||||
|
||||
/// Validate and clean post data (only for local posts).
|
||||
pub fn clean(&mut self, character_limit: usize) -> Result<(), ValidationError> {
|
||||
pub fn clean(&mut self) -> Result<(), ValidationError> {
|
||||
assert!(self.object_id.is_none());
|
||||
self.content = clean_content(&self.content, character_limit)?;
|
||||
self.content = clean_content(&self.content)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -277,15 +277,13 @@ pub struct PostUpdateData {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const POST_CHARACTER_LIMIT: usize = 1000;
|
||||
|
||||
#[test]
|
||||
fn test_post_data_clean() {
|
||||
let mut post_data_2 = PostCreateData {
|
||||
content: "test ".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(post_data_2.clean(POST_CHARACTER_LIMIT).is_ok(), true);
|
||||
assert_eq!(post_data_2.clean().is_ok(), true);
|
||||
assert_eq!(post_data_2.content.as_str(), "test");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::errors::ValidationError;
|
||||
use crate::utils::html::clean_html_strict;
|
||||
|
||||
pub const CONTENT_MAX_SIZE: usize = 100000;
|
||||
const CONTENT_ALLOWED_TAGS: [&str; 7] = [
|
||||
"a",
|
||||
"br",
|
||||
|
@ -13,9 +14,10 @@ const CONTENT_ALLOWED_TAGS: [&str; 7] = [
|
|||
|
||||
pub fn clean_content(
|
||||
content: &str,
|
||||
character_limit: usize,
|
||||
) -> Result<String, ValidationError> {
|
||||
if content.chars().count() > character_limit {
|
||||
// Check content size to not exceed the hard limit
|
||||
// Character limit from config is not enforced at the backend
|
||||
if content.len() > CONTENT_MAX_SIZE {
|
||||
return Err(ValidationError("post is too long"));
|
||||
};
|
||||
let content_safe = clean_html_strict(content, &CONTENT_ALLOWED_TAGS);
|
||||
|
@ -30,19 +32,17 @@ pub fn clean_content(
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const POST_CHARACTER_LIMIT: usize = 1000;
|
||||
|
||||
#[test]
|
||||
fn test_clean_content_empty() {
|
||||
let content = " ";
|
||||
let result = clean_content(content, POST_CHARACTER_LIMIT);
|
||||
let result = clean_content(content);
|
||||
assert_eq!(result.is_ok(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clean_content_trimming() {
|
||||
let content = "test ";
|
||||
let cleaned = clean_content(content, POST_CHARACTER_LIMIT).unwrap();
|
||||
let cleaned = clean_content(content).unwrap();
|
||||
assert_eq!(cleaned, "test");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue