feat: resolveUser and resolveChannel methods + refactor to use them

This commit is contained in:
alina 🌸 2024-02-06 00:25:08 +03:00
parent 27e14472ff
commit 99bd1bc313
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
44 changed files with 134 additions and 131 deletions

View file

@ -223,7 +223,7 @@ import { getProfilePhoto } from './methods/users/get-profile-photo.js'
import { getProfilePhotos } from './methods/users/get-profile-photos.js'
import { getUsers } from './methods/users/get-users.js'
import { iterProfilePhotos } from './methods/users/iter-profile-photos.js'
import { resolvePeer } from './methods/users/resolve-peer.js'
import { resolveChannel, resolvePeer, resolveUser } from './methods/users/resolve-peer.js'
import { resolvePeerMany } from './methods/users/resolve-peer-many.js'
import { setGlobalTtl } from './methods/users/set-global-ttl.js'
import { setMyEmojiStatus } from './methods/users/set-my-emoji-status.js'
@ -5024,6 +5024,18 @@ export interface TelegramClient extends ITelegramClient {
* @param [force=false] Whether to force re-fetch the peer from the server (only for usernames and phone numbers)
*/
resolvePeer(peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputPeer>
/**
* Shorthand for `resolvePeer` that converts the input peer to `InputUser`.
* **Available**: both users and bots
*
*/
resolveUser(peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputUser>
/**
* Shorthand for `resolvePeer` that converts the input peer to `InputChannel`.
* **Available**: both users and bots
*
*/
resolveChannel(peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputChannel>
/**
* Changes the current default value of the Time-To-Live setting,
* applied to all new chats.
@ -5867,6 +5879,12 @@ TelegramClient.prototype.resolvePeerMany = function (...args) {
TelegramClient.prototype.resolvePeer = function (...args) {
return resolvePeer(this._client, ...args)
}
TelegramClient.prototype.resolveUser = function (...args) {
return resolveUser(this._client, ...args)
}
TelegramClient.prototype.resolveChannel = function (...args) {
return resolveChannel(this._client, ...args)
}
TelegramClient.prototype.setGlobalTtl = function (...args) {
return setGlobalTtl(this._client, ...args)
}

View file

@ -2,8 +2,7 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
/**
* Gets information about a bot the current uzer owns (or the current bot)
@ -28,7 +27,7 @@ export async function getBotInfo(
return client.call({
_: 'bots.getBotInfo',
bot: bot ? toInputUser(await resolvePeer(client, bot), bot) : undefined,
bot: bot ? await resolveUser(client, bot) : undefined,
langCode: langCode,
})
}

View file

@ -2,8 +2,7 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
/**
* Fetches the menu button set for the given user.
@ -11,6 +10,6 @@ import { resolvePeer } from '../users/resolve-peer.js'
export async function getBotMenuButton(client: ITelegramClient, user: InputPeerLike): Promise<tl.TypeBotMenuButton> {
return await client.call({
_: 'bots.getBotMenuButton',
userId: toInputUser(await resolvePeer(client, user), user),
userId: await resolveUser(client, user),
})
}

View file

@ -3,8 +3,7 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { GameHighScore, InputMessageId, InputPeerLike, normalizeInputMessageId, PeersIndex } from '../../types/index.js'
import { normalizeInlineId } from '../../utils/inline-utils.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolvePeer, resolveUser } from '../users/resolve-peer.js'
/**
* Get high scores of a game
@ -24,7 +23,7 @@ export async function getGameHighScores(
let user: tl.TypeInputUser
if (userId) {
user = toInputUser(await resolvePeer(client, userId), userId)
user = await resolveUser(client, userId)
} else {
user = { _: 'inputUserEmpty' }
}
@ -57,7 +56,7 @@ export async function getInlineGameHighScores(
let user: tl.TypeInputUser
if (userId) {
user = toInputUser(await resolvePeer(client, userId), userId)
user = await resolveUser(client, userId)
} else {
user = { _: 'inputUserEmpty' }
}

View file

@ -3,8 +3,7 @@ import { tl } from '@mtcute/tl'
import { assertNever } from '../../../types/utils.js'
import { ITelegramClient } from '../../client.types.js'
import { BotCommands } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolvePeer, resolveUser } from '../users/resolve-peer.js'
/** @internal */
export async function _normalizeCommandScope(
@ -24,7 +23,7 @@ export async function _normalizeCommandScope(
}
}
case 'member': {
const user = toInputUser(await resolvePeer(client, scope.user), scope.user)
const user = await resolveUser(client, scope.user)
const chat = await resolvePeer(client, scope.chat)
return {

View file

@ -1,8 +1,7 @@
import { assertTrue } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
/**
* Sets information about a bot the current uzer owns (or the current bot)
@ -36,7 +35,7 @@ export async function setBotInfo(
const r = await client.call({
_: 'bots.setBotInfo',
bot: bot ? toInputUser(await resolvePeer(client, bot), bot) : undefined,
bot: bot ? await resolveUser(client, bot) : undefined,
langCode: langCode,
name,
about: bio,

View file

@ -3,8 +3,7 @@ import { tl } from '@mtcute/tl'
import { assertTrue } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
/**
* Sets a menu button for the given user.
@ -16,7 +15,7 @@ export async function setBotMenuButton(
): Promise<void> {
const r = await client.call({
_: 'bots.setBotMenuButton',
userId: toInputUser(await resolvePeer(client, user), user),
userId: await resolveUser(client, user),
button,
})

View file

@ -4,9 +4,8 @@ import { assertTrue } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { InputMessageId, InputPeerLike, Message, normalizeInputMessageId } from '../../types/index.js'
import { normalizeInlineId } from '../../utils/inline-utils.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { _findMessageInUpdate } from '../messages/find-in-update.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolvePeer, resolveUser } from '../users/resolve-peer.js'
/**
* Set a score of a user in a game
@ -45,7 +44,7 @@ export async function setGameScore(
const { userId, score, noEdit, force, shouldDispatch } = params
const { chatId, message } = normalizeInputMessageId(params)
const user = toInputUser(await resolvePeer(client, userId), userId)
const user = await resolveUser(client, userId)
const chat = await resolvePeer(client, chatId)
const res = await client.call({
@ -91,7 +90,7 @@ export async function setInlineGameScore(
): Promise<void> {
const { messageId, userId, score, noEdit, force } = params
const user = toInputUser(await resolvePeer(client, userId), userId)
const user = await resolveUser(client, userId)
const id = normalizeInlineId(messageId)

View file

@ -2,7 +2,7 @@ import { MaybeArray } from '../../../types/utils.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, MtInvalidPeerTypeError } from '../../types/index.js'
import { isInputPeerChannel, isInputPeerChat, toInputChannel, toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolvePeer, resolveUser } from '../users/resolve-peer.js'
import { resolvePeerMany } from '../users/resolve-peer-many.js'
/**
@ -33,7 +33,7 @@ export async function addChatMembers(
if (isInputPeerChat(chat)) {
for (const user of users) {
const p = toInputUser(await resolvePeer(client, user))
const p = await resolveUser(client, user)
const updates = await client.call({
_: 'messages.addChatUser',

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
// @alias=deleteSupergroup
/**
@ -12,7 +11,7 @@ import { resolvePeer } from '../users/resolve-peer.js'
export async function deleteChannel(client: ITelegramClient, chatId: InputPeerLike): Promise<void> {
const res = await client.call({
_: 'channels.deleteChannel',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
})
client.handleClientUpdate(res)
}

View file

@ -3,8 +3,7 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { createDummyUpdate } from '../../updates/utils.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel, resolvePeer } from '../users/resolve-peer.js'
/**
* Delete all messages of a user (or channel) in a supergroup
@ -20,7 +19,7 @@ export async function deleteUserHistory(
): Promise<void> {
const { chatId, participantId } = params
const channel = toInputChannel(await resolvePeer(client, chatId), chatId)
const channel = await resolveChannel(client, chatId)
const peer = await resolvePeer(client, participantId)

View file

@ -2,8 +2,7 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel, toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel, resolveUser } from '../users/resolve-peer.js'
/**
* Edit supergroup/channel admin rights of a user.
@ -23,8 +22,8 @@ export async function editAdminRights(
): Promise<void> {
const { chatId, userId, rights, rank = '' } = params
const chat = toInputChannel(await resolvePeer(client, chatId), chatId)
const user = toInputUser(await resolvePeer(client, userId), userId)
const chat = await resolveChannel(client, chatId)
const user = await resolveUser(client, userId)
const res = await client.call({
_: 'channels.editAdmin',

View file

@ -5,8 +5,8 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { ChatEvent, InputPeerLike, PeersIndex } from '../../types/index.js'
import { InputChatEventFilters, normalizeChatEventFilters } from '../../types/peers/chat-event/filters.js'
import { toInputChannel, toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolveChannel } from '../users/resolve-peer.js'
import { resolvePeerMany } from '../users/resolve-peer-many.js'
/**
@ -77,7 +77,7 @@ export async function getChatEventLog(
): Promise<ChatEvent[]> {
const { maxId = Long.ZERO, minId = Long.ZERO, query = '', limit = 100, users, filters } = params ?? {}
const channel = toInputChannel(await resolvePeer(client, chatId), chatId)
const channel = await resolveChannel(client, chatId)
const admins: tl.TypeInputUser[] | undefined = users ? await resolvePeerMany(client, users, toInputUser) : undefined

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import { ArrayWithTotal, Chat, InputPeerLike } from '../../types/index.js'
import { makeArrayWithTotal } from '../../utils/misc-utils.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
// @available=user
/**
@ -20,7 +19,7 @@ export async function getSimilarChannels(
): Promise<ArrayWithTotal<Chat>> {
const res = await client.call({
_: 'channels.getChannelRecommendations',
channel: toInputChannel(await resolvePeer(client, channel), channel),
channel: await resolveChannel(client, channel),
})
const parsed = res.chats.map((chat) => new Chat(chat))

View file

@ -5,8 +5,8 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { ChatEvent, InputPeerLike } from '../../types/index.js'
import { normalizeChatEventFilters } from '../../types/peers/chat-event/filters.js'
import { toInputChannel, toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolveChannel } from '../users/resolve-peer.js'
import { resolvePeerMany } from '../users/resolve-peer-many.js'
import { getChatEventLog } from './get-chat-event-log.js'
@ -40,7 +40,7 @@ export async function* iterChatEventLog(
): AsyncIterableIterator<ChatEvent> {
if (!params) params = {}
const channel = toInputChannel(await resolvePeer(client, chatId), chatId)
const channel = await resolveChannel(client, chatId)
const { minId = Long.ZERO, query = '', limit = Infinity, chunkSize = 100, users, filters } = params

View file

@ -1,8 +1,8 @@
import { ITelegramClient } from '../../client.types.js'
import { Chat, InputPeerLike } from '../../types/index.js'
import { assertIsUpdatesGroup } from '../../updates/utils.js'
import { INVITE_LINK_REGEX, toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { INVITE_LINK_REGEX } from '../../utils/peer-utils.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Join a channel or supergroup
@ -34,7 +34,7 @@ export async function joinChat(client: ITelegramClient, chatId: InputPeerLike):
const res = await client.call({
_: 'channels.joinChannel',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
})
assertIsUpdatesGroup('channels.joinChannel', res)

View file

@ -1,8 +1,7 @@
import { assertTrue } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Change supergroup/channel username
@ -19,7 +18,7 @@ export async function setChatUsername(
): Promise<void> {
const r = await client.call({
_: 'channels.updateUsername',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
username: username || '',
})

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Set supergroup's slow mode interval.
@ -15,7 +14,7 @@ import { resolvePeer } from '../users/resolve-peer.js'
export async function setSlowMode(client: ITelegramClient, chatId: InputPeerLike, seconds = 0): Promise<void> {
const res = await client.call({
_: 'channels.toggleSlowMode',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
seconds,
})
client.handleClientUpdate(res)

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Set whether a channel/supergroup has join requests enabled.
@ -19,7 +18,7 @@ export async function toggleJoinRequests(
): Promise<void> {
const res = await client.call({
_: 'channels.toggleJoinRequest',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
enabled,
})
client.handleClientUpdate(res)

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Set whether a channel/supergroup has join-to-send setting enabled.
@ -12,14 +11,10 @@ import { resolvePeer } from '../users/resolve-peer.js'
* @param chatId Chat ID or username
* @param enabled Whether join-to-send setting should be enabled
*/
export async function toggleJoinToSend(
client: ITelegramClient,
chatId: InputPeerLike,
enabled = false,
): Promise<void> {
export async function toggleJoinToSend(client: ITelegramClient, chatId: InputPeerLike, enabled = false): Promise<void> {
const res = await client.call({
_: 'channels.toggleJoinToSend',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
enabled,
})
client.handleClientUpdate(res)

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, User } from '../../types/index.js'
import { assertIsUpdatesGroup } from '../../updates/utils.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
/**
* Add an existing Telegram user as a contact
@ -38,7 +37,7 @@ export async function addContact(
},
): Promise<User> {
const { userId, firstName, lastName = '', phone = '', sharePhone = false } = params
const peer = toInputUser(await resolvePeer(client, userId), userId)
const peer = await resolveUser(client, userId)
const res = await client.call({
_: 'contacts.addContact',

View file

@ -3,9 +3,8 @@ import { tl } from '@mtcute/tl'
import { randomLong } from '../../../utils/long-utils.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, Message } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { _findMessageInUpdate } from '../messages/find-in-update.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel, resolvePeer } from '../users/resolve-peer.js'
/**
* Create a topic in a forum
@ -51,7 +50,7 @@ export async function createForumTopic(
const res = await client.call({
_: 'channels.createForumTopic',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
title,
iconColor: typeof icon === 'number' ? icon : undefined,
iconEmojiId: typeof icon !== 'number' ? icon : undefined,

View file

@ -2,8 +2,7 @@ import { assertTypeIsNot } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import type { ForumTopic, InputPeerLike } from '../../types/index.js'
import { createDummyUpdate } from '../../updates/utils.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Delete a forum topic and all its history
@ -16,7 +15,7 @@ export async function deleteForumTopicHistory(
chat: InputPeerLike,
topicId: number | ForumTopic,
): Promise<void> {
const channel = toInputChannel(await resolvePeer(client, chat), chat)
const channel = await resolveChannel(client, chat)
assertTypeIsNot('deleteForumTopicHistory', channel, 'inputChannelEmpty')
const res = await client.call({

View file

@ -4,9 +4,8 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import type { ForumTopic, InputPeerLike, Message } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { _findMessageInUpdate } from '../messages/find-in-update.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Modify a topic in a forum
@ -50,7 +49,7 @@ export async function editForumTopic(
const res = await client.call({
_: 'channels.editForumTopic',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
topicId: typeof topicId === 'number' ? topicId : topicId.id,
title,
iconEmojiId: icon ? icon ?? Long.ZERO : undefined,

View file

@ -1,8 +1,7 @@
import { MaybeArray } from '../../../types/utils.js'
import { ITelegramClient } from '../../client.types.js'
import { ForumTopic, InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/index.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Get forum topics by their IDs
@ -18,7 +17,7 @@ export async function getForumTopicsById(
const res = await client.call({
_: 'channels.getForumTopicsByID',
channel: toInputChannel(await resolvePeer(client, chatId)),
channel: await resolveChannel(client, chatId),
topics: ids,
})

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import { ArrayPaginated, ForumTopic, InputPeerLike } from '../../types/index.js'
import { makeArrayPaginated } from '../../utils/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
// @exported
export interface GetForumTopicsOffset {
@ -54,7 +53,7 @@ export async function getForumTopics(
const res = await client.call({
_: 'channels.getForumTopics',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
q: query,
offsetDate,
offsetId,

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { ForumTopic, InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
import { getForumTopics } from './get-forum-topics.js'
/**
@ -30,7 +29,7 @@ export async function* iterForumTopics(
const { query, limit = Infinity, chunkSize = 100 } = params
const peer = toInputChannel(await resolvePeer(client, chatId))
const peer = await resolveChannel(client, chatId)
let { offset } = params
let current = 0

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import type { ForumTopic, InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Reorder pinned forum topics
@ -28,7 +27,7 @@ export async function reorderPinnedForumTopics(
const { chatId, order, force } = params
await client.call({
_: 'channels.reorderPinnedForumTopics',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
order: order.map((it) => (typeof it === 'number' ? it : it.id)),
force,
})

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import type { ForumTopic, InputPeerLike, Message } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { _findMessageInUpdate } from '../messages/find-in-update.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Toggle open/close status of a topic in a forum
@ -34,7 +33,7 @@ export async function toggleForumTopicClosed(
const res = await client.call({
_: 'channels.editForumTopic',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
topicId: typeof topicId === 'number' ? topicId : topicId.id,
closed,
})

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { ForumTopic, InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Toggle whether a topic in a forum is pinned
@ -23,7 +22,7 @@ export async function toggleForumTopicPinned(
await client.call({
_: 'channels.updatePinnedForumTopic',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
topicId: typeof topicId === 'number' ? topicId : topicId.id,
pinned,
})

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Set whether a supergroup is a forum.
@ -14,7 +13,7 @@ import { resolvePeer } from '../users/resolve-peer.js'
export async function toggleForum(client: ITelegramClient, chatId: InputPeerLike, enabled = false): Promise<void> {
const res = await client.call({
_: 'channels.toggleForum',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
enabled,
})
client.handleClientUpdate(res)

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, Message } from '../../types/index.js'
import { toInputChannel } from '../../utils/peer-utils.js'
import { _findMessageInUpdate } from '../messages/find-in-update.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Toggle whether "General" topic in a forum is hidden or not
@ -29,7 +28,7 @@ export async function toggleGeneralTopicHidden(
const { chatId, hidden, shouldDispatch } = params
const res = await client.call({
_: 'channels.editForumTopic',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
topicId: 1,
hidden,
})

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import { ArrayPaginated, ChatInviteLink, InputPeerLike, PeersIndex } from '../../types/index.js'
import { makeArrayPaginated } from '../../utils/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolvePeer, resolveUser } from '../users/resolve-peer.js'
// @exported
export interface GetInviteLinksOffset {
@ -58,7 +57,7 @@ export async function getInviteLinks(
_: 'messages.getExportedChatInvites',
peer: await resolvePeer(client, chatId),
revoked,
adminId: admin ? toInputUser(await resolvePeer(client, admin), admin) : { _: 'inputUserSelf' },
adminId: admin ? await resolveUser(client, admin) : { _: 'inputUserSelf' },
limit,
offsetDate: offset?.date,
offsetLink: offset?.link,

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolvePeer, resolveUser } from '../users/resolve-peer.js'
/**
* Approve or decline join request to a chat.
@ -19,7 +18,7 @@ export async function hideJoinRequest(
): Promise<void> {
const { chatId, user, action } = params
const userId = toInputUser(await resolvePeer(client, user), user)
const userId = await resolveUser(client, user)
await client.call({
_: 'messages.hideChatJoinRequest',

View file

@ -2,8 +2,7 @@ import { tl } from '@mtcute/tl'
import { ITelegramClient } from '../../client.types.js'
import { InputText } from '../../types/misc/entities.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
const empty: [string, undefined] = ['', undefined]
@ -27,7 +26,7 @@ export async function _normalizeInputText(
for (const ent of entities) {
if (ent._ === 'messageEntityMentionName') {
try {
const inputPeer = toInputUser(await resolvePeer(client, ent.userId), ent.userId)
const inputPeer = await resolveUser(client, ent.userId)
const ent_ = ent as unknown as tl.RawInputMessageEntityMentionName
ent_._ = 'inputMessageEntityMentionName'

View file

@ -10,9 +10,8 @@ import {
StickerSourceType,
StickerType,
} from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { _normalizeFileToDocument } from '../files/normalize-file-to-document.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveUser } from '../users/resolve-peer.js'
/**
* Create a new sticker set.
@ -98,7 +97,7 @@ export async function createStickerSet(
progressCallback?: (idx: number, uploaded: number, total: number) => void
},
): Promise<StickerSet> {
const owner = toInputUser(await resolvePeer(client, params.owner), params.owner)
const owner = await resolveUser(client, params.owner)
const inputStickers: tl.TypeInputStickerSetItem[] = []

View file

@ -1,8 +1,7 @@
import { assertTrue } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, InputStickerSet, normalizeInputStickerSet } from '../../types/index.js'
import { toInputChannel } from '../../utils/index.js'
import { resolvePeer } from '../users/resolve-peer.js'
import { resolveChannel } from '../users/resolve-peer.js'
/**
* Set group sticker set for a supergroup
@ -19,7 +18,7 @@ export async function setChatStickerSet(
): Promise<void> {
const r = await client.call({
_: 'channels.setStickers',
channel: toInputChannel(await resolvePeer(client, chatId), chatId),
channel: await resolveChannel(client, chatId),
stickerset: normalizeInputStickerSet(setId),
})

View file

@ -1,7 +1,6 @@
import { ITelegramClient } from '../../client.types.js'
import { Chat, InputPeerLike } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from './resolve-peer.js'
import { resolveUser } from './resolve-peer.js'
/**
* Get a list of common chats you have with a given user
@ -13,7 +12,7 @@ export async function getCommonChats(client: ITelegramClient, userId: InputPeerL
return client
.call({
_: 'messages.getCommonChats',
userId: toInputUser(await resolvePeer(client, userId), userId),
userId: await resolveUser(client, userId),
maxId: 0,
limit: 100,
})

View file

@ -3,8 +3,7 @@ import { tl } from '@mtcute/tl'
import { assertTypeIs } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, Photo } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from './resolve-peer.js'
import { resolveUser } from './resolve-peer.js'
/**
* Get a single profile picture of a user by its ID
@ -20,7 +19,7 @@ export async function getProfilePhoto(
): Promise<Photo> {
const res = await client.call({
_: 'photos.getUserPhotos',
userId: toInputUser(await resolvePeer(client, userId), userId),
userId: await resolveUser(client, userId),
offset: -1,
limit: 1,
maxId: photoId,

View file

@ -6,8 +6,7 @@ import { assertTypeIs } from '../../../utils/type-assertions.js'
import { ITelegramClient } from '../../client.types.js'
import { ArrayPaginated, InputPeerLike, Photo } from '../../types/index.js'
import { makeArrayPaginated } from '../../utils/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { resolvePeer } from './resolve-peer.js'
import { resolveUser } from './resolve-peer.js'
/**
* Get a list of profile pictures of a user
@ -40,7 +39,7 @@ export async function getProfilePhotos(
const res = await client.call({
_: 'photos.getUserPhotos',
userId: toInputUser(await resolvePeer(client, userId), userId),
userId: await resolveUser(client, userId),
offset,
limit,
maxId: Long.ZERO,

View file

@ -3,7 +3,7 @@ import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, User } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { _getUsersBatched } from '../chats/batched-queries.js'
import { resolvePeer } from './resolve-peer.js'
import { resolveUser } from './resolve-peer.js'
import { resolvePeerMany } from './resolve-peer-many.js'
/**
@ -17,7 +17,7 @@ import { resolvePeerMany } from './resolve-peer-many.js'
export async function getUsers(client: ITelegramClient, ids: MaybeArray<InputPeerLike>): Promise<(User | null)[]> {
if (!Array.isArray(ids)) {
// avoid unnecessary overhead of Promise.all and resolvePeerMany
const res = await _getUsersBatched(client, toInputUser(await resolvePeer(client, ids)))
const res = await _getUsersBatched(client, await resolveUser(client, ids))
return [res ? new User(res) : null]
}

View file

@ -1,8 +1,7 @@
import { ITelegramClient } from '../../client.types.js'
import { InputPeerLike, Photo } from '../../types/index.js'
import { toInputUser } from '../../utils/peer-utils.js'
import { getProfilePhotos } from './get-profile-photos.js'
import { resolvePeer } from './resolve-peer.js'
import { resolveUser } from './resolve-peer.js'
/**
* Iterate over profile photos
@ -31,7 +30,7 @@ export async function* iterProfilePhotos(
): AsyncIterableIterator<Photo> {
if (!params) params = {}
const peer = toInputUser(await resolvePeer(client, userId), userId)
const peer = await resolveUser(client, userId)
const { limit = Infinity, chunkSize = 100 } = params

View file

@ -7,7 +7,7 @@ import { getMarkedPeerId, parseMarkedPeerId, toggleChannelIdMark } from '../../.
import { ITelegramClient } from '../../client.types.js'
import { MtPeerNotFoundError } from '../../types/errors.js'
import { InputPeerLike } from '../../types/peers/index.js'
import { toInputPeer } from '../../utils/peer-utils.js'
import { toInputChannel, toInputPeer, toInputUser } from '../../utils/peer-utils.js'
// @available=both
/**
@ -159,3 +159,25 @@ export async function resolvePeer(
}
}
}
/**
* Shorthand for `resolvePeer` that converts the input peer to `InputUser`.
*/
export async function resolveUser(
client: ITelegramClient,
peerId: InputPeerLike,
force = false,
): Promise<tl.TypeInputUser> {
return toInputUser(await resolvePeer(client, peerId, force), peerId)
}
/**
* Shorthand for `resolvePeer` that converts the input peer to `InputChannel`.
*/
export async function resolveChannel(
client: ITelegramClient,
peerId: InputPeerLike,
force = false,
): Promise<tl.TypeInputChannel> {
return toInputChannel(await resolvePeer(client, peerId, force), peerId)
}

View file

@ -184,6 +184,9 @@ export class RepliedMessageInfo {
/**
* Offset of the start of the {@link quoteText} in the replied-to message.
*
* Note that this offset should only be used as a hint, as the actual
* quote offset may be different due to message being edited after the quote
*
* `null` if not available, in which case it should be assumed that the quote
* starts at `.indexOf(quoteText)` of the replied-to message text.
*/