fix(chatwoot): handle missing remoteJidAlt for LID contacts without crashing - #2747
Conversation
…rashing Co-authored-by: Cursor <cursoragent@cursor.com>
Reviewer's GuideUpdates Chatwoot conversation handling so unresolved WhatsApp LID contacts can be created using the LID as an identifier without persisting fake phone numbers, while making contact searches, mapping, and downstream JID parsing resilient to missing data and API failures. Sequence diagram for unresolved LID conversation creationsequenceDiagram
participant WA as WhatsApp
participant CW as ChatwootService
participant Search as Chatwoot contact search
participant C as Chatwoot conversation creation
WA->>CW: createConversation(remoteJid, remoteJidAlt?)
CW->>CW: resolveLidToPhone()
alt phone resolution succeeds
CW->>CW: findContact(phoneNumber)
else unresolved LID or missing remoteJidAlt
CW->>CW: phoneNumber = remoteJid
CW->>Search: findContactByIdentifier(remoteJid)
Search->>Search: contacts.search(q)
Search->>Search: POST contacts/filter
Search-->>CW: matching LID contact or null
end
CW->>C: create/update conversation using LID identifier
C-->>CW: conversation result
Flow diagram for safe LID contact mappingflowchart TD
A[Receive LID message] --> B{LID resolved to phone?}
B -->|Yes| C[Update phone contact mapping]
B -->|No| D[Fallback phoneNumber to remoteJid]
D --> E[Skip phone_number for @lid]
E --> F[Skip phone contact mapping]
F --> G[Search contact by identifier]
C --> H[Create or update conversation]
G --> H
H --> I[Optional JID parsing prevents missing-data crash]
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.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. If the LID fallback or revised contact lookup is wrong, Chatwoot could create or update a contact with an incorrect identifier, and the resulting persisted contact or conversation data would remain after reverting. Those records are bounded and can be corrected or rebuilt, rather than causing an irreversible external side effect.
|
Running into this exact crash in prod (we track develop, currently on 2.4.0-rc2). Some @lid contacts just never send remoteJidAlt, so line 642 in chatwoot.service.ts ends up calling .split() on undefined and the message gets swallowed by the catch block. Took us a while to figure out because nothing retries or surfaces the drop. We've been running a local backport of the same fallback idea on 2.3.7 since the end of September (both remoteJidAlt and participantAlt) and haven't seen the crash since, so it holds up in practice. Is there anything holding the review? Happy to test or provide more details if useful. Messages silently disappearing for those contacts is the worst kind of bug to have in production. |
Problem
When a WhatsApp contact sends messages with
addressingMode: 'lid'butremoteJidAltis missing, ChatwootcreateConversationcan still drop the message.developalready hasresolveLidToPhone/saveLidMappingand safer splits. The remaining gap: ifresolveLidToPhonefails andremoteJidAltis absent,phoneNumberstaysundefinedand conversation creation returns null (Contact not created or found).findContactByIdentifieralso calledclient.get, which throwst.get is not a function.Fix
remoteJid(the LID itself) whenremoteJidAltis absent so conversation creation can proceed.phone_numberfor@lididentifiers (Sourcery).isLid && !remoteJidAlt) so we do not overwrite an unrelated phone contact (Sourcery).client.getinfindContactByIdentifierwithcontacts.search+contacts/filter..split()calls optional-chained and wrap LID mapping in try/catch.Notes
Replaces #2718, which targeted
main. This branch is based on currentdevelop.Related: #1872, #2324.
Summary by Sourcery
Handle unresolved WhatsApp LID contacts safely so messages can continue creating Chatwoot conversations without corrupting phone contact mappings.
Bug Fixes:
Enhancements: