Fix internal channel id prefixing (#4484)

This commit is contained in:
Alexander Zinchuk 2024-04-19 13:38:57 +04:00
parent 917dc1171b
commit 7221a19cf1
5 changed files with 18 additions and 19 deletions

View File

@ -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}`;

View File

@ -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));

View File

@ -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;

View File

@ -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) {

View File

@ -29,6 +29,7 @@ import {
canSendReaction,
getAllowedAttachmentOptions,
getCanPostInChat,
getCleanPeerId,
getHasAdminRight,
getIsSavedDialog,
getMainUsername,
@ -1428,7 +1429,7 @@ export function selectTopicLink<T extends GlobalState>(
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}` : '';