From aa9e19a19e1ee07286c10bb3a0335f98a8483d6e Mon Sep 17 00:00:00 2001 From: Azeem Haider Date: Mon, 7 Feb 2022 04:27:38 +0500 Subject: [PATCH] fetchMessages gets currently available messages by default (#1015) * get chat default messages fix #1014 * fixing format * Update src/structures/Chat.js Co-authored-by: Pedro S. Lopez Co-authored-by: Pedro S. Lopez --- src/structures/Chat.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/structures/Chat.js b/src/structures/Chat.js index b8aacb1..e4f59d1 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -171,30 +171,30 @@ 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 {Number} [searchOptions.limit=50] The amount of messages to return. 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 {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. * @returns {Promise>} */ async fetchMessages(searchOptions) { - if (!searchOptions || !searchOptions.limit) { - searchOptions = { limit: 50 }; - } - let messages = await this.client.pupPage.evaluate(async (chatId, limit) => { + let messages = await this.client.pupPage.evaluate(async (chatId, searchOptions) => { const msgFilter = m => !m.isNotification; // dont include notification messages const chat = window.Store.Chat.get(chatId); let msgs = chat.msgs.models.filter(msgFilter); - while (msgs.length < limit) { - const loadedMessages = await chat.loadEarlierMsgs(); - if (!loadedMessages) break; - msgs = [...loadedMessages.filter(msgFilter), ...msgs]; + if (searchOptions && searchOptions.limit) { + while (msgs.length < searchOptions.limit) { + const loadedMessages = await chat.loadEarlierMsgs(); + if (!loadedMessages) break; + msgs = [...loadedMessages.filter(msgFilter), ...msgs]; + } + if (msgs.length > searchOptions.limit) + msgs = msgs.splice(msgs.length - searchOptions.limit); } msgs.sort((a, b) => (a.t > b.t) ? 1 : -1); - if (msgs.length > limit) msgs = msgs.splice(msgs.length - limit); return msgs.map(m => window.WWebJS.getMessageModel(m)); - }, this.id._serialized, searchOptions.limit); + }, this.id._serialized, searchOptions); return messages.map(m => new Message(this.client, m)); }