refactor: extract input reactions to InputReaction

This commit is contained in:
alina 🌸 2023-10-01 16:36:29 +03:00
parent 95032d3b9a
commit 55c4f296fb
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 39 additions and 75 deletions

View file

@ -224,6 +224,7 @@ import {
InputInlineResult, InputInlineResult,
InputMediaLike, InputMediaLike,
InputPeerLike, InputPeerLike,
InputReaction,
InputStickerSetItem, InputStickerSetItem,
MaybeDynamic, MaybeDynamic,
Message, Message,
@ -1216,7 +1217,7 @@ export interface TelegramClient extends BaseTelegramClient {
* Defaults to `''` (empty string) * Defaults to `''` (empty string)
* *
* > **Note**: Only used for these values of `filter`: * > **Note**: Only used for these values of `filter`:
* > `all`, `banned`, `restricted`, `contacts` * > `all, banned, restricted, mention, contacts`
*/ */
query?: string query?: string
@ -2674,12 +2675,7 @@ export interface TelegramClient extends BaseTelegramClient {
/** /**
* Get only reactions with the specified emoji * Get only reactions with the specified emoji
*/ */
emoji?: string emoji?: InputReaction
/**
* Get only reactions with the specified custom emoji
*/
customEmoji?: tl.Long
/** /**
* Limit the number of events returned. * 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 chatId Chat ID with the message to react to
* @param message Message ID 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 * @param big (default: `false`) Whether to use a big reaction
* @returns Message to which the reaction was sent * @returns Message to which the reaction was sent
*/ */
sendReaction( sendReaction(chatId: InputPeerLike, message: number, emoji?: InputReaction | null, big?: boolean): Promise<Message>
chatId: InputPeerLike,
message: number,
emoji: string | tl.Long | null,
big?: boolean,
): Promise<Message>
/** /**
* Send s previously scheduled message. * Send s previously scheduled message.
* *

View file

@ -41,6 +41,7 @@ import {
InputInlineResult, InputInlineResult,
InputMediaLike, InputMediaLike,
InputPeerLike, InputPeerLike,
InputReaction,
InputStickerSetItem, InputStickerSetItem,
MaybeDynamic, MaybeDynamic,
Message, Message,

View file

@ -1,7 +1,7 @@
import { tl } from '@mtcute/core' import { tl } from '@mtcute/core'
import { TelegramClient } from '../../client' 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. * Get users who have reacted to the message.
@ -19,12 +19,7 @@ export async function* getReactionUsers(
/** /**
* Get only reactions with the specified emoji * Get only reactions with the specified emoji
*/ */
emoji?: string emoji?: InputReaction
/**
* Get only reactions with the specified custom emoji
*/
customEmoji?: tl.Long
/** /**
* Limit the number of events returned. * Limit the number of events returned.
@ -50,23 +45,7 @@ export async function* getReactionUsers(
const total = params.limit || Infinity const total = params.limit || Infinity
const chunkSize = Math.min(params.chunkSize ?? 100, total) const chunkSize = Math.min(params.chunkSize ?? 100, total)
let reaction: tl.TypeReaction const reaction = normalizeInputReaction(params.emoji)
if (params.customEmoji) {
reaction = {
_: 'reactionCustomEmoji',
documentId: params.customEmoji,
}
} else if (params.emoji) {
reaction = {
_: 'reactionEmoji',
emoticon: params.emoji,
}
} else {
reaction = {
_: 'reactionEmpty',
}
}
for (;;) { for (;;) {
const res: tl.RpcCallReturn['messages.getMessageReactionsList'] = await this.call({ const res: tl.RpcCallReturn['messages.getMessageReactionsList'] = await this.call({

View file

@ -1,9 +1,5 @@
import Long from 'long'
import { MtTypeAssertionError, tl } from '@mtcute/core'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike, Message, PeersIndex } from '../../types' import { InputPeerLike, InputReaction, Message, normalizeInputReaction } from '../../types'
import { assertIsUpdatesGroup } from '../../utils/updates-utils' 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 chatId Chat ID with the message to react to
* @param message Message ID 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 * @param big Whether to use a big reaction
* @returns Message to which the reaction was sent * @returns Message to which the reaction was sent
* @internal * @internal
@ -20,26 +16,10 @@ export async function sendReaction(
this: TelegramClient, this: TelegramClient,
chatId: InputPeerLike, chatId: InputPeerLike,
message: number, message: number,
emoji: string | tl.Long | null, emoji?: InputReaction | null,
big = false, big = false,
): Promise<Message> { ): Promise<Message> {
let reaction: tl.TypeReaction const reaction = normalizeInputReaction(emoji)
if (Long.isLong(emoji)) {
reaction = {
_: 'reactionCustomEmoji',
documentId: emoji,
}
} else if (emoji) {
reaction = {
_: 'reactionEmoji',
emoticon: emoji,
}
} else {
reaction = {
_: 'reactionEmpty',
}
}
const res = await this.call({ const res = await this.call({
_: 'messages.sendReaction', _: 'messages.sendReaction',
@ -57,17 +37,5 @@ export async function sendReaction(
// idk why, they contain literally the same data // idk why, they contain literally the same data
// so we can just return the message from the first one // so we can just return the message from the first one
this._handleUpdate(res, true) return this._findMessageInUpdate(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)
} }

View file

@ -5,6 +5,31 @@ import { TelegramClient } from '../../client'
import { makeInspectable } from '../../utils' import { makeInspectable } from '../../utils'
import { PeersIndex, User } from '../peers' 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 { export class PeerReaction {
constructor( constructor(
readonly client: TelegramClient, readonly client: TelegramClient,