From 389b3cfae0e6066ed2c171c6b1824eb1b88ece95 Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Sat, 2 Dec 2023 21:12:16 +0300 Subject: [PATCH] fix(client): updateShortSentMessage handling oops --- .../src/methods/chats/batched-queries.ts | 25 ++++++++++++++++++- packages/client/src/methods/chats/get-chat.ts | 22 +++------------- .../client/src/methods/messages/send-text.ts | 18 ++----------- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/packages/client/src/methods/chats/batched-queries.ts b/packages/client/src/methods/chats/batched-queries.ts index 90acdadf..c5ce5343 100644 --- a/packages/client/src/methods/chats/batched-queries.ts +++ b/packages/client/src/methods/chats/batched-queries.ts @@ -1,5 +1,12 @@ -import { MtArgumentError, tl } from '@mtcute/core' +import { BaseTelegramClient, MtArgumentError, tl } from '@mtcute/core' +import { + isInputPeerChannel, + isInputPeerChat, + isInputPeerUser, + toInputChannel, + toInputUser, +} from '../../utils/peer-utils.js' import { batchedQuery } from '../../utils/query-batcher.js' import { getAuthState } from '../auth/_state.js' @@ -120,3 +127,19 @@ export const _getChannelsBatched = batchedQuery { + if (isInputPeerUser(peer)) { + return _getUsersBatched(client, toInputUser(peer)) + } else if (isInputPeerChannel(peer)) { + return _getChannelsBatched(client, toInputChannel(peer)) + } else if (isInputPeerChat(peer)) { + return _getChatsBatched(client, peer.chatId) + } + + throw new MtArgumentError('Invalid peer') +} diff --git a/packages/client/src/methods/chats/get-chat.ts b/packages/client/src/methods/chats/get-chat.ts index e03e11cc..51cac4d8 100644 --- a/packages/client/src/methods/chats/get-chat.ts +++ b/packages/client/src/methods/chats/get-chat.ts @@ -1,16 +1,9 @@ -import { BaseTelegramClient, MtArgumentError, tl } from '@mtcute/core' +import { BaseTelegramClient, MtArgumentError } from '@mtcute/core' import { Chat, InputPeerLike, MtPeerNotFoundError } from '../../types/index.js' -import { - INVITE_LINK_REGEX, - isInputPeerChannel, - isInputPeerChat, - isInputPeerUser, - toInputChannel, - toInputUser, -} from '../../utils/peer-utils.js' +import { INVITE_LINK_REGEX } from '../../utils/peer-utils.js' import { resolvePeer } from '../users/resolve-peer.js' -import { _getChannelsBatched, _getChatsBatched, _getUsersBatched } from './batched-queries.js' +import { _getRawPeerBatched } from './batched-queries.js' // @available=both /** @@ -41,14 +34,7 @@ export async function getChat(client: BaseTelegramClient, chatId: InputPeerLike) const peer = await resolvePeer(client, chatId) - let res: tl.TypeChat | tl.TypeUser | null - if (isInputPeerChannel(peer)) { - res = await _getChannelsBatched(client, toInputChannel(peer)) - } else if (isInputPeerUser(peer)) { - res = await _getUsersBatched(client, toInputUser(peer)) - } else if (isInputPeerChat(peer)) { - res = await _getChatsBatched(client, peer.chatId) - } else throw new Error('should not happen') + const res = await _getRawPeerBatched(client, peer) if (!res) throw new MtPeerNotFoundError(`Chat ${JSON.stringify(chatId)} was not found`) diff --git a/packages/client/src/methods/messages/send-text.ts b/packages/client/src/methods/messages/send-text.ts index 4fb5afa7..dca9968f 100644 --- a/packages/client/src/methods/messages/send-text.ts +++ b/packages/client/src/methods/messages/send-text.ts @@ -9,6 +9,7 @@ import { normalizeDate } from '../../utils/misc-utils.js' import { inputPeerToPeer } from '../../utils/peer-utils.js' import { createDummyUpdate } from '../../utils/updates-utils.js' import { getAuthState } from '../auth/_state.js' +import { _getRawPeerBatched } from '../chats/batched-queries.js' import { _normalizeInputText } from '../misc/normalize-text.js' import { resolvePeer } from '../users/resolve-peer.js' import { _findMessageInUpdate } from './find-in-update.js' @@ -97,22 +98,7 @@ export async function sendText( let cached = await client.storage.getFullPeerById(id) if (!cached) { - switch (peer._) { - case 'inputPeerChat': - case 'peerChat': - // resolvePeer does not fetch the chat. - // we need to do it manually - cached = await client - .call({ - _: 'messages.getChats', - id: [peer.chatId], - }) - .then((res) => res.chats[0]) - break - default: - await resolvePeer(client, peer) - cached = await client.storage.getFullPeerById(id) - } + cached = await _getRawPeerBatched(client, await resolvePeer(client, peer)) } if (!cached) {