From c44bf4dde78a773adfd2639f3c3439a72d205c40 Mon Sep 17 00:00:00 2001 From: "tel.medola" Date: Wed, 15 Jul 2026 09:47:03 -0300 Subject: [PATCH] fix(message): find sent message indexed under alternate wid (lid/pn) client.sendMessage() sends via addAndSendMsgToChat and then looks the sent message up with Msg.get(newMsgKey._serialized). For contacts with a lid/phone-number wid pair, WhatsApp Web can index the message under the chat sibling wid, so the lookup fails and the promise resolves with undefined even though the message was delivered. fall back to searching the chat own collection by the random key id (independent of the wid used in the key) and to a key rebuilt with wawebapicontact.getAlternateUserWid, including the sibling chat. When the message still cannot be found, keep the lookup context in window.WWebJS.lastSendMessageDebug as an internal diagnostic aid. related to #3767 co-Authored-By: Claude Fable 5 --- src/util/Injected/Utils.js | 58 +++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/util/Injected/Utils.js b/src/util/Injected/Utils.js index dfa30a579de..585a19ef69a 100644 --- a/src/util/Injected/Utils.js +++ b/src/util/Injected/Utils.js @@ -580,9 +580,65 @@ exports.LoadUtils = () => { if (options.waitUntilMsgSent) await sendMsgResultPromise; - return window + let sentMsg = window .require('WAWebCollections') .Msg.get(newMsgKey._serialized); + + // The sent message can be indexed under a key built with the + // chat's sibling wid (lid <-> phone number); the random id part + // of the key does not depend on the wid, so look it up directly + // in the chat's own collection + if (!sentMsg) { + sentMsg = chat.msgs + .getModelsArray() + .find((m) => m.id.id === newMsgKey.id); + } + + let altWid; + if ( + !sentMsg && + typeof chat.id?.isUser === 'function' && + chat.id.isUser() + ) { + altWid = window + .require('WAWebApiContact') + .getAlternateUserWid(chat.id); + if (altWid) { + const altMsgKey = new (window.require('WAWebMsgKey'))({ + from: from, + to: altWid, + id: newId, + participant: participant, + selfDir: 'out', + }); + sentMsg = + window + .require('WAWebCollections') + .Msg.get(altMsgKey._serialized) || + window + .require('WAWebCollections') + .Chat.get(altWid) + ?.msgs.getModelsArray() + .find((m) => m.id.id === newMsgKey.id); + } + } + + // Keep a debug trail so clients can inspect why the sent message + // could not be found in the collections + if (!sentMsg) { + window.WWebJS.lastSendMessageDebug = { + newMsgKey: newMsgKey._serialized, + chatId: chat.id._serialized, + altWid: altWid ? altWid._serialized : null, + lastChatMsgKeys: chat.msgs + .getModelsArray() + .slice(-3) + .map((m) => m.id._serialized), + time: Date.now(), + }; + } + + return sentMsg; }; window.WWebJS.editMessage = async (msg, content, options = {}) => {