mirror of
https://github.com/rustmailer/bichon.git
synced 2026-08-03 07:48:34 +02:00
fix delete emails
This commit is contained in:
@@ -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>> {
|
pub fn get_all_tags(&self, accounts: Option<HashSet<u64>>) -> BichonResult<Vec<TagCount>> {
|
||||||
let conn = self.conn()?;
|
let conn = self.conn()?;
|
||||||
let mut sql = "
|
let mut sql = "
|
||||||
@@ -491,6 +529,22 @@ impl DuckDBManager {
|
|||||||
.map(|id| id.to_string())
|
.map(|id| id.to_string())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(",");
|
.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!(
|
let query = format!(
|
||||||
"DELETE FROM envelopes
|
"DELETE FROM envelopes
|
||||||
WHERE account_id = ?
|
WHERE account_id = ?
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ use std::{
|
|||||||
use crate::modules::{
|
use crate::modules::{
|
||||||
duckdb::init::duckdb,
|
duckdb::init::duckdb,
|
||||||
message::{content::AttachmentInfo, search::SortBy, tags::TagCount},
|
message::{content::AttachmentInfo, search::SortBy, tags::TagCount},
|
||||||
|
utils::create_hash,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
modules::{
|
modules::{
|
||||||
@@ -629,22 +630,43 @@ impl EmlIndexManager {
|
|||||||
deletes: &HashMap<u64, Vec<u64>>, // HashMap<account_id, envelope_ids>
|
deletes: &HashMap<u64, Vec<u64>>, // HashMap<account_id, envelope_ids>
|
||||||
) -> BichonResult<()> {
|
) -> BichonResult<()> {
|
||||||
if deletes.is_empty() {
|
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(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut writer = self.index_writer.lock().await;
|
let mut writer = self.index_writer.lock().await;
|
||||||
|
|
||||||
for (account_id, envelope_ids) in deletes {
|
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() {
|
if unique_ids.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for eid in unique_ids {
|
|
||||||
let query = self.envelope_query(*account_id, eid);
|
for chunk in unique_ids.chunks(100) {
|
||||||
writer
|
let envelopes = duckdb()?.get_envelopes_by_ids(*account_id, chunk)?;
|
||||||
.delete_query(query)
|
let found_ids_set: HashSet<u64> = envelopes.iter().map(|e| e.id).collect();
|
||||||
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
|
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
|
writer
|
||||||
|
|||||||
Reference in New Issue
Block a user