From 28af919b098f90f4e1a833f2761b2452babfad08 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 7 Mar 2020 01:28:45 +0300 Subject: [PATCH] Create DeleteMsgOnImap jobs before performing IMAP jobs When "delete_server_after" setting is configured, postponed DeleteMsgOnImap jobs are created for incoming messages. This commit adds job::add_imap_deletion_jobs function which creates DeleteMsgOnImap jobs right before performing IMAP jobs. This way even messages that expired when the setting was disabled are going to be deleted. Job creation on message reception is unnecessary now, and even harmful because it will create jobs with an expiration time which may later be reduced. It is planned to be removed in following commits. --- src/job.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/job.rs b/src/job.rs index 160401d85..a84ca30dc 100644 --- a/src/job.rs +++ b/src/job.rs @@ -939,6 +939,40 @@ pub fn job_send_msg(context: &Context, msg_id: MsgId) -> Result<()> { Ok(()) } +fn add_imap_deletion_jobs(context: &Context) -> sql::Result<()> { + if let Some(delete_server_after) = context.get_config_delete_server_after() { + let threshold_timestamp = time() - delete_server_after; + + // Select all expired messages which don't have a + // corresponding message deletion job yet. + let msg_ids = context.sql.query_map( + "SELECT id FROM msgs \ + WHERE timestamp < ? \ + AND server_uid != 0 \ + AND NOT EXISTS (SELECT 1 FROM jobs WHERE foreign_id = msgs.id)", + params![threshold_timestamp], + |row| row.get::<_, MsgId>(0), + |ids| { + ids.collect::, _>>() + .map_err(Into::into) + }, + )?; + + // Schedule IMAP deletion for expired messages. + for msg_id in msg_ids { + job_add( + context, + Action::DeleteMsgOnImap, + msg_id.to_u32() as i32, + Params::new(), + 0, + ) + } + } + + Ok(()) +} + pub fn perform_inbox_jobs(context: &Context) { info!(context, "dc_perform_inbox_jobs starting.",); @@ -946,6 +980,9 @@ pub fn perform_inbox_jobs(context: &Context) { *context.probe_imap_network.write().unwrap() = false; *context.perform_inbox_jobs_needed.write().unwrap() = false; + if let Err(err) = add_imap_deletion_jobs(context) { + warn!(context, "Can't add IMAP message deletion jobs: {}", err); + } job_perform(context, Thread::Imap, probe_imap_network); info!(context, "dc_perform_inbox_jobs ended.",); }