feat(client): sequential message sending

This commit is contained in:
alina 🌸 2023-12-11 01:55:58 +03:00
parent ce2d1b0a9a
commit ee9e2e35c4
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 68 additions and 47 deletions

View file

@ -5,6 +5,7 @@ import { Message } from '../../types/messages/message.js'
import { TextWithEntities } from '../../types/misc/entities.js' import { TextWithEntities } from '../../types/misc/entities.js'
import { InputPeerLike } from '../../types/peers/index.js' import { InputPeerLike } from '../../types/peers/index.js'
import { normalizeDate, normalizeMessageId, toInputUser } from '../../utils/index.js' import { normalizeDate, normalizeMessageId, toInputUser } from '../../utils/index.js'
import { _getPeerChainId } from '../misc/chain-id.js'
import { _normalizeInputText } from '../misc/normalize-text.js' import { _normalizeInputText } from '../misc/normalize-text.js'
import { resolvePeer } from '../users/resolve-peer.js' import { resolvePeer } from '../users/resolve-peer.js'
import { _getDiscussionMessage } from './get-discussion-message.js' import { _getDiscussionMessage } from './get-discussion-message.js'
@ -168,5 +169,6 @@ export async function _processCommonSendParameters(
peer, peer,
replyTo: tlReplyTo, replyTo: tlReplyTo,
scheduleDate, scheduleDate,
chainId: _getPeerChainId(client, peer, 'send'),
} }
} }

View file

@ -49,7 +49,7 @@ export async function sendMediaGroup(
): Promise<Message[]> { ): Promise<Message[]> {
if (!params) params = {} if (!params) params = {}
const { peer, replyTo, scheduleDate } = await _processCommonSendParameters(client, chatId, params) const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const multiMedia: tl.RawInputSingleMedia[] = [] const multiMedia: tl.RawInputSingleMedia[] = []
@ -92,18 +92,21 @@ export async function sendMediaGroup(
}) })
} }
const res = await client.call({ const res = await client.call(
_: 'messages.sendMultiMedia', {
peer, _: 'messages.sendMultiMedia',
multiMedia, peer,
silent: params.silent, multiMedia,
replyTo, silent: params.silent,
scheduleDate, replyTo,
clearDraft: params.clearDraft, scheduleDate,
noforwards: params.forbidForwards, clearDraft: params.clearDraft,
sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined, noforwards: params.forbidForwards,
invertMedia: params.invertMedia, sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined,
}) invertMedia: params.invertMedia,
},
{ chainId },
)
assertIsUpdatesGroup('sendMediaGroup', res) assertIsUpdatesGroup('sendMediaGroup', res)
client.network.handleUpdate(res, true) client.network.handleUpdate(res, true)

View file

@ -81,24 +81,27 @@ export async function sendMedia(
) )
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup) const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const { peer, replyTo, scheduleDate } = await _processCommonSendParameters(client, chatId, params) const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const res = await client.call({ const res = await client.call(
_: 'messages.sendMedia', {
peer, _: 'messages.sendMedia',
media: inputMedia, peer,
silent: params.silent, media: inputMedia,
replyTo, silent: params.silent,
randomId: randomLong(), replyTo,
scheduleDate, randomId: randomLong(),
replyMarkup, scheduleDate,
message, replyMarkup,
entities, message,
clearDraft: params.clearDraft, entities,
noforwards: params.forbidForwards, clearDraft: params.clearDraft,
sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined, noforwards: params.forbidForwards,
invertMedia: params.invert, sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined,
}) invertMedia: params.invert,
},
{ chainId },
)
const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch) const msg = _findMessageInUpdate(client, res, false, !params.shouldDispatch)

View file

@ -52,24 +52,27 @@ export async function sendText(
const [message, entities] = await _normalizeInputText(client, text) const [message, entities] = await _normalizeInputText(client, text)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup) const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const { peer, replyTo, scheduleDate } = await _processCommonSendParameters(client, chatId, params) const { peer, replyTo, scheduleDate, chainId } = await _processCommonSendParameters(client, chatId, params)
const res = await client.call({ const res = await client.call(
_: 'messages.sendMessage', {
peer, _: 'messages.sendMessage',
noWebpage: params.disableWebPreview, peer,
silent: params.silent, noWebpage: params.disableWebPreview,
replyTo, silent: params.silent,
randomId: randomLong(), replyTo,
scheduleDate, randomId: randomLong(),
replyMarkup, scheduleDate,
message, replyMarkup,
entities, message,
clearDraft: params.clearDraft, entities,
noforwards: params.forbidForwards, clearDraft: params.clearDraft,
sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined, noforwards: params.forbidForwards,
invertMedia: params.invertMedia, sendAs: params.sendAs ? await resolvePeer(client, params.sendAs) : undefined,
}) invertMedia: params.invertMedia,
},
{ chainId },
)
if (res._ === 'updateShortSentMessage') { if (res._ === 'updateShortSentMessage') {
// todo extract this to updates manager? // todo extract this to updates manager?

View file

@ -0,0 +1,10 @@
import { BaseTelegramClient, getMarkedPeerId, tl } from '@mtcute/core'
import { getAuthState } from '../auth/_state.js'
/** @internal */
export function _getPeerChainId(client: BaseTelegramClient, peer: tl.TypeInputPeer, prefix = 'peer') {
const id = peer._ === 'inputPeerSelf' ? getAuthState(client).userId! : getMarkedPeerId(peer)
return `${prefix}:${id}`
}