#![allow(clippy::str_to_string)] use async_graphql::{ComplexObject, Enum, SimpleObject, ID};
use url::Url;
pub const SITE_CONFIG_ID: &str = "site_config";
pub const CAPTCHA_CONFIG_ID: &str = "captcha_config";
#[derive(SimpleObject)]
#[graphql(complex)]
#[allow(clippy::struct_excessive_bools)]
pub struct SiteConfig {
captcha_config: Option<CaptchaConfig>,
server_name: String,
policy_uri: Option<Url>,
tos_uri: Option<Url>,
imprint: Option<String>,
email_change_allowed: bool,
display_name_change_allowed: bool,
password_login_enabled: bool,
password_change_allowed: bool,
password_registration_enabled: bool,
minimum_password_complexity: u8,
}
#[derive(SimpleObject)]
#[graphql(complex)]
pub struct CaptchaConfig {
pub service: CaptchaService,
pub site_key: String,
}
#[derive(Enum, Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaptchaService {
RecaptchaV2,
CloudflareTurnstile,
HCaptcha,
}
#[ComplexObject]
impl SiteConfig {
pub async fn id(&self) -> ID {
SITE_CONFIG_ID.into()
}
}
impl SiteConfig {
pub fn new(data_model: &mas_data_model::SiteConfig) -> Self {
Self {
captcha_config: data_model.captcha.as_ref().map(CaptchaConfig::new),
server_name: data_model.server_name.clone(),
policy_uri: data_model.policy_uri.clone(),
tos_uri: data_model.tos_uri.clone(),
imprint: data_model.imprint.clone(),
email_change_allowed: data_model.email_change_allowed,
display_name_change_allowed: data_model.displayname_change_allowed,
password_login_enabled: data_model.password_login_enabled,
password_change_allowed: data_model.password_change_allowed,
password_registration_enabled: data_model.password_registration_enabled,
minimum_password_complexity: data_model.minimum_password_complexity,
}
}
}
#[ComplexObject]
impl CaptchaConfig {
pub async fn id(&self) -> ID {
CAPTCHA_CONFIG_ID.into()
}
}
impl CaptchaConfig {
pub fn new(data_model: &mas_data_model::CaptchaConfig) -> Self {
Self {
service: match data_model.service {
mas_data_model::CaptchaService::RecaptchaV2 => CaptchaService::RecaptchaV2,
mas_data_model::CaptchaService::CloudflareTurnstile => {
CaptchaService::CloudflareTurnstile
}
mas_data_model::CaptchaService::HCaptcha => CaptchaService::HCaptcha,
},
site_key: data_model.site_key.clone(),
}
}
}