From 134178a74a26857b93d51b40287b311af04f9359 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 12 Nov 2019 13:09:43 +0200 Subject: [PATCH] ui/sqlite3: add remove/update for RefreshEvent Remove and/or update envelopes in sqlite3 db when the appropriate events happen. --- ui/src/conf/accounts.rs | 48 +++++++++++++++++++++++++++++++++++++++++ ui/src/sqlite3.rs | 27 ++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/ui/src/conf/accounts.rs b/ui/src/conf/accounts.rs index 2315d6505..743d3b93e 100644 --- a/ui/src/conf/accounts.rs +++ b/ui/src/conf/accounts.rs @@ -477,6 +477,21 @@ impl Account { match kind { RefreshEventKind::Update(old_hash, envelope) => { mailbox!(&folder_hash, self.folders).rename(old_hash, envelope.hash()); + #[cfg(feature = "sqlite3")] + { + if let Err(err) = crate::sqlite3::remove(old_hash).and_then(|_| { + crate::sqlite3::insert(&envelope, &self.backend, &self.name) + }) { + melib::log( + format!( + "Failed to update envelope {} in cache: {}", + envelope.message_id_display(), + err.to_string() + ), + melib::ERROR, + ); + } + } self.collection.update(old_hash, *envelope, folder_hash); return Some(EnvelopeUpdate(old_hash)); } @@ -484,6 +499,23 @@ impl Account { debug!("rename {} to {}", old_hash, new_hash); mailbox!(&folder_hash, self.folders).rename(old_hash, new_hash); self.collection.rename(old_hash, new_hash, folder_hash); + #[cfg(feature = "sqlite3")] + { + let envelopes = self.collection.envelopes.read(); + let envelopes = envelopes.unwrap(); + if let Err(err) = crate::sqlite3::remove(old_hash).and_then(|_| { + crate::sqlite3::insert(&envelopes[&new_hash], &self.backend, &self.name) + }) { + melib::log( + format!( + "Failed to update envelope {} in cache: {}", + &envelopes[&new_hash].message_id_display(), + err.to_string() + ), + melib::ERROR, + ); + } + } return Some(EnvelopeRename(old_hash, new_hash)); } RefreshEventKind::Create(envelope) => { @@ -560,6 +592,22 @@ impl Account { } RefreshEventKind::Remove(envelope_hash) => { mailbox!(&folder_hash, self.folders).remove(envelope_hash); + #[cfg(feature = "sqlite3")] + { + let envelopes = self.collection.envelopes.read(); + let envelopes = envelopes.unwrap(); + if let Err(err) = crate::sqlite3::remove(envelope_hash) { + melib::log( + format!( + "Failed to remove envelope {} [{}] in cache: {}", + &envelopes[&envelope_hash].message_id_display(), + envelope_hash, + err.to_string() + ), + melib::ERROR, + ); + } + } self.collection.remove(envelope_hash, folder_hash); return Some(EnvelopeRemove(envelope_hash)); } diff --git a/ui/src/sqlite3.rs b/ui/src/sqlite3.rs index 70c5f747f..aee707e92 100644 --- a/ui/src/sqlite3.rs +++ b/ui/src/sqlite3.rs @@ -83,7 +83,7 @@ pub fn open_db() -> Result { "CREATE TABLE IF NOT EXISTS envelopes ( id INTEGER PRIMARY KEY, account_id INTEGER REFERENCES accounts ON UPDATE CASCADE, - hash BLOB NOT NULL, + hash BLOB NOT NULL UNIQUE, date TEXT NOT NULL, _from TEXT NOT NULL, _to TEXT NOT NULL, @@ -202,6 +202,31 @@ pub fn insert(envelope: &Envelope, backend: &Arc>>, } Ok(()) } + +pub fn remove(env_hash: EnvelopeHash) -> Result<()> { + let conn = open_db()?; + if let Err(err) = conn.execute( + "DELETE FROM envelopes WHERE hash = ?", + params![env_hash.to_be_bytes().to_vec(), ]) + .map_err(|e| MeliError::new(e.to_string())) { + debug!( + "Failed to remove envelope {}: {}", + env_hash, + err.to_string() + ); + log( + format!( + "Failed to remove envelope {}: {}", + env_hash, + err.to_string() + ), + ERROR, + ); + return Err(err); + } + Ok(()) +} + pub fn index(context: &mut crate::state::Context) -> Result<()> { let conn = open_db()?; let work_context = context.work_controller().get_context();