From b74246d69a0e1d8478c55a45d5cc126c06b4e2f7 Mon Sep 17 00:00:00 2001 From: Roi Greenberg Date: Sun, 14 Aug 2022 18:30:14 +0300 Subject: [PATCH] Add "fromMe" option to fetchMessages (#1444) --- index.d.ts | 4 ++++ src/structures/Chat.js | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index a99c508..edf4684 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1062,6 +1062,10 @@ declare namespace WAWebJS { * Set this to Infinity to load all messages. */ limit?: number + /** + * Return only messages from the bot number or vise versa. To get all messages, leave the option undefined. + */ + fromMe?: boolean } /** diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 6f0f0e2..241d297 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -170,13 +170,22 @@ class Chat extends Base { /** * Loads chat messages, sorted from earliest to latest. - * @param {Object} searchOptions Options for searching messages. Right now only limit is supported. + * @param {Object} searchOptions Options for searching messages. Right now only limit and fromMe is supported. * @param {Number} [searchOptions.limit] The amount of messages to return. If no limit is specified, the available messages will be returned. Note that the actual number of returned messages may be smaller if there aren't enough messages in the conversation. Set this to Infinity to load all messages. + * @param {Boolean} [searchOptions.fromMe] Return only messages from the bot number or vise versa. To get all messages, leave the option undefined. * @returns {Promise>} */ async fetchMessages(searchOptions) { let messages = await this.client.pupPage.evaluate(async (chatId, searchOptions) => { - const msgFilter = m => !m.isNotification; // dont include notification messages + const msgFilter = (m) => { + if (m.isNotification) { + return false; // dont include notification messages + } + if (searchOptions && searchOptions.fromMe && m.id.fromMe !== searchOptions.fromMe) { + return false; + } + return true; + }; const chat = window.Store.Chat.get(chatId); let msgs = chat.msgs.getModelsArray().filter(msgFilter);