Skip to content

Commit ce04e90

Browse files
committed
fix: Sort multiple saved messages by timestamp (#6862)
1 parent 026ddbf commit ce04e90

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/chat.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,7 +4343,7 @@ pub async fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId)
43434343
.sql
43444344
.query_get_value("SELECT timestamp FROM msgs WHERE id=?", (id,))
43454345
.await?
4346-
.context("No message {id}")?;
4346+
.with_context(|| format!("No message {id}"))?;
43474347
msgs.push((ts, *id));
43484348
}
43494349
msgs.sort_unstable();
@@ -4398,7 +4398,17 @@ pub async fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId)
43984398
/// Save a copy of the message in "Saved Messages"
43994399
/// and send a sync messages so that other devices save the message as well, unless deleted there.
44004400
pub async fn save_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> {
4401-
for src_msg_id in msg_ids {
4401+
let mut msgs = Vec::with_capacity(msg_ids.len());
4402+
for id in msg_ids {
4403+
let ts: i64 = context
4404+
.sql
4405+
.query_get_value("SELECT timestamp FROM msgs WHERE id=?", (id,))
4406+
.await?
4407+
.with_context(|| format!("No message {id}"))?;
4408+
msgs.push((ts, *id));
4409+
}
4410+
msgs.sort_unstable();
4411+
for (_, src_msg_id) in msgs {
44024412
let dest_rfc724_mid = create_outgoing_rfc724_mid();
44034413
let src_rfc724_mid = save_copy_in_self_talk(context, src_msg_id, &dest_rfc724_mid).await?;
44044414
context
@@ -4419,11 +4429,11 @@ pub async fn save_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> {
44194429
/// Returns data needed to add a `SaveMessage` sync item.
44204430
pub(crate) async fn save_copy_in_self_talk(
44214431
context: &Context,
4422-
src_msg_id: &MsgId,
4432+
src_msg_id: MsgId,
44234433
dest_rfc724_mid: &String,
44244434
) -> Result<String> {
44254435
let dest_chat_id = ChatId::create_for_contact(context, ContactId::SELF).await?;
4426-
let mut msg = Message::load_from_db(context, *src_msg_id).await?;
4436+
let mut msg = Message::load_from_db(context, src_msg_id).await?;
44274437
msg.param.remove(Param::Cmd);
44284438
msg.param.remove(Param::WebxdcDocument);
44294439
msg.param.remove(Param::WebxdcDocumentTimestamp);
@@ -4461,7 +4471,7 @@ pub(crate) async fn save_copy_in_self_talk(
44614471
.await?;
44624472
let dest_msg_id = MsgId::new(row_id.try_into()?);
44634473

4464-
context.emit_msgs_changed(msg.chat_id, *src_msg_id);
4474+
context.emit_msgs_changed(msg.chat_id, src_msg_id);
44654475
context.emit_msgs_changed(dest_chat_id, dest_msg_id);
44664476
chatlist_events::emit_chatlist_changed(context);
44674477
chatlist_events::emit_chatlist_item_changed(context, dest_chat_id);

src/chat/chat_tests.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,40 @@ async fn test_save_msgs() -> Result<()> {
22862286
Ok(())
22872287
}
22882288

2289+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2290+
async fn test_save_msgs_order() -> Result<()> {
2291+
let mut tcm = TestContextManager::new();
2292+
let alice = &tcm.alice().await;
2293+
let alice1 = &tcm.alice().await;
2294+
for a in [alice, alice1] {
2295+
a.set_config_bool(Config::SyncMsgs, true).await?;
2296+
}
2297+
let chat_id = create_group_chat(alice, ProtectionStatus::Protected, "grp").await?;
2298+
let sent = [
2299+
alice.send_text(chat_id, "0").await,
2300+
alice.send_text(chat_id, "1").await,
2301+
];
2302+
// NB: This doesn't work if sync messages are fetched earlier than the referenced ones.
2303+
for msg in &sent {
2304+
alice1.recv_msg(msg).await;
2305+
}
2306+
save_msgs(alice, &[sent[1].sender_msg_id, sent[0].sender_msg_id]).await?;
2307+
sync(alice, alice1).await;
2308+
2309+
for a in [alice, alice1] {
2310+
let self_chat = a.get_self_chat().await;
2311+
let msgs = get_chat_msgs(a, self_chat.id).await?;
2312+
for i in [0, 1] {
2313+
let ChatItem::Message { msg_id } = msgs[msgs.len() - 2 + i] else {
2314+
panic!("Wrong item type");
2315+
};
2316+
let msg = Message::load_from_db(a, msg_id).await.unwrap();
2317+
assert_eq!(msg.get_text(), format!("{i}"));
2318+
}
2319+
}
2320+
Ok(())
2321+
}
2322+
22892323
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
22902324
async fn test_saved_msgs_not_added_to_shared_chats() -> Result<()> {
22912325
let mut tcm = TestContextManager::new();

src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl Context {
298298

299299
async fn save_message(&self, src_rfc724_mid: &str, dest_rfc724_mid: &String) -> Result<()> {
300300
if let Some((src_msg_id, _)) = message::rfc724_mid_exists(self, src_rfc724_mid).await? {
301-
chat::save_copy_in_self_talk(self, &src_msg_id, dest_rfc724_mid).await?;
301+
chat::save_copy_in_self_talk(self, src_msg_id, dest_rfc724_mid).await?;
302302
}
303303
Ok(())
304304
}

0 commit comments

Comments
 (0)