@@ -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,43 +1446,37 @@ 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: Seen incoming messages don't really break sorting of fresh ones, they rather mean
1454
1456
// that older incoming messages are actually seen as well.
1455
1457
// NB: Locally sent messages have zero `timestamp_sent`.
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
AND (state!=? OR timestamp_sent=0)
1463
1465
HAVING COUNT(*) > 0" ,
1464
1466
(
1465
- MessageState :: InSeen ,
1466
1467
self ,
1467
- MessageState :: InFresh ,
1468
+ match incoming {
1469
+ true => MessageState :: InFresh ,
1470
+ false => MessageState :: InSeen ,
1471
+ } ,
1468
1472
MessageState :: OutDelivered ,
1469
1473
) ,
1470
1474
|row| {
1471
1475
let ts: i64 = row. get ( 0 ) ?;
1472
- let ts_sent_seen: i64 = row. get ( 1 ) ?;
1473
- Ok ( ( ts, ts_sent_seen) )
1476
+ Ok ( ts)
1474
1477
} ,
1475
1478
)
1476
1479
. await ?
1477
- . and_then ( |( ts, ts_sent_seen) | {
1478
- match incoming || ts_sent_seen <= message_timestamp {
1479
- true => Some ( ts) ,
1480
- false => None ,
1481
- }
1482
- } )
1483
1480
} else {
1484
1481
None
1485
1482
} ;
0 commit comments