Skip to content

Commit ad7f873

Browse files
committed
fix: Ensure that message being sent is added to the bottom (#8027)
Before, if the user fixed their clock incorrectly set to the future, they needed to delete previously sent messages or wait until this future comes again so that new sent messages are added to the bottom. Strictly speaking, the problem isn't fixable because we don't know if messages were incorrectly timestamped into the future or they are timestamped correctly and the clock is now incorrectly set to the past. Anyway, adding messages to the middle of the chat isn't a good way to inform the user about the problem.
1 parent 3236c8b commit ad7f873

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/chat.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2931,11 +2931,24 @@ pub(crate) async fn create_send_msg_jobs(context: &Context, msg: &mut Message) -
29312931
msg.param.remove(Param::GuaranteeE2ee);
29322932
}
29332933
msg.subject.clone_from(&rendered_msg.subject);
2934+
// Sort the message to the bottom. Employ `msgs_index7` to compute `timestamp`.
29342935
context
29352936
.sql
29362937
.execute(
2937-
"UPDATE msgs SET pre_rfc724_mid=?, subject=?, param=? WHERE id=?",
2938+
"
2939+
UPDATE msgs SET
2940+
timestamp=(
2941+
SELECT MAX(timestamp) FROM msgs WHERE
2942+
-- From `InFresh` to `OutMdnRcvd` inclusive except `OutDraft`.
2943+
state IN(10,13,16,18,20,24,26,28) AND
2944+
hidden IN(0,1) AND
2945+
chat_id=?
2946+
),
2947+
pre_rfc724_mid=?, subject=?, param=?
2948+
WHERE id=?
2949+
",
29382950
(
2951+
msg.chat_id,
29392952
&msg.pre_rfc724_mid,
29402953
&msg.subject,
29412954
msg.param.to_string(),

src/chat/chat_tests.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5027,6 +5027,31 @@ async fn test_do_not_overwrite_draft() -> Result<()> {
50275027
Ok(())
50285028
}
50295029

5030+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
5031+
async fn test_outgoing_msg_after_another_from_future() -> Result<()> {
5032+
let mut tcm = TestContextManager::new();
5033+
let t = &tcm.alice().await;
5034+
let chat_id = t.get_self_chat().await.id;
5035+
5036+
// Simulate sending a message with clock set to the future.
5037+
SystemTime::shift(Duration::from_secs(3600));
5038+
let msg_id = send_text_msg(t, chat_id, "test".to_string()).await?;
5039+
SystemTime::shift_back(Duration::from_secs(3600));
5040+
5041+
let timestamp_sent: i64 = t
5042+
.sql
5043+
.query_get_value("SELECT timestamp_sent FROM msgs WHERE id=?", (msg_id,))
5044+
.await?
5045+
.unwrap();
5046+
// Let's have a check here that locally sent messages have zero `timestamp_sent`, it can be a
5047+
// useful invariant.
5048+
assert_eq!(timestamp_sent, 0);
5049+
5050+
let msg_id = send_text_msg(t, chat_id, "Fixed my clock".to_string()).await?;
5051+
assert_eq!(t.get_last_msg_in(chat_id).await.id, msg_id);
5052+
Ok(())
5053+
}
5054+
50305055
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
50315056
async fn test_info_contact_id() -> Result<()> {
50325057
let mut tcm = TestContextManager::new();

0 commit comments

Comments
 (0)