@@ -28,7 +28,7 @@ import {MyDocument} from './appDocsManager';
28
28
import { MyPhoto } from './appPhotosManager' ;
29
29
import DEBUG from '../../config/debug' ;
30
30
import SlicedArray , { Slice , SliceEnd } from '../../helpers/slicedArray' ;
31
- import { FOLDER_ID_ALL , FOLDER_ID_ARCHIVE , GENERAL_TOPIC_ID , HIDDEN_PEER_ID , MUTE_UNTIL , NULL_PEER_ID , REAL_FOLDERS , REAL_FOLDER_ID , REPLIES_HIDDEN_CHANNEL_ID , REPLIES_PEER_ID , SERVICE_PEER_ID , TEST_NO_SAVED , THUMB_TYPE_FULL } from '../mtproto/mtproto_config' ;
31
+ import { FOLDER_ID_ALL , FOLDER_ID_ARCHIVE , GENERAL_TOPIC_ID , HIDDEN_PEER_ID , MESSAGES_ALBUM_MAX_SIZE , MUTE_UNTIL , NULL_PEER_ID , REAL_FOLDERS , REAL_FOLDER_ID , REPLIES_HIDDEN_CHANNEL_ID , REPLIES_PEER_ID , SERVICE_PEER_ID , TEST_NO_SAVED , THUMB_TYPE_FULL } from '../mtproto/mtproto_config' ;
32
32
import { getMiddleware } from '../../helpers/middleware' ;
33
33
import assumeType from '../../helpers/assumeType' ;
34
34
import copy from '../../helpers/object/copy' ;
@@ -7948,35 +7948,60 @@ export class AppMessagesManager extends AppManager {
7948
7948
}
7949
7949
}
7950
7950
7951
- // * load grouped missing messages
7952
- const firstMessage = messages [ 0 ] as Message . message ;
7953
- const lastMessage = messages [ messages . length - 1 ] as Message . message ;
7951
+ // * load grouped missing messages (only once per recursion)
7952
+ if ( ! inputFilter && ! recursion ) {
7953
+ const firstMessage = messages [ 0 ] as Message . message ;
7954
+ const lastMessage = messages [ messages . length - 1 ] as Message . message ;
7954
7955
7955
- // if(!inputFilter && !isBottomEnd && firstMessage?.grouped_id) {
7956
- // await this.getHistory({
7957
- // ...options,
7958
- // offsetId: firstMessage.mid,
7959
- // limit: 20,
7960
- // addOffset: -10
7961
- // });
7956
+ const fillMissingGroupedMessages = async ( bottom : boolean ) => {
7957
+ if ( ! middleware ( ) ) {
7958
+ return ;
7959
+ }
7962
7960
7963
- // if(!middleware()) {
7964
- // return;
7965
- // }
7966
- // }
7961
+ const {
7962
+ isEnd,
7963
+ history,
7964
+ messages = history . map ( ( mid ) => this . getMessageByPeer ( peerId , mid ) )
7965
+ } = await this . getHistory ( {
7966
+ ...options ,
7967
+ offsetId : ( bottom ? firstMessage : lastMessage ) . mid ,
7968
+ limit : ( MESSAGES_ALBUM_MAX_SIZE + 1 ) * 2 ,
7969
+ addOffset : - ( MESSAGES_ALBUM_MAX_SIZE + 1 )
7970
+ } ) ;
7967
7971
7968
- // if(!inputFilter && !isTopEnd && lastMessage?.grouped_id && lastMessage.grouped_id !== firstMessage?.grouped_id) {
7969
- // await this.getHistory({
7970
- // ...options,
7971
- // offsetId: lastMessage.mid,
7972
- // limit: 20,
7973
- // addOffset: -10
7974
- // });
7972
+ if ( ! middleware ( ) ) {
7973
+ return ;
7974
+ }
7975
7975
7976
- // if(!middleware()) {
7977
- // return;
7978
- // }
7979
- // }
7976
+ // * erase unfilled grouped messages if they're last elements in their history slices
7977
+ if ( ! isEnd [ bottom ? 'bottom' : 'top' ] ) {
7978
+ if ( ! bottom ) messages . reverse ( ) ;
7979
+ const messagesSlice = messages . slice ( 0 , MESSAGES_ALBUM_MAX_SIZE ) as Message . message [ ] ;
7980
+ const groupedIds = messagesSlice . map ( ( message ) => message . grouped_id ) ;
7981
+ const slice = messagesSlice [ 0 ] && historyStorage . history . findSlice ( messagesSlice [ 0 ] . mid ) ;
7982
+ if (
7983
+ groupedIds [ 0 ] &&
7984
+ groupedIds [ 0 ] !== groupedIds [ MESSAGES_ALBUM_MAX_SIZE - 1 ] &&
7985
+ slice ?. index === ( bottom ? 0 : slice . slice . length - 1 )
7986
+ ) {
7987
+ messagesSlice . forEach ( ( message ) => {
7988
+ if ( message . grouped_id === groupedIds [ 0 ] ) {
7989
+ historyStorage . history . delete ( message . mid ) ;
7990
+ }
7991
+ } ) ;
7992
+ }
7993
+ }
7994
+ } ;
7995
+
7996
+
7997
+ if ( ! isBottomEnd && firstMessage ?. grouped_id ) {
7998
+ await fillMissingGroupedMessages ( true ) ;
7999
+ }
8000
+
8001
+ if ( ! isTopEnd && lastMessage ?. grouped_id && lastMessage . grouped_id !== firstMessage ?. grouped_id ) {
8002
+ await fillMissingGroupedMessages ( false ) ;
8003
+ }
8004
+ }
7980
8005
// * grouped end
7981
8006
7982
8007
if ( options . threadId ) {
@@ -8085,8 +8110,8 @@ export class AppMessagesManager extends AppManager {
8085
8110
return this . getHistory ( {
8086
8111
peerId : message . peerId ,
8087
8112
offsetId : message . mid ,
8088
- limit : 20 ,
8089
- addOffset : - 10
8113
+ limit : ( MESSAGES_ALBUM_MAX_SIZE + 1 ) * 2 ,
8114
+ addOffset : - ( MESSAGES_ALBUM_MAX_SIZE + 1 )
8090
8115
} ) ;
8091
8116
} ) ) ;
8092
8117
if ( ! middleware ( ) ) {
0 commit comments