ui/sqlite3: add remove/update for RefreshEvent

Remove and/or update envelopes in sqlite3 db when the appropriate events
happen.
master
Manos Pitsidianakis 2019-11-12 13:09:43 +02:00
parent c6a4fcb959
commit 134178a74a
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 74 additions and 1 deletions

View File

@ -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));
}

View File

@ -83,7 +83,7 @@ pub fn open_db() -> Result<Connection> {
"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<RwLock<Box<dyn MailBackend>>>,
}
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();