feat(core): shouldDispatch option for methods that use dummy updates (#65)

This commit is contained in:
alina sireneva 2024-07-18 08:25:06 -07:00 committed by GitHub
commit 40baa6dd5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 124 additions and 25 deletions

View file

@ -1531,6 +1531,12 @@ export interface TelegramClient extends ITelegramClient {
chatId: InputPeerLike
/** User/channel ID whose messages to delete */
participantId: InputPeerLike
/**
* Whether to dispatch the updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
}): Promise<void>
/**
* Edit supergroup/channel admin rights of a user.
@ -2668,7 +2674,17 @@ export interface TelegramClient extends ITelegramClient {
* @param chat Chat or user ID, username, phone number, `"me"` or `"self"`
* @param topicId ID of the topic (i.e. its top message ID)
*/
deleteForumTopicHistory(chat: InputPeerLike, topicId: number | ForumTopic): Promise<void>
deleteForumTopicHistory(
chat: InputPeerLike,
topicId: number | ForumTopic,
params?: {
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void>
/**
* Modify a topic in a forum
*
@ -3756,6 +3772,12 @@ export interface TelegramClient extends ITelegramClient {
* Whether to also clear all mentions in the chat
*/
clearMentions?: boolean
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void>
/**
@ -3765,7 +3787,16 @@ export interface TelegramClient extends ITelegramClient {
*
* @param chatId Chat ID
*/
readReactions(chatId: InputPeerLike): Promise<void>
readReactions(
chatId: InputPeerLike,
params?: {
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void>
/**
* Search for messages globally from all of your chats
*
@ -4333,6 +4364,12 @@ export interface TelegramClient extends ITelegramClient {
* For forums - unpin only messages from the given topic
*/
topicId?: number
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void>
/**

View file

@ -15,9 +15,15 @@ export async function deleteUserHistory(
chatId: InputPeerLike
/** User/channel ID whose messages to delete */
participantId: InputPeerLike
/**
* Whether to dispatch the updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void> {
const { chatId, participantId } = params
const { chatId, participantId, shouldDispatch } = params
const channel = await resolveChannel(client, chatId)
@ -29,5 +35,7 @@ export async function deleteUserHistory(
participant: peer,
})
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, (channel as tl.RawInputChannel).channelId))
if (!shouldDispatch) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, (channel as tl.RawInputChannel).channelId))
}
}

View file

@ -14,7 +14,16 @@ export async function deleteForumTopicHistory(
client: ITelegramClient,
chat: InputPeerLike,
topicId: number | ForumTopic,
params?: {
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void> {
const { shouldDispatch } = params ?? {}
const channel = await resolveChannel(client, chat)
assertTypeIsNot('deleteForumTopicHistory', channel, 'inputChannelEmpty')
@ -24,5 +33,7 @@ export async function deleteForumTopicHistory(
topMsgId: typeof topicId === 'number' ? topicId : topicId.id,
})
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, channel.channelId))
if (!shouldDispatch) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, channel.channelId))
}
}

View file

@ -16,6 +16,12 @@ export interface DeleteMessagesParams {
* @default true
*/
revoke?: boolean
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
}
/**
@ -30,7 +36,7 @@ export async function deleteMessagesById(
ids: number[],
params?: DeleteMessagesParams,
): Promise<void> {
const { revoke = true } = params ?? {}
const { revoke = true, shouldDispatch } = params ?? {}
const peer = await resolvePeer(client, chatId)
@ -53,7 +59,9 @@ export async function deleteMessagesById(
upd = createDummyUpdate(res.pts, res.ptsCount)
}
client.handleClientUpdate(upd)
if (!shouldDispatch) {
client.handleClientUpdate(upd)
}
}
/**

View file

@ -25,9 +25,15 @@ export async function readHistory(
* Whether to also clear all mentions in the chat
*/
clearMentions?: boolean
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void> {
const { maxId = 0, clearMentions } = params ?? {}
const { maxId = 0, clearMentions, shouldDispatch } = params ?? {}
const peer = await resolvePeer(client, chatId)
@ -37,10 +43,12 @@ export async function readHistory(
peer,
})
if (isInputPeerChannel(peer)) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId))
} else {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
if (!shouldDispatch) {
if (isInputPeerChannel(peer)) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId))
} else {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
}
}
@ -58,6 +66,9 @@ export async function readHistory(
peer,
maxId,
})
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
if (!shouldDispatch) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
}
}

View file

@ -8,10 +8,24 @@ import { resolvePeer } from '../users/resolve-peer.js'
*
* @param chatId Chat ID
*/
export async function readReactions(client: ITelegramClient, chatId: InputPeerLike): Promise<void> {
export async function readReactions(
client: ITelegramClient,
chatId: InputPeerLike,
params?: {
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void> {
const { shouldDispatch } = params ?? {}
const res = await client.call({
_: 'messages.readReactions',
peer: await resolvePeer(client, chatId),
})
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
if (!shouldDispatch) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
}

View file

@ -107,8 +107,8 @@ export interface CommonSendParams {
quickReply?: number | string
/**
* Whether to dispatch the returned message
* to the client's update handler.
* Whether to dispatch the returned message to the client's update handler.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true

View file

@ -100,9 +100,9 @@ export async function sendText(
entities: res.entities,
}
// is this needed?
// this._date = res.date
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
if (!params.shouldDispatch) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
const peers = new PeersIndex()

View file

@ -17,9 +17,15 @@ export async function unpinAllMessages(
* For forums - unpin only messages from the given topic
*/
topicId?: number
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void> {
const { topicId } = params ?? {}
const { topicId, shouldDispatch } = params ?? {}
const peer = await resolvePeer(client, chatId)
@ -29,9 +35,11 @@ export async function unpinAllMessages(
topMsgId: topicId,
})
if (isInputPeerChannel(peer)) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId))
} else {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
if (!shouldDispatch) {
if (isInputPeerChannel(peer)) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId))
} else {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
}
}

View file

@ -39,6 +39,8 @@ export interface UpdatesManagerParams {
*
* > **Note**: you can disable this on per-request basis by passing
* > `shouldDispatch: true` to the method call that accepts it.
* > For some methods you need to always pass `shouldDispatch: true` explicitly.
* > This is noted in the corresponding method's documentation by "Doesn't follow `disableNoDispatch`"
*
* @default false
*/