diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 15bf77d5..7fac26c4 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -224,6 +224,7 @@ import { InputInlineResult, InputMediaLike, InputPeerLike, + InputReaction, InputStickerSetItem, MaybeDynamic, Message, @@ -1216,7 +1217,7 @@ export interface TelegramClient extends BaseTelegramClient { * Defaults to `''` (empty string) * * > **Note**: Only used for these values of `filter`: - * > `all`, `banned`, `restricted`, `contacts` + * > `all, banned, restricted, mention, contacts` */ query?: string @@ -2674,12 +2675,7 @@ export interface TelegramClient extends BaseTelegramClient { /** * Get only reactions with the specified emoji */ - emoji?: string - - /** - * Get only reactions with the specified custom emoji - */ - customEmoji?: tl.Long + emoji?: InputReaction /** * Limit the number of events returned. @@ -3186,16 +3182,11 @@ export interface TelegramClient extends BaseTelegramClient { * * @param chatId Chat ID with the message to react to * @param message Message ID to react to - * @param emoji Reaction emoji (if `tl.Long` then this is a custom emoji) or `null` to remove + * @param emoji Reaction emoji (or `null` to remove reaction) * @param big (default: `false`) Whether to use a big reaction * @returns Message to which the reaction was sent */ - sendReaction( - chatId: InputPeerLike, - message: number, - emoji: string | tl.Long | null, - big?: boolean, - ): Promise + sendReaction(chatId: InputPeerLike, message: number, emoji?: InputReaction | null, big?: boolean): Promise /** * Send s previously scheduled message. * diff --git a/packages/client/src/methods/_imports.ts b/packages/client/src/methods/_imports.ts index 1bd68c42..f15fc22b 100644 --- a/packages/client/src/methods/_imports.ts +++ b/packages/client/src/methods/_imports.ts @@ -41,6 +41,7 @@ import { InputInlineResult, InputMediaLike, InputPeerLike, + InputReaction, InputStickerSetItem, MaybeDynamic, Message, diff --git a/packages/client/src/methods/messages/get-reaction-users.ts b/packages/client/src/methods/messages/get-reaction-users.ts index e7c5c328..2d7250d0 100644 --- a/packages/client/src/methods/messages/get-reaction-users.ts +++ b/packages/client/src/methods/messages/get-reaction-users.ts @@ -1,7 +1,7 @@ import { tl } from '@mtcute/core' import { TelegramClient } from '../../client' -import { InputPeerLike, PeerReaction, PeersIndex } from '../../types' +import { InputPeerLike, InputReaction, normalizeInputReaction, PeerReaction, PeersIndex } from '../../types' /** * Get users who have reacted to the message. @@ -19,12 +19,7 @@ export async function* getReactionUsers( /** * Get only reactions with the specified emoji */ - emoji?: string - - /** - * Get only reactions with the specified custom emoji - */ - customEmoji?: tl.Long + emoji?: InputReaction /** * Limit the number of events returned. @@ -50,23 +45,7 @@ export async function* getReactionUsers( const total = params.limit || Infinity const chunkSize = Math.min(params.chunkSize ?? 100, total) - let reaction: tl.TypeReaction - - if (params.customEmoji) { - reaction = { - _: 'reactionCustomEmoji', - documentId: params.customEmoji, - } - } else if (params.emoji) { - reaction = { - _: 'reactionEmoji', - emoticon: params.emoji, - } - } else { - reaction = { - _: 'reactionEmpty', - } - } + const reaction = normalizeInputReaction(params.emoji) for (;;) { const res: tl.RpcCallReturn['messages.getMessageReactionsList'] = await this.call({ diff --git a/packages/client/src/methods/messages/send-reaction.ts b/packages/client/src/methods/messages/send-reaction.ts index 840ed88a..bd3aec5b 100644 --- a/packages/client/src/methods/messages/send-reaction.ts +++ b/packages/client/src/methods/messages/send-reaction.ts @@ -1,9 +1,5 @@ -import Long from 'long' - -import { MtTypeAssertionError, tl } from '@mtcute/core' - import { TelegramClient } from '../../client' -import { InputPeerLike, Message, PeersIndex } from '../../types' +import { InputPeerLike, InputReaction, Message, normalizeInputReaction } from '../../types' import { assertIsUpdatesGroup } from '../../utils/updates-utils' /** @@ -11,7 +7,7 @@ import { assertIsUpdatesGroup } from '../../utils/updates-utils' * * @param chatId Chat ID with the message to react to * @param message Message ID to react to - * @param emoji Reaction emoji (if `tl.Long` then this is a custom emoji) or `null` to remove + * @param emoji Reaction emoji (or `null` to remove reaction) * @param big Whether to use a big reaction * @returns Message to which the reaction was sent * @internal @@ -20,26 +16,10 @@ export async function sendReaction( this: TelegramClient, chatId: InputPeerLike, message: number, - emoji: string | tl.Long | null, + emoji?: InputReaction | null, big = false, ): Promise { - let reaction: tl.TypeReaction - - if (Long.isLong(emoji)) { - reaction = { - _: 'reactionCustomEmoji', - documentId: emoji, - } - } else if (emoji) { - reaction = { - _: 'reactionEmoji', - emoticon: emoji, - } - } else { - reaction = { - _: 'reactionEmpty', - } - } + const reaction = normalizeInputReaction(emoji) const res = await this.call({ _: 'messages.sendReaction', @@ -57,17 +37,5 @@ export async function sendReaction( // idk why, they contain literally the same data // so we can just return the message from the first one - this._handleUpdate(res, true) - - const upd = res.updates.find((it) => it._ === 'updateEditChannelMessage') as - | tl.RawUpdateEditChannelMessage - | undefined - - if (!upd) { - throw new MtTypeAssertionError('messages.sendReaction (@ .updates[*])', 'updateEditChannelMessage', 'undefined') - } - - const peers = PeersIndex.from(res) - - return new Message(this, upd.message, peers) + return this._findMessageInUpdate(res, true) } diff --git a/packages/client/src/types/messages/reactions.ts b/packages/client/src/types/messages/reactions.ts index 8fb81c60..19199ed0 100644 --- a/packages/client/src/types/messages/reactions.ts +++ b/packages/client/src/types/messages/reactions.ts @@ -5,6 +5,31 @@ import { TelegramClient } from '../../client' import { makeInspectable } from '../../utils' import { PeersIndex, User } from '../peers' +/** + * Emoji describing a reaction. + * + * Either a `string` with a unicode emoji, or a `tl.Long` for a custom emoji + */ +export type InputReaction = string | tl.Long + +export function normalizeInputReaction(reaction?: InputReaction | null): tl.TypeReaction { + if (typeof reaction === 'string') { + return { + _: 'reactionEmoji', + emoticon: reaction, + } + } else if (reaction) { + return { + _: 'reactionCustomEmoji', + documentId: reaction, + } + } + + return { + _: 'reactionEmpty', + } +} + export class PeerReaction { constructor( readonly client: TelegramClient,