use chrono::Utc; use regex::Regex; use serde::{Deserialize, Serialize}; use validator::{Validate, ValidationError}; lazy_static! { static ref VALID_CHARACTERS: Regex = Regex::new(r"^[a-zA-Z0-9\_]+$").unwrap(); } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Validate)] pub struct UserRow { pub pubkey: String, pub username: String, inserted_at: i64, admin: bool, } impl UserRow { pub fn new(pubkey: String, username: String, admin: bool) -> Self { Self { pubkey, username, inserted_at: Utc::now().timestamp(), admin, } } } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Validate)] pub struct User { #[validate(custom = "validate_pubkey")] pub pubkey: Option, #[validate(length(min = 1), regex = "VALID_CHARACTERS")] pub name: Option, } pub fn validate_pubkey(value: &str) -> Result<(), ValidationError> { use nostr::prelude::FromPkStr; match nostr::Keys::from_pk_str(value) { Ok(_) => Ok(()), Err(_) => Err(ValidationError::new("Unable to parse pubkey")), } }