From fb1b95bb2a20c68b103865e11b2b132edb6f85ba Mon Sep 17 00:00:00 2001 From: mdecimus Date: Mon, 2 Jun 2025 16:46:04 +0200 Subject: [PATCH] IMAP: Return the message UID in the destination mailbox if the message already exists (#1201) --- crates/imap/src/op/copy_move.rs | 40 ++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/crates/imap/src/op/copy_move.rs b/crates/imap/src/op/copy_move.rs index 13da7977..3325928d 100644 --- a/crates/imap/src/op/copy_move.rs +++ b/crates/imap/src/op/copy_move.rs @@ -210,15 +210,45 @@ impl SessionData { .mailboxes .iter() .any(|mailbox| mailbox.mailbox_id == src_mailbox.id.mailbox_id) - || data - .inner - .mailboxes - .iter() - .any(|mailbox| mailbox.mailbox_id == dest_mailbox_id.mailbox_id) { continue; } + // If the message is already in the destination mailbox, skip it. + if let Some(mailbox) = data + .inner + .mailboxes + .iter() + .find(|mailbox| mailbox.mailbox_id == dest_mailbox_id.mailbox_id) + { + copied_ids.push((imap_id.uid, mailbox.uid.to_native())); + + if is_move { + let mut new_data = data + .deserialize() + .imap_ctx(&arguments.tag, trc::location!())?; + new_data.remove_mailbox(src_mailbox.id.mailbox_id); + batch + .with_account_id(account_id) + .with_collection(Collection::Email) + .update_document(id) + .custom( + ObjectIndexBuilder::new() + .with_current(data) + .with_changes(new_data), + ) + .imap_ctx(&arguments.tag, trc::location!())? + .log_vanished_item( + VanishedCollection::Email, + (src_mailbox.id.mailbox_id, imap_id.uid), + ) + .commit_point(); + did_move = true; + } + + continue; + } + // Prepare changes let mut new_data = data .deserialize()