Skip to content

Commit 3dd66db

Browse files
committed
fix: make start messages stick to the top of the chat
We already set sort_timestamp to 0 for "Messages are end-to-end encrypted." since 8f1bf96. Do this for "Others will only see this group after you sent a first message." and "Messages in this chat use classic email and are not encrypted." as well so no messages can be added on top.
1 parent e3bf6bf commit 3dd66db

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

src/chat.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,27 @@ impl ChatId {
497497
Ok(())
498498
}
499499

500+
/// Adds info message to the beginning of the chat.
501+
///
502+
/// Used for messages such as
503+
/// "Others will only see this group after you sent a first message."
504+
pub(crate) async fn add_start_info_message(self, context: &Context, text: &str) -> Result<()> {
505+
let sort_timestamp = 0;
506+
add_info_msg_with_cmd(
507+
context,
508+
self,
509+
text,
510+
SystemMessage::Unknown,
511+
Some(sort_timestamp),
512+
time(),
513+
None,
514+
None,
515+
None,
516+
)
517+
.await?;
518+
Ok(())
519+
}
520+
500521
/// Archives or unarchives a chat.
501522
pub async fn set_visibility(self, context: &Context, visibility: ChatVisibility) -> Result<()> {
502523
self.set_visibility_ex(context, Sync, visibility).await
@@ -3611,7 +3632,7 @@ pub(crate) async fn create_group_ex(
36113632
// Add "Messages in this chat use classic email and are not encrypted." message.
36123633
stock_str::chat_unencrypted_explanation(context)
36133634
};
3614-
add_info_msg(context, chat_id, &text).await?;
3635+
chat_id.add_start_info_message(context, &text).await?;
36153636
}
36163637
if let (true, true) = (sync.into(), !grpid.is_empty()) {
36173638
let id = SyncId::Grpid(grpid);

src/chat/chat_tests.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::constants::{DC_GCL_ARCHIVED_ONLY, DC_GCL_NO_SPECIALS};
77
use crate::ephemeral::Timer;
88
use crate::headerdef::HeaderDef;
99
use crate::imex::{ImexMode, has_backup, imex};
10-
use crate::message::{MessengerMessage, delete_msgs};
10+
use crate::message::{Message, MessengerMessage, delete_msgs};
1111
use crate::mimeparser::{self, MimeMessage};
1212
use crate::receive_imf::receive_imf;
1313
use crate::securejoin::{get_securejoin_qr, join_securejoin};
@@ -6156,3 +6156,67 @@ async fn test_late_message_above_seen() -> Result<()> {
61566156

61576157
Ok(())
61586158
}
6159+
6160+
/// Tests that start message for unpromoted groups sticks to the top of the chat.
6161+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
6162+
async fn test_unpromoted_group_start_message() -> Result<()> {
6163+
let mut tcm = TestContextManager::new();
6164+
let alice = &tcm.alice().await;
6165+
let bob = &tcm.bob().await;
6166+
6167+
// Start messages are disabled for test context by default,
6168+
// but this test is specifically about start messages.
6169+
alice.set_config(Config::SkipStartMessages, None).await?;
6170+
6171+
// Shift the clock forward, so we can rewind it back later.
6172+
SystemTime::shift(Duration::from_secs(3600));
6173+
6174+
// Alice creates unpromoted group with Bob.
6175+
let chat_id = create_group(alice, "Group").await?;
6176+
let bob_id = alice.add_or_lookup_contact_id(bob).await;
6177+
add_contact_to_chat(alice, chat_id, bob_id).await?;
6178+
6179+
let [
6180+
ChatItem::Message {
6181+
msg_id: e2ee_msg_id,
6182+
},
6183+
ChatItem::Message {
6184+
msg_id: info_msg_id,
6185+
},
6186+
] = get_chat_msgs(alice, chat_id).await?[..]
6187+
else {
6188+
panic!("Expected two message in the chat");
6189+
};
6190+
let msg = Message::load_from_db(alice, e2ee_msg_id).await?;
6191+
assert_eq!(msg.text, "Messages are end-to-end encrypted.");
6192+
let msg = Message::load_from_db(alice, info_msg_id).await?;
6193+
assert_eq!(
6194+
msg.text,
6195+
"Others will only see this group after you sent a first message."
6196+
);
6197+
6198+
// Alice rewinds the clock.
6199+
SystemTime::shift_back(Duration::from_secs(3600));
6200+
6201+
let text_msg_id = send_text_msg(alice, chat_id, "Hello".to_string()).await?;
6202+
6203+
let [
6204+
ChatItem::Message {
6205+
msg_id: e2ee_msg_id2,
6206+
},
6207+
ChatItem::Message {
6208+
msg_id: info_msg_id2,
6209+
},
6210+
ChatItem::Message {
6211+
msg_id: text_msg_id2,
6212+
},
6213+
] = get_chat_msgs(alice, chat_id).await?[..]
6214+
else {
6215+
panic!("Expected three message in the chat");
6216+
};
6217+
assert_eq!(e2ee_msg_id2, e2ee_msg_id);
6218+
assert_eq!(info_msg_id2, info_msg_id);
6219+
assert_eq!(text_msg_id2, text_msg_id);
6220+
6221+
Ok(())
6222+
}

0 commit comments

Comments
 (0)