diff --git a/src/Client.js b/src/Client.js index 3fb9555..e49e87b 100644 --- a/src/Client.js +++ b/src/Client.js @@ -610,6 +610,37 @@ class Client extends EventEmitter { return ContactFactory.create(this, contact); } + async getMessageById(messageId) { + const msg = await this.pupPage.evaluate(async messageId => { + let msg = window.Store.Msg.get(messageId); + if(msg) return window.WWebJS.getMessageModel(msg); + + const params = messageId.split('_'); + if(params.length !== 3) throw new Error('Invalid serialized message id specified'); + + const [fromMe, chatId, id] = params; + const chatWid = window.Store.WidFactory.createWid(chatId); + const fullMsgId = { + fromMe: Boolean(fromMe), + remote: chatWid, + id, + }; + + const msgKey = new window.Store.MsgKey(fullMsgId); + const chat = await window.Store.Chat.find(msgKey.remote); + const ctx = await chat.getSearchContext(msgKey); + if(ctx.collection && ctx.collection.loadAroundPromise) { + await ctx.collection.loadAroundPromise; + } + + msg = window.Store.Msg.get(messageId); + if(msg) return window.WWebJS.getMessageModel(msg); + }, messageId); + + if(msg) return new Message(this, msg); + return null; + } + /** * Returns an object with information about the invite code's group * @param {string} inviteCode