@@ -298,7 +298,10 @@ impl ChatId {
298
298
let chat = Chat :: load_from_db ( context, chat_id) . await ?;
299
299
300
300
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 ?;
302
305
}
303
306
304
307
info ! (
@@ -459,15 +462,23 @@ impl ChatId {
459
462
}
460
463
461
464
/// 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 < ( ) > {
463
471
let text = stock_str:: messages_e2e_encrypted ( context) . await ;
464
472
add_info_msg_with_cmd (
465
473
context,
466
474
self ,
467
475
& text,
468
476
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) ,
471
482
None ,
472
483
None ,
473
484
None ,
@@ -1220,37 +1231,35 @@ impl ChatId {
1220
1231
)
1221
1232
. await ?
1222
1233
} else if received {
1223
- // Received messages shouldn't mingle with just sent ones and appear somewhere in the
1224
- // middle of the chat, so we go after the newest non fresh message.
1225
- //
1226
- // But if a received outgoing message is older than some seen message, better sort the
1227
- // received message purely by timestamp. We could place it just before that seen
1228
- // message, but anyway the user may not notice it.
1234
+ // Received incoming messages shouldn't mingle with just sent ones and appear somewhere
1235
+ // in the middle of the chat, so we go after the newest non fresh message. Received
1236
+ // outgoing messages are allowed to mingle with seen messages though to avoid seen
1237
+ // replies appearing before messages sent from another device (cases like the user
1238
+ // sharing the account with others or bots are rare, so let them break sometimes).
1229
1239
//
1230
1240
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
1231
1241
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
1232
1242
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1233
1243
context
1234
1244
. sql
1235
1245
. query_row_optional (
1236
- "SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1246
+ "SELECT MAX(timestamp)
1237
1247
FROM msgs
1238
1248
WHERE chat_id=? AND hidden=0 AND state>?
1239
1249
HAVING COUNT(*) > 0" ,
1240
- ( MessageState :: InSeen , self , MessageState :: InFresh ) ,
1250
+ (
1251
+ self ,
1252
+ match incoming {
1253
+ true => MessageState :: InFresh ,
1254
+ false => MessageState :: InSeen ,
1255
+ } ,
1256
+ ) ,
1241
1257
|row| {
1242
1258
let ts: i64 = row. get ( 0 ) ?;
1243
- let ts_sent_seen: i64 = row. get ( 1 ) ?;
1244
- Ok ( ( ts, ts_sent_seen) )
1259
+ Ok ( ts)
1245
1260
} ,
1246
1261
)
1247
1262
. await ?
1248
- . and_then ( |( ts, ts_sent_seen) | {
1249
- match incoming || ts_sent_seen <= message_timestamp {
1250
- true => Some ( ts) ,
1251
- false => None ,
1252
- }
1253
- } )
1254
1263
} else {
1255
1264
None
1256
1265
} ;
@@ -2425,7 +2434,10 @@ impl ChatIdBlocked {
2425
2434
&& !chat. param . exists ( Param :: Devicetalk )
2426
2435
&& !chat. param . exists ( Param :: Selftalk )
2427
2436
{
2428
- chat_id. add_encrypted_msg ( context, smeared_time) . await ?;
2437
+ let respect_delayed_msgs = true ;
2438
+ chat_id
2439
+ . add_encrypted_msg ( context, smeared_time, respect_delayed_msgs)
2440
+ . await ?;
2429
2441
}
2430
2442
2431
2443
Ok ( Self {
@@ -3487,8 +3499,10 @@ pub(crate) async fn create_group_ex(
3487
3499
chatlist_events:: emit_chatlist_item_changed ( context, chat_id) ;
3488
3500
3489
3501
if is_encrypted {
3490
- // Add "Messages are end-to-end encrypted." message.
3491
- chat_id. add_encrypted_msg ( context, timestamp) . await ?;
3502
+ let respect_delayed_msgs = false ;
3503
+ chat_id
3504
+ . add_encrypted_msg ( context, timestamp, respect_delayed_msgs)
3505
+ . await ?;
3492
3506
}
3493
3507
3494
3508
if !context. get_config_bool ( Config :: Bot ) . await ?
0 commit comments