@@ -589,7 +589,7 @@ impl ChatId {
589
589
///
590
590
/// This function is rather slow because it does a lot of database queries,
591
591
/// 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 < ( ) > {
593
593
let chat = Chat :: load_from_db ( context, self ) . await ?;
594
594
595
595
// as secure-join adds its own message on success (after some other messasges),
@@ -611,8 +611,11 @@ impl ChatId {
611
611
self ,
612
612
& text,
613
613
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) ,
616
619
None ,
617
620
None ,
618
621
None ,
@@ -1443,37 +1446,35 @@ impl ChatId {
1443
1446
)
1444
1447
. await ?
1445
1448
} 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).
1452
1454
//
1453
1455
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
1454
1456
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
1455
1457
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1456
1458
context
1457
1459
. sql
1458
1460
. query_row_optional (
1459
- "SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1461
+ "SELECT MAX(timestamp)
1460
1462
FROM msgs
1461
1463
WHERE chat_id=? AND hidden=0 AND state>?
1462
1464
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
+ ) ,
1464
1472
|row| {
1465
1473
let ts: i64 = row. get ( 0 ) ?;
1466
- let ts_sent_seen: i64 = row. get ( 1 ) ?;
1467
- Ok ( ( ts, ts_sent_seen) )
1474
+ Ok ( ts)
1468
1475
} ,
1469
1476
)
1470
1477
. 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
- } )
1477
1478
} else {
1478
1479
None
1479
1480
} ;
0 commit comments