fix(core): check for message id in _findMessageInUpdate (#23)

This commit is contained in:
alina sireneva 2024-03-17 06:47:45 +03:00 committed by GitHub
commit 999c18c616
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 15 deletions

View file

@ -1,4 +1,4 @@
/* eslint-disable max-params */
/* eslint-disable max-params,no-lonely-if */
import { tl } from '@mtcute/tl'
import { MtTypeAssertionError } from '../../../types/errors.js'
@ -17,6 +17,7 @@ export function _findMessageInUpdate(
isEdit?: boolean,
noDispatch?: boolean,
allowNull?: false,
randomId?: tl.Long,
): Message
/**
* @internal
@ -28,6 +29,7 @@ export function _findMessageInUpdate(
isEdit?: boolean,
noDispatch?: boolean,
allowNull?: true,
randomId?: tl.Long,
): Message | null
/**
@ -40,23 +42,40 @@ export function _findMessageInUpdate(
isEdit = false,
noDispatch = true,
allowNull = false,
randomId?: tl.Long,
): Message | null {
assertIsUpdatesGroup('_findMessageInUpdate', res)
client.handleClientUpdate(res, noDispatch)
for (const u of res.updates) {
if (
(isEdit && (u._ === 'updateEditMessage' || u._ === 'updateEditChannelMessage')) ||
(!isEdit &&
(u._ === 'updateNewMessage' ||
u._ === 'updateNewChannelMessage' ||
u._ === 'updateNewScheduledMessage'))
) {
const peers = PeersIndex.from(res)
let ourMessageId = 0
return new Message(u.message, peers, u._ === 'updateNewScheduledMessage')
for (const u of res.updates) {
if (randomId && u._ === 'updateMessageID' && u.randomId.eq(randomId)) {
ourMessageId = u.id
continue
}
if (isEdit) {
if (!(u._ === 'updateEditMessage' || u._ === 'updateEditChannelMessage')) continue
} else {
if (
!(
u._ === 'updateNewMessage' ||
u._ === 'updateNewChannelMessage' ||
u._ === 'updateNewScheduledMessage'
)
) { continue }
}
// this *may* break if updateMessageID comes after the message update
// but it's unlikely and is not worth the effort to fix
// we should eventually move to properly handling updateMessageID
if (ourMessageId !== 0 && u.message.id !== ourMessageId) continue
const peers = PeersIndex.from(res)
return new Message(u.message, peers, u._ === 'updateNewScheduledMessage')
}
if (allowNull) return null

View file

@ -82,6 +82,7 @@ export async function sendMedia(
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const randomId = randomLong()
const res = await client.call(
{
_: 'messages.sendMedia',
@ -89,7 +90,7 @@ export async function sendMedia(
media: inputMedia,
silent: params.silent,
replyTo,
randomId: randomLong(),
randomId,
scheduleDate,
replyMarkup,
message,
@ -102,7 +103,7 @@ export async function sendMedia(
{ chainId },
)
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch)
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch, false, randomId)
return msg
}

View file

@ -56,6 +56,7 @@ export async function sendText(
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const randomId = randomLong()
const res = await client.call(
{
_: 'messages.sendMessage',
@ -63,7 +64,7 @@ export async function sendText(
noWebpage: params.disableWebPreview,
silent: params.silent,
replyTo,
randomId: randomLong(),
randomId,
scheduleDate,
replyMarkup,
message,
@ -135,7 +136,7 @@ export async function sendText(
return ret
}
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch)
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch, false, randomId)
return msg
}