From b8954374585d96f9c266c1ea766c932de2127ab0 Mon Sep 17 00:00:00 2001 From: Wictor Nogueira <57378387+wictornogueira@users.noreply.github.com> Date: Tue, 1 Jun 2021 22:54:27 -0300 Subject: [PATCH] add searchMessages method (#586) * add searchMessages method * fix typing * fix typing * remove unused statement Co-authored-by: Pedro S. Lopez --- index.d.ts | 3 +++ src/Client.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/index.d.ts b/index.d.ts index f68bc34..5737af4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -99,6 +99,9 @@ declare namespace WAWebJS { /** Send a message to a specific chatId */ sendMessage(chatId: string, content: MessageContent, options?: MessageSendOptions): Promise + /** Searches for messages */ + searchMessages(query: string, options?: { chatId?: string, page?: number, limit?: number }): Promise + /** Marks the client as online */ sendPresenceAvailable(): Promise diff --git a/src/Client.js b/src/Client.js index 89ca39f..580af22 100644 --- a/src/Client.js +++ b/src/Client.js @@ -510,6 +510,24 @@ class Client extends EventEmitter { return new Message(this, newMessage); } + /** + * Searches for messages + * @param {string} query + * @param {Object} [options] + * @param {number} [options.page] + * @param {number} [options.limit] + * @param {string} [options.chatId] + * @returns {Promise} + */ + async searchMessages(query, options = {}) { + const messages = await this.pupPage.evaluate(async (query, page, count, remote) => { + const { messages } = await window.Store.Msg.search(query, page, count, remote); + return messages.map(msg => window.WWebJS.getMessageModel(msg)); + }, query, options.page, options.limit, options.chatId); + + return messages.map(msg => new Message(this, msg)); + } + /** * Get all current chat instances * @returns {Promise>}