fix(client): do not propagate Conversation updates

This commit is contained in:
alina 🌸 2023-12-19 00:13:30 +03:00
parent c278599612
commit 8e07d7f552
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 29 additions and 8 deletions

View file

@ -58,6 +58,14 @@ interface TelegramClientOptions extends Omit<BaseTelegramClientOptions, 'storage
* you should manually add a handler using `client.network.setUpdateHandler`.
*/
disableUpdatesManager?: boolean
/**
* If `true`, the updates that were handled by some {@link Conversation}
* will not be dispatched any further.
*
* @default true
*/
skipConversationUpdates?: boolean
}
// @initialize=super
@ -79,6 +87,7 @@ function _initializeClientSuper(this: TelegramClient, opts: TelegramClientOption
/** @internal */
function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
this._disableUpdatesManager = opts.disableUpdatesManager ?? false
const skipConversationUpdates = opts.skipConversationUpdates ?? true
if (!opts.disableUpdates && !opts.disableUpdatesManager) {
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
@ -88,7 +97,8 @@ function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
onUpdate: makeParsedUpdateHandler({
messageGroupingInterval,
onUpdate: (update) => {
Conversation.handleUpdate(this, update)
if (Conversation.handleUpdate(this, update) && skipConversationUpdates) return
this.emit('update', update)
this.emit(update.name, update.data)
},

View file

@ -71,9 +71,15 @@ export class Conversation {
return (client as any)[CONVERSATION_SYMBOL] as ConversationsState
}
static handleUpdate(client: BaseTelegramClient, update: ParsedUpdate): void {
/**
* Pass the update to the conversation manager and all registered
* conversations on this client.
*
* @returns `true` if the update was handled by some conversation
*/
static handleUpdate(client: BaseTelegramClient, update: ParsedUpdate): boolean {
const state = Conversation._getState(client)
if (!state?.hasConversations) return
if (!state?.hasConversations) return false
let chatId
@ -86,25 +92,30 @@ export class Conversation {
chatId = update.data.chatId
break
default:
return
return false
}
const conv = state.pendingConversations.get(chatId)
if (!conv) return
if (!conv) return false
for (const c of conv) {
switch (update.name) {
case 'new_message':
c._onNewMessage(update.data)
break
return true
case 'edit_message':
c._onEditMessage(update.data)
break
return true
case 'history_read':
c._onHistoryRead(update.data)
break
return true
}
}
return false
}
/**