refactor: replace native_db with memdb and add tests

This commit is contained in:
rustmailer
2026-05-14 02:29:23 +08:00
parent 0abaa66a40
commit 5406c4322c
102 changed files with 7225 additions and 2735 deletions
+6 -11
View File
@@ -16,23 +16,18 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use crate::autoconfig::entity::{MailServerConfig, ServerConfig};
use crate::error::code::ErrorCode;
use crate::{
{
account::entity::Encryption, autoconfig::CachedMailSettings, error::BichonResult,
},
raise_error,
{account::entity::Encryption, autoconfig::CachedMailSettings, error::BichonResult},
};
use autoconfig::config::{Server, ServerType};
use email_address::EmailAddress;
use std::str::FromStr;
use tracing::error;
pub async fn resolve_autoconfig(
email: impl AsRef<str>,
) -> BichonResult<Option<MailServerConfig>> {
pub async fn resolve_autoconfig(email: impl AsRef<str>) -> BichonResult<Option<MailServerConfig>> {
let email = email.as_ref();
let email_address = EmailAddress::from_str(email).map_err(|error| {
raise_error!(
@@ -43,7 +38,7 @@ pub async fn resolve_autoconfig(
let domain = email_address.domain();
// try read local cache first
if let Some(cached_entity) = CachedMailSettings::get(domain).await? {
if let Some(cached_entity) = CachedMailSettings::get(domain)? {
return Ok(Some(cached_entity.config));
}
@@ -66,12 +61,12 @@ pub async fn resolve_autoconfig(
.incoming_servers()
.into_iter()
.find(|s| matches!(s.server_type(), ServerType::Imap));
let imap_server = match imap_server {
Some(imap) => imap,
None => return Ok(None),
};
let get_encryption = |server: &Server| {
server
.security_type()
@@ -109,6 +104,6 @@ pub async fn resolve_autoconfig(
imap: imap_config,
oauth2: config.oauth2().map(|f| f.into()),
};
CachedMailSettings::add(domain.into(), result.clone()).await?;
CachedMailSettings::add(domain.into(), result.clone())?;
Ok(Some(result))
}
+18 -30
View File
@@ -16,16 +16,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use crate::database::manager::DB_MANAGER;
use crate::database::{delete_impl, async_find_impl, upsert_impl};
use crate::error::code::ErrorCode;
use crate::raise_error;
use crate::{
autoconfig::entity::MailServerConfig, error::BichonResult, utc_now,
};
use native_db::*;
use native_model::{native_model, Model};
use crate::database::{delete_impl, upsert_impl};
use crate::database::{find_impl, MemDbModel};
use crate::{autoconfig::entity::MailServerConfig, error::BichonResult, utc_now};
use serde::{Deserialize, Serialize};
pub mod entity;
@@ -36,45 +30,39 @@ mod tests;
const EXPIRE_TIME_MS: i64 = 30 * 24 * 60 * 60 * 1000;
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[native_model(id = 3, version = 1)]
#[native_db]
pub struct CachedMailSettings {
#[primary_key]
pub domain: String,
pub config: MailServerConfig,
pub created_at: i64,
}
impl MemDbModel for CachedMailSettings {
fn collection() -> &'static str {
"autoconfig"
}
fn key(&self) -> String {
self.domain.clone()
}
}
impl CachedMailSettings {
pub async fn add(domain: String, config: MailServerConfig) -> BichonResult<()> {
pub fn add(domain: String, config: MailServerConfig) -> BichonResult<()> {
Self {
domain,
config,
created_at: utc_now!(),
}
.save()
.await
}
async fn save(&self) -> BichonResult<()> {
upsert_impl(DB_MANAGER.meta_db(), self.to_owned()).await
fn save(&self) -> BichonResult<()> {
upsert_impl(DB_MANAGER.db(), self.to_owned())
}
pub async fn get(domain: &str) -> BichonResult<Option<CachedMailSettings>> {
if let Some(found) =
async_find_impl::<CachedMailSettings>(DB_MANAGER.meta_db(), domain.to_string()).await?
{
pub fn get(domain: &str) -> BichonResult<Option<CachedMailSettings>> {
if let Some(found) = find_impl::<CachedMailSettings>(DB_MANAGER.db(), domain)? {
if (utc_now!() - found.created_at) > EXPIRE_TIME_MS {
let domain = domain.to_string();
delete_impl(DB_MANAGER.meta_db(), |rw| {
rw.get()
.primary::<CachedMailSettings>(domain)
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?
.ok_or_else(|| {
raise_error!("auto config cache miss".into(), ErrorCode::InternalError)
})
})
.await?;
delete_impl::<CachedMailSettings>(DB_MANAGER.db(), domain)?;
Ok(None)
} else {
Ok(Some(found))