@@ -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 ,
@@ -1220,37 +1231,35 @@ impl ChatId {
12201231 )
12211232 . await ?
12221233 } 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).
12291239 //
12301240 // NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12311241 // shouldn't happen frequently. Seen incoming messages don't really break sorting of
12321242 // fresh ones, they rather mean that older incoming messages are actually seen as well.
12331243 context
12341244 . sql
12351245 . query_row_optional (
1236- "SELECT MAX(timestamp), MAX(IIF(state=?,timestamp_sent,0))
1246+ "SELECT MAX(timestamp)
12371247 FROM msgs
12381248 WHERE chat_id=? AND hidden=0 AND state>?
12391249 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+ ) ,
12411257 |row| {
12421258 let ts: i64 = row. get ( 0 ) ?;
1243- let ts_sent_seen: i64 = row. get ( 1 ) ?;
1244- Ok ( ( ts, ts_sent_seen) )
1259+ Ok ( ts)
12451260 } ,
12461261 )
12471262 . 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- } )
12541263 } else {
12551264 None
12561265 } ;
@@ -2425,7 +2434,10 @@ impl ChatIdBlocked {
24252434 && !chat. param . exists ( Param :: Devicetalk )
24262435 && !chat. param . exists ( Param :: Selftalk )
24272436 {
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 ?;
24292441 }
24302442
24312443 Ok ( Self {
@@ -3488,8 +3500,10 @@ pub(crate) async fn create_group_ex(
34883500 chatlist_events:: emit_chatlist_item_changed ( context, chat_id) ;
34893501
34903502 if is_encrypted {
3491- // Add "Messages are end-to-end encrypted." message.
3492- chat_id. add_encrypted_msg ( context, timestamp) . await ?;
3503+ let respect_delayed_msgs = false ;
3504+ chat_id
3505+ . add_encrypted_msg ( context, timestamp, respect_delayed_msgs)
3506+ . await ?;
34933507 }
34943508
34953509 if !context. get_config_bool ( Config :: Bot ) . await ?
0 commit comments