refactor(client): made resolvePeer return InputPeer, and removed now redundant normalizeTo calls

This commit is contained in:
teidesu 2021-05-15 20:25:59 +03:00
parent b634aa01eb
commit f7e8051a94
52 changed files with 127 additions and 166 deletions

View file

@ -2756,9 +2756,7 @@ export interface TelegramClient extends BaseTelegramClient {
T extends tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
>(
peerIds: InputPeerLike[],
normalizer: (
obj: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
) => T | null
normalizer: (obj: tl.TypeInputPeer) => T | null
): Promise<T[]>
/**
* Get multiple `InputPeer`s at once.
@ -2767,18 +2765,14 @@ export interface TelegramClient extends BaseTelegramClient {
*
* @param peerIds Peer Ids
*/
resolvePeerMany(
peerIds: InputPeerLike[]
): Promise<(tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel)[]>
resolvePeerMany(peerIds: InputPeerLike[]): Promise<tl.TypeInputPeer[]>
/**
* Get the `InputPeer` of a known peer id.
* Useful when an `InputPeer` is needed.
*
* @param peerId The peer identifier that you want to extract the `InputPeer` from.
*/
resolvePeer(
peerId: InputPeerLike
): Promise<tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel>
resolvePeer(peerId: InputPeerLike): Promise<tl.TypeInputPeer>
/**
* Change user status to offline or online
*

View file

@ -5,10 +5,8 @@ import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
normalizeToInputUser,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
/**
* Add new members to a group, supergroup or channel.
@ -26,7 +24,7 @@ export async function addChatMembers(
users: MaybeArray<InputPeerLike>,
forwardCount = 100
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
if (!Array.isArray(users)) users = [users]

View file

@ -2,7 +2,6 @@ import { TelegramClient } from '../../client'
import { MaybeArray } from '@mtcute/core'
import { InputPeerLike } from '../../types'
import { tl } from '@mtcute/tl'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Archive one or more chats
@ -21,7 +20,7 @@ export async function archiveChats(
for (const chat of chats) {
folderPeers.push({
_: 'inputFolderPeer',
peer: normalizeToInputPeer(await this.resolvePeer(chat)),
peer: await this.resolvePeer(chat),
folderId: 1
})
}

View file

@ -9,7 +9,6 @@ import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
normalizeToInputUser,
} from '../../utils/peer-utils'
@ -28,8 +27,8 @@ export async function banChatMember(
chatId: InputPeerLike,
userId: InputPeerLike
): Promise<Message | null> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const user = normalizeToInputPeer(await this.resolvePeer(userId))
const chat = await this.resolvePeer(chatId)
const user = await this.resolvePeer(userId)
let res
if (isInputPeerChannel(chat)) {

View file

@ -4,7 +4,6 @@ import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
/**
@ -19,7 +18,7 @@ export async function deleteChatPhoto(
this: TelegramClient,
chatId: InputPeerLike
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
let res
if (isInputPeerChat(chat)) {

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
import { isInputPeerChat } from '../../utils/peer-utils'
/**
* Delete a legacy group chat for all members
@ -12,8 +12,8 @@ export async function deleteGroup(
this: TelegramClient,
chatId: InputPeerLike
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
if (chat._ !== 'inputPeerChat')
const chat = await this.resolvePeer(chatId)
if (!isInputPeerChat(chat))
throw new MtCuteInvalidPeerTypeError(chatId, 'chat')
const res = await this.call({

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils'
import { normalizeToInputChannel } from '../../utils/peer-utils'
import { createDummyUpdate } from '../../utils/updates-utils'
import { tl } from '@mtcute/tl'
@ -25,7 +25,7 @@ export async function deleteHistory(
mode: 'delete' | 'clear' | 'revoke' = 'delete',
maxId = 0
): Promise<void> {
const peer = normalizeToInputPeer(await this.resolvePeer(chat))
const peer = await this.resolvePeer(chat)
const res = await this.call({
_: 'messages.deleteHistory',

View file

@ -1,12 +1,11 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import {
InputPeerLike,
MtCuteInvalidPeerTypeError,
} from '../../types'
import {
createUsersChatsIndex, isInputPeerChannel, isInputPeerChat, isInputPeerUser,
createUsersChatsIndex,
isInputPeerChannel,
isInputPeerChat,
isInputPeerUser,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { assertTypeIs } from '../../utils/type-assertion'
import { tl } from '@mtcute/tl'
@ -26,8 +25,8 @@ export async function getChatMember(
chatId: InputPeerLike,
userId: InputPeerLike
): Promise<ChatMember> {
const user = normalizeToInputPeer(await this.resolvePeer(userId))
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const user = await this.resolvePeer(userId)
const chat = await this.resolvePeer(chatId)
if (isInputPeerChat(chat)) {
if (!isInputPeerUser(user))
@ -71,10 +70,6 @@ export async function getChatMember(
const { users } = createUsersChatsIndex(res)
return new ChatMember(
this,
res.participant,
users
)
return new ChatMember(this, res.participant, users)
} else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
}

View file

@ -5,9 +5,10 @@ import {
} from '../../types'
import { TelegramClient } from '../../client'
import {
createUsersChatsIndex, isInputPeerChannel, isInputPeerChat,
createUsersChatsIndex,
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { assertTypeIs } from '../../utils/type-assertion'
import { tl } from '@mtcute/tl'
@ -70,7 +71,7 @@ export async function getChatMembers(
): Promise<ChatMember[]> {
if (!params) params = {}
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
if (isInputPeerChat(chat)) {
const res = await this.call({
@ -145,7 +146,7 @@ export async function getChatMembers(
)
const { users } = createUsersChatsIndex(res)
return res.participants.map(i => new ChatMember(this, i, users))
return res.participants.map((i) => new ChatMember(this, i, users))
}
throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')

View file

@ -6,7 +6,6 @@ import {
isInputPeerChat,
isInputPeerUser,
normalizeToInputChannel,
normalizeToInputPeer,
normalizeToInputUser,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
@ -42,7 +41,7 @@ export async function getChat(
}
}
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
let res: tl.TypeChat | tl.TypeUser
if (isInputPeerChannel(peer)) {

View file

@ -1,9 +1,11 @@
import { Chat, InputPeerLike, MtCuteArgumentError } from '../../types'
import { TelegramClient } from '../../client'
import {
INVITE_LINK_REGEX, isInputPeerChannel, isInputPeerChat, isInputPeerUser,
INVITE_LINK_REGEX,
isInputPeerChannel,
isInputPeerChat,
isInputPeerUser,
normalizeToInputChannel,
normalizeToInputPeer,
normalizeToInputUser,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
@ -26,11 +28,13 @@ export async function getFullChat(
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)}`
)
}
// we still need to fetch full chat info
@ -38,23 +42,23 @@ export async function getFullChat(
}
}
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
let res: tl.messages.TypeChatFull | tl.TypeUserFull
if (isInputPeerChannel(peer)) {
res = await this.call({
_: 'channels.getFullChannel',
channel: normalizeToInputChannel(peer)
channel: normalizeToInputChannel(peer),
})
} else if (isInputPeerUser(peer)) {
res = await this.call({
_: 'users.getFullUser',
id: normalizeToInputUser(peer)!
id: normalizeToInputUser(peer)!,
})
} else if (isInputPeerChat(peer)) {
res = await this.call({
_: 'messages.getFullChat',
chatId: peer.chatId
chatId: peer.chatId,
})
} else throw new Error('should not happen')

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { isInputPeerChannel, normalizeToInputPeer } from '../../utils/peer-utils'
import { isInputPeerChannel } from '../../utils/peer-utils'
/**
* Kick a user from a chat.
@ -16,8 +16,8 @@ export async function kickChatMember(
chatId: InputPeerLike,
userId: InputPeerLike
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const user = normalizeToInputPeer(await this.resolvePeer(userId))
const chat = await this.resolvePeer(chatId)
const user = await this.resolvePeer(userId)
await this.banChatMember(chat, user)

View file

@ -1,9 +1,9 @@
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { TelegramClient } from '../../client'
import {
isInputPeerChannel, isInputPeerChat,
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
/**
@ -18,7 +18,7 @@ export async function leaveChat(
chatId: InputPeerLike,
clear = false
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
if (isInputPeerChannel(chat)) {
const res = await this.call({

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Mark a chat as unread
@ -16,7 +15,7 @@ export async function markChatUnread(
_: 'messages.markDialogUnread',
peer: {
_: 'inputDialogPeer',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
},
unread: true
})

View file

@ -3,7 +3,6 @@ import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import {
isInputPeerChannel,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
import { normalizeDate } from '../../utils/misc-utils'
@ -32,11 +31,11 @@ export async function restrictChatMember(
restrictions: Omit<tl.RawChatBannedRights, '_' | 'untilDate'>,
until?: number | Date
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
if (!isInputPeerChannel(chat))
throw new MtCuteInvalidPeerTypeError(chatId, 'channel')
const user = normalizeToInputPeer(await this.resolvePeer(userId))
const user = await this.resolvePeer(userId)
const res = await this.call({
_: 'channels.editBanned',

View file

@ -1,7 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { tl } from '@mtcute/tl'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Save or delete a draft message associated with some chat
@ -15,7 +14,7 @@ export async function saveDraft(
chatId: InputPeerLike,
draft: null | Omit<tl.RawDraftMessage, '_' | 'date'>
): Promise<void> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
if (draft) {
await this.call({

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { Chat, InputChatPermissions, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
import { Chat, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
import { tl } from '@mtcute/tl'
/**
@ -21,7 +20,7 @@ export async function setChatDefaultPermissions(
chatId: InputPeerLike,
restrictions: Omit<tl.RawChatBannedRights, '_' | 'untilDate'>
): Promise<Chat> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const res = await this.call({
_: 'messages.editChatDefaultBannedRights',

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Change chat description
@ -16,7 +15,7 @@ export async function setChatDescription(
chatId: InputPeerLike,
description: string
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
await this.call({
_: 'messages.editChatAbout',

View file

@ -10,7 +10,6 @@ import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
import { fileIdToInputPhoto, tdFileId } from '@mtcute/file-id'
@ -35,7 +34,7 @@ export async function setChatPhoto(
media: InputFileLike,
previewSec?: number
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
if (!(isInputPeerChannel(chat) || isInputPeerChat(chat)))
throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')

View file

@ -7,7 +7,6 @@ import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
/**
@ -24,7 +23,7 @@ export async function setChatTitle(
chatId: InputPeerLike,
title: string
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const chat = await this.resolvePeer(chatId)
let res
if (isInputPeerChat(chat)) {

View file

@ -2,7 +2,6 @@ import { TelegramClient } from '../../client'
import { MaybeArray } from '@mtcute/core'
import { InputPeerLike } from '../../types'
import { tl } from '@mtcute/tl'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Unarchive one or more chats
@ -21,7 +20,7 @@ export async function unarchiveChats(
for (const chat of chats) {
folderPeers.push({
_: 'inputFolderPeer',
peer: normalizeToInputPeer(await this.resolvePeer(chat)),
peer: await this.resolvePeer(chat),
folderId: 0
})
}

View file

@ -4,7 +4,6 @@ import {
isInputPeerChannel,
isInputPeerChat,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
// @alias=unrestrictChatMember
@ -25,8 +24,8 @@ export async function unbanChatMember(
chatId: InputPeerLike,
userId: InputPeerLike
): Promise<void> {
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
const user = normalizeToInputPeer(await this.resolvePeer(userId))
const chat = await this.resolvePeer(chatId)
const user = await this.resolvePeer(userId)
if (isInputPeerChannel(chat)) {
const res = await this.call({

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { ChatInviteLink, InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
import { normalizeDate } from '../../utils/misc-utils'
/**
@ -35,7 +34,7 @@ export async function createInviteLink(
const res = await this.call({
_: 'messages.exportChatInvite',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
expireDate: normalizeDate(params.expires),
usageLimit: params.usageLimit
})

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { ChatInviteLink, InputPeerLike } from '../../types'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
import { normalizeDate } from '../../utils/misc-utils'
/**
@ -37,7 +37,7 @@ export async function editInviteLink(
): Promise<ChatInviteLink> {
const res = await this.call({
_: 'messages.editExportedChatInvite',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
link,
expireDate: normalizeDate(params.expires),
usageLimit: params.usageLimit

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { ChatInviteLink, InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Generate a new primary invite link for a chat,
@ -18,7 +17,7 @@ export async function exportInviteLink(
): Promise<ChatInviteLink> {
const res = await this.call({
_: 'messages.exportChatInvite',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
legacyRevokePermanent: true
})

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { ChatInviteLink, InputPeerLike, User } from '../../types'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
/**
@ -18,7 +18,7 @@ export async function* getInviteLinkMembers(
link: string,
limit = Infinity
): AsyncIterableIterator<ChatInviteLink.JoinedMember> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
let current = 0

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { ChatInviteLink, InputPeerLike } from '../../types'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
/**
* Get detailed information about an invite link
@ -16,7 +16,7 @@ export async function getInviteLink(
): Promise<ChatInviteLink> {
const res = await this.call({
_: 'messages.getExportedChatInvite',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
link
})

View file

@ -6,7 +6,6 @@ import {
} from '../../types'
import {
createUsersChatsIndex,
normalizeToInputPeer,
normalizeToInputUser,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
@ -53,7 +52,7 @@ export async function* getInviteLinks(
const total = params.limit || Infinity
const chunkSize = Math.min(params.chunkSize ?? 100, total)
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const admin = normalizeToInputUser(await this.resolvePeer(adminId))
if (!admin) throw new MtCuteInvalidPeerTypeError(adminId, 'user')

View file

@ -4,10 +4,7 @@ import {
InputPeerLike,
MtCuteTypeAssertionError,
} from '../../types'
import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
/**
* Get primary invite link of a chat
@ -21,7 +18,7 @@ export async function getPrimaryInviteLink(
): Promise<ChatInviteLink> {
const res = await this.call({
_: 'messages.getExportedChatInvites',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
adminId: { _: 'inputUserSelf' },
limit: 1,
revoked: false,

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { ChatInviteLink, InputPeerLike } from '../../types'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
/**
* Revoke an invite link.
@ -20,7 +20,7 @@ export async function revokeInviteLink(
): Promise<ChatInviteLink> {
const res = await this.call({
_: 'messages.editExportedChatInvite',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
link,
revoked: true
})

View file

@ -1,10 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteTypeAssertionError, Poll } from '../../types'
import { tl } from '@mtcute/tl'
import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
import bigInt from 'big-integer'
import { assertTypeIs } from '../../utils/type-assertion'
@ -25,7 +21,7 @@ export async function closePoll(
): Promise<Poll> {
const res = await this.call({
_: 'messages.editMessage',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
id: message,
media: {
_: 'inputMediaPoll',
@ -49,7 +45,11 @@ export async function closePoll(
this._handleUpdate(res, true)
const upd = res.updates[0]
assertTypeIs('messages.editMessage (@ .updates[0])', upd, 'updateMessagePoll')
assertTypeIs(
'messages.editMessage (@ .updates[0])',
upd,
'updateMessagePoll'
)
if (!upd.poll) {
throw new MtCuteTypeAssertionError(
'messages.editMessage (@ .updates[0].poll)',

View file

@ -1,9 +1,11 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { MaybeArray } from '@mtcute/core'
import { isInputPeerChannel, normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils'
import {
isInputPeerChannel,
normalizeToInputChannel,
} from '../../utils/peer-utils'
import { createDummyUpdate } from '../../utils/updates-utils'
import { tl } from '@mtcute/tl'
/**
* Delete messages, including service messages.
@ -21,7 +23,7 @@ export async function deleteMessages(
): Promise<void> {
if (!Array.isArray(ids)) ids = [ids]
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
let upd
if (isInputPeerChannel(peer)) {
@ -29,14 +31,14 @@ export async function deleteMessages(
const res = await this.call({
_: 'channels.deleteMessages',
channel,
id: ids
id: ids,
})
upd = createDummyUpdate(res.pts, res.ptsCount, peer.channelId)
} else {
const res = await this.call({
_: 'messages.deleteMessages',
id: ids,
revoke
revoke,
})
upd = createDummyUpdate(res.pts, res.ptsCount)
}

View file

@ -7,7 +7,6 @@ import {
ReplyMarkup,
} from '../../types'
import { tl } from '@mtcute/tl'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Edit message text, media, reply markup and schedule date.
@ -105,7 +104,7 @@ export async function editMessage(
const res = await this.call({
_: 'messages.editMessage',
id: typeof message === 'number' ? message : message.id,
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
noWebpage: params.disableWebPreview,
replyMarkup: BotKeyboard._convertToTl(params.replyMarkup),
message: content,

View file

@ -10,7 +10,6 @@ import { MaybeArray } from '@mtcute/core'
import { tl } from '@mtcute/tl'
import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { normalizeDate, randomUlong } from '../../utils/misc-utils'
@ -193,7 +192,7 @@ export async function forwardMessages(
'You can forward no more than 100 messages at once'
)
const toPeer = normalizeToInputPeer(await this.resolvePeer(toChatId))
const toPeer = await this.resolvePeer(toChatId)
let captionMessage: Message | null = null
if (params.caption) {
@ -221,7 +220,7 @@ export async function forwardMessages(
const res = await this.call({
_: 'messages.forwardMessages',
toPeer,
fromPeer: normalizeToInputPeer(await this.resolvePeer(fromChatId)),
fromPeer: await this.resolvePeer(fromChatId),
id: messages as number[],
silent: params.silent,
scheduleDate: normalizeDate(params.schedule),

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, Message, MtCuteArgumentError, MtCuteTypeAssertionError } from '../../types'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils'
import { InputPeerLike, Message, MtCuteTypeAssertionError } from '../../types'
import { createUsersChatsIndex, } from '../../utils/peer-utils'
import { normalizeDate } from '../../utils/misc-utils'
/**
@ -57,7 +57,7 @@ export async function getHistory(
params.offsetId ?? (params.reverse && !params.offsetDate ? 1 : 0)
const limit = params.limit || 100
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const res = await this.call({
_: 'messages.getHistory',

View file

@ -4,7 +4,6 @@ import {
createUsersChatsIndex,
isInputPeerChannel,
normalizeToInputChannel,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl'
import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
@ -53,7 +52,7 @@ export async function getMessages(
messageIds: MaybeArray<number>,
fromReply = false
): Promise<MaybeArray<Message>> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const isSingle = !Array.isArray(messageIds)
if (isSingle) messageIds = [messageIds as number]

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, Message } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Iterate through a chat history sequentially.
@ -68,7 +67,7 @@ export async function* iterHistory(
const limit = Math.min(params.chunkSize || 100, total)
// resolve peer once and pass an InputPeer afterwards
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
for (;;) {
const messages = await this.getHistory(peer, {

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Pin a message in a group, supergroup, channel or PM.
@ -23,7 +22,7 @@ export async function pinMessage(
): Promise<void> {
const res = await this.call({
_: 'messages.updatePinnedMessage',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
id: messageId,
silent: !notify,
pmOneside: !bothSides

View file

@ -1,6 +1,9 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { isInputPeerChannel, normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils'
import {
isInputPeerChannel,
normalizeToInputChannel,
} from '../../utils/peer-utils'
import { createDummyUpdate } from '../../utils/updates-utils'
/**
@ -17,16 +20,18 @@ export async function readHistory(
message = 0,
clearMentions = false
): Promise<void> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
if (clearMentions) {
const res = await this.call({
_: 'messages.readMentions',
peer
peer,
})
if (isInputPeerChannel(peer)) {
this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId))
this._handleUpdate(
createDummyUpdate(res.pts, res.ptsCount, peer.channelId)
)
} else {
this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
@ -36,15 +41,14 @@ export async function readHistory(
await this.call({
_: 'channels.readHistory',
channel: normalizeToInputChannel(peer),
maxId: message
maxId: message,
})
} else {
const res = await this.call({
_: 'messages.readHistory',
peer,
maxId: message
maxId: message,
})
this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
}

View file

@ -1,10 +1,7 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, Message, MtCuteTypeAssertionError } from '../../types'
import { tl } from '@mtcute/tl'
import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
import { SearchFilters } from '../../types'
/**
@ -72,11 +69,10 @@ export async function* searchMessages(
const total = params.limit || Infinity
const limit = Math.min(params.chunkSize || 100, total)
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const fromUser =
(params.fromUser
? normalizeToInputPeer(await this.resolvePeer(params.fromUser))
: null) || undefined
(params.fromUser ? await this.resolvePeer(params.fromUser) : null) ||
undefined
for (;;) {
const res = await this.call({

View file

@ -1,7 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, Message, ReplyMarkup } from '../../types'
import { tl } from '@mtcute/tl'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Copy a message (i.e. send the same message,
@ -79,7 +78,7 @@ export async function sendCopy(
clearDraft?: boolean
}
): Promise<Message> {
const fromPeer = normalizeToInputPeer(await this.resolvePeer(fromChatId))
const fromPeer = await this.resolvePeer(fromChatId)
const msg = await this.getMessages(fromPeer, message)
return msg.sendCopy(toChatId, params)

View file

@ -6,7 +6,6 @@ import {
Message,
ReplyMarkup,
} from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
import { normalizeDate, randomUlong } from '../../utils/misc-utils'
import { tl } from '@mtcute/tl'
@ -79,7 +78,7 @@ export async function sendMediaGroup(
): Promise<Message> {
if (!params) params = {}
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const multiMedia: tl.RawInputSingleMedia[] = []

View file

@ -6,7 +6,6 @@ import {
Message,
ReplyMarkup,
} from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
import { normalizeDate, randomUlong } from '../../utils/misc-utils'
/**
@ -94,7 +93,7 @@ export async function sendMedia(
(media as any).entities
)
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const res = await this.call({

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { tl } from '@mtcute/tl'
import { inputPeerToPeer, normalizeToInputPeer } from '../../utils/peer-utils'
import { inputPeerToPeer } from '../../utils/peer-utils'
import { normalizeDate, randomUlong } from '../../utils/misc-utils'
import { InputPeerLike, Message, BotKeyboard, ReplyMarkup } from '../../types'
@ -76,7 +76,7 @@ export async function sendText(
params.entities
)
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const res = await this.call({

View file

@ -1,7 +1,6 @@
import { TelegramClient } from '../../client'
import { tl } from '@mtcute/tl'
import { InputPeerLike, TypingStatus } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Sends a current user/bot typing event
@ -74,7 +73,7 @@ export async function sendTyping(
await this.call({
_: 'messages.setTyping',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
action: status
})
}

View file

@ -6,10 +6,7 @@ import {
Poll,
} from '../../types'
import { MaybeArray } from '@mtcute/core'
import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { createUsersChatsIndex } from '../../utils/peer-utils'
import { assertTypeIs } from '../../utils/type-assertion'
/**
@ -33,7 +30,7 @@ export async function sendVote(
if (options === null) options = []
if (!Array.isArray(options)) options = [options]
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
let poll: Poll | undefined = undefined
if (options.some((it) => typeof it === 'number')) {

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { isInputPeerChannel, normalizeToInputPeer } from '../../utils/peer-utils'
import { isInputPeerChannel } from '../../utils/peer-utils'
import { createDummyUpdate } from '../../utils/updates-utils'
/**
@ -13,7 +13,7 @@ export async function unpinAllMessages(
this: TelegramClient,
chatId: InputPeerLike
): Promise<void> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const peer = await this.resolvePeer(chatId)
const res = await this.call({
_: 'messages.unpinAllMessages',

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Unpin a message in a group, supergroup, channel or PM.
@ -19,7 +18,7 @@ export async function unpinMessage(
): Promise<void> {
const res = await this.call({
_: 'messages.updatePinnedMessage',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
peer: await this.resolvePeer(chatId),
id: messageId,
unpin: true
})

View file

@ -1,6 +1,5 @@
import { InputPeerLike } from '../../types'
import { TelegramClient } from '../../client'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Block a user
@ -14,6 +13,6 @@ export async function blockUser(
): Promise<void> {
await this.call({
_: 'contacts.block',
id: normalizeToInputPeer(await this.resolvePeer(id)),
id: await this.resolvePeer(id),
})
}

View file

@ -20,7 +20,7 @@ export async function resolvePeerMany<
this: TelegramClient,
peerIds: InputPeerLike[],
normalizer: (
obj: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
obj: tl.TypeInputPeer
) => T | null
): Promise<T[]>
@ -35,7 +35,7 @@ export async function resolvePeerMany<
export async function resolvePeerMany(
this: TelegramClient,
peerIds: InputPeerLike[]
): Promise<(tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel)[]>
): Promise<tl.TypeInputPeer[]>
/**
* @internal
@ -44,7 +44,7 @@ export async function resolvePeerMany(
this: TelegramClient,
peerIds: InputPeerLike[],
normalizer?: (
obj: tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel
obj: tl.TypeInputPeer
) => tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel | null
): Promise<(tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel)[]> {
const ret: (

View file

@ -3,6 +3,7 @@ import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteNotFoundError } from '../../types'
import { getBasicPeerType, MAX_CHANNEL_ID } from '@mtcute/core'
import bigInt from 'big-integer'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Get the `InputPeer` of a known peer id.
@ -14,9 +15,9 @@ import bigInt from 'big-integer'
export async function resolvePeer(
this: TelegramClient,
peerId: InputPeerLike
): Promise<tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel> {
): Promise<tl.TypeInputPeer> {
// for convenience we also accept tl objects directly
if (typeof peerId === 'object') return peerId
if (typeof peerId === 'object') return normalizeToInputPeer(peerId)
if (typeof peerId === 'number') {
const fromStorage = await this.storage.getPeerById(peerId)

View file

@ -1,6 +1,5 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Unblock a user
@ -14,6 +13,6 @@ export async function unblockUser(
): Promise<void> {
await this.call({
_: 'contacts.unblock',
id: normalizeToInputPeer(await this.resolvePeer(id)),
id: await this.resolvePeer(id),
})
}