Skip to content

Commit

Permalink
feat: Sort received outgoing message down if it's fresher than all no…
Browse files Browse the repository at this point in the history
…n fresh messages

Received messages shouldn't mingle with just sent ones and appear somewhere in the middle of the
chat, so we go after the newest non fresh message.

But if a received outgoing message is older than some non fresh message, better sort the received
message purely by timestamp. We could place it just before that non fresh message, but anyway the
user may not notice it.

At least this fixes outgoing messages sorting for shared accounts where messages from other devices
should be sorted the same way as incoming ones.
  • Loading branch information
iequidoo committed Jul 25, 2024
1 parent 5fb5fd4 commit 254f6c4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
39 changes: 26 additions & 13 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,9 @@ impl ChatId {
contact_id: Option<ContactId>,
) -> Result<()> {
let sort_to_bottom = true;
let (received, incoming) = (false, false);
let ts = self
.calc_sort_timestamp(context, timestamp_sent, sort_to_bottom, false)
.calc_sort_timestamp(context, timestamp_sent, sort_to_bottom, received, incoming)
.await?
// Always sort protection messages below `SystemMessage::SecurejoinWait{,Timeout}` ones
// in case of race conditions.
Expand Down Expand Up @@ -1381,12 +1382,14 @@ impl ChatId {
/// corresponding event in case of a system message (usually the current system time).
/// `always_sort_to_bottom` makes this ajust the returned timestamp up so that the message goes
/// to the chat bottom.
/// `received` -- whether the message is received. Otherwise being sent.
/// `incoming` -- whether the message is incoming.
pub(crate) async fn calc_sort_timestamp(
self,
context: &Context,
message_timestamp: i64,
always_sort_to_bottom: bool,
received: bool,
incoming: bool,
) -> Result<i64> {
let mut sort_timestamp = cmp::min(message_timestamp, smeared_time(context));
Expand All @@ -1404,22 +1407,32 @@ impl ChatId {
(self, MessageState::OutDraft),
)
.await?
} else if incoming {
// get newest non fresh message for this chat.

// If a user hasn't been online for some time, the Inbox is fetched first and then the
// Sentbox. In order for Inbox and Sent messages to be allowed to mingle, outgoing
// messages are purely sorted by their sent timestamp. NB: The Inbox must be fetched
// first otherwise Inbox messages would be always below old Sentbox messages. We could
// take in the query below only incoming messages, but then new incoming messages would
// mingle with just sent outgoing ones and apear somewhere in the middle of the chat.
} else if received {
// Received messages shouldn't mingle with just sent ones and appear somewhere in the
// middle of the chat, so we go after the newest non fresh message.
//
// But if a received outgoing message is older than some non fresh message, better sort
// the received message purely by timestamp. We could place it just before that non
// fresh message, but anyway the user may not notice it.
//
// NB: received outgoing messages may break sorting of fresh incoming ones, but this
// shouldn't happen frequently.
context
.sql
.query_get_value(
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND hidden=0 AND state>?",
(self, MessageState::InFresh),
.query_row_optional(
"SELECT MAX(timestamp), MAX(IIF(state<?,timestamp_sent,0)) FROM msgs \
WHERE chat_id=? AND hidden=0 AND state>?",
(MessageState::OutPreparing, self, MessageState::InFresh),
|row| {
let ts: i64 = row.get(0)?;
let ts_sent_incoming: i64 = row.get(1)?;
Ok((ts, ts_sent_incoming))
},
)
.await?
.and_then(|(ts, ts_sent_incoming)| {
Some(ts).filter(|_| incoming || ts_sent_incoming <= message_timestamp)
})
} else {
None
};
Expand Down
2 changes: 2 additions & 0 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,11 +1255,13 @@ async fn add_parts(

let in_fresh = state == MessageState::InFresh;
let sort_to_bottom = false;
let received = true;
let sort_timestamp = chat_id
.calc_sort_timestamp(
context,
mime_parser.timestamp_sent,
sort_to_bottom,
received,
mime_parser.incoming,
)
.await?;
Expand Down
35 changes: 35 additions & 0 deletions src/receive_imf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4691,6 +4691,41 @@ async fn test_protected_group_add_remove_member_missing_key() -> Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_older_message_from_2nd_device() -> Result<()> {
let alice = &TestContext::new_alice().await;
receive_imf(
alice,
b"From: alice@example.org\n\
To: bob@example.net\n\
Message-ID: <1234-2-3@example.org>\n\
Date: Sat, 07 Dec 2019 19:00:27 +0000\n\
\n\
We share this account\n",
true,
)
.await?;
let received = receive_imf(
alice,
b"From: alice@example.org\n\
To: bob@example.net\n\
Message-ID: <1234-2-4@example.org>\n\
Date: Sat, 07 Dec 2019 19:00:26 +0000\n\
\n\
I'm Alice too\n",
true,
)
.await?
.unwrap();
alice
.golden_test_chat(
received.chat_id,
"receive_imf_older_message_from_2nd_device",
)
.await;
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_dont_create_adhoc_group_on_member_removal() -> Result<()> {
let mut tcm = TestContextManager::new();
Expand Down
3 changes: 2 additions & 1 deletion src/securejoin/bob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ pub(super) async fn start_protocol(context: &Context, invite: QrInvite) -> Resul
// Calculate the sort timestamp before checking the chat protection status so that if we
// race with its change, we don't add our message below the protection message.
let sort_to_bottom = true;
let (received, incoming) = (false, false);
let ts_sort = chat_id
.calc_sort_timestamp(context, 0, sort_to_bottom, false)
.calc_sort_timestamp(context, 0, sort_to_bottom, received, incoming)
.await?;
if chat_id.is_protected(context).await? == ProtectionStatus::Unprotected {
let ts_start = time();
Expand Down
5 changes: 5 additions & 0 deletions test-data/golden/receive_imf_older_message_from_2nd_device
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Single#Chat#10: bob@example.net [bob@example.net]
--------------------------------------------------------------------------------
Msg#10: Me (Contact#Contact#Self): We share this account √
Msg#11: Me (Contact#Contact#Self): I'm Alice too √
--------------------------------------------------------------------------------

0 comments on commit 254f6c4

Please sign in to comment.