fix(core): check for message id in _findMessageInUpdate (#23)
This commit is contained in:
commit
999c18c616
3 changed files with 36 additions and 15 deletions
|
@ -1,4 +1,4 @@
|
||||||
/* eslint-disable max-params */
|
/* eslint-disable max-params,no-lonely-if */
|
||||||
import { tl } from '@mtcute/tl'
|
import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
import { MtTypeAssertionError } from '../../../types/errors.js'
|
import { MtTypeAssertionError } from '../../../types/errors.js'
|
||||||
|
@ -17,6 +17,7 @@ export function _findMessageInUpdate(
|
||||||
isEdit?: boolean,
|
isEdit?: boolean,
|
||||||
noDispatch?: boolean,
|
noDispatch?: boolean,
|
||||||
allowNull?: false,
|
allowNull?: false,
|
||||||
|
randomId?: tl.Long,
|
||||||
): Message
|
): Message
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
|
@ -28,6 +29,7 @@ export function _findMessageInUpdate(
|
||||||
isEdit?: boolean,
|
isEdit?: boolean,
|
||||||
noDispatch?: boolean,
|
noDispatch?: boolean,
|
||||||
allowNull?: true,
|
allowNull?: true,
|
||||||
|
randomId?: tl.Long,
|
||||||
): Message | null
|
): Message | null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,23 +42,40 @@ export function _findMessageInUpdate(
|
||||||
isEdit = false,
|
isEdit = false,
|
||||||
noDispatch = true,
|
noDispatch = true,
|
||||||
allowNull = false,
|
allowNull = false,
|
||||||
|
randomId?: tl.Long,
|
||||||
): Message | null {
|
): Message | null {
|
||||||
assertIsUpdatesGroup('_findMessageInUpdate', res)
|
assertIsUpdatesGroup('_findMessageInUpdate', res)
|
||||||
|
|
||||||
client.handleClientUpdate(res, noDispatch)
|
client.handleClientUpdate(res, noDispatch)
|
||||||
|
|
||||||
for (const u of res.updates) {
|
let ourMessageId = 0
|
||||||
if (
|
|
||||||
(isEdit && (u._ === 'updateEditMessage' || u._ === 'updateEditChannelMessage')) ||
|
|
||||||
(!isEdit &&
|
|
||||||
(u._ === 'updateNewMessage' ||
|
|
||||||
u._ === 'updateNewChannelMessage' ||
|
|
||||||
u._ === 'updateNewScheduledMessage'))
|
|
||||||
) {
|
|
||||||
const peers = PeersIndex.from(res)
|
|
||||||
|
|
||||||
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
|
if (allowNull) return null
|
||||||
|
|
|
@ -82,6 +82,7 @@ export async function sendMedia(
|
||||||
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
||||||
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
|
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
|
||||||
|
|
||||||
|
const randomId = randomLong()
|
||||||
const res = await client.call(
|
const res = await client.call(
|
||||||
{
|
{
|
||||||
_: 'messages.sendMedia',
|
_: 'messages.sendMedia',
|
||||||
|
@ -89,7 +90,7 @@ export async function sendMedia(
|
||||||
media: inputMedia,
|
media: inputMedia,
|
||||||
silent: params.silent,
|
silent: params.silent,
|
||||||
replyTo,
|
replyTo,
|
||||||
randomId: randomLong(),
|
randomId,
|
||||||
scheduleDate,
|
scheduleDate,
|
||||||
replyMarkup,
|
replyMarkup,
|
||||||
message,
|
message,
|
||||||
|
@ -102,7 +103,7 @@ export async function sendMedia(
|
||||||
{ chainId },
|
{ chainId },
|
||||||
)
|
)
|
||||||
|
|
||||||
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch)
|
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch, false, randomId)
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,7 @@ export async function sendText(
|
||||||
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
||||||
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
|
const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
|
||||||
|
|
||||||
|
const randomId = randomLong()
|
||||||
const res = await client.call(
|
const res = await client.call(
|
||||||
{
|
{
|
||||||
_: 'messages.sendMessage',
|
_: 'messages.sendMessage',
|
||||||
|
@ -63,7 +64,7 @@ export async function sendText(
|
||||||
noWebpage: params.disableWebPreview,
|
noWebpage: params.disableWebPreview,
|
||||||
silent: params.silent,
|
silent: params.silent,
|
||||||
replyTo,
|
replyTo,
|
||||||
randomId: randomLong(),
|
randomId,
|
||||||
scheduleDate,
|
scheduleDate,
|
||||||
replyMarkup,
|
replyMarkup,
|
||||||
message,
|
message,
|
||||||
|
@ -135,7 +136,7 @@ export async function sendText(
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch)
|
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch, false, randomId)
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue