From 80af012962009aea26d165da025801ea4629a1ec Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 6 Aug 2025 00:54:34 +0000 Subject: [PATCH] fix: set correct sent_timestamp for saved outgoing messages --- src/chat.rs | 19 ++++++++++++++----- src/chat/chat_tests.rs | 11 +++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index acb63da39..d3c5ee7fb 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -4481,15 +4481,24 @@ pub(crate) async fn save_copy_in_self_talk( bail!("message already saved."); } - let copy_fields = "from_id, to_id, timestamp_sent, timestamp_rcvd, type, txt, \ - mime_modified, mime_headers, mime_compressed, mime_in_reply_to, subject, msgrmsg"; + let copy_fields = "from_id, to_id, timestamp_rcvd, type, txt, + mime_modified, mime_headers, mime_compressed, mime_in_reply_to, subject, msgrmsg"; let row_id = context .sql .insert( &format!( - "INSERT INTO msgs ({copy_fields}, chat_id, rfc724_mid, state, timestamp, param, starred) \ - SELECT {copy_fields}, ?, ?, ?, ?, ?, ? \ - FROM msgs WHERE id=?;" + "INSERT INTO msgs ({copy_fields}, + timestamp_sent, + chat_id, rfc724_mid, state, timestamp, param, starred) + SELECT {copy_fields}, + -- Outgoing messages on originating device + -- have timestamp_sent == 0. + -- We copy sort timestamp instead + -- so UIs display the same timestamp + -- for saved and original message. + IIF(timestamp_sent == 0, timestamp, timestamp_sent), + ?, ?, ?, ?, ?, ? + FROM msgs WHERE id=?;" ), ( dest_chat_id, diff --git a/src/chat/chat_tests.rs b/src/chat/chat_tests.rs index b6c263ba4..3a7ca45de 100644 --- a/src/chat/chat_tests.rs +++ b/src/chat/chat_tests.rs @@ -2280,14 +2280,19 @@ async fn test_only_minimal_data_are_forwarded() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_save_msgs() -> Result<()> { - let alice = TestContext::new_alice().await; - let bob = TestContext::new_bob().await; + let mut tcm = TestContextManager::new(); + let alice = tcm.alice().await; + let bob = tcm.bob().await; let alice_chat = alice.create_chat(&bob).await; let sent = alice.send_text(alice_chat.get_id(), "hi, bob").await; let sent_msg = Message::load_from_db(&alice, sent.sender_msg_id).await?; assert!(sent_msg.get_saved_msg_id(&alice).await?.is_none()); assert!(sent_msg.get_original_msg_id(&alice).await?.is_none()); + let sent_timestamp = sent_msg.get_timestamp(); + assert!(sent_timestamp > 0); + + SystemTime::shift(Duration::from_secs(60)); let self_chat = alice.get_self_chat().await; save_msgs(&alice, &[sent.sender_msg_id]).await?; @@ -2305,6 +2310,8 @@ async fn test_save_msgs() -> Result<()> { assert_eq!(saved_msg.get_from_id(), ContactId::SELF); assert_eq!(saved_msg.get_state(), MessageState::OutDelivered); assert_ne!(saved_msg.rfc724_mid(), sent_msg.rfc724_mid()); + let saved_timestamp = saved_msg.get_timestamp(); + assert_eq!(saved_timestamp, sent_timestamp); let sent_msg = Message::load_from_db(&alice, sent.sender_msg_id).await?; assert_eq!(