From b4d82d84255b66ebb9e8fbb06db2be5e2fd5b303 Mon Sep 17 00:00:00 2001 From: Shir Serlui <70711723+shirser121@users.noreply.github.com> Date: Mon, 7 Aug 2023 20:10:11 +0300 Subject: [PATCH] Feat: get message by id (#1634) * feat: get message by id Add the feature of getting messages by id. It's loads messages that are not loaded in the Store.Msg, and add them into the Store.Msg. I don't use the draft because this is already work * Fix typo * Fix for test * Update index.d.ts --------- Co-authored-by: Rajeh Taher Co-authored-by: Codeboss <131376628+codebossdev@users.noreply.github.com> --- index.d.ts | 3 +++ src/Client.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/index.d.ts b/index.d.ts index 7b54fdd..ea8be4b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -60,6 +60,9 @@ declare namespace WAWebJS { /** Get contact instance by ID */ getContactById(contactId: string): Promise + /** Get message by ID */ + getMessageById(messageId: string): Promise + /** Get all current contact instances */ getContacts(): Promise diff --git a/src/Client.js b/src/Client.js index 97a2a89..c673cef 100644 --- a/src/Client.js +++ b/src/Client.js @@ -898,6 +898,24 @@ 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'); + + let messagesObject = await window.Store.Msg.getMessagesById([messageId]); + if (messagesObject && messagesObject.messages.length) msg = messagesObject.messages[0]; + + 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