-
-
Notifications
You must be signed in to change notification settings - Fork 125
fix: do not sort received messages below the last seen one #8031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c3bc635
c3439c6
966f829
2bb3085
700cb6b
c4cd83c
1e40834
3671857
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -477,12 +477,17 @@ impl ChatId { | |
| /// Adds message "Messages are end-to-end encrypted". | ||
| pub(crate) async fn add_e2ee_notice(self, context: &Context, timestamp: i64) -> Result<()> { | ||
| let text = stock_str::messages_e2ee_info_msg(context); | ||
|
|
||
| // Sort this notice to the very beginning of the chat. | ||
| // We don't want any message to appear before this notice | ||
| // which is normally added when encrypted chat is created. | ||
| let sort_timestamp = 0; | ||
| add_info_msg_with_cmd( | ||
| context, | ||
| self, | ||
| &text, | ||
| SystemMessage::ChatE2ee, | ||
| Some(timestamp), | ||
| Some(sort_timestamp), | ||
| timestamp, | ||
| None, | ||
| None, | ||
|
|
@@ -925,6 +930,17 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=? | |
| .unwrap_or(0)) | ||
| } | ||
|
|
||
| /// Returns timestamp of us joining the chat if we are the member of the chat. | ||
| pub(crate) async fn join_timestamp(self, context: &Context) -> Result<Option<i64>> { | ||
| context | ||
| .sql | ||
| .query_get_value( | ||
| "SELECT add_timestamp FROM chats_contacts WHERE chat_id=? AND contact_id=?", | ||
| (self, ContactId::SELF), | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| /// Returns timestamp of the latest message in the chat, | ||
| /// including hidden messages or a draft if there is one. | ||
| pub(crate) async fn get_timestamp(self, context: &Context) -> Result<Option<i64>> { | ||
|
|
@@ -1212,15 +1228,11 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=? | |
| /// corresponding event in case of a system message (usually the current system time). | ||
| /// `always_sort_to_bottom` makes this adjust 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)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So messages may still be ordered differently on devices if some chat members have clocks in the future and devices e.g. come online at different time.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this prevents the message from someone with such clock set into the future from sticking at the end of the chat, even after outgoing messages.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this ceiling is good, i just want to say that receiver-side reordering (and different order on user's devices) still may happen if sender's clock is in the future. Also, sender- or server-side reordering could happen even before multi-relay, e.g. for messages sent while being offline or using a bad network, (EDIT: removed not useful part of comment) |
||
|
|
||
|
|
@@ -1240,38 +1252,6 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=? | |
| (self, MessageState::OutDraft), | ||
| ) | ||
| .await? | ||
| } 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 seen message, better sort the | ||
| // received message purely by timestamp. We could place it just before that seen | ||
| // 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. Seen incoming messages don't really break sorting of | ||
| // fresh ones, they rather mean that older incoming messages are actually seen as well. | ||
| context | ||
| .sql | ||
| .query_row_optional( | ||
| "SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0)) | ||
| FROM msgs | ||
| WHERE chat_id=? AND hidden=0 AND state>? | ||
| HAVING COUNT(*) > 0", | ||
| (MessageState::InSeen, self, MessageState::InFresh), | ||
| |row| { | ||
| let ts: i64 = row.get(0)?; | ||
| let ts_sent_seen: i64 = row.get(1)?; | ||
| Ok((ts, ts_sent_seen)) | ||
| }, | ||
| ) | ||
| .await? | ||
| .and_then(|(ts, ts_sent_seen)| { | ||
| match incoming || ts_sent_seen <= message_timestamp { | ||
| true => Some(ts), | ||
| false => None, | ||
| } | ||
| }) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
@@ -1282,7 +1262,16 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=? | |
| sort_timestamp = last_msg_time; | ||
| } | ||
|
|
||
| Ok(sort_timestamp) | ||
| if let Some(join_timestamp) = self.join_timestamp(context).await? { | ||
| // If we are the member of the chat, don't add messages | ||
| // before the timestamp of us joining it. | ||
| // This is needed to avoid sorting "Member added" | ||
| // or automatically sent bot welcome messages | ||
| // above SecureJoin system messages. | ||
| Ok(std::cmp::max(sort_timestamp, join_timestamp)) | ||
| } else { | ||
| Ok(sort_timestamp) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -4938,15 +4927,8 @@ pub(crate) async fn add_info_msg_with_cmd( | |
| ts | ||
| } else { | ||
| let sort_to_bottom = true; | ||
| let (received, incoming) = (false, false); | ||
| chat_id | ||
| .calc_sort_timestamp( | ||
| context, | ||
| smeared_time(context), | ||
| sort_to_bottom, | ||
| received, | ||
| incoming, | ||
| ) | ||
| .calc_sort_timestamp(context, smeared_time(context), sort_to_bottom) | ||
| .await? | ||
| }; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.