fix(whatsmeow): rotear Chat para DestinationJID em mensagens enviadas por aparelhos vinculados (multi-device) - #191
Conversation
Reviewer's GuideUpdates WhatsMeow event handling so outbound private messages synchronized from linked devices use DeviceSentMeta.DestinationJID as the conversation and recipient JID in webhook payloads, while preserving group-chat routing and supporting standard WhatsApp and LID identifiers. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pkg/whatsmeow/service/whatsmeow.go" line_range="1230" />
<code_context>
+ // O whatsmeow preenche evt.Info.Chat como o próprio número da empresa e armazena o lead de destino
+ // em evt.Info.DeviceSentMeta.DestinationJID.
+ if evt.Info.IsFromMe && evt.Info.DeviceSentMeta != nil && evt.Info.DeviceSentMeta.DestinationJID != "" && !evt.Info.IsGroup {
+ if destJID, err := types.ParseJID(evt.Info.DeviceSentMeta.DestinationJID); err == nil && !destJID.IsEmpty() {
+ mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Outbound multi-device message detected - routing Chat from %s to DestinationJID %s",
+ mycli.userID, evt.Info.Chat.String(), destJID.String())
</code_context>
<issue_to_address>
**issue (bug_risk):** When `DestinationJID` is non-empty but malformed, `types.ParseJID` fails and the code silently leaves `evt.Info.Chat` pointing to the company JID while still publishing the invalid raw value in both `Recipient` fields. Consumers receive contradictory routing data instead of a rejected or clearly invalid event.
**Triggers:** When WhatsMeow emits an invalid or unsupported `DestinationJID`.
**Suggested fix:** Validate the destination once and only populate `Recipient` or rewrite `Chat` after successful parsing; otherwise log the parse error and retain the original fields without claiming the invalid value is the recipient.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the destination JID is routed incorrectly, an outbound message can be associated with the wrong chat and the emitted recipient/chat data can remain in stored records or downstream events after a revert. The impact is bounded and repairable by correcting or replaying affected message data; no money movement, deletion, or access decision is introduced.
Blocking findings: pkg/whatsmeow/service/whatsmeow.go:1230
|
Corrigido no commit f58565c: a validação de |
Descrição do Problema
Quando uma mensagem é enviada a partir de um aparelho vinculado (celular físico primário da conta ou WhatsApp Web multi-device), a biblioteca WhatsMeow emite o evento
*events.MessagecomIsFromMe = true.Nesse fluxo do WhatsMeow:
evt.Info.Chaté preenchido com o JID da própria instância/empresa (o remetente), e não com o contato de destino.evt.Info.DeviceSentMeta.DestinationJID.Impacto
No payload JSON entregue pelos webhooks:
Chatchega com o próprio número do WhatsApp conectado em vez do número do cliente que recebeu a mensagem.Recipientfica ausente ou não reflete o destinatário.O que esta alteração faz
IsFromMe = true), contiver metadados multi-device (DeviceSentMeta != nil) e for uma conversa privada (!IsGroup), converte oDestinationJIDemtypes.JIDe atualizaevt.Info.Chatpara o destinatário real.Recipienterecipientcom oDestinationJID, além de garantir queChatreflita o destinatário nodataMap.@g.us) não são alteradas, preservando o JID do grupo emChat.Como foi testado
@s.whatsapp.net) quanto com identificadores LID (@lid).ChateRecipient, permitindo a conciliação imediata da conversa.Summary by Sourcery
Direcione mensagens outbound de multi-dispositivo ao destinatário correto nos eventos e webhooks, preservando o comportamento para grupos.
Bug Fixes:
ChateRecipientdos webhooks com o JID de destino, incluindo destinatários em formato tradicional e LID.Enhancements: