Skip to content

Commit a7188f8

Browse files
committed
Fix infinite loading of nearest albums
1 parent d71311a commit a7188f8

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/lib/appManagers/appMessagesManager.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {MyDocument} from './appDocsManager';
2828
import {MyPhoto} from './appPhotosManager';
2929
import DEBUG from '../../config/debug';
3030
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';
3232
import {getMiddleware} from '../../helpers/middleware';
3333
import assumeType from '../../helpers/assumeType';
3434
import copy from '../../helpers/object/copy';
@@ -7941,33 +7941,58 @@ export class AppMessagesManager extends AppManager {
79417941
}
79427942
}
79437943

7944-
// * load grouped missing messages
7945-
const firstMessage = messages[0] as Message.message;
7946-
const lastMessage = messages[messages.length - 1] as Message.message;
7944+
// * load grouped missing messages (only once per recursion)
7945+
if(!inputFilter && !recursion) {
7946+
const firstMessage = messages[0] as Message.message;
7947+
const lastMessage = messages[messages.length - 1] as Message.message;
79477948

7948-
if(!inputFilter && !isBottomEnd && firstMessage?.grouped_id) {
7949-
await this.getHistory({
7950-
...options,
7951-
offsetId: firstMessage.mid,
7952-
limit: 20,
7953-
addOffset: -10
7954-
});
7949+
const fillMissingGroupedMessages = async(bottom: boolean) => {
7950+
if(!middleware()) {
7951+
return;
7952+
}
79557953

7956-
if(!middleware()) {
7957-
return;
7958-
}
7959-
}
7954+
const {
7955+
isEnd,
7956+
history,
7957+
messages = history.map((mid) => this.getMessageByPeer(peerId, mid))
7958+
} = await this.getHistory({
7959+
...options,
7960+
offsetId: (bottom ? firstMessage : lastMessage).mid,
7961+
limit: (MESSAGES_ALBUM_MAX_SIZE + 1) * 2,
7962+
addOffset: -(MESSAGES_ALBUM_MAX_SIZE + 1)
7963+
});
79607964

7961-
if(!inputFilter && !isTopEnd && lastMessage?.grouped_id && lastMessage.grouped_id !== firstMessage?.grouped_id) {
7962-
await this.getHistory({
7963-
...options,
7964-
offsetId: lastMessage.mid,
7965-
limit: 20,
7966-
addOffset: -10
7967-
});
7965+
if(!middleware()) {
7966+
return;
7967+
}
79687968

7969-
if(!middleware()) {
7970-
return;
7969+
// * erase unfilled grouped messages if they're last elements in their history slices
7970+
if(!isEnd[bottom ? 'bottom' : 'top']) {
7971+
if(!bottom) messages.reverse();
7972+
const messagesSlice = messages.slice(0, MESSAGES_ALBUM_MAX_SIZE) as Message.message[];
7973+
const groupedIds = messagesSlice.map((message) => message.grouped_id);
7974+
const slice = messagesSlice[0] && historyStorage.history.findSlice(messagesSlice[0].mid);
7975+
if(
7976+
groupedIds[0] &&
7977+
groupedIds[0] !== groupedIds[MESSAGES_ALBUM_MAX_SIZE - 1] &&
7978+
slice?.index === (bottom ? 0 : slice.slice.length - 1)
7979+
) {
7980+
messagesSlice.forEach((message) => {
7981+
if(message.grouped_id === groupedIds[0]) {
7982+
historyStorage.history.delete(message.mid);
7983+
}
7984+
});
7985+
}
7986+
}
7987+
};
7988+
7989+
7990+
if(!isBottomEnd && firstMessage?.grouped_id) {
7991+
await fillMissingGroupedMessages(true);
7992+
}
7993+
7994+
if(!isTopEnd && lastMessage?.grouped_id && lastMessage.grouped_id !== firstMessage?.grouped_id) {
7995+
await fillMissingGroupedMessages(false);
79717996
}
79727997
}
79737998
// * grouped end

src/lib/mtproto/mtproto_config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const MAX_FILE_SAVE_SIZE = 20 * 1024 * 1024;
2424
export const THUMB_TYPE_FULL = '';
2525
export const TOPIC_COLORS = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
2626
export const ATTACH_MENU_BOT_ICON_NAME = 'default_static';
27+
export const MESSAGES_ALBUM_MAX_SIZE = 10;
2728
export const MESSAGE_ID_OFFSET = 0x100000000;
2829
export const GENERAL_TOPIC_ID = MESSAGE_ID_OFFSET + 1;
2930
export const CAN_HIDE_TOPIC = false;

0 commit comments

Comments
 (0)