fix delete emails

This commit is contained in:
rustmailer
2026-03-06 20:49:18 +08:00
parent a5ea98f731
commit a9a9b4a85f
2 changed files with 83 additions and 7 deletions
+54
View File
@@ -330,6 +330,44 @@ impl DuckDBManager {
}
}
pub fn get_envelopes_by_ids(
&self,
account_id: u64,
envelope_ids: &[u64],
) -> BichonResult<Vec<Envelope>> {
if envelope_ids.is_empty() {
return Ok(vec![]);
}
let conn = self.conn()?;
let ids_str = envelope_ids
.iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(",");
let query = format!(
"SELECT * FROM envelopes WHERE account_id = ? AND id IN ({})",
ids_str
);
let mut stmt = conn
.prepare(&query)
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
let rows = stmt
.query_map(params![account_id], |row| Envelope::from_row(row))
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
let mut result = Vec::new();
for row in rows {
result.push(
row.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?,
);
}
Ok(result)
}
pub fn get_all_tags(&self, accounts: Option<HashSet<u64>>) -> BichonResult<Vec<TagCount>> {
let conn = self.conn()?;
let mut sql = "
@@ -491,6 +529,22 @@ impl DuckDBManager {
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(",");
let del_attachments_query = format!(
"DELETE FROM envelope_attachments
WHERE account_id = ?
AND envelope_id IN ({})",
ids_str
);
tx.execute(&del_attachments_query, params![account_id])
.map_err(|e| {
raise_error!(
format!("Delete attachments fail: {:#?}", e),
ErrorCode::InternalError
)
})?;
let query = format!(
"DELETE FROM envelopes
WHERE account_id = ?
+29 -7
View File
@@ -26,6 +26,7 @@ use std::{
use crate::modules::{
duckdb::init::duckdb,
message::{content::AttachmentInfo, search::SortBy, tags::TagCount},
utils::create_hash,
};
use crate::{
modules::{
@@ -629,22 +630,43 @@ impl EmlIndexManager {
deletes: &HashMap<u64, Vec<u64>>, // HashMap<account_id, envelope_ids>
) -> BichonResult<()> {
if deletes.is_empty() {
tracing::warn!("delete_envelopes_multi_account: deletes is empty, nothing to delete");
tracing::warn!("delete_email_multi_account: deletes is empty, nothing to delete");
return Ok(());
}
let mut writer = self.index_writer.lock().await;
for (account_id, envelope_ids) in deletes {
let unique_ids: HashSet<u64> = envelope_ids.iter().copied().collect();
let unique_ids: Vec<u64> = envelope_ids
.iter()
.copied()
.collect::<HashSet<_>>()
.into_iter()
.collect();
if unique_ids.is_empty() {
continue;
}
for eid in unique_ids {
let query = self.envelope_query(*account_id, eid);
writer
.delete_query(query)
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
for chunk in unique_ids.chunks(100) {
let envelopes = duckdb()?.get_envelopes_by_ids(*account_id, chunk)?;
let found_ids_set: HashSet<u64> = envelopes.iter().map(|e| e.id).collect();
for &original_id in chunk {
if !found_ids_set.contains(&original_id) {
tracing::warn!(
"delete_email_multi_account: envelope not found in DB, skipping tantivy delete. account_id: {}, envelope_id: {}",
account_id, original_id
);
}
}
for envelope in envelopes {
let hashed_id = create_hash(*account_id, &envelope.message_id);
let query = self.envelope_query(*account_id, hashed_id);
writer
.delete_query(query)
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
}
}
}
writer