mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 12:39:20 +00:00
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 <pedroslopez@me.com> Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
This commit is contained in:
@@ -171,30 +171,30 @@ class Chat extends Base {
|
|||||||
/**
|
/**
|
||||||
* Loads chat messages, sorted from earliest to latest.
|
* 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 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<Array<Message>>}
|
* @returns {Promise<Array<Message>>}
|
||||||
*/
|
*/
|
||||||
async fetchMessages(searchOptions) {
|
async fetchMessages(searchOptions) {
|
||||||
if (!searchOptions || !searchOptions.limit) {
|
let messages = await this.client.pupPage.evaluate(async (chatId, searchOptions) => {
|
||||||
searchOptions = { limit: 50 };
|
|
||||||
}
|
|
||||||
let messages = await this.client.pupPage.evaluate(async (chatId, limit) => {
|
|
||||||
const msgFilter = m => !m.isNotification; // dont include notification messages
|
const msgFilter = m => !m.isNotification; // dont include notification messages
|
||||||
|
|
||||||
const chat = window.Store.Chat.get(chatId);
|
const chat = window.Store.Chat.get(chatId);
|
||||||
let msgs = chat.msgs.models.filter(msgFilter);
|
let msgs = chat.msgs.models.filter(msgFilter);
|
||||||
|
|
||||||
while (msgs.length < limit) {
|
if (searchOptions && searchOptions.limit) {
|
||||||
const loadedMessages = await chat.loadEarlierMsgs();
|
while (msgs.length < searchOptions.limit) {
|
||||||
if (!loadedMessages) break;
|
const loadedMessages = await chat.loadEarlierMsgs();
|
||||||
msgs = [...loadedMessages.filter(msgFilter), ...msgs];
|
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);
|
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));
|
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));
|
return messages.map(m => new Message(this.client, m));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user