Add post character limit config option

This commit is contained in:
silverpill 2022-02-08 21:04:28 +00:00
parent bf2e38a397
commit 2747e7b174
5 changed files with 20 additions and 4 deletions

View file

@ -607,6 +607,10 @@ components:
login_message:
description: Login message for signer.
type: string
post_character_limit:
description: Post character limit.
type: integer
example: 5000
blockchain_explorer_url:
description: Blockchain explorer base URL.
type: string

View file

@ -65,6 +65,8 @@ fn default_environment() -> Environment { Environment::Development }
fn default_log_level() -> LogLevel { LogLevel::Info }
fn default_post_character_limit() -> usize { 2000 }
#[derive(Clone, Deserialize)]
pub struct BlockchainConfig {
pub chain_id: String,
@ -116,6 +118,9 @@ pub struct Config {
pub login_message: String,
#[serde(default = "default_post_character_limit")]
pub post_character_limit: usize,
// Blockchain integration
pub blockchain: Option<BlockchainConfig>,

View file

@ -13,6 +13,7 @@ pub struct InstanceInfo {
registrations: bool,
login_message: String,
post_character_limit: usize,
blockchain_explorer_url: Option<String>,
blockchain_contract_address: Option<String>,
ipfs_gateway_url: Option<String>,
@ -36,6 +37,7 @@ impl From<&Config> for InstanceInfo {
version: get_full_api_version(&config.version),
registrations: config.registrations_open,
login_message: config.login_message.clone(),
post_character_limit: config.post_character_limit,
blockchain_explorer_url: config.blockchain.as_ref()
.and_then(|val| val.explorer_url.clone()),
blockchain_contract_address: config.blockchain.as_ref()

View file

@ -62,7 +62,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(data.into_inner())?;
post_data.clean()?;
post_data.clean(config.post_character_limit)?;
// Mentions
let mention_map = find_mentioned_profiles(
db_client,

View file

@ -216,7 +216,10 @@ pub struct PostCreateData {
impl PostCreateData {
/// Validate and clean post data.
pub fn clean(&mut self) -> Result<(), ValidationError> {
pub fn clean(&mut self, character_limit: usize) -> Result<(), ValidationError> {
if self.content.chars().count() > character_limit {
return Err(ValidationError("post is too long"));
};
let content_safe = clean_html(&self.content);
let content_trimmed = content_safe.trim();
if content_trimmed.is_empty() {
@ -231,6 +234,8 @@ impl PostCreateData {
mod tests {
use super::*;
const POST_CHARACTER_LIMIT: usize = 1000;
#[test]
fn test_validate_post_data() {
let mut post_data_1 = PostCreateData {
@ -244,7 +249,7 @@ mod tests {
object_id: None,
created_at: None,
};
assert_eq!(post_data_1.clean().is_ok(), false);
assert_eq!(post_data_1.clean(POST_CHARACTER_LIMIT).is_ok(), false);
}
#[test]
@ -260,7 +265,7 @@ mod tests {
object_id: None,
created_at: None,
};
assert_eq!(post_data_2.clean().is_ok(), true);
assert_eq!(post_data_2.clean(POST_CHARACTER_LIMIT).is_ok(), true);
assert_eq!(post_data_2.content.as_str(), "test");
}
}