diff --git a/packages/client/src/methods/bots/get-bot-menu-button.ts b/packages/client/src/methods/bots/get-bot-menu-button.ts index 06ef04c7..11f1d615 100644 --- a/packages/client/src/methods/bots/get-bot-menu-button.ts +++ b/packages/client/src/methods/bots/get-bot-menu-button.ts @@ -13,13 +13,8 @@ export async function getBotMenuButton( this: TelegramClient, user: InputPeerLike ): Promise { - const userId = normalizeToInputUser(await this.resolvePeer(user)) - if (!userId) { - throw new MtInvalidPeerTypeError(user, 'user') - } - return await this.call({ _: 'bots.getBotMenuButton', - userId, + userId: normalizeToInputUser(await this.resolvePeer(user), user), }) } diff --git a/packages/client/src/methods/bots/get-game-high-scores.ts b/packages/client/src/methods/bots/get-game-high-scores.ts index 31ecd04c..b1d125de 100644 --- a/packages/client/src/methods/bots/get-game-high-scores.ts +++ b/packages/client/src/methods/bots/get-game-high-scores.ts @@ -27,10 +27,7 @@ export async function getGameHighScores( let user: tl.TypeInputUser if (userId) { - const res = normalizeToInputUser(await this.resolvePeer(userId)) - if (!res) throw new MtInvalidPeerTypeError(userId, 'user') - - user = res + user = normalizeToInputUser(await this.resolvePeer(userId), userId) } else { user = { _: 'inputUserEmpty' } } @@ -63,10 +60,7 @@ export async function getInlineGameHighScores( let user: tl.TypeInputUser if (userId) { - const res = normalizeToInputUser(await this.resolvePeer(userId)) - if (!res) throw new MtInvalidPeerTypeError(userId, 'user') - - user = res + user = normalizeToInputUser(await this.resolvePeer(userId), userId) } else { user = { _: 'inputUserEmpty' } } diff --git a/packages/client/src/methods/bots/normalize-command-scope.ts b/packages/client/src/methods/bots/normalize-command-scope.ts index 0575304d..bb3b7cb8 100644 --- a/packages/client/src/methods/bots/normalize-command-scope.ts +++ b/packages/client/src/methods/bots/normalize-command-scope.ts @@ -26,12 +26,11 @@ export async function _normalizeCommandScope( } } case 'member': { - const chat = await this.resolvePeer(scope.chat) const user = normalizeToInputUser( - await this.resolvePeer(scope.user) + await this.resolvePeer(scope.user), + scope.user ) - - if (!user) throw new MtInvalidPeerTypeError(scope.user, 'user') + const chat = await this.resolvePeer(scope.chat) return { _: 'botCommandScopePeerUser', diff --git a/packages/client/src/methods/bots/set-bot-menu-button.ts b/packages/client/src/methods/bots/set-bot-menu-button.ts index f49740d2..113a8c7a 100644 --- a/packages/client/src/methods/bots/set-bot-menu-button.ts +++ b/packages/client/src/methods/bots/set-bot-menu-button.ts @@ -14,14 +14,9 @@ export async function setBotMenuButton( user: InputPeerLike, button: tl.TypeBotMenuButton ): Promise { - const userId = normalizeToInputUser(await this.resolvePeer(user)) - if (!userId) { - throw new MtInvalidPeerTypeError(user, 'user') - } - await this.call({ _: 'bots.setBotMenuButton', - userId, + userId: normalizeToInputUser(await this.resolvePeer(user), user), button, }) } diff --git a/packages/client/src/methods/bots/set-game-score.ts b/packages/client/src/methods/bots/set-game-score.ts index 268b9e2a..d61559c2 100644 --- a/packages/client/src/methods/bots/set-game-score.ts +++ b/packages/client/src/methods/bots/set-game-score.ts @@ -37,9 +37,8 @@ export async function setGameScore( ): Promise { if (!params) params = {} + const user = normalizeToInputUser(await this.resolvePeer(userId), userId) const chat = await this.resolvePeer(chatId) - const user = normalizeToInputUser(await this.resolvePeer(userId)) - if (!user) throw new MtInvalidPeerTypeError(userId, 'user') const res = await this.call({ _: 'messages.setGameScore', @@ -85,8 +84,7 @@ export async function setInlineGameScore( ): Promise { if (!params) params = {} - const user = normalizeToInputUser(await this.resolvePeer(userId)) - if (!user) throw new MtInvalidPeerTypeError(userId, 'user') + const user = normalizeToInputUser(await this.resolvePeer(userId), userId) const [id, connection] = await this._normalizeInline(messageId) diff --git a/packages/client/src/methods/chats/add-chat-members.ts b/packages/client/src/methods/chats/add-chat-members.ts index 5be289fe..a3f7e02c 100644 --- a/packages/client/src/methods/chats/add-chat-members.ts +++ b/packages/client/src/methods/chats/add-chat-members.ts @@ -32,7 +32,6 @@ export async function addChatMembers( if (isInputPeerChat(chat)) { for (const user of users) { const p = normalizeToInputUser(await this.resolvePeer(user)) - if (!p) continue const updates = await this.call({ _: 'messages.addChatUser', diff --git a/packages/client/src/methods/chats/ban-chat-member.ts b/packages/client/src/methods/chats/ban-chat-member.ts index c702fda5..f8e6d051 100644 --- a/packages/client/src/methods/chats/ban-chat-member.ts +++ b/packages/client/src/methods/chats/ban-chat-member.ts @@ -44,13 +44,10 @@ export async function banChatMember( }, }) } else if (isInputPeerChat(chat)) { - const normUser = normalizeToInputUser(user) - if (!normUser) throw new MtInvalidPeerTypeError(userId, 'user') - res = await this.call({ _: 'messages.deleteChatUser', chatId: chat.chatId, - userId: normUser, + userId: normalizeToInputUser(user), }) } else throw new MtInvalidPeerTypeError(chatId, 'chat or channel') diff --git a/packages/client/src/methods/chats/delete-channel.ts b/packages/client/src/methods/chats/delete-channel.ts index 5ceef901..53c742fd 100644 --- a/packages/client/src/methods/chats/delete-channel.ts +++ b/packages/client/src/methods/chats/delete-channel.ts @@ -13,12 +13,9 @@ export async function deleteChannel( this: TelegramClient, chatId: InputPeerLike ): Promise { - const peer = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!peer) throw new MtInvalidPeerTypeError(chatId, 'channel') - const res = await this.call({ _: 'channels.deleteChannel', - channel: peer, + channel: normalizeToInputChannel(await this.resolvePeer(chatId), chatId), }) this._handleUpdate(res) } diff --git a/packages/client/src/methods/chats/delete-history.ts b/packages/client/src/methods/chats/delete-history.ts index b78545d2..f16c14e0 100644 --- a/packages/client/src/methods/chats/delete-history.ts +++ b/packages/client/src/methods/chats/delete-history.ts @@ -2,7 +2,7 @@ import { tl } from '@mtcute/tl' import { TelegramClient } from '../../client' import { InputPeerLike } from '../../types' -import { normalizeToInputChannel } from '../../utils/peer-utils' +import { isInputPeerChannel, normalizeToInputChannel } from "../../utils/peer-utils"; import { createDummyUpdate } from '../../utils/updates-utils' /** @@ -36,13 +36,12 @@ export async function deleteHistory( maxId, }) - const channel = normalizeToInputChannel(peer) - if (channel) { + if (isInputPeerChannel(peer)) { this._handleUpdate( createDummyUpdate( res.pts, res.ptsCount, - (channel as tl.RawInputChannel).channelId + peer.channelId ) ) } else { diff --git a/packages/client/src/methods/chats/delete-user-history.ts b/packages/client/src/methods/chats/delete-user-history.ts index 5bf4af5d..ce34dd09 100644 --- a/packages/client/src/methods/chats/delete-user-history.ts +++ b/packages/client/src/methods/chats/delete-user-history.ts @@ -2,10 +2,7 @@ import { tl } from '@mtcute/tl' import { TelegramClient } from '../../client' import { InputPeerLike, MtInvalidPeerTypeError } from '../../types' -import { - normalizeToInputChannel, - normalizeToInputPeer, -} from '../../utils/peer-utils' +import { normalizeToInputChannel } from '../../utils/peer-utils' import { createDummyUpdate } from '../../utils/updates-utils' /** @@ -20,10 +17,9 @@ export async function deleteUserHistory( chatId: InputPeerLike, participantId: InputPeerLike ): Promise { - const channel = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!channel) throw new MtInvalidPeerTypeError(chatId, 'channel') + const channel = normalizeToInputChannel(await this.resolvePeer(chatId), chatId) - const peer = normalizeToInputPeer(await this.resolvePeer(participantId)) + const peer = await this.resolvePeer(participantId) const res = await this.call({ _: 'channels.deleteParticipantHistory', diff --git a/packages/client/src/methods/chats/edit-admin-rights.ts b/packages/client/src/methods/chats/edit-admin-rights.ts index 505e294e..92d09fdf 100644 --- a/packages/client/src/methods/chats/edit-admin-rights.ts +++ b/packages/client/src/methods/chats/edit-admin-rights.ts @@ -23,11 +23,8 @@ export async function editAdminRights( rights: Omit, rank = '' ): Promise { - const chat = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!chat) throw new MtInvalidPeerTypeError(chatId, 'channel') - - const user = normalizeToInputUser(await this.resolvePeer(userId)) - if (!user) throw new MtInvalidPeerTypeError(userId, 'user') + const chat = normalizeToInputChannel(await this.resolvePeer(chatId), chatId) + const user = normalizeToInputUser(await this.resolvePeer(userId), userId) const res = await this.call({ _: 'channels.editAdmin', diff --git a/packages/client/src/methods/chats/get-chat-event-log.ts b/packages/client/src/methods/chats/get-chat-event-log.ts index 08722bb2..453b33ce 100644 --- a/packages/client/src/methods/chats/get-chat-event-log.ts +++ b/packages/client/src/methods/chats/get-chat-event-log.ts @@ -86,8 +86,7 @@ export async function* getChatEventLog( ): AsyncIterableIterator { if (!params) params = {} - const channel = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!channel) throw new MtInvalidPeerTypeError(chatId, 'channel') + const channel = normalizeToInputChannel(await this.resolvePeer(chatId), chatId) let current = 0 let maxId = params.maxId ?? Long.ZERO diff --git a/packages/client/src/methods/chats/join-chat.ts b/packages/client/src/methods/chats/join-chat.ts index 6a67aee7..a3b822b6 100644 --- a/packages/client/src/methods/chats/join-chat.ts +++ b/packages/client/src/methods/chats/join-chat.ts @@ -37,12 +37,9 @@ export async function joinChat( } } - const peer = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!peer) throw new MtNotFoundError() - const res = await this.call({ _: 'channels.joinChannel', - channel: peer, + channel: normalizeToInputChannel(await this.resolvePeer(chatId), chatId), }) assertIsUpdatesGroup('channels.joinChannel', res) diff --git a/packages/client/src/methods/chats/set-chat-username.ts b/packages/client/src/methods/chats/set-chat-username.ts index ee2d8786..0c17ad87 100644 --- a/packages/client/src/methods/chats/set-chat-username.ts +++ b/packages/client/src/methods/chats/set-chat-username.ts @@ -16,12 +16,9 @@ export async function setChatUsername( chatId: InputPeerLike, username: string | null ): Promise { - const chat = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!chat) throw new MtInvalidPeerTypeError(chatId, 'channel') - await this.call({ _: 'channels.updateUsername', - channel: chat, + channel: normalizeToInputChannel(await this.resolvePeer(chatId), chatId), username: username || '', }) } diff --git a/packages/client/src/methods/chats/set-slow-mode.ts b/packages/client/src/methods/chats/set-slow-mode.ts index 8fcc2858..efc224ca 100644 --- a/packages/client/src/methods/chats/set-slow-mode.ts +++ b/packages/client/src/methods/chats/set-slow-mode.ts @@ -17,12 +17,9 @@ export async function setSlowMode( chatId: InputPeerLike, seconds = 0 ): Promise { - const chat = normalizeToInputChannel(await this.resolvePeer(chatId)) - if (!chat) throw new MtInvalidPeerTypeError(chatId, 'channel') - const res = await this.call({ _: 'channels.toggleSlowMode', - channel: chat, + channel: normalizeToInputChannel(await this.resolvePeer(chatId), chatId), seconds, }) this._handleUpdate(res) diff --git a/packages/client/src/methods/contacts/add-contact.ts b/packages/client/src/methods/contacts/add-contact.ts index aea7bf38..6869e4b3 100644 --- a/packages/client/src/methods/contacts/add-contact.ts +++ b/packages/client/src/methods/contacts/add-contact.ts @@ -36,8 +36,7 @@ export async function addContact( sharePhone?: boolean } ): Promise { - const peer = normalizeToInputUser(await this.resolvePeer(userId)) - if (!peer) throw new MtInvalidPeerTypeError(userId, 'user') + const peer = normalizeToInputUser(await this.resolvePeer(userId), userId) const res = await this.call({ _: 'contacts.addContact', diff --git a/packages/client/src/methods/invite-links/get-invite-links.ts b/packages/client/src/methods/invite-links/get-invite-links.ts index 0c88a926..901836ab 100644 --- a/packages/client/src/methods/invite-links/get-invite-links.ts +++ b/packages/client/src/methods/invite-links/get-invite-links.ts @@ -53,9 +53,7 @@ export async function* getInviteLinks( const chunkSize = Math.min(params.chunkSize ?? 100, total) const peer = await this.resolvePeer(chatId) - const admin = normalizeToInputUser(await this.resolvePeer(adminId)) - - if (!admin) throw new MtInvalidPeerTypeError(adminId, 'user') + const admin = normalizeToInputUser(await this.resolvePeer(adminId), adminId) let offsetDate: number | undefined = undefined let offsetLink: string | undefined = undefined diff --git a/packages/client/src/methods/invite-links/hide-all-join-requests.ts b/packages/client/src/methods/invite-links/hide-all-join-requests.ts index e1594781..e415d8ed 100644 --- a/packages/client/src/methods/invite-links/hide-all-join-requests.ts +++ b/packages/client/src/methods/invite-links/hide-all-join-requests.ts @@ -18,10 +18,7 @@ export async function hideAllJoinRequests( action: 'approve' | 'deny', link?: string ): Promise { - const userId = normalizeToInputUser(await this.resolvePeer(user)) - if (!userId) { - throw new MtInvalidPeerTypeError(user, 'user') - } + const userId = normalizeToInputUser(await this.resolvePeer(user), user) await this.call({ _: 'messages.hideAllChatJoinRequests', diff --git a/packages/client/src/methods/invite-links/hide-join-request.ts b/packages/client/src/methods/invite-links/hide-join-request.ts index fc71c497..4046ed67 100644 --- a/packages/client/src/methods/invite-links/hide-join-request.ts +++ b/packages/client/src/methods/invite-links/hide-join-request.ts @@ -16,10 +16,7 @@ export async function hideJoinRequest( user: InputPeerLike, action: 'approve' | 'deny' ): Promise { - const userId = normalizeToInputUser(await this.resolvePeer(user)) - if (!userId) { - throw new MtInvalidPeerTypeError(user, 'user') - } + const userId = normalizeToInputUser(await this.resolvePeer(user), user) await this.call({ _: 'messages.hideChatJoinRequest', diff --git a/packages/client/src/methods/messages/get-messages.ts b/packages/client/src/methods/messages/get-messages.ts index 603d3d7f..57b2e853 100644 --- a/packages/client/src/methods/messages/get-messages.ts +++ b/packages/client/src/methods/messages/get-messages.ts @@ -74,7 +74,7 @@ export async function getMessages( ? { _: 'channels.getMessages', id: ids, - channel: normalizeToInputChannel(peer)!, + channel: normalizeToInputChannel(peer), } : { _: 'messages.getMessages', diff --git a/packages/client/src/methods/messages/parse-entities.ts b/packages/client/src/methods/messages/parse-entities.ts index 6a189ad8..9a15ea09 100644 --- a/packages/client/src/methods/messages/parse-entities.ts +++ b/packages/client/src/methods/messages/parse-entities.ts @@ -41,7 +41,8 @@ export async function _parseEntities( if (ent._ === 'messageEntityMentionName') { try { const inputPeer = normalizeToInputUser( - await this.resolvePeer(ent.userId) + await this.resolvePeer(ent.userId), + ent.userId ) // not a user diff --git a/packages/client/src/methods/stickers/create-sticker-set.ts b/packages/client/src/methods/stickers/create-sticker-set.ts index 95ef59c0..3af844e2 100644 --- a/packages/client/src/methods/stickers/create-sticker-set.ts +++ b/packages/client/src/methods/stickers/create-sticker-set.ts @@ -106,8 +106,7 @@ export async function createStickerSet( ) } - const owner = normalizeToInputUser(await this.resolvePeer(params.owner)) - if (!owner) throw new MtInvalidPeerTypeError(params.owner, 'user') + const owner = normalizeToInputUser(await this.resolvePeer(params.owner), params.owner) const inputStickers: tl.TypeInputStickerSetItem[] = [] diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index 5db04384..f909d3cd 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -1800,7 +1800,7 @@ async function _fetchChannelDifference( try { channel = normalizeToInputChannel( await this.resolvePeer(toggleChannelIdMark(channelId)) - )! + ) } catch (e) { this._updsLog.warn( 'fetchChannelDifference failed for channel %d: input peer not found', diff --git a/packages/client/src/methods/users/get-common-chats.ts b/packages/client/src/methods/users/get-common-chats.ts index 74953972..8861cc1a 100644 --- a/packages/client/src/methods/users/get-common-chats.ts +++ b/packages/client/src/methods/users/get-common-chats.ts @@ -14,12 +14,9 @@ export async function getCommonChats( this: TelegramClient, userId: InputPeerLike ): Promise { - const peer = normalizeToInputUser(await this.resolvePeer(userId)) - if (!peer) throw new MtInvalidPeerTypeError(userId, 'user') - return this.call({ _: 'messages.getCommonChats', - userId: peer, + userId: normalizeToInputUser(await this.resolvePeer(userId), userId), maxId: 0, limit: 100, }).then((res) => res.chats.map((it) => new Chat(this, it))) diff --git a/packages/client/src/methods/users/get-profile-photos.ts b/packages/client/src/methods/users/get-profile-photos.ts index 56950944..79cc9fba 100644 --- a/packages/client/src/methods/users/get-profile-photos.ts +++ b/packages/client/src/methods/users/get-profile-photos.ts @@ -33,12 +33,9 @@ export async function getProfilePhotos( ): Promise { if (!params) params = {} - const peer = normalizeToInputUser(await this.resolvePeer(userId)) - if (!peer) throw new MtInvalidPeerTypeError(userId, 'user') - const res = await this.call({ _: 'photos.getUserPhotos', - userId: peer, + userId: normalizeToInputUser(await this.resolvePeer(userId), userId), offset: params.offset ?? 0, limit: params.limit ?? 100, maxId: Long.ZERO, diff --git a/packages/client/src/methods/users/iter-profile-photos.ts b/packages/client/src/methods/users/iter-profile-photos.ts index cca034a8..f5e10797 100644 --- a/packages/client/src/methods/users/iter-profile-photos.ts +++ b/packages/client/src/methods/users/iter-profile-photos.ts @@ -46,8 +46,7 @@ export async function* iterProfilePhotos( ): AsyncIterableIterator { if (!params) params = {} - const peer = normalizeToInputUser(await this.resolvePeer(userId)) - if (!peer) throw new MtInvalidPeerTypeError(userId, 'user') + const peer = normalizeToInputUser(await this.resolvePeer(userId), userId) let offset = params.offset || 0 let current = 0 diff --git a/packages/client/src/types/bots/keyboards.ts b/packages/client/src/types/bots/keyboards.ts index b5159397..c5424518 100644 --- a/packages/client/src/types/bots/keyboards.ts +++ b/packages/client/src/types/bots/keyboards.ts @@ -362,15 +362,10 @@ export namespace BotKeyboard { text: string, user: tl.TypeInputPeer ): tl.RawInputKeyboardButtonUserProfile { - const userId = normalizeToInputUser(user) - if (!userId) { - throw new MtInvalidPeerTypeError(user, 'user') - } - return { _: 'inputKeyboardButtonUserProfile', text, - userId, + userId: normalizeToInputUser(user), } } diff --git a/packages/client/src/utils/peer-utils.ts b/packages/client/src/utils/peer-utils.ts index abbe0996..47f298bd 100644 --- a/packages/client/src/utils/peer-utils.ts +++ b/packages/client/src/utils/peer-utils.ts @@ -1,6 +1,7 @@ import Long from 'long' import { tl } from '@mtcute/tl' import { assertNever } from '@mtcute/core' +import { InputPeerLike, MtInvalidPeerTypeError } from '../types' export const INVITE_LINK_REGEX = /^(?:https?:\/\/)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)\/(?:joinchat\/|\+))([\w-]+)$/i @@ -50,18 +51,9 @@ export function normalizeToInputPeer( } export function normalizeToInputUser( - res: - | tl.TypeInputUser - | tl.RawInputPeerUser - | tl.RawInputPeerUserFromMessage - | tl.RawInputPeerSelf -): tl.TypeInputUser -export function normalizeToInputUser( - res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel -): tl.TypeInputUser | null -export function normalizeToInputUser( - res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel -): tl.TypeInputUser | null { + res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel, + input?: InputPeerLike +): tl.TypeInputUser { if (tl.isAnyInputUser(res)) return res switch (res._) { @@ -82,21 +74,13 @@ export function normalizeToInputUser( } } - return null + throw new MtInvalidPeerTypeError(input ?? res, 'user') } export function normalizeToInputChannel( - res: - | tl.TypeInputChannel - | tl.RawInputPeerChannel - | tl.RawInputPeerChannelFromMessage -): tl.TypeInputChannel -export function normalizeToInputChannel( - res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel -): tl.TypeInputChannel | null -export function normalizeToInputChannel( - res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel -): tl.TypeInputChannel | null { + res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel, + input?: InputPeerLike +): tl.TypeInputChannel { if (tl.isAnyInputChannel(res)) return res switch (res._) { @@ -115,7 +99,7 @@ export function normalizeToInputChannel( } } - return null + throw new MtInvalidPeerTypeError(input ?? res, 'user') } export function isInputPeerUser(