From f7e0e7693262ff29b5145208837d977a204760ae Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Thu, 18 Jul 2024 17:37:39 +0300 Subject: [PATCH] feat(core): `shouldDispatch` option for methods that use dummy updates --- packages/core/src/highlevel/client.ts | 41 ++++++++++++++++++- .../methods/chats/delete-user-history.ts | 12 +++++- .../forums/delete-forum-topic-history.ts | 13 +++++- .../methods/messages/delete-messages.ts | 12 +++++- .../methods/messages/read-history.ts | 23 ++++++++--- .../methods/messages/read-reactions.ts | 18 +++++++- .../highlevel/methods/messages/send-common.ts | 4 +- .../highlevel/methods/messages/send-text.ts | 6 +-- .../methods/messages/unpin-all-messages.ts | 18 +++++--- packages/core/src/highlevel/updates/types.ts | 2 + 10 files changed, 124 insertions(+), 25 deletions(-) diff --git a/packages/core/src/highlevel/client.ts b/packages/core/src/highlevel/client.ts index ceddd3c2..160b2b97 100644 --- a/packages/core/src/highlevel/client.ts +++ b/packages/core/src/highlevel/client.ts @@ -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 /** * 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 + 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 /** * 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 /** @@ -3765,7 +3787,16 @@ export interface TelegramClient extends ITelegramClient { * * @param chatId Chat ID */ - readReactions(chatId: InputPeerLike): Promise + readReactions( + chatId: InputPeerLike, + params?: { + /** + * Whether to dispatch updates that will be generated by this call. + * Doesn't follow `disableNoDispatch` + */ + shouldDispatch?: true + }, + ): Promise /** * 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 /** diff --git a/packages/core/src/highlevel/methods/chats/delete-user-history.ts b/packages/core/src/highlevel/methods/chats/delete-user-history.ts index d0fbcaaf..1ff225fd 100644 --- a/packages/core/src/highlevel/methods/chats/delete-user-history.ts +++ b/packages/core/src/highlevel/methods/chats/delete-user-history.ts @@ -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 { - 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)) + } } diff --git a/packages/core/src/highlevel/methods/forums/delete-forum-topic-history.ts b/packages/core/src/highlevel/methods/forums/delete-forum-topic-history.ts index 5704a5e3..f4f3f271 100644 --- a/packages/core/src/highlevel/methods/forums/delete-forum-topic-history.ts +++ b/packages/core/src/highlevel/methods/forums/delete-forum-topic-history.ts @@ -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 { + 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)) + } } diff --git a/packages/core/src/highlevel/methods/messages/delete-messages.ts b/packages/core/src/highlevel/methods/messages/delete-messages.ts index cb4f8967..e158a7f5 100644 --- a/packages/core/src/highlevel/methods/messages/delete-messages.ts +++ b/packages/core/src/highlevel/methods/messages/delete-messages.ts @@ -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 { - 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) + } } /** diff --git a/packages/core/src/highlevel/methods/messages/read-history.ts b/packages/core/src/highlevel/methods/messages/read-history.ts index 09e3f042..279e7bd7 100644 --- a/packages/core/src/highlevel/methods/messages/read-history.ts +++ b/packages/core/src/highlevel/methods/messages/read-history.ts @@ -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 { - 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)) + } } } diff --git a/packages/core/src/highlevel/methods/messages/read-reactions.ts b/packages/core/src/highlevel/methods/messages/read-reactions.ts index 94f55430..adf448e9 100644 --- a/packages/core/src/highlevel/methods/messages/read-reactions.ts +++ b/packages/core/src/highlevel/methods/messages/read-reactions.ts @@ -8,10 +8,24 @@ import { resolvePeer } from '../users/resolve-peer.js' * * @param chatId Chat ID */ -export async function readReactions(client: ITelegramClient, chatId: InputPeerLike): Promise { +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 { + 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)) + } } diff --git a/packages/core/src/highlevel/methods/messages/send-common.ts b/packages/core/src/highlevel/methods/messages/send-common.ts index f083511f..ccfc0d21 100644 --- a/packages/core/src/highlevel/methods/messages/send-common.ts +++ b/packages/core/src/highlevel/methods/messages/send-common.ts @@ -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 diff --git a/packages/core/src/highlevel/methods/messages/send-text.ts b/packages/core/src/highlevel/methods/messages/send-text.ts index 70291d43..78281b86 100644 --- a/packages/core/src/highlevel/methods/messages/send-text.ts +++ b/packages/core/src/highlevel/methods/messages/send-text.ts @@ -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() diff --git a/packages/core/src/highlevel/methods/messages/unpin-all-messages.ts b/packages/core/src/highlevel/methods/messages/unpin-all-messages.ts index a9e187a9..ad1ede20 100644 --- a/packages/core/src/highlevel/methods/messages/unpin-all-messages.ts +++ b/packages/core/src/highlevel/methods/messages/unpin-all-messages.ts @@ -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 { - 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)) + } } } diff --git a/packages/core/src/highlevel/updates/types.ts b/packages/core/src/highlevel/updates/types.ts index aa37336d..616278f4 100644 --- a/packages/core/src/highlevel/updates/types.ts +++ b/packages/core/src/highlevel/updates/types.ts @@ -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 */ -- 2.45.2