Mark message for deletion right after receiving it

This commit is contained in:
Hocuri
2026-05-06 14:18:55 +02:00
parent 0b4d8f5fee
commit 6ad3f80d8d
4 changed files with 31 additions and 37 deletions

View File

@@ -623,19 +623,6 @@ impl Context {
self.get_config_bool(Config::MdnsEnabled).await
}
/// Gets configured "delete_server_after"
///
/// `None` means never delete the message, `Some(0)` means delete
/// at once, `Some(x)` is never returned
// TODO rename and refactor
pub async fn get_config_delete_server_after(&self) -> Result<Option<i64>> {
let val = match !self.get_config_bool(Config::BccSelf).await? && self.is_chatmail().await? {
true => Some(0),
false => None,
};
Ok(val)
}
/// Gets the configured provider.
///
/// The provider is determined by the current primary transport.

View File

@@ -61,7 +61,6 @@
//! the database entries which are expired either according to their
//! ephemeral message timers.
use std::cmp::max;
use std::collections::BTreeSet;
use std::fmt;
use std::num::ParseIntError;
@@ -652,22 +651,6 @@ pub(crate) async fn ephemeral_loop(context: &Context, interrupt_receiver: Receiv
#[expect(clippy::arithmetic_side_effects)]
pub(crate) async fn delete_expired_imap_messages(context: &Context) -> Result<()> {
let now = time();
// TODO if is_chatmail, but not bcc_self, then delete after downloading
// apart from this, we may be able to remove the delete_server_after part
let (threshold_timestamp, threshold_timestamp_extended) =
match context.get_config_delete_server_after().await? {
None => (0, 0),
Some(delete_server_after) => (
match delete_server_after {
// Guarantee immediate deletion.
0 => i64::MAX,
_ => now - delete_server_after,
},
now - max(delete_server_after, 48 * 60 * 60),
),
};
context
.sql
.execute(
@@ -675,11 +658,9 @@ pub(crate) async fn delete_expired_imap_messages(context: &Context) -> Result<()
SET target=''
WHERE rfc724_mid IN (
SELECT rfc724_mid FROM msgs
WHERE ((download_state = 0 AND timestamp < ?) OR
(download_state != 0 AND timestamp < ?) OR
(ephemeral_timestamp != 0 AND ephemeral_timestamp <= ?))
WHERE ephemeral_timestamp != 0 AND ephemeral_timestamp <= ?
)",
(threshold_timestamp, threshold_timestamp_extended, now),
(now,),
)
.await?;

View File

@@ -1284,6 +1284,7 @@ impl Session {
if request_uids.is_empty() {
return Ok(());
}
let is_chatmail = self.is_chatmail();
for (request_uids, set) in build_sequence_sets(&request_uids)? {
info!(context, "Starting UID FETCH of message set \"{}\".", set);
@@ -1381,6 +1382,29 @@ impl Session {
"Passing message UID {} to receive_imf().", request_uid
);
let res = receive_imf_inner(context, rfc724_mid, body, is_seen).await;
// If the message is not needed anymore on the server, mark it for deletion:
info!(
context,
"dbg Marking for deletion?: bcc_self={}, is_chatmail={}",
context.get_config_bool(Config::BccSelf).await?,
is_chatmail
);
if !context.get_config_bool(Config::BccSelf).await? && is_chatmail {
info!(context, "dbg Marking {rfc724_mid} for deletion");
context
.sql
.execute(
&format!("UPDATE imap SET target='' WHERE rfc724_mid=?"),
(rfc724_mid,),
)
.await?;
context.scheduler.interrupt_inbox().await;
} else {
info!(context, "dbg NOT marking {rfc724_mid} for deletion");
}
// If there was an error receiving the message, show a device message:
let received_msg = match res {
Err(err) => {
warn!(context, "receive_imf error: {err:#}.");

View File

@@ -904,10 +904,12 @@ UPDATE config SET value=? WHERE keyname='configured_addr' AND value!=?1
}
// Get user-configured server deletion
let delete_server_after = context.get_config_delete_server_after().await?;
if !received_msg.msg_ids.is_empty() {
let target = if received_msg.needs_delete_job || delete_server_after == Some(0) {
info!(
context,
"dbg Would mark for deletion previously: rfc724_mid={rfc724_mid}, rfc724_mid_orig={rfc724_mid_orig}"
);
let target = if received_msg.needs_delete_job {
Some("".to_string())
} else {
None