mirror of
https://github.com/chatmail/core.git
synced 2026-05-22 16:26:31 +03:00
refactor(login_param): use Config:: constants to avoid typos in key names
This commit is contained in:
@@ -122,27 +122,34 @@ pub struct EnteredLoginParam {
|
|||||||
impl EnteredLoginParam {
|
impl EnteredLoginParam {
|
||||||
/// Loads entered account settings.
|
/// Loads entered account settings.
|
||||||
pub async fn load(context: &Context) -> Result<Self> {
|
pub async fn load(context: &Context) -> Result<Self> {
|
||||||
let sql = &context.sql;
|
let addr = context
|
||||||
|
.get_config(Config::Addr)
|
||||||
let addr = sql
|
|
||||||
.get_raw_config("addr")
|
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.trim()
|
.trim()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let mail_server = sql.get_raw_config("mail_server").await?.unwrap_or_default();
|
let mail_server = context
|
||||||
let mail_port = sql
|
.get_config(Config::MailServer)
|
||||||
.get_raw_config_int("mail_port")
|
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let mail_security = sql
|
let mail_port = context
|
||||||
.get_raw_config_int("mail_security")
|
.get_config_parsed::<u16>(Config::MailPort)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
let mail_security = context
|
||||||
|
.get_config_parsed::<i32>(Config::MailSecurity)
|
||||||
.await?
|
.await?
|
||||||
.and_then(num_traits::FromPrimitive::from_i32)
|
.and_then(num_traits::FromPrimitive::from_i32)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let mail_user = sql.get_raw_config("mail_user").await?.unwrap_or_default();
|
let mail_user = context
|
||||||
let mail_pw = sql.get_raw_config("mail_pw").await?.unwrap_or_default();
|
.get_config(Config::MailUser)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
let mail_pw = context
|
||||||
|
.get_config(Config::MailPw)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
// The setting is named `imap_certificate_checks`
|
// The setting is named `imap_certificate_checks`
|
||||||
// for backwards compatibility,
|
// for backwards compatibility,
|
||||||
@@ -158,21 +165,30 @@ impl EnteredLoginParam {
|
|||||||
Default::default()
|
Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let send_server = sql.get_raw_config("send_server").await?.unwrap_or_default();
|
let send_server = context
|
||||||
let send_port = sql
|
.get_config(Config::SendServer)
|
||||||
.get_raw_config_int("send_port")
|
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let send_security = sql
|
let send_port = context
|
||||||
.get_raw_config_int("send_security")
|
.get_config_parsed::<u16>(Config::SendPort)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
let send_security = context
|
||||||
|
.get_config_parsed::<i32>(Config::SendSecurity)
|
||||||
.await?
|
.await?
|
||||||
.and_then(num_traits::FromPrimitive::from_i32)
|
.and_then(num_traits::FromPrimitive::from_i32)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let send_user = sql.get_raw_config("send_user").await?.unwrap_or_default();
|
let send_user = context
|
||||||
let send_pw = sql.get_raw_config("send_pw").await?.unwrap_or_default();
|
.get_config(Config::SendUser)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
let send_pw = context
|
||||||
|
.get_config(Config::SendPw)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
let server_flags = sql
|
let server_flags = context
|
||||||
.get_raw_config_int("server_flags")
|
.get_config_parsed::<i32>(Config::ServerFlags)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let oauth2 = matches!(server_flags & DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2);
|
let oauth2 = matches!(server_flags & DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2);
|
||||||
@@ -183,14 +199,14 @@ impl EnteredLoginParam {
|
|||||||
addr,
|
addr,
|
||||||
imap: EnteredServerLoginParam {
|
imap: EnteredServerLoginParam {
|
||||||
server: mail_server,
|
server: mail_server,
|
||||||
port: mail_port as u16,
|
port: mail_port,
|
||||||
security: mail_security,
|
security: mail_security,
|
||||||
user: mail_user,
|
user: mail_user,
|
||||||
password: mail_pw,
|
password: mail_pw,
|
||||||
},
|
},
|
||||||
smtp: EnteredServerLoginParam {
|
smtp: EnteredServerLoginParam {
|
||||||
server: send_server,
|
server: send_server,
|
||||||
port: send_port as u16,
|
port: send_port,
|
||||||
security: send_security,
|
security: send_security,
|
||||||
user: send_user,
|
user: send_user,
|
||||||
password: send_pw,
|
password: send_pw,
|
||||||
@@ -397,21 +413,20 @@ impl ConfiguredLoginParam {
|
|||||||
///
|
///
|
||||||
/// Returns `None` if account is not configured.
|
/// Returns `None` if account is not configured.
|
||||||
pub async fn load(context: &Context) -> Result<Option<Self>> {
|
pub async fn load(context: &Context) -> Result<Option<Self>> {
|
||||||
let sql = &context.sql;
|
|
||||||
|
|
||||||
if !context.get_config_bool(Config::Configured).await? {
|
if !context.get_config_bool(Config::Configured).await? {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let addr = sql
|
let addr = context
|
||||||
.get_raw_config("configured_addr")
|
.get_config(Config::ConfiguredAddr)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.trim()
|
.trim()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let certificate_checks: ConfiguredCertificateChecks = if let Some(certificate_checks) = sql
|
let certificate_checks: ConfiguredCertificateChecks = if let Some(certificate_checks) =
|
||||||
.get_raw_config_int("configured_imap_certificate_checks")
|
context
|
||||||
|
.get_config_parsed::<i32>(Config::ConfiguredImapCertificateChecks)
|
||||||
.await?
|
.await?
|
||||||
{
|
{
|
||||||
num_traits::FromPrimitive::from_i32(certificate_checks)
|
num_traits::FromPrimitive::from_i32(certificate_checks)
|
||||||
@@ -431,8 +446,8 @@ impl ConfiguredLoginParam {
|
|||||||
.await?
|
.await?
|
||||||
.context("IMAP password is not configured")?;
|
.context("IMAP password is not configured")?;
|
||||||
|
|
||||||
let server_flags = sql
|
let server_flags = context
|
||||||
.get_raw_config_int("configured_server_flags")
|
.get_config_parsed::<i32>(Config::ConfiguredServerFlags)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let oauth2 = matches!(server_flags & DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2);
|
let oauth2 = matches!(server_flags & DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2);
|
||||||
@@ -442,8 +457,8 @@ impl ConfiguredLoginParam {
|
|||||||
let imap;
|
let imap;
|
||||||
let smtp;
|
let smtp;
|
||||||
|
|
||||||
let legacy_mail_user = sql.get_raw_config("configured_mail_user").await?;
|
let legacy_mail_user = context.get_config(Config::ConfiguredMailUser).await?;
|
||||||
let legacy_send_user = sql.get_raw_config("configured_send_user").await?;
|
let legacy_send_user = context.get_config(Config::ConfiguredSendUser).await?;
|
||||||
|
|
||||||
if let Some(provider) = provider {
|
if let Some(provider) = provider {
|
||||||
let addr_localpart = if let Some(at) = addr.find('@') {
|
let addr_localpart = if let Some(at) = addr.find('@') {
|
||||||
@@ -519,18 +534,18 @@ impl ConfiguredLoginParam {
|
|||||||
.context("Failed to parse configured SMTP servers")?;
|
.context("Failed to parse configured SMTP servers")?;
|
||||||
} else {
|
} else {
|
||||||
// Load legacy settings storing a single IMAP and single SMTP server.
|
// Load legacy settings storing a single IMAP and single SMTP server.
|
||||||
let mail_server = sql
|
let mail_server = context
|
||||||
.get_raw_config("configured_mail_server")
|
.get_config(Config::ConfiguredMailServer)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let mail_port = sql
|
let mail_port = context
|
||||||
.get_raw_config_int("configured_mail_port")
|
.get_config_parsed::<u16>(Config::ConfiguredMailPort)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let mail_user = legacy_mail_user.unwrap_or_default();
|
let mail_user = legacy_mail_user.unwrap_or_default();
|
||||||
let mail_security: Socket = sql
|
let mail_security: Socket = context
|
||||||
.get_raw_config_int("configured_mail_security")
|
.get_config_parsed::<i32>(Config::ConfiguredMailSecurity)
|
||||||
.await?
|
.await?
|
||||||
.and_then(num_traits::FromPrimitive::from_i32)
|
.and_then(num_traits::FromPrimitive::from_i32)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
@@ -539,13 +554,13 @@ impl ConfiguredLoginParam {
|
|||||||
.get_config(Config::ConfiguredSendServer)
|
.get_config(Config::ConfiguredSendServer)
|
||||||
.await?
|
.await?
|
||||||
.context("SMTP server is not configured")?;
|
.context("SMTP server is not configured")?;
|
||||||
let send_port = sql
|
let send_port = context
|
||||||
.get_raw_config_int("configured_send_port")
|
.get_config_parsed::<u16>(Config::ConfiguredSendPort)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let send_user = legacy_send_user.unwrap_or_default();
|
let send_user = legacy_send_user.unwrap_or_default();
|
||||||
let send_security: Socket = sql
|
let send_security: Socket = context
|
||||||
.get_raw_config_int("configured_send_security")
|
.get_config_parsed::<i32>(Config::ConfiguredSendSecurity)
|
||||||
.await?
|
.await?
|
||||||
.and_then(num_traits::FromPrimitive::from_i32)
|
.and_then(num_traits::FromPrimitive::from_i32)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
@@ -553,7 +568,7 @@ impl ConfiguredLoginParam {
|
|||||||
imap = vec![ConfiguredServerLoginParam {
|
imap = vec![ConfiguredServerLoginParam {
|
||||||
connection: ConnectionCandidate {
|
connection: ConnectionCandidate {
|
||||||
host: mail_server,
|
host: mail_server,
|
||||||
port: mail_port as u16,
|
port: mail_port,
|
||||||
security: mail_security.try_into()?,
|
security: mail_security.try_into()?,
|
||||||
},
|
},
|
||||||
user: mail_user,
|
user: mail_user,
|
||||||
@@ -561,7 +576,7 @@ impl ConfiguredLoginParam {
|
|||||||
smtp = vec![ConfiguredServerLoginParam {
|
smtp = vec![ConfiguredServerLoginParam {
|
||||||
connection: ConnectionCandidate {
|
connection: ConnectionCandidate {
|
||||||
host: send_server,
|
host: send_server,
|
||||||
port: send_port as u16,
|
port: send_port,
|
||||||
security: send_security.try_into()?,
|
security: send_security.try_into()?,
|
||||||
},
|
},
|
||||||
user: send_user,
|
user: send_user,
|
||||||
@@ -585,8 +600,6 @@ impl ConfiguredLoginParam {
|
|||||||
|
|
||||||
/// Save this loginparam to the database.
|
/// Save this loginparam to the database.
|
||||||
pub async fn save_as_configured_params(&self, context: &Context) -> Result<()> {
|
pub async fn save_as_configured_params(&self, context: &Context) -> Result<()> {
|
||||||
let sql = &context.sql;
|
|
||||||
|
|
||||||
context.set_primary_self_addr(&self.addr).await?;
|
context.set_primary_self_addr(&self.addr).await?;
|
||||||
|
|
||||||
context
|
context
|
||||||
@@ -609,14 +622,16 @@ impl ConfiguredLoginParam {
|
|||||||
.set_config(Config::ConfiguredSendPw, Some(&self.smtp_password))
|
.set_config(Config::ConfiguredSendPw, Some(&self.smtp_password))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
sql.set_raw_config_int(
|
context
|
||||||
"configured_imap_certificate_checks",
|
.set_config_u32(
|
||||||
self.certificate_checks as i32,
|
Config::ConfiguredImapCertificateChecks,
|
||||||
|
self.certificate_checks as u32,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
sql.set_raw_config_int(
|
context
|
||||||
"configured_smtp_certificate_checks",
|
.set_config_u32(
|
||||||
self.certificate_checks as i32,
|
Config::ConfiguredSmtpCertificateChecks,
|
||||||
|
self.certificate_checks as u32,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -642,11 +657,13 @@ impl ConfiguredLoginParam {
|
|||||||
true => DC_LP_AUTH_OAUTH2,
|
true => DC_LP_AUTH_OAUTH2,
|
||||||
false => DC_LP_AUTH_NORMAL,
|
false => DC_LP_AUTH_NORMAL,
|
||||||
};
|
};
|
||||||
sql.set_raw_config_int("configured_server_flags", server_flags)
|
context
|
||||||
|
.set_config_u32(Config::ConfiguredServerFlags, server_flags as u32)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
sql.set_raw_config(
|
context
|
||||||
"configured_provider",
|
.set_config(
|
||||||
|
Config::ConfiguredProvider,
|
||||||
self.provider.map(|provider| provider.id),
|
self.provider.map(|provider| provider.id),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
Reference in New Issue
Block a user