diff --git a/melib/src/backends/demo.rs b/melib/src/backends/demo.rs index d91208bf..0c24d9fd 100644 --- a/melib/src/backends/demo.rs +++ b/melib/src/backends/demo.rs @@ -119,14 +119,14 @@ impl BackendMailbox for DemoMailbox { pub struct DemoOp { hash: EnvelopeHash, index: Arc>, - flags: Arc, Flag)>>>, + flags: Arc)>>>, tag_index: Arc>>, } impl DemoOp { pub fn new( hash: EnvelopeHash, - flags: Arc, Flag)>>>, + flags: Arc)>>>, index: Arc>, tag_index: Arc>>, ) -> Self { @@ -146,21 +146,23 @@ impl BackendOp for DemoOp { } fn fetch_flags(&self) -> ResultFuture { - let ret = self.flags.read().unwrap()[&self.hash].1; + let ret = self.flags.read().unwrap()[&self.hash].0; Ok(Box::pin(async move { Ok(ret) })) } } /// Demo backend -#[derive(Debug, Default)] +#[derive(Debug)] pub struct DemoType { account_name: String, + account_hash: AccountHash, root_mailbox: String, mailboxes: Arc>>, envelopes: HashMap, index: Arc>, - flags: Arc, Flag)>>>, + flags: Arc)>>>, tag_index: Arc>>, + event_consumer: BackendEventConsumer, } impl MailBackend for DemoType { @@ -201,7 +203,7 @@ impl MailBackend for DemoType { } fn refresh(&mut self, _mailbox_hash: MailboxHash) -> ResultFuture<()> { - Err(MeliError::new("Unimplemented.")) + Ok(Box::pin(async { Ok(()) })) } fn mailboxes(&self) -> ResultFuture> { @@ -248,25 +250,53 @@ impl MailBackend for DemoType { fn set_flags( &mut self, env_hashes: EnvelopeHashBatch, - _mailbox_hash: MailboxHash, + mailbox_hash: MailboxHash, flags: SmallVec<[(std::result::Result, bool); 8]>, ) -> ResultFuture<()> { - for hash in env_hashes.iter() { + let mut tag_lck = self.tag_index.write().unwrap(); + let mut flags_lck = self.flags.write().unwrap(); + for env_hash in env_hashes.iter() { + let mut env_flags = flags_lck.get_mut(&env_hash).unwrap(); for set in flags.iter() { match set { (Ok(flag), value) => { - let mut flags = self.flags.read().unwrap()[&hash].1; + let mut flags = env_flags.0; flags.set(*flag, *value); - self.flags.write().unwrap().get_mut(&hash).unwrap().1 = flags; + env_flags.0 = flags; } (Err(tag), value) => { let hash = tag_hash!(tag); if *value { - self.tag_index.write().unwrap().insert(hash, tag.into()); + tag_lck.insert(hash, tag.into()); + if !env_flags.1.contains(&hash) { + env_flags.1.push(hash); + } + } else { + if let Some(pos) = env_flags.1.iter().position(|h| *h == hash) { + env_flags.1.remove(pos); + } } } } } + (self.event_consumer)( + self.account_hash, + BackendEvent::Refresh(RefreshEvent { + mailbox_hash, + account_hash: self.account_hash, + kind: RefreshEventKind::NewFlags( + env_hash, + ( + env_flags.0, + env_flags + .1 + .iter() + .map(|tag_hash| tag_lck[tag_hash].to_string()) + .collect::>(), + ), + ), + }), + ); } Ok(Box::pin(async { Ok(()) })) } @@ -284,12 +314,25 @@ impl DemoType { pub fn new( s: &AccountSettings, _is_subscribed: Box bool>, - _event_consumer: BackendEventConsumer, + event_consumer: BackendEventConsumer, ) -> Result> { + let account_hash = { + use std::collections::hash_map::DefaultHasher; + use std::hash::Hasher; + let mut hasher = DefaultHasher::new(); + hasher.write(s.name().as_bytes()); + hasher.finish() + }; let mut ret = DemoType { account_name: s.name().to_string(), + account_hash, root_mailbox: s.root_mailbox.to_string(), - ..Default::default() + event_consumer, + mailboxes: Default::default(), + envelopes: Default::default(), + index: Default::default(), + flags: Default::default(), + tag_index: Default::default(), }; { let mut mbox_lck = ret.mailboxes.lock().unwrap(); @@ -323,7 +366,7 @@ impl DemoType { for bytes in *envelopes { let env = Envelope::from_bytes(bytes.as_bytes(), None).unwrap(); index_lck.insert(env.hash(), bytes); - flags_lck.insert(env.hash(), (vec![], env.flags())); + flags_lck.insert(env.hash(), (env.flags(), vec![])); mbox.envs.push(env.hash()); ret.envelopes.insert(env.hash(), env); } diff --git a/pkg/meli.js b/pkg/meli.js index a24b963e..1fcf539a 100644 --- a/pkg/meli.js +++ b/pkg/meli.js @@ -561,8 +561,8 @@ async function init(input) { imports.wbg.__wbindgen_rethrow = function(arg0) { throw takeObject(arg0); }; - imports.wbg.__wbindgen_closure_wrapper3449 = function(arg0, arg1, arg2) { - var ret = makeMutClosure(arg0, arg1, 751, __wbg_adapter_22); + imports.wbg.__wbindgen_closure_wrapper3446 = function(arg0, arg1, arg2) { + var ret = makeMutClosure(arg0, arg1, 753, __wbg_adapter_22); return addHeapObject(ret); }; diff --git a/pkg/meli_bg.wasm b/pkg/meli_bg.wasm index 17dd40eb..82af191f 100644 Binary files a/pkg/meli_bg.wasm and b/pkg/meli_bg.wasm differ diff --git a/src/components/mail/listing.rs b/src/components/mail/listing.rs index c6dcbd24..f04b4e4a 100644 --- a/src/components/mail/listing.rs +++ b/src/components/mail/listing.rs @@ -221,6 +221,27 @@ pub trait MailListingTrait: ListingTrait { } } } + ListingAction::Delete => { + let job = account + .backend + .write() + .unwrap() + .delete_messages(env_hashes.clone(), mailbox_hash); + match job { + Err(err) => { + context.replies.push_back(UIEvent::StatusEvent( + StatusEvent::DisplayMessage(err.to_string()), + )); + } + Ok(fut) => { + let (channel, handle, job_id) = account.job_executor.spawn_specialized(fut); + account.insert_job( + job_id, + JobRequest::DeleteMessages(env_hashes, handle, channel), + ); + } + } + } ListingAction::Tag(Add(ref tag_str)) => { let job = account.backend.write().unwrap().set_flags( env_hashes.clone(), diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index 9101dcdd..894060f3 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -143,7 +143,7 @@ impl MailboxEntry { #[derive(Debug)] pub struct Account { name: String, - hash: AccountHash, + pub hash: AccountHash, pub is_online: Result<()>, pub(crate) mailbox_entries: IndexMap, pub(crate) mailboxes_order: Vec, diff --git a/src/state.rs b/src/state.rs index 867abec0..a57cfa77 100644 --- a/src/state.rs +++ b/src/state.rs @@ -363,7 +363,6 @@ impl State { * and startup a thread to remind us to poll it every now and then till it's finished. */ pub fn refresh_event(&mut self, event: RefreshEvent) { - /* let account_hash = event.account_hash; let mailbox_hash = event.mailbox_hash; if self.context.accounts[&account_hash] @@ -392,7 +391,6 @@ impl State { debug!(err); } } - */ } /// Switch back to the terminal's main screen (The command line the user sees before opening @@ -1438,6 +1436,10 @@ impl State { self.overlay.push(dialog); return; } + UIEvent::RefreshEvent(ev) => { + self.refresh_event(*ev); + return; + } _ => {} } let Self {