Skip to content

Commit 17fe4c7

Browse files
committed
fix: do not trash pre-message if it is received twice
1 parent b355f44 commit 17fe4c7

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/receive_imf.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,17 @@ pub(crate) async fn receive_imf_inner(
554554
.await?
555555
.filter(|msg| msg.download_state() != DownloadState::Done)
556556
{
557-
// the message was partially downloaded before and is fully downloaded now.
558-
info!(context, "Message already partly in DB, replacing.");
559-
Some(msg.chat_id)
557+
// The message was partially downloaded before.
558+
match mime_parser.pre_message {
559+
PreMessageMode::Post | PreMessageMode::None => {
560+
info!(context, "Message already partly in DB, replacing.");
561+
Some(msg.chat_id)
562+
}
563+
PreMessageMode::Pre { .. } => {
564+
info!(context, "Cannot replace pre-message with a pre-message");
565+
None
566+
}
567+
}
560568
} else {
561569
// The message was already fully downloaded
562570
// or cannot be loaded because it is deleted.

src/tests/pre_messages/receiving.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Tests about receiving Pre-Messages and Post-Message
2-
use anyhow::Result;
2+
use anyhow::{Context as _, Result};
33
use pretty_assertions::assert_eq;
44

55
use crate::EventType;
@@ -175,6 +175,44 @@ async fn test_receive_pre_message_and_dl_post_message() -> Result<()> {
175175
Ok(())
176176
}
177177

178+
/// Test receiving the Post-Message after receiving the pre-message twice.
179+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
180+
async fn test_receive_pre_message_twice() -> Result<()> {
181+
let mut tcm = TestContextManager::new();
182+
let alice = &tcm.alice().await;
183+
let bob = &tcm.bob().await;
184+
let alice_group_id = alice.create_group_with_members("test group", &[bob]).await;
185+
186+
let (pre_message, post_message, _alice_msg_id) =
187+
send_large_file_message(alice, alice_group_id, Viewtype::File, &vec![0u8; 1_000_000])
188+
.await?;
189+
190+
let msg = bob.recv_msg(&pre_message).await;
191+
assert!(bob.recv_msg_opt(&pre_message).await.is_none());
192+
193+
// Pre-message should still be there.
194+
// Due to a bug receiving pre-message second time
195+
// deleted it in 2.44.0.
196+
// This is a regression test.
197+
let msg = Message::load_from_db(bob, msg.id)
198+
.await
199+
.context("Pre-message should still exist after receiving it twice")?;
200+
assert_eq!(msg.download_state(), DownloadState::Available);
201+
assert_eq!(msg.viewtype, Viewtype::Text);
202+
assert!(msg.param.exists(Param::PostMessageViewtype));
203+
assert!(msg.param.exists(Param::PostMessageFileBytes));
204+
assert_eq!(msg.text, "test".to_owned());
205+
206+
let _ = bob.recv_msg_trash(&post_message).await;
207+
let msg = Message::load_from_db(bob, msg.id).await?;
208+
assert_eq!(msg.download_state(), DownloadState::Done);
209+
assert_eq!(msg.viewtype, Viewtype::File);
210+
assert_eq!(msg.param.exists(Param::PostMessageViewtype), false);
211+
assert_eq!(msg.param.exists(Param::PostMessageFileBytes), false);
212+
assert_eq!(msg.text, "test".to_owned());
213+
Ok(())
214+
}
215+
178216
/// Test out of order receiving. Post-Message is received & downloaded before pre-message.
179217
/// In that case pre-message shall be trashed.
180218
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]

0 commit comments

Comments
 (0)