GramJS: Limit max id count per request for some methods (#4043)

This commit is contained in:
Alexander Zinchuk 2023-12-04 14:39:26 +01:00
parent 76629db59f
commit 19d3bf6953
5 changed files with 45 additions and 19 deletions

View File

@ -30,6 +30,7 @@ import {
import { import {
ALL_FOLDER_ID, ALL_FOLDER_ID,
API_GENERAL_ID_LIMIT,
DEBUG, GIF_MIME_TYPE, MAX_INT_32, MENTION_UNREAD_SLICE, DEBUG, GIF_MIME_TYPE, MAX_INT_32, MENTION_UNREAD_SLICE,
PINNED_MESSAGES_LIMIT, REACTION_UNREAD_SLICE, PINNED_MESSAGES_LIMIT, REACTION_UNREAD_SLICE,
SUPPORTED_IMAGE_CONTENT_TYPES, SUPPORTED_IMAGE_CONTENT_TYPES,
@ -37,7 +38,7 @@ import {
} from '../../../config'; } from '../../../config';
import { getEmojiOnlyCountForMessage } from '../../../global/helpers/getEmojiOnlyCountForMessage'; import { getEmojiOnlyCountForMessage } from '../../../global/helpers/getEmojiOnlyCountForMessage';
import { fetchFile } from '../../../util/files'; import { fetchFile } from '../../../util/files';
import { compact } from '../../../util/iteratees'; import { compact, split } from '../../../util/iteratees';
import { getServerTimeOffset } from '../../../util/serverTime'; import { getServerTimeOffset } from '../../../util/serverTime';
import { interpolateArray } from '../../../util/waveform'; import { interpolateArray } from '../../../util/waveform';
import { buildApiChatFromPreview, buildApiSendAsPeerId } from '../apiBuilders/chats'; import { buildApiChatFromPreview, buildApiSendAsPeerId } from '../apiBuilders/chats';
@ -901,16 +902,23 @@ export async function fetchMessageViews({
ids: number[]; ids: number[];
shouldIncrement?: boolean; shouldIncrement?: boolean;
}) { }) {
const result = await invokeRequest(new GramJs.messages.GetMessagesViews({ const chunks = split(ids, API_GENERAL_ID_LIMIT);
peer: buildInputPeer(chat.id, chat.accessHash), const results = await Promise.all(chunks.map((chunkIds) => (
id: ids, invokeRequest(new GramJs.messages.GetMessagesViews({
increment: shouldIncrement, peer: buildInputPeer(chat.id, chat.accessHash),
})); id: chunkIds,
increment: shouldIncrement,
}))
)));
if (!result) return undefined; if (!results || results.find((result) => !result)) return undefined;
return ids.map((id, index) => { const viewsList = results.flatMap((result) => result!.views);
const { views, forwards, replies } = result.views[index]; const users = results.flatMap((result) => result!.users);
const chats = results.flatMap((result) => result!.chats);
const viewsInfo = ids.map((id, index) => {
const { views, forwards, replies } = viewsList[index];
return { return {
id, id,
views, views,
@ -921,6 +929,12 @@ export async function fetchMessageViews({
readMaxId: replies?.readMaxId, readMaxId: replies?.readMaxId,
}; };
}); });
return {
viewsInfo,
users: users.map(buildApiUser).filter(Boolean),
chats: chats.map((c) => buildApiChatFromPreview(c)).filter(Boolean),
};
} }
export async function requestThreadInfoUpdate({ export async function requestThreadInfoUpdate({

View File

@ -3,7 +3,13 @@ import { Api as GramJs } from '../../../lib/gramjs';
import type { ApiChat, ApiReaction } from '../../types'; import type { ApiChat, ApiReaction } from '../../types';
import { REACTION_LIST_LIMIT, RECENT_REACTIONS_LIMIT, TOP_REACTIONS_LIMIT } from '../../../config'; import {
API_GENERAL_ID_LIMIT,
REACTION_LIST_LIMIT,
RECENT_REACTIONS_LIMIT,
TOP_REACTIONS_LIMIT,
} from '../../../config';
import { split } from '../../../util/iteratees';
import { buildApiChatFromPreview } from '../apiBuilders/chats'; import { buildApiChatFromPreview } from '../apiBuilders/chats';
import { buildApiAvailableReaction, buildApiReaction, buildMessagePeerReaction } from '../apiBuilders/reactions'; import { buildApiAvailableReaction, buildApiReaction, buildMessagePeerReaction } from '../apiBuilders/reactions';
import { buildApiUser } from '../apiBuilders/users'; import { buildApiUser } from '../apiBuilders/users';
@ -108,12 +114,15 @@ export function fetchMessageReactions({
}: { }: {
ids: number[]; chat: ApiChat; ids: number[]; chat: ApiChat;
}) { }) {
return invokeRequest(new GramJs.messages.GetMessagesReactions({ const chunks = split(ids, API_GENERAL_ID_LIMIT);
id: ids, chunks.forEach((chunkIds) => {
peer: buildInputPeer(chat.id, chat.accessHash), invokeRequest(new GramJs.messages.GetMessagesReactions({
}), { id: chunkIds,
shouldReturnTrue: true, peer: buildInputPeer(chat.id, chat.accessHash),
abortControllerChatId: chat.id, }), {
shouldReturnTrue: true,
abortControllerChatId: chat.id,
});
}); });
} }

View File

@ -120,8 +120,8 @@ type StateProps = {
isServiceNotificationsChat?: boolean; isServiceNotificationsChat?: boolean;
}; };
const MESSAGE_REACTIONS_POLLING_INTERVAL = 15 * 1000; const MESSAGE_REACTIONS_POLLING_INTERVAL = 20 * 1000;
const MESSAGE_COMMENTS_POLLING_INTERVAL = 15 * 1000; const MESSAGE_COMMENTS_POLLING_INTERVAL = 20 * 1000;
const MESSAGE_STORY_POLLING_INTERVAL = 5 * 60 * 1000; const MESSAGE_STORY_POLLING_INTERVAL = 5 * 60 * 1000;
const BOTTOM_THRESHOLD = 50; const BOTTOM_THRESHOLD = 50;
const UNREAD_DIVIDER_TOP = 10; const UNREAD_DIVIDER_TOP = 10;

View File

@ -89,6 +89,7 @@ export const TOPIC_LIST_SENSITIVE_AREA = 600;
export const COMMON_CHATS_LIMIT = 100; export const COMMON_CHATS_LIMIT = 100;
export const GROUP_CALL_PARTICIPANTS_LIMIT = 100; export const GROUP_CALL_PARTICIPANTS_LIMIT = 100;
export const STORY_LIST_LIMIT = 100; export const STORY_LIST_LIMIT = 100;
export const API_GENERAL_ID_LIMIT = 100;
export const STORY_VIEWS_MIN_SEARCH = 15; export const STORY_VIEWS_MIN_SEARCH = 15;
export const STORY_MIN_REACTIONS_SORT = 10; export const STORY_MIN_REACTIONS_SORT = 10;

View File

@ -1724,7 +1724,9 @@ addActionHandler('loadMessageViews', async (global, actions, payload): Promise<v
if (!result) return; if (!result) return;
global = getGlobal(); global = getGlobal();
result.forEach((update) => { global = addUsers(global, buildCollectionByKey(result.users, 'id'));
global = addChats(global, buildCollectionByKey(result.chats, 'id'));
result.viewsInfo.forEach((update) => {
global = updateChatMessage(global, chatId, update.id, { global = updateChatMessage(global, chatId, update.id, {
views: update.views, views: update.views,
forwards: update.forwards, forwards: update.forwards,