Move all validators to validators module

This commit is contained in:
silverpill 2023-03-22 23:37:13 +00:00
parent 37ab3dc456
commit f5dd0a17c9
20 changed files with 62 additions and 50 deletions

View file

@ -25,7 +25,6 @@ use mitra::models::{
find_unused_remote_emojis,
get_emoji_by_name_and_hostname,
},
emojis::validators::EMOJI_LOCAL_MAX_SIZE,
oauth::queries::delete_oauth_tokens,
posts::queries::{delete_post, find_extraneous_posts, get_post_by_id},
profiles::queries::{
@ -49,6 +48,7 @@ use mitra::monero::{
helpers::check_expired_invoice,
wallet::create_monero_wallet,
};
use mitra::validators::emojis::EMOJI_LOCAL_MAX_SIZE;
use mitra_config::Config;
use mitra_utils::{
crypto_rsa::{

View file

@ -13,7 +13,6 @@ use crate::activitypub::{
};
use crate::database::DatabaseClient;
use crate::models::{
posts::validators::EMOJIS_MAX_NUM,
profiles::queries::{create_profile, update_profile},
profiles::types::{
DbActorProfile,
@ -22,6 +21,7 @@ use crate::models::{
ProfileUpdateData,
},
};
use crate::validators::posts::EMOJIS_MAX_NUM;
pub const ACTOR_IMAGE_MAX_SIZE: usize = 5 * 1000 * 1000; // 5 MB

View file

@ -35,24 +35,26 @@ use crate::models::{
update_emoji,
},
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,
EMOJI_MAX_SIZE,
EMOJI_MEDIA_TYPES,
},
posts::{
hashtags::normalize_hashtag,
queries::create_post,
types::{Post, PostCreateData, Visibility},
validators::{
content_allowed_classes,
ATTACHMENTS_MAX_NUM,
CONTENT_MAX_SIZE,
EMOJIS_MAX_NUM,
},
content_allowed_classes,
ATTACHMENTS_MAX_NUM,
CONTENT_MAX_SIZE,
EMOJIS_MAX_NUM,
},
profiles::types::DbActorProfile,
users::queries::get_user_by_name,
};
use crate::webfinger::types::ActorAddress;
use super::HandlerResult;

View file

@ -15,5 +15,6 @@ pub mod media;
pub mod models;
pub mod monero;
pub mod nodeinfo;
pub mod validators;
pub mod webfinger;
pub mod web_client;

View file

@ -29,15 +29,17 @@ use crate::models::{
ProfileImage,
ProfileUpdateData,
},
profiles::validators::validate_username,
subscriptions::types::Subscription,
users::types::{
validate_local_username,
Role,
Permission,
User,
},
};
use crate::validators::{
profiles::validate_username,
users::validate_local_username,
};
/// https://docs.joinmastodon.org/entities/field/
#[derive(Serialize)]

View file

@ -12,7 +12,7 @@ use mitra_utils::markdown::markdown_to_html;
use crate::ethereum::contracts::ContractSet;
use crate::mastodon_api::MASTODON_API_VERSION;
use crate::media::SUPPORTED_MEDIA_TYPES;
use crate::models::posts::validators::ATTACHMENTS_MAX_NUM;
use crate::validators::posts::ATTACHMENTS_MAX_NUM;
#[derive(Serialize)]
struct InstanceStats {

View file

@ -49,17 +49,17 @@ use crate::models::{
delete_post,
},
posts::types::{PostCreateData, Visibility},
posts::validators::{
clean_content,
ATTACHMENTS_MAX_NUM,
EMOJIS_MAX_NUM,
},
reactions::queries::{
create_reaction,
delete_reaction,
},
relationships::queries::get_subscribers,
};
use crate::validators::posts::{
clean_content,
ATTACHMENTS_MAX_NUM,
EMOJIS_MAX_NUM,
};
use super::helpers::{
build_status,
build_status_list,

View file

@ -1,4 +1,3 @@
pub mod helpers;
pub mod queries;
pub mod types;
pub mod validators;

View file

@ -4,9 +4,9 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
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)]
#[cfg_attr(test, derive(Default))]

View file

@ -9,7 +9,7 @@ use crate::models::{
};
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.]+):";
/// Finds emoji shortcodes in text

View file

@ -5,4 +5,3 @@ pub mod links;
pub mod mentions;
pub mod queries;
pub mod types;
pub mod validators;

View file

@ -1,4 +1,3 @@
pub mod helpers;
pub mod queries;
pub mod types;
pub mod validators;

View file

@ -22,7 +22,7 @@ use crate::database::{
};
use crate::errors::ValidationError;
use crate::models::emojis::types::DbEmoji;
use super::validators::{
use crate::validators::profiles::{
validate_username,
validate_display_name,
clean_bio,

View file

@ -1,6 +1,5 @@
use chrono::{DateTime, Utc};
use postgres_types::FromSql;
use regex::Regex;
use uuid::Uuid;
use mitra_utils::{
@ -12,7 +11,6 @@ use crate::database::{
int_enum::{int_enum_from_sql, int_enum_to_sql},
DatabaseTypeError,
};
use crate::errors::ValidationError;
use crate::models::profiles::types::DbActorProfile;
#[allow(dead_code)]
@ -172,15 +170,6 @@ pub struct UserCreateData {
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)]
mod tests {
use super::*;
@ -194,12 +183,4 @@ mod tests {
let ethereum = Currency::Ethereum;
assert_eq!(user.public_wallet_address(&ethereum), 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
View file

@ -0,0 +1,4 @@
pub mod emojis;
pub mod posts;
pub mod profiles;
pub mod users;

View file

@ -3,7 +3,7 @@ use regex::Regex;
use mitra_utils::html::{clean_html, clean_html_strict};
use crate::errors::ValidationError;
use super::types::ExtraField;
use crate::models::profiles::types::ExtraField;
const USERNAME_RE: &str = r"^[a-zA-Z0-9_\.-]+$";
const DISPLAY_NAME_MAX_LENGTH: usize = 200;

25
src/validators/users.rs Normal file
View 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);
}
}

View file

@ -11,7 +11,7 @@ use serde::{Serialize, Deserialize};
use crate::errors::ValidationError;
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\.-]+)$";
pub const JRD_CONTENT_TYPE: &str = "application/jrd+json";