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 = {}) => {