event-buffer: prevent loss of type due to buffering (#2179)

This commit is contained in:
Rajeh Taher
2025-12-14 14:16:28 +02:00
committed by GitHub
parent b7960dbb9a
commit 4609a3764e
+22
View File
@@ -163,6 +163,28 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
}
},
emit<T extends BaileysEvent>(event: BaileysEvent, evData: BaileysEventMap[T]) {
// Check if this is a messages.upsert with a different type than what's buffered
// If so, flush the buffered messages first to avoid type overshadowing
if (event === 'messages.upsert') {
const { type } = evData as BaileysEventMap['messages.upsert']
const existingUpserts = Object.values(data.messageUpserts)
if (existingUpserts.length > 0) {
const bufferedType = existingUpserts[0]!.type
if (bufferedType !== type) {
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages')
// Emit the buffered messages with their correct type
ev.emit('event', {
'messages.upsert': {
messages: existingUpserts.map(m => m.message),
type: bufferedType
}
})
// Clear the message upserts from the buffer
data.messageUpserts = {}
}
}
}
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
append(data, historyCache, event as BufferableEvent, evData, logger)
return true