@@ -589,7 +589,7 @@ impl ChatId {
589589 ///
590590 /// This function is rather slow because it does a lot of database queries,
591591 /// but this is fine because it is only called on chat creation.
592- async fn maybe_add_encrypted_msg ( self , context : & Context , timestamp_sort : i64 ) -> Result < ( ) > {
592+ async fn maybe_add_encrypted_msg ( self , context : & Context , timestamp_sent : i64 ) -> Result < ( ) > {
593593 let chat = Chat :: load_from_db ( context, self ) . await ?;
594594
595595 // as secure-join adds its own message on success (after some other messasges),
@@ -611,8 +611,11 @@ impl ChatId {
611611 self ,
612612 & text,
613613 SystemMessage :: ChatE2ee ,
614- timestamp_sort,
615- None ,
614+ // Create a time window for delayed encrypted messages so that they are sorted under
615+ // "Messages are end-to-end encrypted." This way time still monotonically increases and
616+ // there's no magic "N years ago" which should be adjusted in the future.
617+ timestamp_sent / 2 ,
618+ Some ( timestamp_sent) ,
616619 None ,
617620 None ,
618621 None ,
@@ -1443,37 +1446,35 @@ impl ChatId {
14431446 )
14441447 . await ?
14451448 } else if received {
1446- // Received messages shouldn't mingle with just sent ones and appear somewhere in the
1447- // middle of the chat, so we go after the newest non fresh message.
1448- //
1449- // But if a received outgoing message is older than some seen message, better sort the
1450- // received message purely by timestamp. We could place it just before that seen
1451- // message, but anyway the user may not notice it.
1449+ // Received incoming messages shouldn't mingle with just sent ones and appear somewhere
1450+ // in the middle of the chat, so we go after the newest non fresh message. Received
1451+ // outgoing messages are allowed to mingle with seen messages though to avoid seen
1452+ // replies appearing before messages sent from another device (cases like the user
1453+ // sharing the account with others or bots are rare, so let them break sometimes).
14521454 //
14531455 // NB: Received outgoing messages may break sorting of fresh incoming ones, but this
14541456 // shouldn't happen frequently. Seen incoming messages don't really break sorting of
14551457 // fresh ones, they rather mean that older incoming messages are actually seen as well.
14561458 context
14571459 . sql
14581460 . query_row_optional (
1459- "SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1461+ "SELECT MAX(timestamp)
14601462 FROM msgs
14611463 WHERE chat_id=? AND hidden=0 AND state>?
14621464 HAVING COUNT(*) > 0" ,
1463- ( MessageState :: InSeen , self , MessageState :: InFresh ) ,
1465+ (
1466+ self ,
1467+ match incoming {
1468+ true => MessageState :: InFresh ,
1469+ false => MessageState :: InSeen ,
1470+ } ,
1471+ ) ,
14641472 |row| {
14651473 let ts: i64 = row. get ( 0 ) ?;
1466- let ts_sent_seen: i64 = row. get ( 1 ) ?;
1467- Ok ( ( ts, ts_sent_seen) )
1474+ Ok ( ts)
14681475 } ,
14691476 )
14701477 . await ?
1471- . and_then ( |( ts, ts_sent_seen) | {
1472- match incoming || ts_sent_seen <= message_timestamp {
1473- true => Some ( ts) ,
1474- false => None ,
1475- }
1476- } )
14771478 } else {
14781479 None
14791480 } ;
0 commit comments