diff --git a/src/receive_imf.rs b/src/receive_imf.rs index 218019024..a38217251 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -539,9 +539,17 @@ pub(crate) async fn receive_imf_inner( .await? .filter(|msg| msg.download_state() != DownloadState::Done) { - // the message was partially downloaded before and is fully downloaded now. - info!(context, "Message already partly in DB, replacing."); - Some(msg.chat_id) + // The message was partially downloaded before. + match mime_parser.pre_message { + PreMessageMode::Post | PreMessageMode::None => { + info!(context, "Message already partly in DB, replacing."); + Some(msg.chat_id) + } + PreMessageMode::Pre { .. } => { + info!(context, "Cannot replace pre-message with a pre-message"); + None + } + } } else { // The message was already fully downloaded // or cannot be loaded because it is deleted. diff --git a/src/tests/pre_messages/receiving.rs b/src/tests/pre_messages/receiving.rs index cb28eda19..1926bbd07 100644 --- a/src/tests/pre_messages/receiving.rs +++ b/src/tests/pre_messages/receiving.rs @@ -1,5 +1,5 @@ //! Tests about receiving Pre-Messages and Post-Message -use anyhow::Result; +use anyhow::{Context as _, Result}; use pretty_assertions::assert_eq; use crate::EventType; @@ -175,6 +175,44 @@ async fn test_receive_pre_message_and_dl_post_message() -> Result<()> { Ok(()) } +/// Test receiving the Post-Message after receiving the pre-message twice. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_receive_pre_message_twice() -> Result<()> { + let mut tcm = TestContextManager::new(); + let alice = &tcm.alice().await; + let bob = &tcm.bob().await; + let alice_group_id = alice.create_group_with_members("test group", &[bob]).await; + + let (pre_message, post_message, _alice_msg_id) = + send_large_file_message(alice, alice_group_id, Viewtype::File, &vec![0u8; 1_000_000]) + .await?; + + let msg = bob.recv_msg(&pre_message).await; + assert!(bob.recv_msg_opt(&pre_message).await.is_none()); + + // Pre-message should still be there. + // Due to a bug receiving pre-message second time + // deleted it in 2.44.0. + // This is a regression test. + let msg = Message::load_from_db(bob, msg.id) + .await + .context("Pre-message should still exist after receiving it twice")?; + assert_eq!(msg.download_state(), DownloadState::Available); + assert_eq!(msg.viewtype, Viewtype::Text); + assert!(msg.param.exists(Param::PostMessageViewtype)); + assert!(msg.param.exists(Param::PostMessageFileBytes)); + assert_eq!(msg.text, "test".to_owned()); + + let _ = bob.recv_msg_trash(&post_message).await; + let msg = Message::load_from_db(bob, msg.id).await?; + assert_eq!(msg.download_state(), DownloadState::Done); + assert_eq!(msg.viewtype, Viewtype::File); + assert_eq!(msg.param.exists(Param::PostMessageViewtype), false); + assert_eq!(msg.param.exists(Param::PostMessageFileBytes), false); + assert_eq!(msg.text, "test".to_owned()); + Ok(()) +} + /// Test out of order receiving. Post-Message is received & downloaded before pre-message. /// In that case pre-message shall be trashed. #[tokio::test(flavor = "multi_thread", worker_threads = 2)]