refactor(client): improve resolvePeer normalization

This commit is contained in:
teidesu 2021-05-11 23:21:35 +03:00
parent 8b3caeb3d0
commit f19fdf76b8
13 changed files with 218 additions and 144 deletions

View file

@ -2,6 +2,8 @@ import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
normalizeToInputUser, normalizeToInputUser,
@ -24,28 +26,27 @@ export async function addChatMembers(
users: MaybeArray<InputPeerLike>, users: MaybeArray<InputPeerLike>,
forwardCount = 100 forwardCount = 100
): Promise<void> { ): Promise<void> {
const chat = await this.resolvePeer(chatId) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const input = normalizeToInputPeer(chat)
if (!Array.isArray(users)) users = [users] if (!Array.isArray(users)) users = [users]
if (input._ === 'inputPeerChat') { if (isInputPeerChat(chat)) {
for (const user of users) { for (const user of users) {
const p = normalizeToInputUser(await this.resolvePeer(user)) const p = normalizeToInputUser(await this.resolvePeer(user))
if (!p) continue if (!p) continue
const updates = await this.call({ const updates = await this.call({
_: 'messages.addChatUser', _: 'messages.addChatUser',
chatId: input.chatId, chatId: chat.chatId,
userId: p, userId: p,
fwdLimit: forwardCount, fwdLimit: forwardCount,
}) })
this._handleUpdate(updates) this._handleUpdate(updates)
} }
} else if (input._ === 'inputPeerChannel') { } else if (isInputPeerChannel(chat)) {
const updates = await this.call({ const updates = await this.call({
_: 'channels.inviteToChannel', _: 'channels.inviteToChannel',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
users: await this.resolvePeerMany( users: await this.resolvePeerMany(
users as InputPeerLike[], users as InputPeerLike[],
normalizeToInputUser normalizeToInputUser

View file

@ -1,9 +1,11 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { import {
InputPeerLike, isInputPeerChannel,
MtCuteInvalidPeerTypeError, isInputPeerChat,
} from '../../types' normalizeToInputChannel,
import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' normalizeToInputPeer,
} from '../../utils/peer-utils'
/** /**
* Delete a chat photo * Delete a chat photo
@ -18,23 +20,21 @@ export async function deleteChatPhoto(
chatId: InputPeerLike chatId: InputPeerLike
): Promise<void> { ): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel'))
throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
let res let res
if (chat._ === 'inputPeerChat') { if (isInputPeerChat(chat)) {
res = await this.call({ res = await this.call({
_: 'messages.editChatPhoto', _: 'messages.editChatPhoto',
chatId: chat.chatId, chatId: chat.chatId,
photo: { _: 'inputChatPhotoEmpty' } photo: { _: 'inputChatPhotoEmpty' },
}) })
} else { } else if (isInputPeerChannel(chat)) {
res = await this.call({ res = await this.call({
_: 'channels.editPhoto', _: 'channels.editPhoto',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
photo: { _: 'inputChatPhotoEmpty' } photo: { _: 'inputChatPhotoEmpty' },
}) })
} } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
this._handleUpdate(res) this._handleUpdate(res)
} }

View file

@ -4,7 +4,7 @@ import {
MtCuteInvalidPeerTypeError, MtCuteInvalidPeerTypeError,
} from '../../types' } from '../../types'
import { import {
createUsersChatsIndex, createUsersChatsIndex, isInputPeerChannel, isInputPeerChat, isInputPeerUser,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
} from '../../utils/peer-utils' } from '../../utils/peer-utils'
@ -27,13 +27,15 @@ export async function getChatMember(
userId: InputPeerLike userId: InputPeerLike
): Promise<ChatMember> { ): Promise<ChatMember> {
const user = normalizeToInputPeer(await this.resolvePeer(userId)) const user = normalizeToInputPeer(await this.resolvePeer(userId))
const chat = await this.resolvePeer(chatId) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chatInput = normalizeToInputPeer(chat)
if (isInputPeerChat(chat)) {
if (!isInputPeerUser(user))
throw new MtCuteInvalidPeerTypeError(userId, 'user')
if (chatInput._ === 'inputPeerChat') {
const res = await this.call({ const res = await this.call({
_: 'messages.getFullChat', _: 'messages.getFullChat',
chatId: chatInput.chatId, chatId: chat.chatId,
}) })
assertTypeIs( assertTypeIs(
@ -60,10 +62,10 @@ export async function getChatMember(
} }
throw new UserNotParticipantError() throw new UserNotParticipantError()
} else if (chatInput._ === 'inputPeerChannel') { } else if (isInputPeerChannel(chat)) {
const res = await this.call({ const res = await this.call({
_: 'channels.getParticipant', _: 'channels.getParticipant',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
participant: user, participant: user,
}) })

View file

@ -5,7 +5,7 @@ import {
} from '../../types' } from '../../types'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { import {
createUsersChatsIndex, createUsersChatsIndex, isInputPeerChannel, isInputPeerChat,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
} from '../../utils/peer-utils' } from '../../utils/peer-utils'
@ -71,10 +71,8 @@ export async function getChatMembers(
if (!params) params = {} if (!params) params = {}
const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) 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({ const res = await this.call({
_: 'messages.getFullChat', _: 'messages.getFullChat',
chatId: chat.chatId, chatId: chat.chatId,
@ -99,7 +97,7 @@ export async function getChatMembers(
return members.map((m) => new ChatMember(this, m, users)) return members.map((m) => new ChatMember(this, m, users))
} }
if (chat._ === 'inputPeerChannel') { if (isInputPeerChannel(chat)) {
const q = params.query?.toLowerCase() ?? '' const q = params.query?.toLowerCase() ?? ''
const type = params.type ?? 'recent' const type = params.type ?? 'recent'
@ -126,7 +124,7 @@ export async function getChatMembers(
const res = await this.call({ const res = await this.call({
_: 'channels.getParticipants', _: 'channels.getParticipants',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
filter, filter,
offset: params.offset ?? 0, offset: params.offset ?? 0,
limit: params.limit ?? 200, limit: params.limit ?? 200,
@ -143,5 +141,5 @@ export async function getChatMembers(
return res.participants.map(i => new ChatMember(this, i, users)) return res.participants.map(i => new ChatMember(this, i, users))
} }
throw new Error('should not happen') throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
} }

View file

@ -2,6 +2,9 @@ import { Chat, InputPeerLike, MtCuteArgumentError } from '../../types'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { import {
INVITE_LINK_REGEX, INVITE_LINK_REGEX,
isInputPeerChannel,
isInputPeerChat,
isInputPeerUser,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
normalizeToInputUser, normalizeToInputUser,
@ -26,37 +29,38 @@ export async function getChat(
if (m) { if (m) {
const res = await this.call({ const res = await this.call({
_: 'messages.checkChatInvite', _: 'messages.checkChatInvite',
hash: m[1] hash: m[1],
}) })
if (res._ === 'chatInvite') { 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) return new Chat(this, res.chat)
} }
} }
const peer = await this.resolvePeer(chatId) const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const input = normalizeToInputPeer(peer)
let res: tl.TypeChat | tl.TypeUser let res: tl.TypeChat | tl.TypeUser
if (input._ === 'inputPeerChannel') { if (isInputPeerChannel(peer)) {
const r = await this.call({ const r = await this.call({
_: 'channels.getChannels', _: 'channels.getChannels',
id: [normalizeToInputChannel(peer)!] id: [normalizeToInputChannel(peer)],
}) })
res = r.chats[0] res = r.chats[0]
} else if (input._ === 'inputPeerUser' || input._ === 'inputPeerSelf') { } else if (isInputPeerUser(peer)) {
const r = await this.call({ const r = await this.call({
_: 'users.getUsers', _: 'users.getUsers',
id: [normalizeToInputUser(peer)!] id: [normalizeToInputUser(peer)],
}) })
res = r[0] res = r[0]
} else if (input._ === 'inputPeerChat') { } else if (isInputPeerChat(peer)) {
const r = await this.call({ const r = await this.call({
_: 'messages.getChats', _: 'messages.getChats',
id: [input.chatId] id: [peer.chatId],
}) })
res = r.chats[0] res = r.chats[0]
} else throw new Error('should not happen') } else throw new Error('should not happen')

View file

@ -1,7 +1,7 @@
import { Chat, InputPeerLike, MtCuteArgumentError } from '../../types' import { Chat, InputPeerLike, MtCuteArgumentError } from '../../types'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { import {
INVITE_LINK_REGEX, INVITE_LINK_REGEX, isInputPeerChannel, isInputPeerChat, isInputPeerUser,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
normalizeToInputUser, normalizeToInputUser,
@ -38,24 +38,23 @@ export async function getFullChat(
} }
} }
const peer = await this.resolvePeer(chatId) const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const input = normalizeToInputPeer(peer)
let res: tl.messages.TypeChatFull | tl.TypeUserFull let res: tl.messages.TypeChatFull | tl.TypeUserFull
if (input._ === 'inputPeerChannel') { if (isInputPeerChannel(peer)) {
res = await this.call({ res = await this.call({
_: 'channels.getFullChannel', _: 'channels.getFullChannel',
channel: normalizeToInputChannel(peer)! channel: normalizeToInputChannel(peer)
}) })
} else if (input._ === 'inputPeerUser' || input._ === 'inputPeerSelf') { } else if (isInputPeerUser(peer)) {
res = await this.call({ res = await this.call({
_: 'users.getFullUser', _: 'users.getFullUser',
id: normalizeToInputUser(peer)! id: normalizeToInputUser(peer)!
}) })
} else if (input._ === 'inputPeerChat') { } else if (isInputPeerChat(peer)) {
res = await this.call({ res = await this.call({
_: 'messages.getFullChat', _: 'messages.getFullChat',
chatId: input.chatId chatId: peer.chatId
}) })
} else throw new Error('should not happen') } else throw new Error('should not happen')

View file

@ -1,6 +1,7 @@
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { import {
isInputPeerChannel, isInputPeerChat,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
} from '../../utils/peer-utils' } from '../../utils/peer-utils'
@ -17,25 +18,24 @@ export async function leaveChat(
chatId: InputPeerLike, chatId: InputPeerLike,
clear = false clear = false
): Promise<void> { ): Promise<void> {
const chat = await this.resolvePeer(chatId) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const input = normalizeToInputPeer(chat)
if (input._ === 'inputPeerChannel') { if (isInputPeerChannel(chat)) {
const res = await this.call({ const res = await this.call({
_: 'channels.leaveChannel', _: 'channels.leaveChannel',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
}) })
this._handleUpdate(res) this._handleUpdate(res)
} else if (input._ === 'inputPeerChat') { } else if (isInputPeerChat(chat)) {
const res = await this.call({ const res = await this.call({
_: 'messages.deleteChatUser', _: 'messages.deleteChatUser',
chatId: input.chatId, chatId: chat.chatId,
userId: { _: 'inputUserSelf' }, userId: { _: 'inputUserSelf' },
}) })
this._handleUpdate(res) this._handleUpdate(res)
if (clear) { if (clear) {
await this.deleteHistory(input) await this.deleteHistory(chat)
} }
} else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
} }

View file

@ -1,8 +1,5 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { import { InputPeerLike } from '../../types'
InputPeerLike,
MtCuteInvalidPeerTypeError,
} from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils' import { normalizeToInputPeer } from '../../utils/peer-utils'
/** /**
@ -20,12 +17,10 @@ export async function setChatDescription(
description: string description: string
): Promise<void> { ): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel'))
throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
await this.call({ await this.call({
_: 'messages.editChatAbout', _: 'messages.editChatAbout',
peer: chat, peer: chat,
about: description about: description,
}) })
} }

View file

@ -7,6 +7,8 @@ import {
MtCuteInvalidPeerTypeError, MtCuteInvalidPeerTypeError,
} from '../../types' } from '../../types'
import { import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer, normalizeToInputPeer,
} from '../../utils/peer-utils' } from '../../utils/peer-utils'
@ -34,13 +36,7 @@ export async function setChatPhoto(
previewSec?: number previewSec?: number
): Promise<void> { ): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
if ( if (!(isInputPeerChannel(chat) || isInputPeerChat(chat)))
!(
chat._ === 'inputPeerChat' ||
chat._ === 'inputPeerChannel' ||
chat._ === 'inputPeerChannelFromMessage'
)
)
throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
let photo: tl.TypeInputChatPhoto | undefined = undefined let photo: tl.TypeInputChatPhoto | undefined = undefined
@ -88,7 +84,7 @@ export async function setChatPhoto(
} }
let res let res
if (chat._ === 'inputPeerChat') { if (isInputPeerChat(chat)) {
res = await this.call({ res = await this.call({
_: 'messages.editChatPhoto', _: 'messages.editChatPhoto',
chatId: chat.chatId, chatId: chat.chatId,
@ -97,7 +93,7 @@ export async function setChatPhoto(
} else { } else {
res = await this.call({ res = await this.call({
_: 'channels.editPhoto', _: 'channels.editPhoto',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
photo, photo,
}) })
} }

View file

@ -3,7 +3,12 @@ import {
InputPeerLike, InputPeerLike,
MtCuteInvalidPeerTypeError, MtCuteInvalidPeerTypeError,
} from '../../types' } from '../../types'
import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
/** /**
* Change chat title * Change chat title
@ -20,22 +25,21 @@ export async function setChatTitle(
title: string title: string
): Promise<void> { ): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel'))
throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
let res let res
if (chat._ === 'inputPeerChat') { if (isInputPeerChat(chat)) {
res = await this.call({ res = await this.call({
_: 'messages.editChatTitle', _: 'messages.editChatTitle',
chatId: chat.chatId, chatId: chat.chatId,
title title
}) })
} else { } else if (isInputPeerChannel(chat)) {
res = await this.call({ res = await this.call({
_: 'channels.editTitle', _: 'channels.editTitle',
channel: normalizeToInputChannel(chat)!, channel: normalizeToInputChannel(chat),
title title
}) })
} } else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
this._handleUpdate(res) this._handleUpdate(res)
} }

View file

@ -1,7 +1,7 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types' import { InputPeerLike } from '../../types'
import { MaybeArray } from '@mtcute/core' 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 { createDummyUpdate } from '../../utils/updates-utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
@ -21,12 +21,11 @@ export async function deleteMessages(
): Promise<void> { ): Promise<void> {
if (!Array.isArray(ids)) ids = [ids] if (!Array.isArray(ids)) ids = [ids]
const peer = await this.resolvePeer(chatId) const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const inputPeer = normalizeToInputPeer(peer)
let upd let upd
if (inputPeer._ === 'inputPeerChannel') { if (isInputPeerChannel(peer)) {
const channel = normalizeToInputChannel(peer)! const channel = normalizeToInputChannel(peer)
const res = await this.call({ const res = await this.call({
_: 'channels.deleteMessages', _: 'channels.deleteMessages',
channel, channel,

View file

@ -2,7 +2,9 @@ import { TelegramClient } from '../../client'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { import {
createUsersChatsIndex, createUsersChatsIndex,
isInputPeerChannel,
normalizeToInputChannel, normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils' } from '../../utils/peer-utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
@ -51,7 +53,7 @@ export async function getMessages(
messageIds: MaybeArray<number>, messageIds: MaybeArray<number>,
fromReply = false fromReply = false
): Promise<MaybeArray<Message>> { ): Promise<MaybeArray<Message>> {
const peer = await this.resolvePeer(chatId) const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const isSingle = !Array.isArray(messageIds) const isSingle = !Array.isArray(messageIds)
if (isSingle) messageIds = [messageIds as number] if (isSingle) messageIds = [messageIds as number]
@ -63,14 +65,11 @@ export async function getMessages(
})) }))
const res = await this.call( const res = await this.call(
peer._ === 'inputPeerChannel' || isInputPeerChannel(peer)
peer._ === 'inputChannel' ||
peer._ === 'inputPeerChannelFromMessage' ||
peer._ === 'inputChannelFromMessage'
? { ? {
_: 'channels.getMessages', _: 'channels.getMessages',
id: ids, id: ids,
channel: normalizeToInputChannel(peer)!, channel: normalizeToInputChannel(peer),
} }
: { : {
_: 'messages.getMessages', _: 'messages.getMessages',

View file

@ -11,87 +11,164 @@ export function normalizeToInputPeer(
): tl.TypeInputPeer { ): tl.TypeInputPeer {
if (tl.isAnyInputPeer(res)) return res if (tl.isAnyInputPeer(res)) return res
if (res._ === 'inputChannelEmpty' || res._ === 'inputUserEmpty') { switch (res._) {
case 'inputChannelEmpty':
case 'inputUserEmpty':
return { _: 'inputPeerEmpty' } return { _: 'inputPeerEmpty' }
case 'inputUser':
return {
_: 'inputPeerUser',
userId: res.userId,
accessHash: res.accessHash,
} }
case 'inputUserSelf':
if (res._ === 'inputUser') {
return { ...res, _: 'inputPeerUser' }
}
if (res._ === 'inputUserSelf') {
return { _: 'inputPeerSelf' } 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._ === 'inputChannel') { export function normalizeToInputUser(
return { ...res, _: 'inputPeerChannel' } res: tl.TypeInputUser | tl.RawInputPeerUser | tl.RawInputPeerUserFromMessage | tl.RawInputPeerSelf
} ): tl.TypeInputUser
export function normalizeToInputUser(
if (res._ === 'inputChannelFromMessage') { res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
return { ...res, _: 'inputPeerChannelFromMessage' } ): tl.TypeInputUser | null
}
if (res._ === 'inputUserFromMessage') {
return { ...res, _: 'inputPeerUserFromMessage' }
}
return res as never
}
export function normalizeToInputUser( export function normalizeToInputUser(
res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
): tl.TypeInputUser | null { ): tl.TypeInputUser | null {
if (tl.isAnyInputUser(res)) return res if (tl.isAnyInputUser(res)) return res
if (res._ === 'inputPeerUser') { switch (res._) {
return { ...res, _: 'inputUser' } case 'inputPeerUser':
return {
_: 'inputUser',
userId: res.userId,
accessHash: res.accessHash,
}
case 'inputPeerUserFromMessage':
return {
_: 'inputUserFromMessage',
userId: res.userId,
msgId: res.msgId,
peer: res.peer,
} }
if (res._ === 'inputPeerUserFromMessage') {
return { ...res, _: 'inputUserFromMessage' }
} }
return null 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( export function normalizeToInputChannel(
res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel res: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
): tl.TypeInputChannel | null { ): tl.TypeInputChannel | null {
if (tl.isAnyInputChannel(res)) return res if (tl.isAnyInputChannel(res)) return res
if (res._ === 'inputPeerChannel') { switch (res._) {
return { ...res, _: 'inputChannel' } case 'inputPeerChannel':
return {
_: 'inputChannel',
channelId: res.channelId,
accessHash: res.accessHash,
}
case 'inputPeerChannelFromMessage':
return {
_: 'inputChannelFromMessage',
channelId: res.channelId,
msgId: res.msgId,
peer: res.peer,
} }
if (res._ === 'inputPeerChannelFromMessage') {
return { ...res, _: 'inputChannelFromMessage' }
} }
return null return null
} }
export function inputPeerToPeer(inp: tl.TypeInputPeer): tl.TypePeer { export function isInputPeerUser(
if (inp._ === 'inputPeerUser' || inp._ === 'inputPeerUserFromMessage') obj: tl.TypeInputPeer
return { _: 'peerUser', userId: inp.userId } ): obj is
| tl.RawInputPeerUser
if (inp._ === 'inputPeerChannel' || inp._ === 'inputPeerChannelFromMessage') | tl.RawInputPeerUserFromMessage
return { _: 'peerChannel', channelId: inp.channelId } | tl.RawInputPeerSelf {
switch (obj._) {
if (inp._ === 'inputPeerChat') return { _: 'peerChat', chatId: inp.chatId } case 'inputPeerUser':
case 'inputPeerUserFromMessage':
throw new Error(`Cannot convert ${inp._} to peer`) case 'inputPeerSelf':
return true
}
return false
} }
export function peerToInputPeer(peer: tl.TypePeer, accessHash = bigInt.zero): tl.TypeInputPeer { export function isInputPeerChannel(
if (peer._ === 'peerUser') obj: tl.TypeInputPeer
): obj is tl.RawInputPeerChannel | tl.RawInputPeerChannelFromMessage {
switch (obj._) {
case 'inputPeerChannel':
case 'inputPeerChannelFromMessage':
return true
}
return false
}
export function isInputPeerChat(
obj: tl.TypeInputPeer
): obj is tl.RawInputPeerChat {
return obj._ === 'inputPeerChat'
}
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`)
}
}
export function peerToInputPeer(
peer: tl.TypePeer,
accessHash = bigInt.zero
): tl.TypeInputPeer {
switch (peer._) {
case 'peerUser':
return { _: 'inputPeerUser', userId: peer.userId, accessHash } return { _: 'inputPeerUser', userId: peer.userId, accessHash }
case 'peerChannel':
if (peer._ === 'peerChannel') return {
return { _: 'inputPeerChannel', channelId: peer.channelId, accessHash } _: 'inputPeerChannel',
channelId: peer.channelId,
if (peer._ === 'peerChat') return { _: 'inputPeerChat', chatId: peer.chatId } accessHash,
}
return peer as never case 'peerChat':
return { _: 'inputPeerChat', chatId: peer.chatId }
}
} }
export function createUsersChatsIndex( export function createUsersChatsIndex(