Repository navigation
fix: send on-demand history sync request to own primary device - #218
jaquisonsferreira wants to merge 2 commits into
Conversation
HistorySyncRequest sent the peer data operation to the chat JID, so the request reached the contact instead of the account's primary phone and no HistorySync ON_DEMAND event was ever emitted. Use SendPeerMessage, which targets the account's own JID as whatsmeow requires.
Reviewer's GuideFix on-demand history sync by building the same request payload but sending it through whatsmeow’s SendPeerMessage API, which targets the account’s own primary device rather than the chat contact. The change is isolated behind a testable sender interface and includes payload and error-propagation tests. Sequence diagram for the on-demand history sync requestsequenceDiagram
participant API as Chat API
participant Service as chatService
participant Sender as historySyncSender
participant Primary as Own primary device
participant Contact as Chat contact
API->>Service: HistorySyncRequest(data, instance)
Service->>Sender: BuildHistorySyncRequest(messageInfo, count)
Sender-->>Service: waE2E.Message
Service->>Sender: SendPeerMessage(ctx, message)
Sender->>Primary: Send peer request
Primary-->>Sender: SendResponse
Sender-->>Service: SendResponse or error
Service-->>API: response or propagated error
Note over Contact: Request is no longer sent to the chat contact
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
We hit this bug in production on v0.7.2 and can confirm the fix here is the right one. Context: our bot's number was logged out for ~45h ("Logged out for reason 401: logged out from another device", primary phone was off). After re-pairing, we tried to recover the gap with POST /chat/history-sync, anchored on a real group message (ID and timestamp checked against our DB). What happened:
Root cause is exactly what this PR addresses: chat_service.go calls SendMessage(ctx, messageInfo.Chat, ..., SendRequestExtra{Peer: true}) instead of sending the peer message to our own JID (SendPeerMessage). Without this fix there is no way to recover messages from a disconnection window. A merge and a release would be much appreciated. Thanks for the work! 🙏 |
Problem
POST /chat/history-syncreturns 200, but noHistorySyncevent (typeON_DEMAND) ever arrives, so on-demand history never works.HistorySyncRequestsends the peer data operation built byBuildHistorySyncRequesttomessageInfo.ChatwithSendRequestExtra{Peer: true}. A peer message is meant for the account's own primary device, so the request ends up addressed to the contact and the phone never answers.whatsmeow documents that the built message must be sent with
Client.SendPeerMessage, which targets the account's own JID:Fix
SendPeerMessagethrough a smallhistorySyncSenderinterface (satisfied by*whatsmeow.Client).fromMe, timestamp and count are still taken frommessageInfo, so the request payload is unchanged.Tests
pkg/chat/service/history_sync_test.gochecks that the request goes through the peer sender with the expected chat, anchor message, timestamp and count, and that send errors are propagated.Verified in production
Running this patch on a real instance, calling
/chat/history-syncwith the oldest known message of a chat (LID addressed) now produces theHistorySyncON_DEMANDwebhook with the previous messages of that chat. Before the patch the same call returned 200 and nothing else happened.Summary by Sourcery
Send on-demand history sync requests through the client’s peer messaging path to enable reliable synchronization from the primary device.
Bug Fixes:
Enhancements:
Tests: