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

View file

@ -15,9 +15,15 @@ export async function deleteUserHistory(
chatId: InputPeerLike chatId: InputPeerLike
/** User/channel ID whose messages to delete */ /** User/channel ID whose messages to delete */
participantId: InputPeerLike participantId: InputPeerLike
/**
* Whether to dispatch the updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
}, },
): Promise<void> { ): Promise<void> {
const { chatId, participantId } = params const { chatId, participantId, shouldDispatch } = params
const channel = await resolveChannel(client, chatId) const channel = await resolveChannel(client, chatId)
@ -29,5 +35,7 @@ export async function deleteUserHistory(
participant: peer, 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, client: ITelegramClient,
chat: InputPeerLike, chat: InputPeerLike,
topicId: number | ForumTopic, topicId: number | ForumTopic,
params?: {
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
},
): Promise<void> { ): Promise<void> {
const { shouldDispatch } = params ?? {}
const channel = await resolveChannel(client, chat) const channel = await resolveChannel(client, chat)
assertTypeIsNot('deleteForumTopicHistory', channel, 'inputChannelEmpty') assertTypeIsNot('deleteForumTopicHistory', channel, 'inputChannelEmpty')
@ -24,5 +33,7 @@ export async function deleteForumTopicHistory(
topMsgId: typeof topicId === 'number' ? topicId : topicId.id, 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 * @default true
*/ */
revoke?: boolean 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[], ids: number[],
params?: DeleteMessagesParams, params?: DeleteMessagesParams,
): Promise<void> { ): Promise<void> {
const { revoke = true } = params ?? {} const { revoke = true, shouldDispatch } = params ?? {}
const peer = await resolvePeer(client, chatId) const peer = await resolvePeer(client, chatId)
@ -53,7 +59,9 @@ export async function deleteMessagesById(
upd = createDummyUpdate(res.pts, res.ptsCount) 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 * Whether to also clear all mentions in the chat
*/ */
clearMentions?: boolean clearMentions?: boolean
/**
* Whether to dispatch updates that will be generated by this call.
* Doesn't follow `disableNoDispatch`
*/
shouldDispatch?: true
}, },
): Promise<void> { ): Promise<void> {
const { maxId = 0, clearMentions } = params ?? {} const { maxId = 0, clearMentions, shouldDispatch } = params ?? {}
const peer = await resolvePeer(client, chatId) const peer = await resolvePeer(client, chatId)
@ -37,10 +43,12 @@ export async function readHistory(
peer, peer,
}) })
if (isInputPeerChannel(peer)) { if (!shouldDispatch) {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId)) if (isInputPeerChannel(peer)) {
} else { client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, peer.channelId))
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount)) } else {
client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount))
}
} }
} }
@ -58,6 +66,9 @@ export async function readHistory(
peer, peer,
maxId, 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 * @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({ const res = await client.call({
_: 'messages.readReactions', _: 'messages.readReactions',
peer: await resolvePeer(client, chatId), 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 quickReply?: number | string
/** /**
* Whether to dispatch the returned message * Whether to dispatch the returned message to the client's update handler.
* to the client's update handler. * Doesn't follow `disableNoDispatch`
*/ */
shouldDispatch?: true shouldDispatch?: true

View file

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

View file

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