diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index ca9a9bad0..73fc4f510 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -16,7 +16,7 @@ import type { } from '../../types'; import { - DEBUG, ARCHIVED_FOLDER_ID, MEMBERS_LOAD_SLICE, SERVICE_NOTIFICATIONS_USER_ID, ALL_FOLDER_ID, + DEBUG, ARCHIVED_FOLDER_ID, MEMBERS_LOAD_SLICE, SERVICE_NOTIFICATIONS_USER_ID, ALL_FOLDER_ID, MAX_INT_32, } from '../../../config'; import { invokeRequest, uploadFile } from './client'; import { @@ -57,7 +57,6 @@ type FullChatData = { membersCount?: number; }; -const MAX_INT_32 = 2 ** 31 - 1; let onUpdate: OnApiUpdate; export function init(_onUpdate: OnApiUpdate) { diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index 0089b1e05..70f199998 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -24,7 +24,7 @@ import { import { ALL_FOLDER_ID, - DEBUG, MENTION_UNREAD_SLICE, + DEBUG, MAX_INT_32, MENTION_UNREAD_SLICE, PINNED_MESSAGES_LIMIT, REACTION_UNREAD_SLICE, SUPPORTED_IMAGE_CONTENT_TYPES, SUPPORTED_VIDEO_CONTENT_TYPES, @@ -97,7 +97,7 @@ export async function fetchMessages({ }), ...(offsetId && { // Workaround for local message IDs overflowing some internal `Buffer` range check - offsetId: Math.min(offsetId, 2 ** (32 - 1) - 1), + offsetId: Math.min(offsetId, MAX_INT_32), }), ...pagination, }), undefined, true); @@ -763,27 +763,29 @@ export async function sendMessageAction({ } export async function markMessageListRead({ - chat, threadId, maxId, serverTimeOffset, + chat, threadId, maxId = -1, serverTimeOffset, }: { chat: ApiChat; threadId: number; maxId?: number; serverTimeOffset: number; }) { const isChannel = getEntityTypeById(chat.id) === 'channel'; + // Workaround for local message IDs overflowing some internal `Buffer` range check + const fixedMaxId = Math.min(maxId, MAX_INT_32); if (isChannel && threadId === MAIN_THREAD_ID) { await invokeRequest(new GramJs.channels.ReadHistory({ channel: buildInputEntity(chat.id, chat.accessHash) as GramJs.InputChannel, - maxId, + maxId: fixedMaxId, })); } else if (isChannel) { await invokeRequest(new GramJs.messages.ReadDiscussion({ peer: buildInputPeer(chat.id, chat.accessHash), msgId: threadId, - readMaxId: maxId, + readMaxId: fixedMaxId, })); } else { await invokeRequest(new GramJs.messages.ReadHistory({ peer: buildInputPeer(chat.id, chat.accessHash), - maxId, + maxId: fixedMaxId, })); } diff --git a/src/api/gramjs/methods/settings.ts b/src/api/gramjs/methods/settings.ts index 7dbe5c273..69689ab86 100644 --- a/src/api/gramjs/methods/settings.ts +++ b/src/api/gramjs/methods/settings.ts @@ -10,9 +10,9 @@ import type { ApiNotifyException, ApiPhoto, } from '../../types'; import type { ApiPrivacyKey, InputPrivacyRules, LangCode } from '../../../types'; - import type { LANG_PACKS } from '../../../config'; -import { BLOCKED_LIST_LIMIT, DEFAULT_LANG_PACK } from '../../../config'; + +import { BLOCKED_LIST_LIMIT, DEFAULT_LANG_PACK, MAX_INT_32 } from '../../../config'; import { ACCEPTABLE_USERNAME_ERRORS } from './management'; import { buildApiConfig, @@ -39,7 +39,6 @@ import { getServerTime } from '../../../util/serverTime'; import { addEntitiesWithPhotosToLocalDb, addPhotoToLocalDb } from '../helpers'; import localDb from '../localDb'; -const MAX_INT_32 = 2 ** 31 - 1; const BETA_LANG_CODES = ['ar', 'fa', 'id', 'ko', 'uz', 'en']; export function updateProfile({ diff --git a/src/components/common/CalendarModal.tsx b/src/components/common/CalendarModal.tsx index 2b73a7d1e..fd158af71 100644 --- a/src/components/common/CalendarModal.tsx +++ b/src/components/common/CalendarModal.tsx @@ -3,6 +3,7 @@ import React, { memo, useState, useEffect, useMemo, useCallback, } from '../../lib/teact/teact'; +import { MAX_INT_32 } from '../../config'; import buildClassName from '../../util/buildClassName'; import { formatTime, formatDateToString, getDayStart } from '../../util/dateFormat'; import type { LangFn } from '../../hooks/useLang'; @@ -15,7 +16,7 @@ import Button from '../ui/Button'; import './CalendarModal.scss'; -const MAX_SAFE_DATE = 2147483647 * 1000; // API has int for dates +const MAX_SAFE_DATE = MAX_INT_32 * 1000; const MIN_SAFE_DATE = 0; export type OwnProps = { diff --git a/src/config.ts b/src/config.ts index 6db148140..4f0b3d89f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -120,6 +120,7 @@ export const MOBILE_SCREEN_LANDSCAPE_MAX_WIDTH = 950; // px export const MOBILE_SCREEN_LANDSCAPE_MAX_HEIGHT = 450; // px export const LOCAL_MESSAGE_MIN_ID = 1e11; // `Date.now()` is always used as base +export const MAX_INT_32 = 2 ** 31 - 1; export const TMP_CHAT_ID = '0'; export const ANIMATION_END_DELAY = 100;