Browse Source

ui/sqlite3: add remove/update for RefreshEvent

Remove and/or update envelopes in sqlite3 db when the appropriate events
happen.
jmap
Manos Pitsidianakis 3 years ago
parent
commit
134178a74a
Signed by untrusted user: epilys GPG Key ID: 73627C2F690DF710
  1. 48
      ui/src/conf/accounts.rs
  2. 27
      ui/src/sqlite3.rs

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

27
ui/src/sqlite3.rs

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

Loading…
Cancel
Save