-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(baileys): prevent message loss from WhatsApp stub placeholders #2259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(baileys): prevent message loss from WhatsApp stub placeholders #2259
Conversation
Mensagens do WhatsApp estavam sendo perdidas e não eram salvas no banco de dados, especialmente mensagens de canais/newsletters (@lid) e mensagens com criptografia complexa. O WhatsApp/Baileys envia mensagens criptografadas em duas etapas: 1. Primeiro: Envia um stub (placeholder) com messageStubParameters: ['Message absent from node'] enquanto descriptografa a mensagem 2. Depois: Envia a mensagem real com o conteúdo descriptografado O problema ocorria porque: - O stub chegava primeiro e era adicionado ao cache de mensagens duplicadas - O stub era descartado (corretamente) por não ter conteúdo (!received?.message) - A mensagem real chegava depois, mas era ignorada como duplicata porque o ID já estava no cache - Resultado: mensagem nunca era salva no banco de dados Solução: - Detectar stubs do WhatsApp através de messageStubParameters contendo 'Message absent from node' - Não adicionar stubs ao cache de mensagens duplicadas - Permitir que a mensagem real seja processada quando chegar - Manter o descarte do stub para evitar salvar placeholders vazios
Reviewer's guide (collapsed on small PRs)Reviewer's GuideExtends the WhatsApp Baileys decryption error-handling logic to recognize Baileys placeholder stub messages ("Message absent from node") so they are treated as transient decryption issues and not added to the duplicate-message cache, preventing loss of the subsequent real message. Sequence diagram for WhatsApp stub placeholder handling in BaileyssequenceDiagram
participant WhatsAppServer
participant BaileysLibrary
participant BaileysStartupService
participant DuplicateMessageCache
participant Database
rect rgb(235, 245, 255)
Note over WhatsAppServer,BaileysStartupService: Stub placeholder message (Message absent from node)
WhatsAppServer->>BaileysLibrary: sendEncryptedStubMessage
BaileysLibrary->>BaileysStartupService: onMessage(stubWithMessageStubParameters)
BaileysStartupService->>BaileysStartupService: detectStub(messageStubParameters contains Message_absent_from_node)
BaileysStartupService-->>DuplicateMessageCache: doNotAddStubToCache
BaileysStartupService-->>Database: doNotPersistStub
end
rect rgb(235, 255, 235)
Note over WhatsAppServer,BaileysStartupService: Real decrypted message arrives later with same message ID
WhatsAppServer->>BaileysLibrary: sendDecryptedMessage
BaileysLibrary->>BaileysStartupService: onMessage(realMessage)
BaileysStartupService->>DuplicateMessageCache: isDuplicate(messageId)?
DuplicateMessageCache-->>BaileysStartupService: notFound
BaileysStartupService->>Database: saveMessage(realMessage)
BaileysStartupService->>DuplicateMessageCache: addMessageIdToCache
end
Class diagram for updated BaileysStartupService decryption error handlingclassDiagram
class ChannelStartupService {
}
class BaileysStartupService {
- duplicateMessageCache
+ handleIncomingMessage(rawMessage)
+ isDecryptionStub(messageStubParameters) bool
+ shouldSkipDuplicateCache(messageStubParameters) bool
}
class DuplicateMessageCache {
+ has(messageId) bool
+ add(messageId)
}
class IncomingMessage {
+ id
+ message
+ messageStubParameters
}
ChannelStartupService <|-- BaileysStartupService
BaileysStartupService --> DuplicateMessageCache
BaileysStartupService --> IncomingMessage
%% Highlight of logic change
BaileysStartupService : isDecryptionStub(messageStubParameters) checks for
BaileysStartupService : 'Invalid PreKey ID'
BaileysStartupService : 'No session record'
BaileysStartupService : 'No session found to decrypt message'
BaileysStartupService : 'Message absent from node' %% newly added
BaileysStartupService : if isDecryptionStub then
BaileysStartupService : skip adding to duplicateMessageCache
BaileysStartupService : do not persist placeholder message
BaileysStartupService : else
BaileysStartupService : normal duplicate check and persistence flow
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Since this list of Baileys/WhatsApp stub error substrings is growing, consider extracting it to a named constant (e.g.,
WHATSAPP_STUB_MESSAGE_ERRORS) with a short comment explaining the semantics, to make future additions and maintenance clearer. - If Baileys or WhatsApp ever localize or slightly change these stub messages, the current
includes-based matching on fixed English strings may fail silently; it might be worth centralizing this logic with tests or adding a brief note about the dependency on these exact message texts.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since this list of Baileys/WhatsApp stub error substrings is growing, consider extracting it to a named constant (e.g., `WHATSAPP_STUB_MESSAGE_ERRORS`) with a short comment explaining the semantics, to make future additions and maintenance clearer.
- If Baileys or WhatsApp ever localize or slightly change these stub messages, the current `includes`-based matching on fixed English strings may fail silently; it might be worth centralizing this logic with tests or adding a brief note about the dependency on these exact message texts.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
📋 Description
Corrige perda de mensagens do WhatsApp que não eram salvas no banco de dados, especialmente mensagens de canais/newsletters (
@lid) e mensagens com criptografia complexa.🔍 Causa Raiz
O WhatsApp/Baileys envia mensagens criptografadas em duas etapas:
messageStubParameters: ['Message absent from node']enquanto descriptografa a mensagemO problema ocorria porque:
!received?.message)✅ Solução Implementada
messageStubParameterscontendo'Message absent from node'🔗 Related Issue
🧪 Type of Change
🧪 Testing
Cenários Testados:
@lid)✅ Checklist
📝 Additional Notes
O que é "Message absent from node"?
É um placeholder/stub que o WhatsApp envia quando:
Impacto da Mudança
Summary by Sourcery
Bug Fixes: