From f19fdf76b8bbae06ac2151f29596f49ee5d80368 Mon Sep 17 00:00:00 2001 From: teidesu Date: Tue, 11 May 2021 23:21:35 +0300 Subject: [PATCH] refactor(client): improve resolvePeer normalization --- .../src/methods/chats/add-chat-members.ts | 13 +- .../src/methods/chats/delete-chat-photo.ts | 24 +-- .../src/methods/chats/get-chat-member.ts | 16 +- .../src/methods/chats/get-chat-members.ts | 12 +- packages/client/src/methods/chats/get-chat.ts | 24 ++- .../client/src/methods/chats/get-full-chat.ts | 15 +- .../client/src/methods/chats/leave-chat.ts | 14 +- .../src/methods/chats/set-chat-description.ts | 9 +- .../src/methods/chats/set-chat-photo.ts | 14 +- .../src/methods/chats/set-chat-title.ts | 18 +- .../src/methods/messages/delete-messages.ts | 9 +- .../src/methods/messages/get-messages.ts | 11 +- packages/client/src/utils/peer-utils.ts | 183 +++++++++++++----- 13 files changed, 218 insertions(+), 144 deletions(-) diff --git a/packages/client/src/methods/chats/add-chat-members.ts b/packages/client/src/methods/chats/add-chat-members.ts index 1c35ba07..a83ac039 100644 --- a/packages/client/src/methods/chats/add-chat-members.ts +++ b/packages/client/src/methods/chats/add-chat-members.ts @@ -2,6 +2,8 @@ import { TelegramClient } from '../../client' import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' import { MaybeArray } from '@mtcute/core' import { + isInputPeerChannel, + isInputPeerChat, normalizeToInputChannel, normalizeToInputPeer, normalizeToInputUser, @@ -24,28 +26,27 @@ export async function addChatMembers( users: MaybeArray, forwardCount = 100 ): Promise { - const chat = await this.resolvePeer(chatId) - const input = normalizeToInputPeer(chat) + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) if (!Array.isArray(users)) users = [users] - if (input._ === 'inputPeerChat') { + 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', - chatId: input.chatId, + chatId: chat.chatId, userId: p, fwdLimit: forwardCount, }) this._handleUpdate(updates) } - } else if (input._ === 'inputPeerChannel') { + } else if (isInputPeerChannel(chat)) { const updates = await this.call({ _: 'channels.inviteToChannel', - channel: normalizeToInputChannel(chat)!, + channel: normalizeToInputChannel(chat), users: await this.resolvePeerMany( users as InputPeerLike[], normalizeToInputUser diff --git a/packages/client/src/methods/chats/delete-chat-photo.ts b/packages/client/src/methods/chats/delete-chat-photo.ts index 94b1a22e..9aeffef0 100644 --- a/packages/client/src/methods/chats/delete-chat-photo.ts +++ b/packages/client/src/methods/chats/delete-chat-photo.ts @@ -1,9 +1,11 @@ import { TelegramClient } from '../../client' +import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' import { - InputPeerLike, - MtCuteInvalidPeerTypeError, -} from '../../types' -import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' + isInputPeerChannel, + isInputPeerChat, + normalizeToInputChannel, + normalizeToInputPeer, +} from '../../utils/peer-utils' /** * Delete a chat photo @@ -18,23 +20,21 @@ export async function deleteChatPhoto( chatId: InputPeerLike ): Promise { const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) - if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) - throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') let res - if (chat._ === 'inputPeerChat') { + if (isInputPeerChat(chat)) { res = await this.call({ _: 'messages.editChatPhoto', chatId: chat.chatId, - photo: { _: 'inputChatPhotoEmpty' } + photo: { _: 'inputChatPhotoEmpty' }, }) - } else { + } else if (isInputPeerChannel(chat)) { res = await this.call({ _: 'channels.editPhoto', - channel: normalizeToInputChannel(chat)!, - photo: { _: 'inputChatPhotoEmpty' } + channel: normalizeToInputChannel(chat), + photo: { _: 'inputChatPhotoEmpty' }, }) - } + } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') this._handleUpdate(res) } diff --git a/packages/client/src/methods/chats/get-chat-member.ts b/packages/client/src/methods/chats/get-chat-member.ts index 3e9ad8ac..a3f02503 100644 --- a/packages/client/src/methods/chats/get-chat-member.ts +++ b/packages/client/src/methods/chats/get-chat-member.ts @@ -4,7 +4,7 @@ import { MtCuteInvalidPeerTypeError, } from '../../types' import { - createUsersChatsIndex, + createUsersChatsIndex, isInputPeerChannel, isInputPeerChat, isInputPeerUser, normalizeToInputChannel, normalizeToInputPeer, } from '../../utils/peer-utils' @@ -27,13 +27,15 @@ export async function getChatMember( userId: InputPeerLike ): Promise { const user = normalizeToInputPeer(await this.resolvePeer(userId)) - const chat = await this.resolvePeer(chatId) - const chatInput = normalizeToInputPeer(chat) + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) + + if (isInputPeerChat(chat)) { + if (!isInputPeerUser(user)) + throw new MtCuteInvalidPeerTypeError(userId, 'user') - if (chatInput._ === 'inputPeerChat') { const res = await this.call({ _: 'messages.getFullChat', - chatId: chatInput.chatId, + chatId: chat.chatId, }) assertTypeIs( @@ -60,10 +62,10 @@ export async function getChatMember( } throw new UserNotParticipantError() - } else if (chatInput._ === 'inputPeerChannel') { + } else if (isInputPeerChannel(chat)) { const res = await this.call({ _: 'channels.getParticipant', - channel: normalizeToInputChannel(chat)!, + channel: normalizeToInputChannel(chat), participant: user, }) diff --git a/packages/client/src/methods/chats/get-chat-members.ts b/packages/client/src/methods/chats/get-chat-members.ts index 175fc620..31301910 100644 --- a/packages/client/src/methods/chats/get-chat-members.ts +++ b/packages/client/src/methods/chats/get-chat-members.ts @@ -5,7 +5,7 @@ import { } from '../../types' import { TelegramClient } from '../../client' import { - createUsersChatsIndex, + createUsersChatsIndex, isInputPeerChannel, isInputPeerChat, normalizeToInputChannel, normalizeToInputPeer, } from '../../utils/peer-utils' @@ -71,10 +71,8 @@ export async function getChatMembers( if (!params) params = {} const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) - if (chat._ === 'inputPeerUser') - throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') - if (chat._ === 'inputPeerChat') { + if (isInputPeerChat(chat)) { const res = await this.call({ _: 'messages.getFullChat', chatId: chat.chatId, @@ -99,7 +97,7 @@ export async function getChatMembers( return members.map((m) => new ChatMember(this, m, users)) } - if (chat._ === 'inputPeerChannel') { + if (isInputPeerChannel(chat)) { const q = params.query?.toLowerCase() ?? '' const type = params.type ?? 'recent' @@ -126,7 +124,7 @@ export async function getChatMembers( const res = await this.call({ _: 'channels.getParticipants', - channel: normalizeToInputChannel(chat)!, + channel: normalizeToInputChannel(chat), filter, offset: params.offset ?? 0, limit: params.limit ?? 200, @@ -143,5 +141,5 @@ export async function getChatMembers( return res.participants.map(i => new ChatMember(this, i, users)) } - throw new Error('should not happen') + throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') } diff --git a/packages/client/src/methods/chats/get-chat.ts b/packages/client/src/methods/chats/get-chat.ts index 773957eb..1b2d9708 100644 --- a/packages/client/src/methods/chats/get-chat.ts +++ b/packages/client/src/methods/chats/get-chat.ts @@ -2,6 +2,9 @@ import { Chat, InputPeerLike, MtCuteArgumentError } from '../../types' import { TelegramClient } from '../../client' import { INVITE_LINK_REGEX, + isInputPeerChannel, + isInputPeerChat, + isInputPeerUser, normalizeToInputChannel, normalizeToInputPeer, normalizeToInputUser, @@ -26,37 +29,38 @@ export async function getChat( if (m) { const res = await this.call({ _: 'messages.checkChatInvite', - hash: m[1] + hash: m[1], }) if (res._ === 'chatInvite') { - throw new MtCuteArgumentError(`You haven't joined ${JSON.stringify(res.title)}`) + throw new MtCuteArgumentError( + `You haven't joined ${JSON.stringify(res.title)}` + ) } return new Chat(this, res.chat) } } - const peer = await this.resolvePeer(chatId) - const input = normalizeToInputPeer(peer) + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) let res: tl.TypeChat | tl.TypeUser - if (input._ === 'inputPeerChannel') { + if (isInputPeerChannel(peer)) { const r = await this.call({ _: 'channels.getChannels', - id: [normalizeToInputChannel(peer)!] + id: [normalizeToInputChannel(peer)], }) res = r.chats[0] - } else if (input._ === 'inputPeerUser' || input._ === 'inputPeerSelf') { + } else if (isInputPeerUser(peer)) { const r = await this.call({ _: 'users.getUsers', - id: [normalizeToInputUser(peer)!] + id: [normalizeToInputUser(peer)], }) res = r[0] - } else if (input._ === 'inputPeerChat') { + } else if (isInputPeerChat(peer)) { const r = await this.call({ _: 'messages.getChats', - id: [input.chatId] + id: [peer.chatId], }) res = r.chats[0] } else throw new Error('should not happen') diff --git a/packages/client/src/methods/chats/get-full-chat.ts b/packages/client/src/methods/chats/get-full-chat.ts index db85ee66..d3a09980 100644 --- a/packages/client/src/methods/chats/get-full-chat.ts +++ b/packages/client/src/methods/chats/get-full-chat.ts @@ -1,7 +1,7 @@ import { Chat, InputPeerLike, MtCuteArgumentError } from '../../types' import { TelegramClient } from '../../client' import { - INVITE_LINK_REGEX, + INVITE_LINK_REGEX, isInputPeerChannel, isInputPeerChat, isInputPeerUser, normalizeToInputChannel, normalizeToInputPeer, normalizeToInputUser, @@ -38,24 +38,23 @@ export async function getFullChat( } } - const peer = await this.resolvePeer(chatId) - const input = normalizeToInputPeer(peer) + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) let res: tl.messages.TypeChatFull | tl.TypeUserFull - if (input._ === 'inputPeerChannel') { + if (isInputPeerChannel(peer)) { res = await this.call({ _: 'channels.getFullChannel', - channel: normalizeToInputChannel(peer)! + channel: normalizeToInputChannel(peer) }) - } else if (input._ === 'inputPeerUser' || input._ === 'inputPeerSelf') { + } else if (isInputPeerUser(peer)) { res = await this.call({ _: 'users.getFullUser', id: normalizeToInputUser(peer)! }) - } else if (input._ === 'inputPeerChat') { + } else if (isInputPeerChat(peer)) { res = await this.call({ _: 'messages.getFullChat', - chatId: input.chatId + chatId: peer.chatId }) } else throw new Error('should not happen') diff --git a/packages/client/src/methods/chats/leave-chat.ts b/packages/client/src/methods/chats/leave-chat.ts index c2e1a817..1e833503 100644 --- a/packages/client/src/methods/chats/leave-chat.ts +++ b/packages/client/src/methods/chats/leave-chat.ts @@ -1,6 +1,7 @@ import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' import { TelegramClient } from '../../client' import { + isInputPeerChannel, isInputPeerChat, normalizeToInputChannel, normalizeToInputPeer, } from '../../utils/peer-utils' @@ -17,25 +18,24 @@ export async function leaveChat( chatId: InputPeerLike, clear = false ): Promise { - const chat = await this.resolvePeer(chatId) - const input = normalizeToInputPeer(chat) + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) - if (input._ === 'inputPeerChannel') { + if (isInputPeerChannel(chat)) { const res = await this.call({ _: 'channels.leaveChannel', - channel: normalizeToInputChannel(chat)!, + channel: normalizeToInputChannel(chat), }) this._handleUpdate(res) - } else if (input._ === 'inputPeerChat') { + } else if (isInputPeerChat(chat)) { const res = await this.call({ _: 'messages.deleteChatUser', - chatId: input.chatId, + chatId: chat.chatId, userId: { _: 'inputUserSelf' }, }) this._handleUpdate(res) if (clear) { - await this.deleteHistory(input) + await this.deleteHistory(chat) } } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') } diff --git a/packages/client/src/methods/chats/set-chat-description.ts b/packages/client/src/methods/chats/set-chat-description.ts index a937a77e..1e870ffa 100644 --- a/packages/client/src/methods/chats/set-chat-description.ts +++ b/packages/client/src/methods/chats/set-chat-description.ts @@ -1,8 +1,5 @@ import { TelegramClient } from '../../client' -import { - InputPeerLike, - MtCuteInvalidPeerTypeError, -} from '../../types' +import { InputPeerLike } from '../../types' import { normalizeToInputPeer } from '../../utils/peer-utils' /** @@ -20,12 +17,10 @@ export async function setChatDescription( description: string ): Promise { const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) - if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) - throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') await this.call({ _: 'messages.editChatAbout', peer: chat, - about: description + about: description, }) } diff --git a/packages/client/src/methods/chats/set-chat-photo.ts b/packages/client/src/methods/chats/set-chat-photo.ts index 726849f9..b9b4b843 100644 --- a/packages/client/src/methods/chats/set-chat-photo.ts +++ b/packages/client/src/methods/chats/set-chat-photo.ts @@ -7,6 +7,8 @@ import { MtCuteInvalidPeerTypeError, } from '../../types' import { + isInputPeerChannel, + isInputPeerChat, normalizeToInputChannel, normalizeToInputPeer, } from '../../utils/peer-utils' @@ -34,13 +36,7 @@ export async function setChatPhoto( previewSec?: number ): Promise { const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) - if ( - !( - chat._ === 'inputPeerChat' || - chat._ === 'inputPeerChannel' || - chat._ === 'inputPeerChannelFromMessage' - ) - ) + if (!(isInputPeerChannel(chat) || isInputPeerChat(chat))) throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') let photo: tl.TypeInputChatPhoto | undefined = undefined @@ -88,7 +84,7 @@ export async function setChatPhoto( } let res - if (chat._ === 'inputPeerChat') { + if (isInputPeerChat(chat)) { res = await this.call({ _: 'messages.editChatPhoto', chatId: chat.chatId, @@ -97,7 +93,7 @@ export async function setChatPhoto( } else { res = await this.call({ _: 'channels.editPhoto', - channel: normalizeToInputChannel(chat)!, + channel: normalizeToInputChannel(chat), photo, }) } diff --git a/packages/client/src/methods/chats/set-chat-title.ts b/packages/client/src/methods/chats/set-chat-title.ts index a330a7fb..6a370b4b 100644 --- a/packages/client/src/methods/chats/set-chat-title.ts +++ b/packages/client/src/methods/chats/set-chat-title.ts @@ -3,7 +3,12 @@ import { InputPeerLike, MtCuteInvalidPeerTypeError, } from '../../types' -import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' +import { + isInputPeerChannel, + isInputPeerChat, + normalizeToInputChannel, + normalizeToInputPeer, +} from '../../utils/peer-utils' /** * Change chat title @@ -20,22 +25,21 @@ export async function setChatTitle( title: string ): Promise { const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) - if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) - throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') let res - if (chat._ === 'inputPeerChat') { + if (isInputPeerChat(chat)) { res = await this.call({ _: 'messages.editChatTitle', chatId: chat.chatId, title }) - } else { + } else if (isInputPeerChannel(chat)) { res = await this.call({ _: 'channels.editTitle', - channel: normalizeToInputChannel(chat)!, + channel: normalizeToInputChannel(chat), title }) - } + } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') + this._handleUpdate(res) } diff --git a/packages/client/src/methods/messages/delete-messages.ts b/packages/client/src/methods/messages/delete-messages.ts index fec3e4f6..f90b579e 100644 --- a/packages/client/src/methods/messages/delete-messages.ts +++ b/packages/client/src/methods/messages/delete-messages.ts @@ -1,7 +1,7 @@ import { TelegramClient } from '../../client' import { InputPeerLike } from '../../types' import { MaybeArray } from '@mtcute/core' -import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' +import { isInputPeerChannel, normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' import { createDummyUpdate } from '../../utils/updates-utils' import { tl } from '@mtcute/tl' @@ -21,12 +21,11 @@ export async function deleteMessages( ): Promise { if (!Array.isArray(ids)) ids = [ids] - const peer = await this.resolvePeer(chatId) - const inputPeer = normalizeToInputPeer(peer) + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) let upd - if (inputPeer._ === 'inputPeerChannel') { - const channel = normalizeToInputChannel(peer)! + if (isInputPeerChannel(peer)) { + const channel = normalizeToInputChannel(peer) const res = await this.call({ _: 'channels.deleteMessages', channel, diff --git a/packages/client/src/methods/messages/get-messages.ts b/packages/client/src/methods/messages/get-messages.ts index f8002735..37d4abbe 100644 --- a/packages/client/src/methods/messages/get-messages.ts +++ b/packages/client/src/methods/messages/get-messages.ts @@ -2,7 +2,9 @@ import { TelegramClient } from '../../client' import { MaybeArray } from '@mtcute/core' import { createUsersChatsIndex, + isInputPeerChannel, normalizeToInputChannel, + normalizeToInputPeer, } from '../../utils/peer-utils' import { tl } from '@mtcute/tl' import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' @@ -51,7 +53,7 @@ export async function getMessages( messageIds: MaybeArray, fromReply = false ): Promise> { - const peer = await this.resolvePeer(chatId) + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) const isSingle = !Array.isArray(messageIds) if (isSingle) messageIds = [messageIds as number] @@ -63,14 +65,11 @@ export async function getMessages( })) const res = await this.call( - peer._ === 'inputPeerChannel' || - peer._ === 'inputChannel' || - peer._ === 'inputPeerChannelFromMessage' || - peer._ === 'inputChannelFromMessage' + isInputPeerChannel(peer) ? { _: 'channels.getMessages', id: ids, - channel: normalizeToInputChannel(peer)!, + channel: normalizeToInputChannel(peer), } : { _: 'messages.getMessages', diff --git a/packages/client/src/utils/peer-utils.ts b/packages/client/src/utils/peer-utils.ts index 38968773..0d06218b 100644 --- a/packages/client/src/utils/peer-utils.ts +++ b/packages/client/src/utils/peer-utils.ts @@ -11,87 +11,164 @@ export function normalizeToInputPeer( ): tl.TypeInputPeer { if (tl.isAnyInputPeer(res)) return res - if (res._ === 'inputChannelEmpty' || res._ === 'inputUserEmpty') { - return { _: 'inputPeerEmpty' } + switch (res._) { + case 'inputChannelEmpty': + case 'inputUserEmpty': + return { _: 'inputPeerEmpty' } + case 'inputUser': + return { + _: 'inputPeerUser', + userId: res.userId, + accessHash: res.accessHash, + } + case 'inputUserSelf': + return { _: 'inputPeerSelf' } + case 'inputChannel': + return { + _: 'inputPeerChannel', + channelId: res.channelId, + accessHash: res.accessHash, + } + case 'inputChannelFromMessage': + return { + _: 'inputPeerChannelFromMessage', + channelId: res.channelId, + msgId: res.msgId, + peer: res.peer, + } + case 'inputUserFromMessage': + return { + _: 'inputPeerUserFromMessage', + userId: res.userId, + msgId: res.msgId, + peer: res.peer, + } } - - if (res._ === 'inputUser') { - return { ...res, _: 'inputPeerUser' } - } - - if (res._ === 'inputUserSelf') { - return { _: 'inputPeerSelf' } - } - - if (res._ === 'inputChannel') { - return { ...res, _: 'inputPeerChannel' } - } - - if (res._ === 'inputChannelFromMessage') { - return { ...res, _: 'inputPeerChannelFromMessage' } - } - - if (res._ === 'inputUserFromMessage') { - return { ...res, _: 'inputPeerUserFromMessage' } - } - - return res as never } +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 { if (tl.isAnyInputUser(res)) return res - if (res._ === 'inputPeerUser') { - return { ...res, _: 'inputUser' } - } - - if (res._ === 'inputPeerUserFromMessage') { - return { ...res, _: 'inputUserFromMessage' } + switch (res._) { + case 'inputPeerUser': + return { + _: 'inputUser', + userId: res.userId, + accessHash: res.accessHash, + } + case 'inputPeerUserFromMessage': + return { + _: 'inputUserFromMessage', + userId: res.userId, + msgId: res.msgId, + peer: res.peer, + } } return null } +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 { if (tl.isAnyInputChannel(res)) return res - if (res._ === 'inputPeerChannel') { - return { ...res, _: 'inputChannel' } - } - - if (res._ === 'inputPeerChannelFromMessage') { - return { ...res, _: 'inputChannelFromMessage' } + switch (res._) { + case 'inputPeerChannel': + return { + _: 'inputChannel', + channelId: res.channelId, + accessHash: res.accessHash, + } + case 'inputPeerChannelFromMessage': + return { + _: 'inputChannelFromMessage', + channelId: res.channelId, + msgId: res.msgId, + peer: res.peer, + } } return null } -export function inputPeerToPeer(inp: tl.TypeInputPeer): tl.TypePeer { - if (inp._ === 'inputPeerUser' || inp._ === 'inputPeerUserFromMessage') - return { _: 'peerUser', userId: inp.userId } - - if (inp._ === 'inputPeerChannel' || inp._ === 'inputPeerChannelFromMessage') - return { _: 'peerChannel', channelId: inp.channelId } - - if (inp._ === 'inputPeerChat') return { _: 'peerChat', chatId: inp.chatId } - - throw new Error(`Cannot convert ${inp._} to peer`) +export function isInputPeerUser( + obj: tl.TypeInputPeer +): obj is + | tl.RawInputPeerUser + | tl.RawInputPeerUserFromMessage + | tl.RawInputPeerSelf { + switch (obj._) { + case 'inputPeerUser': + case 'inputPeerUserFromMessage': + case 'inputPeerSelf': + return true + } + return false } -export function peerToInputPeer(peer: tl.TypePeer, accessHash = bigInt.zero): tl.TypeInputPeer { - if (peer._ === 'peerUser') - return { _: 'inputPeerUser', userId: peer.userId, accessHash } +export function isInputPeerChannel( + obj: tl.TypeInputPeer +): obj is tl.RawInputPeerChannel | tl.RawInputPeerChannelFromMessage { + switch (obj._) { + case 'inputPeerChannel': + case 'inputPeerChannelFromMessage': + return true + } + return false +} - if (peer._ === 'peerChannel') - return { _: 'inputPeerChannel', channelId: peer.channelId, accessHash } +export function isInputPeerChat( + obj: tl.TypeInputPeer +): obj is tl.RawInputPeerChat { + return obj._ === 'inputPeerChat' +} - if (peer._ === 'peerChat') return { _: 'inputPeerChat', chatId: peer.chatId } +export function inputPeerToPeer(inp: tl.TypeInputPeer): tl.TypePeer { + switch (inp._) { + case 'inputPeerUser': + case 'inputPeerUserFromMessage': + return { _: 'peerUser', userId: inp.userId } + case 'inputPeerChannel': + case 'inputPeerChannelFromMessage': + return { _: 'peerChannel', channelId: inp.channelId } + case 'inputPeerChat': + return { _: 'peerChat', chatId: inp.chatId } + default: + throw new Error(`Cannot convert ${inp._} to peer`) + } +} - return peer as never +export function peerToInputPeer( + peer: tl.TypePeer, + accessHash = bigInt.zero +): tl.TypeInputPeer { + switch (peer._) { + case 'peerUser': + return { _: 'inputPeerUser', userId: peer.userId, accessHash } + case 'peerChannel': + return { + _: 'inputPeerChannel', + channelId: peer.channelId, + accessHash, + } + case 'peerChat': + return { _: 'inputPeerChat', chatId: peer.chatId } + } } export function createUsersChatsIndex(