whatsapp-web.js icon indicating copy to clipboard operation
whatsapp-web.js copied to clipboard

Version 1.17.1 fetchMessages({limit: 100}) only returns a subset of expected messages.

Open garrit-schroeder opened this issue 2 years ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

chat.fetchMessages({limit: 100}); only returns a subset of the newest messages. The limit search option is ignored.

Expected behavior

Return every message within the limit

Steps to Reproduce the Bug or Issue

const client = new Client(); client.on('ready', async () => { console.log('Client is ready!'); var chats = await client.getChats(); var messages = await chats[5].fetchMessages({limit: 100}); client.logout() });

Relevant Code

$$$ By setting a breakpoint in my IDE at the second line the issue is resolved. There might be a timeout issue.

async fetchMessages(searchOptions) { $$$$$ 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.getModelsArray().filter(msgFilter);

        if (searchOptions && searchOptions.limit > 0) {
            while (msgs.length < searchOptions.limit) {
                const loadedMessages = await window.Store.ConversationMsgs.loadEarlierMsgs(chat);
                if (!loadedMessages || !loadedMessages.length) break;
                msgs = [...loadedMessages.filter(msgFilter), ...msgs];
            }
            
            if (msgs.length > searchOptions.limit) {
                msgs.sort((a, b) => (a.t > b.t) ? 1 : -1);
                msgs = msgs.splice(msgs.length - searchOptions.limit);
            }
        }

        return msgs.map(m => window.WWebJS.getMessageModel(m));

    }, this.id._serialized, searchOptions);

    return messages.map(m => new Message(this.client, m));
}

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Ubuntu 18 Android "whatsapp-web.js": "^1.17.1" WA Web version 2.2228.12 Node version: v12.22.12

Additional context

no

garrit-schroeder avatar Aug 03 '22 09:08 garrit-schroeder

By adding a sleep timeout of 20 seconds the request works.

client.on('ready', async () => {
    console.log('Client is ready!');
    console.log(await client.getWWebVersion());
    var chats = await client.getChats();
    await new Promise(resolve => setTimeout(resolve, 20000));
    var messages = await chats[5].fetchMessages({limit: 100});
    client.logout()
});

garrit-schroeder avatar Aug 03 '22 09:08 garrit-schroeder

Seems like it is a timeout issue where even after ready it takes a while for the messages to load.

The function should load all messages up to the limit regardless so this is weird

purpshell avatar Aug 04 '22 00:08 purpshell

mismo problema.

no cargan los mensajes.

¿alguna solución?

alan2797 avatar Aug 04 '22 21:08 alan2797

I also have this issue...

The 20 second sleep made no difference.

dbrnz avatar Oct 01 '22 09:10 dbrnz

Downgrading to v1.16.7 works

tonxxd avatar Oct 14 '22 12:10 tonxxd

Any update?

shsandro avatar Mar 21 '23 14:03 shsandro