Move all validators to validators module
This commit is contained in:
parent
37ab3dc456
commit
f5dd0a17c9
20 changed files with 62 additions and 50 deletions
|
@ -25,7 +25,6 @@ use mitra::models::{
|
||||||
find_unused_remote_emojis,
|
find_unused_remote_emojis,
|
||||||
get_emoji_by_name_and_hostname,
|
get_emoji_by_name_and_hostname,
|
||||||
},
|
},
|
||||||
emojis::validators::EMOJI_LOCAL_MAX_SIZE,
|
|
||||||
oauth::queries::delete_oauth_tokens,
|
oauth::queries::delete_oauth_tokens,
|
||||||
posts::queries::{delete_post, find_extraneous_posts, get_post_by_id},
|
posts::queries::{delete_post, find_extraneous_posts, get_post_by_id},
|
||||||
profiles::queries::{
|
profiles::queries::{
|
||||||
|
@ -49,6 +48,7 @@ use mitra::monero::{
|
||||||
helpers::check_expired_invoice,
|
helpers::check_expired_invoice,
|
||||||
wallet::create_monero_wallet,
|
wallet::create_monero_wallet,
|
||||||
};
|
};
|
||||||
|
use mitra::validators::emojis::EMOJI_LOCAL_MAX_SIZE;
|
||||||
use mitra_config::Config;
|
use mitra_config::Config;
|
||||||
use mitra_utils::{
|
use mitra_utils::{
|
||||||
crypto_rsa::{
|
crypto_rsa::{
|
||||||
|
|
|
@ -13,7 +13,6 @@ use crate::activitypub::{
|
||||||
};
|
};
|
||||||
use crate::database::DatabaseClient;
|
use crate::database::DatabaseClient;
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
posts::validators::EMOJIS_MAX_NUM,
|
|
||||||
profiles::queries::{create_profile, update_profile},
|
profiles::queries::{create_profile, update_profile},
|
||||||
profiles::types::{
|
profiles::types::{
|
||||||
DbActorProfile,
|
DbActorProfile,
|
||||||
|
@ -22,6 +21,7 @@ use crate::models::{
|
||||||
ProfileUpdateData,
|
ProfileUpdateData,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use crate::validators::posts::EMOJIS_MAX_NUM;
|
||||||
|
|
||||||
pub const ACTOR_IMAGE_MAX_SIZE: usize = 5 * 1000 * 1000; // 5 MB
|
pub const ACTOR_IMAGE_MAX_SIZE: usize = 5 * 1000 * 1000; // 5 MB
|
||||||
|
|
||||||
|
|
|
@ -35,24 +35,26 @@ use crate::models::{
|
||||||
update_emoji,
|
update_emoji,
|
||||||
},
|
},
|
||||||
emojis::types::{DbEmoji, EmojiImage},
|
emojis::types::{DbEmoji, EmojiImage},
|
||||||
emojis::validators::{
|
posts::{
|
||||||
|
hashtags::normalize_hashtag,
|
||||||
|
queries::create_post,
|
||||||
|
types::{Post, PostCreateData, Visibility},
|
||||||
|
},
|
||||||
|
profiles::types::DbActorProfile,
|
||||||
|
users::queries::get_user_by_name,
|
||||||
|
};
|
||||||
|
use crate::validators::{
|
||||||
|
emojis::{
|
||||||
validate_emoji_name,
|
validate_emoji_name,
|
||||||
EMOJI_MAX_SIZE,
|
EMOJI_MAX_SIZE,
|
||||||
EMOJI_MEDIA_TYPES,
|
EMOJI_MEDIA_TYPES,
|
||||||
},
|
},
|
||||||
posts::{
|
posts::{
|
||||||
hashtags::normalize_hashtag,
|
|
||||||
queries::create_post,
|
|
||||||
types::{Post, PostCreateData, Visibility},
|
|
||||||
validators::{
|
|
||||||
content_allowed_classes,
|
content_allowed_classes,
|
||||||
ATTACHMENTS_MAX_NUM,
|
ATTACHMENTS_MAX_NUM,
|
||||||
CONTENT_MAX_SIZE,
|
CONTENT_MAX_SIZE,
|
||||||
EMOJIS_MAX_NUM,
|
EMOJIS_MAX_NUM,
|
||||||
},
|
},
|
||||||
},
|
|
||||||
profiles::types::DbActorProfile,
|
|
||||||
users::queries::get_user_by_name,
|
|
||||||
};
|
};
|
||||||
use crate::webfinger::types::ActorAddress;
|
use crate::webfinger::types::ActorAddress;
|
||||||
use super::HandlerResult;
|
use super::HandlerResult;
|
||||||
|
|
|
@ -15,5 +15,6 @@ pub mod media;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod monero;
|
pub mod monero;
|
||||||
pub mod nodeinfo;
|
pub mod nodeinfo;
|
||||||
|
pub mod validators;
|
||||||
pub mod webfinger;
|
pub mod webfinger;
|
||||||
pub mod web_client;
|
pub mod web_client;
|
||||||
|
|
|
@ -29,15 +29,17 @@ use crate::models::{
|
||||||
ProfileImage,
|
ProfileImage,
|
||||||
ProfileUpdateData,
|
ProfileUpdateData,
|
||||||
},
|
},
|
||||||
profiles::validators::validate_username,
|
|
||||||
subscriptions::types::Subscription,
|
subscriptions::types::Subscription,
|
||||||
users::types::{
|
users::types::{
|
||||||
validate_local_username,
|
|
||||||
Role,
|
Role,
|
||||||
Permission,
|
Permission,
|
||||||
User,
|
User,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use crate::validators::{
|
||||||
|
profiles::validate_username,
|
||||||
|
users::validate_local_username,
|
||||||
|
};
|
||||||
|
|
||||||
/// https://docs.joinmastodon.org/entities/field/
|
/// https://docs.joinmastodon.org/entities/field/
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
|
|
@ -12,7 +12,7 @@ use mitra_utils::markdown::markdown_to_html;
|
||||||
use crate::ethereum::contracts::ContractSet;
|
use crate::ethereum::contracts::ContractSet;
|
||||||
use crate::mastodon_api::MASTODON_API_VERSION;
|
use crate::mastodon_api::MASTODON_API_VERSION;
|
||||||
use crate::media::SUPPORTED_MEDIA_TYPES;
|
use crate::media::SUPPORTED_MEDIA_TYPES;
|
||||||
use crate::models::posts::validators::ATTACHMENTS_MAX_NUM;
|
use crate::validators::posts::ATTACHMENTS_MAX_NUM;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct InstanceStats {
|
struct InstanceStats {
|
||||||
|
|
|
@ -49,17 +49,17 @@ use crate::models::{
|
||||||
delete_post,
|
delete_post,
|
||||||
},
|
},
|
||||||
posts::types::{PostCreateData, Visibility},
|
posts::types::{PostCreateData, Visibility},
|
||||||
posts::validators::{
|
|
||||||
clean_content,
|
|
||||||
ATTACHMENTS_MAX_NUM,
|
|
||||||
EMOJIS_MAX_NUM,
|
|
||||||
},
|
|
||||||
reactions::queries::{
|
reactions::queries::{
|
||||||
create_reaction,
|
create_reaction,
|
||||||
delete_reaction,
|
delete_reaction,
|
||||||
},
|
},
|
||||||
relationships::queries::get_subscribers,
|
relationships::queries::get_subscribers,
|
||||||
};
|
};
|
||||||
|
use crate::validators::posts::{
|
||||||
|
clean_content,
|
||||||
|
ATTACHMENTS_MAX_NUM,
|
||||||
|
EMOJIS_MAX_NUM,
|
||||||
|
};
|
||||||
use super::helpers::{
|
use super::helpers::{
|
||||||
build_status,
|
build_status,
|
||||||
build_status_list,
|
build_status_list,
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
pub mod queries;
|
pub mod queries;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod validators;
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::database::json_macro::{json_from_sql, json_to_sql};
|
use crate::database::json_macro::{json_from_sql, json_to_sql};
|
||||||
use super::validators::EMOJI_MAX_SIZE;
|
|
||||||
|
|
||||||
fn default_emoji_file_size() -> usize { EMOJI_MAX_SIZE }
|
// Migration
|
||||||
|
fn default_emoji_file_size() -> usize { 250 * 1000 }
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
#[cfg_attr(test, derive(Default))]
|
#[cfg_attr(test, derive(Default))]
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::models::{
|
||||||
};
|
};
|
||||||
use super::links::is_inside_code_block;
|
use super::links::is_inside_code_block;
|
||||||
|
|
||||||
// See also: EMOJI_NAME_RE in models::emojis::validators
|
// See also: EMOJI_NAME_RE in validators::emojis
|
||||||
const SHORTCODE_SEARCH_RE: &str = r"(?m):(?P<name>[\w.]+):";
|
const SHORTCODE_SEARCH_RE: &str = r"(?m):(?P<name>[\w.]+):";
|
||||||
|
|
||||||
/// Finds emoji shortcodes in text
|
/// Finds emoji shortcodes in text
|
||||||
|
|
|
@ -5,4 +5,3 @@ pub mod links;
|
||||||
pub mod mentions;
|
pub mod mentions;
|
||||||
pub mod queries;
|
pub mod queries;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod validators;
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
pub mod queries;
|
pub mod queries;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod validators;
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ use crate::database::{
|
||||||
};
|
};
|
||||||
use crate::errors::ValidationError;
|
use crate::errors::ValidationError;
|
||||||
use crate::models::emojis::types::DbEmoji;
|
use crate::models::emojis::types::DbEmoji;
|
||||||
use super::validators::{
|
use crate::validators::profiles::{
|
||||||
validate_username,
|
validate_username,
|
||||||
validate_display_name,
|
validate_display_name,
|
||||||
clean_bio,
|
clean_bio,
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use postgres_types::FromSql;
|
use postgres_types::FromSql;
|
||||||
use regex::Regex;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use mitra_utils::{
|
use mitra_utils::{
|
||||||
|
@ -12,7 +11,6 @@ use crate::database::{
|
||||||
int_enum::{int_enum_from_sql, int_enum_to_sql},
|
int_enum::{int_enum_from_sql, int_enum_to_sql},
|
||||||
DatabaseTypeError,
|
DatabaseTypeError,
|
||||||
};
|
};
|
||||||
use crate::errors::ValidationError;
|
|
||||||
use crate::models::profiles::types::DbActorProfile;
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -172,15 +170,6 @@ pub struct UserCreateData {
|
||||||
pub role: Role,
|
pub role: Role,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_local_username(username: &str) -> Result<(), ValidationError> {
|
|
||||||
// The username regexp should not allow domain names and IP addresses
|
|
||||||
let username_regexp = Regex::new(r"^[a-z0-9_]+$").unwrap();
|
|
||||||
if !username_regexp.is_match(username) {
|
|
||||||
return Err(ValidationError("invalid username"));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -194,12 +183,4 @@ mod tests {
|
||||||
let ethereum = Currency::Ethereum;
|
let ethereum = Currency::Ethereum;
|
||||||
assert_eq!(user.public_wallet_address(ðereum), None);
|
assert_eq!(user.public_wallet_address(ðereum), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_validate_local_username() {
|
|
||||||
let result_1 = validate_local_username("name_1");
|
|
||||||
assert_eq!(result_1.is_ok(), true);
|
|
||||||
let result_2 = validate_local_username("name&");
|
|
||||||
assert_eq!(result_2.is_ok(), false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
4
src/validators/mod.rs
Normal file
4
src/validators/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
pub mod emojis;
|
||||||
|
pub mod posts;
|
||||||
|
pub mod profiles;
|
||||||
|
pub mod users;
|
|
@ -3,7 +3,7 @@ use regex::Regex;
|
||||||
use mitra_utils::html::{clean_html, clean_html_strict};
|
use mitra_utils::html::{clean_html, clean_html_strict};
|
||||||
|
|
||||||
use crate::errors::ValidationError;
|
use crate::errors::ValidationError;
|
||||||
use super::types::ExtraField;
|
use crate::models::profiles::types::ExtraField;
|
||||||
|
|
||||||
const USERNAME_RE: &str = r"^[a-zA-Z0-9_\.-]+$";
|
const USERNAME_RE: &str = r"^[a-zA-Z0-9_\.-]+$";
|
||||||
const DISPLAY_NAME_MAX_LENGTH: usize = 200;
|
const DISPLAY_NAME_MAX_LENGTH: usize = 200;
|
25
src/validators/users.rs
Normal file
25
src/validators/users.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
use crate::errors::ValidationError;
|
||||||
|
|
||||||
|
pub fn validate_local_username(username: &str) -> Result<(), ValidationError> {
|
||||||
|
// The username regexp should not allow domain names and IP addresses
|
||||||
|
let username_regexp = Regex::new(r"^[a-z0-9_]+$").unwrap();
|
||||||
|
if !username_regexp.is_match(username) {
|
||||||
|
return Err(ValidationError("invalid username"));
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_validate_local_username() {
|
||||||
|
let result_1 = validate_local_username("name_1");
|
||||||
|
assert_eq!(result_1.is_ok(), true);
|
||||||
|
let result_2 = validate_local_username("name&");
|
||||||
|
assert_eq!(result_2.is_ok(), false);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ use serde::{Serialize, Deserialize};
|
||||||
use crate::errors::ValidationError;
|
use crate::errors::ValidationError;
|
||||||
use crate::models::profiles::types::DbActorProfile;
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
|
|
||||||
// See also: USERNAME_RE in models::profiles::validators
|
// See also: USERNAME_RE in validators::profiles
|
||||||
const ACTOR_ADDRESS_RE: &str = r"^(?P<username>[\w\.-]+)@(?P<hostname>[\w\.-]+)$";
|
const ACTOR_ADDRESS_RE: &str = r"^(?P<username>[\w\.-]+)@(?P<hostname>[\w\.-]+)$";
|
||||||
|
|
||||||
pub const JRD_CONTENT_TYPE: &str = "application/jrd+json";
|
pub const JRD_CONTENT_TYPE: &str = "application/jrd+json";
|
||||||
|
|
Loading…
Reference in a new issue