From 4609a3764e537b9e39f0f34a32c4df9a2b5f220e Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Sun, 14 Dec 2025 14:16:28 +0200 Subject: [PATCH] event-buffer: prevent loss of type due to buffering (#2179) --- src/Utils/event-buffer.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Utils/event-buffer.ts b/src/Utils/event-buffer.ts index 4b9e6df0..a0aafff3 100644 --- a/src/Utils/event-buffer.ts +++ b/src/Utils/event-buffer.ts @@ -163,6 +163,28 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter } }, emit(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