From 7221a19cf1e4279d68a751cbb38157031d0cb6bd Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 19 Apr 2024 13:38:57 +0400 Subject: [PATCH] Fix internal channel id prefixing (#4484) --- src/api/gramjs/apiBuilders/peers.ts | 7 ++++++- src/api/gramjs/gramjsBuilders/index.ts | 14 ++------------ src/config.ts | 4 ++-- src/global/helpers/chats.ts | 9 ++++++--- src/global/selectors/messages.ts | 3 ++- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/api/gramjs/apiBuilders/peers.ts b/src/api/gramjs/apiBuilders/peers.ts index 13e18a8a9..a0b25404b 100644 --- a/src/api/gramjs/apiBuilders/peers.ts +++ b/src/api/gramjs/apiBuilders/peers.ts @@ -3,6 +3,8 @@ import { Api as GramJs } from '../../../lib/gramjs'; import type { ApiEmojiStatus, ApiPeerColor } from '../../types'; +import { CHANNEL_ID_LENGTH } from '../../../config'; + export function isPeerUser(peer: GramJs.TypePeer | GramJs.TypeInputPeer): peer is GramJs.PeerUser { return peer.hasOwnProperty('userId'); } @@ -21,7 +23,10 @@ export function buildApiPeerId(id: BigInt.BigInteger, type: 'user' | 'chat' | 'c } if (type === 'channel') { - return `-100${id}`; + // Simulates TDLib https://github.com/tdlib/td/blob/d7203eb719304866a7eb7033ef03d421459335b8/td/telegram/DialogId.cpp#L54 + // But using only string operations. Should be fine until channel ids reach 10^12 + // Example: 12345678 -> -1000012345678 + return `-1${id.toString().padStart(CHANNEL_ID_LENGTH - 2, '0')}`; } return `-${id}`; diff --git a/src/api/gramjs/gramjsBuilders/index.ts b/src/api/gramjs/gramjsBuilders/index.ts index 1f2b85c98..e3710bb6a 100644 --- a/src/api/gramjs/gramjsBuilders/index.ts +++ b/src/api/gramjs/gramjsBuilders/index.ts @@ -37,13 +37,8 @@ import { pick } from '../../../util/iteratees'; import { deserializeBytes } from '../helpers'; import localDb from '../localDb'; -const LEGACY_CHANNEL_ID_MIN_LENGTH = 11; // Example: -1234567890 - function checkIfChannelId(id: string) { - if (id.length >= CHANNEL_ID_LENGTH) return id.startsWith('-100'); - // LEGACY Unprefixed channel id - if (id.length === LEGACY_CHANNEL_ID_MIN_LENGTH && id.startsWith('-4')) return false; - return id.length >= LEGACY_CHANNEL_ID_MIN_LENGTH; + return id.length === CHANNEL_ID_LENGTH && id.startsWith('-1'); } export function getEntityTypeById(chatOrUserId: string) { @@ -545,12 +540,7 @@ export function buildMtpPeerId(id: string, type: 'user' | 'chat' | 'channel') { } if (type === 'channel') { - if (id.length === CHANNEL_ID_LENGTH) { - return BigInt(id.slice(4)); - } - - // LEGACY Unprefixed channel id - return BigInt(id.slice(1)); + return BigInt(id.slice(2)); // Slice "-1", zeroes are trimmed when converting to BigInt } return BigInt(id.slice(1)); diff --git a/src/config.ts b/src/config.ts index 2ff1a7b58..121c76e03 100644 --- a/src/config.ts +++ b/src/config.ts @@ -294,12 +294,12 @@ export const SERVICE_NOTIFICATIONS_USER_ID = '777000'; export const REPLIES_USER_ID = '1271266957'; // TODO For Test connection ID must be equal to 708513 export const ANONYMOUS_USER_ID = '2666000'; export const RESTRICTED_EMOJI_SET_ID = '7173162320003080'; -export const CHANNEL_ID_LENGTH = 14; // 14 symbols, including -100 prefix +export const CHANNEL_ID_LENGTH = 14; // 14 symbols, based on TDLib's `ZERO_CHANNEL_ID = -1000000000000` export const DEFAULT_GIF_SEARCH_BOT_USERNAME = 'gif'; export const ALL_FOLDER_ID = 0; export const ARCHIVED_FOLDER_ID = 1; export const SAVED_FOLDER_ID = -1; -export const DELETED_COMMENTS_CHANNEL_ID = '-100777'; +export const DELETED_COMMENTS_CHANNEL_ID = '-1000000000777'; export const MAX_MEDIA_FILES_FOR_ALBUM = 10; export const MAX_ACTIVE_PINNED_CHATS = 5; export const SCHEDULED_WHEN_ONLINE = 0x7FFFFFFE; diff --git a/src/global/helpers/chats.ts b/src/global/helpers/chats.ts index fb0a4c29f..953ac5f32 100644 --- a/src/global/helpers/chats.ts +++ b/src/global/helpers/chats.ts @@ -28,11 +28,11 @@ export function isUserId(entityId: string) { } export function isChannelId(entityId: string) { - return entityId.length === CHANNEL_ID_LENGTH && entityId.startsWith('-100'); + return entityId.length === CHANNEL_ID_LENGTH && entityId.startsWith('-1'); } export function toChannelId(mtpId: string) { - return `-100${mtpId}`; + return `-1${mtpId.padStart(CHANNEL_ID_LENGTH - 2, '0')}`; } export function isApiPeerChat(peer: ApiPeer): peer is ApiChat { @@ -435,7 +435,10 @@ export function getOrderedTopics( } export function getCleanPeerId(peerId: string) { - return isChannelId(peerId) ? peerId.replace('-100', '') : peerId.replace('-', ''); + return isChannelId(peerId) + // Remove -1 and leading zeros + ? peerId.replace(/^-10+/, '') + : peerId.replace('-', ''); } export function getPeerIdDividend(peerId: string) { diff --git a/src/global/selectors/messages.ts b/src/global/selectors/messages.ts index df7382d50..2050640e5 100644 --- a/src/global/selectors/messages.ts +++ b/src/global/selectors/messages.ts @@ -29,6 +29,7 @@ import { canSendReaction, getAllowedAttachmentOptions, getCanPostInChat, + getCleanPeerId, getHasAdminRight, getIsSavedDialog, getMainUsername, @@ -1428,7 +1429,7 @@ export function selectTopicLink( const chatUsername = getMainUsername(chat); - const normalizedId = chatId.replace('-100', ''); + const normalizedId = getCleanPeerId(chatId); const chatPart = chatUsername || `c/${normalizedId}`; const topicPart = topicId && topicId !== MAIN_THREAD_ID ? `/${topicId}` : '';