fix(client): limit number of messages in forwardMessages method

This commit is contained in:
teidesu 2021-04-24 19:56:09 +03:00
parent cf29ffe31d
commit df2d77ec90

View file

@ -3,11 +3,15 @@ import {
InputMediaLike, InputMediaLike,
InputPeerLike, InputPeerLike,
Message, Message,
MtCuteArgumentError, MtCuteTypeAssertionError, MtCuteArgumentError,
MtCuteTypeAssertionError,
} from '../../types' } from '../../types'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils' import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { normalizeDate, randomUlong } from '../../utils/misc-utils' import { normalizeDate, randomUlong } from '../../utils/misc-utils'
/** /**
@ -43,6 +47,7 @@ export async function forwardMessages(
/** /**
* Forward one or more messages, optionally including a caption message. * Forward one or more messages, optionally including a caption message.
* You can forward no more than 100 messages at once.
* *
* If a caption message was sent, it will be the first message in the resulting array. * If a caption message was sent, it will be the first message in the resulting array.
* *
@ -180,6 +185,14 @@ export async function forwardMessages(
const isSingle = !Array.isArray(messages) const isSingle = !Array.isArray(messages)
if (isSingle) messages = [messages as number] if (isSingle) messages = [messages as number]
// sending more than 100 will not result in a server-sent
// error, instead only first 100 IDs will be forwarded,
// which is definitely not the best outcome.
if ((messages as number[]).length > 100)
throw new MtCuteArgumentError(
'You can forward no more than 100 messages at once'
)
const toPeer = normalizeToInputPeer(await this.resolvePeer(toChatId)) const toPeer = normalizeToInputPeer(await this.resolvePeer(toChatId))
let captionMessage: Message | null = null let captionMessage: Message | null = null
@ -230,7 +243,11 @@ export async function forwardMessages(
const forwarded: Message[] = [] const forwarded: Message[] = []
res.updates.forEach((upd) => { res.updates.forEach((upd) => {
if (upd._ === 'updateNewMessage' || upd._ == 'updateNewChannelMessage' || upd._ === 'updateNewScheduledMessage') { if (
upd._ === 'updateNewMessage' ||
upd._ == 'updateNewChannelMessage' ||
upd._ === 'updateNewScheduledMessage'
) {
forwarded.push(new Message(this, upd.message, users, chats)) forwarded.push(new Message(this, upd.message, users, chats))
} }
}) })