From cb5e50a8c363631e818b9a571fd7caf429150f65 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Thu, 15 Aug 2024 14:17:28 -0300 Subject: [PATCH] feat: Clean up imap, imap_sync tables for disappeared folders (#5870) --- src/imap/scan_folders.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/imap/scan_folders.rs b/src/imap/scan_folders.rs index a4e17d04d6..c3eedd70f4 100644 --- a/src/imap/scan_folders.rs +++ b/src/imap/scan_folders.rs @@ -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}; @@ -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()); @@ -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() { @@ -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) }