Skip to content

Commit

Permalink
feat: Clean up imap, imap_sync tables for disappeared folders (#5870)
Browse files Browse the repository at this point in the history
  • Loading branch information
iequidoo committed Aug 15, 2024
1 parent fbf66ba commit cb5e50a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/imap/scan_folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{get_folder_meaning_by_attrs, get_folder_meaning_by_name};
use crate::config::Config;
use crate::imap::{session::Session, Imap};
use crate::log::LogExt;
use crate::sql;
use crate::tools::{self, time_elapsed};
use crate::{context::Context, imap::FolderMeaning};

Expand Down Expand Up @@ -34,6 +35,7 @@ impl Imap {
let watched_folders = get_watched_folders(context).await?;

let mut folder_configs = BTreeMap::new();
let mut folder_names = Vec::new();

for folder in folders {
let folder_meaning = get_folder_meaning_by_attrs(folder.attributes());
Expand All @@ -44,6 +46,7 @@ impl Imap {
// already been moved and left it in the inbox.
continue;
}
folder_names.push(folder.name().to_string());
let folder_name_meaning = get_folder_meaning_by_name(folder.name());

if let Some(config) = folder_meaning.to_config() {
Expand Down Expand Up @@ -101,6 +104,33 @@ impl Imap {
}
}

info!(context, "Found folders: {folder_names:?}.");
let cnt = context
.sql
.execute(
&format!(
"DELETE FROM imap WHERE folder NOT IN ({})",
sql::repeat_vars(folder_names.len()),
),
rusqlite::params_from_iter(&folder_names),
)
.await
.context("Cannot delete imap rows for disappeared folders")?;
if cnt > 0 {
warn!(context, "Removed {cnt} imap rows for disappeared folders.");
}
context
.sql
.execute(
&format!(
"DELETE FROM imap_sync WHERE folder NOT IN ({})",
sql::repeat_vars(folder_names.len()),
),
rusqlite::params_from_iter(&folder_names),
)
.await
.context("Cannot delete imap_sync rows for disappeared folders")?;

last_scan.replace(tools::Time::now());
Ok(true)
}
Expand Down

0 comments on commit cb5e50a

Please sign in to comment.