@@ -298,7 +298,10 @@ impl ChatId {
298298 let chat = Chat :: load_from_db ( context, chat_id) . await ?;
299299
300300 if chat. is_encrypted ( context) . await ? {
301- chat_id. add_encrypted_msg ( context, timestamp) . await ?;
301+ let respect_delayed_msgs = true ;
302+ chat_id
303+ . add_encrypted_msg ( context, timestamp, respect_delayed_msgs)
304+ . await ?;
302305 }
303306
304307 info ! (
@@ -459,15 +462,23 @@ impl ChatId {
459462 }
460463
461464 /// Adds message "Messages are end-to-end encrypted".
462- async fn add_encrypted_msg ( self , context : & Context , timestamp_sort : i64 ) -> Result < ( ) > {
465+ async fn add_encrypted_msg (
466+ self ,
467+ context : & Context ,
468+ timestamp_sent : i64 ,
469+ respect_delayed_msgs : bool ,
470+ ) -> Result < ( ) > {
463471 let text = stock_str:: messages_e2e_encrypted ( context) . await ;
464472 add_info_msg_with_cmd (
465473 context,
466474 self ,
467475 & text,
468476 SystemMessage :: ChatE2ee ,
469- timestamp_sort,
470- None ,
477+ // Create a time window for delayed encrypted messages so that they are sorted under
478+ // "Messages are end-to-end encrypted." This way time still monotonically increases and
479+ // there's no magic "N years ago" which should be adjusted in the future.
480+ timestamp_sent / if respect_delayed_msgs { 2 } else { 1 } ,
481+ Some ( timestamp_sent) ,
471482 None ,
472483 None ,
473484 None ,
@@ -1218,37 +1229,35 @@ impl ChatId {
12181229 )
12191230 . await ?
12201231 } else if received {
1221- // Received messages shouldn't mingle with just sent ones and appear somewhere in the
1222- // middle of the chat, so we go after the newest non fresh message.
1223- //
1224- // But if a received outgoing message is older than some seen message, better sort the
1225- // received message purely by timestamp. We could place it just before that seen
1226- // message, but anyway the user may not notice it.
1232+ // Received incoming messages shouldn't mingle with just sent ones and appear somewhere
1233+ // in the middle of the chat, so we go after the newest non fresh message. Received
1234+ // outgoing messages are allowed to mingle with seen messages though to avoid seen
1235+ // replies appearing before messages sent from another device (cases like the user
1236+ // sharing the account with others or bots are rare, so let them break sometimes).
12271237 //
12281238 // NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12291239 // shouldn't happen frequently. Seen incoming messages don't really break sorting of
12301240 // fresh ones, they rather mean that older incoming messages are actually seen as well.
12311241 context
12321242 . sql
12331243 . query_row_optional (
1234- "SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1244+ "SELECT MAX(timestamp)
12351245 FROM msgs
12361246 WHERE chat_id=? AND hidden=0 AND state>?
12371247 HAVING COUNT(*) > 0" ,
1238- ( MessageState :: InSeen , self , MessageState :: InFresh ) ,
1248+ (
1249+ self ,
1250+ match incoming {
1251+ true => MessageState :: InFresh ,
1252+ false => MessageState :: InSeen ,
1253+ } ,
1254+ ) ,
12391255 |row| {
12401256 let ts: i64 = row. get ( 0 ) ?;
1241- let ts_sent_seen: i64 = row. get ( 1 ) ?;
1242- Ok ( ( ts, ts_sent_seen) )
1257+ Ok ( ts)
12431258 } ,
12441259 )
12451260 . await ?
1246- . and_then ( |( ts, ts_sent_seen) | {
1247- match incoming || ts_sent_seen <= message_timestamp {
1248- true => Some ( ts) ,
1249- false => None ,
1250- }
1251- } )
12521261 } else {
12531262 None
12541263 } ;
@@ -2423,7 +2432,10 @@ impl ChatIdBlocked {
24232432 && !chat. param . exists ( Param :: Devicetalk )
24242433 && !chat. param . exists ( Param :: Selftalk )
24252434 {
2426- chat_id. add_encrypted_msg ( context, smeared_time) . await ?;
2435+ let respect_delayed_msgs = true ;
2436+ chat_id
2437+ . add_encrypted_msg ( context, smeared_time, respect_delayed_msgs)
2438+ . await ?;
24272439 }
24282440
24292441 Ok ( Self {
@@ -3449,8 +3461,10 @@ pub(crate) async fn create_group_ex(
34493461 chatlist_events:: emit_chatlist_item_changed ( context, chat_id) ;
34503462
34513463 if is_encrypted {
3452- // Add "Messages are end-to-end encrypted." message.
3453- chat_id. add_encrypted_msg ( context, timestamp) . await ?;
3464+ let respect_delayed_msgs = false ;
3465+ chat_id
3466+ . add_encrypted_msg ( context, timestamp, respect_delayed_msgs)
3467+ . await ?;
34543468 }
34553469
34563470 if !context. get_config_bool ( Config :: Bot ) . await ?
0 commit comments