Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat.fetchMessages with limit: Infinity return only one message #2297

Closed
1 task done
TheMacros opened this issue Jun 23, 2023 · 7 comments
Closed
1 task done

Chat.fetchMessages with limit: Infinity return only one message #2297

TheMacros opened this issue Jun 23, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@TheMacros
Copy link

TheMacros commented Jun 23, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying to fetch all messages from all chats. As described in the documentation, I need to pass the Infinity to the limit property of the searchOptions https://docs.wwebjs.dev/Chat.html#fetchMessages. But I received only the latest message from the chat instead of all messages.

Expected behavior

As described in the documentation the method should return all messages from the chat.

Steps to Reproduce the Bug or Issue

  1. use fetchMessages() over a chat.
  2. See the response, you'll see only the latest message in the chat is returned

Relevant Code

const chats = await this.client.getChats();
chats.forEach((chat) => {
    // Also, the chat.fetchMessages without searchOptions returns the same
    chat.fetchMessages({
        fromMe: undefined,
        limit: Infinity
    })
        .then((messages) => {
            console.log('Chat -----', chat.name, chat.id._serialized);
            messages.forEach((message) => {
                console.log("\t Message ----", message.from, message.to, message.body, message.id.id);
            });
        });

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

  • Ubuntu 18.04
  • iOS
  • whatsapp-web.js version 1.21.0
  • WhatsApp Web version 2.2326.10
  • Node.js version 16.13.1

Additional context

No response

@TheMacros TheMacros added the bug Something isn't working label Jun 23, 2023
@krumpking
Copy link

You have to wait a bit while it fetches messages before calling this method, so after client connects, set a TimeOut or something to delay a bit while all messages are loading, then try get the messages, I hope this helps : )

@TheMacros
Copy link
Author

You have to wait a bit while it fetches messages before calling this method, so after client connects, set a TimeOut or something to delay a bit while all messages are loading, then try get the messages, I hope this helps : )

Thanks, but if I run this

chat.fetchMessages({
        fromMe: undefined,
        limit: 1024
});

I get all messages (or 1024 max).

@krumpking
Copy link

interesting, I am yet to experience that, I believe someone with more experience in that

@HardRockPorn
Copy link

"Infinity" should be a string. Works fine.

const messages = await chat.fetchMessages({limit: "Infinity"});

@ivanzigoni
Copy link

ivanzigoni commented Jul 16, 2023

i've stumbled upon this error too

does anyone know if there's a pr for it already? i volunteer to fix this.

@HardRockPorn

this is supposed to be a number (or Infinity)
image

@ivanseidel
Copy link

ivanseidel commented Aug 16, 2023

I just spent the last 4 hours debugging this behavior.

What I can resume from my findings:

There are 3 Layers of data:

  • In memory messages (internally used as chat.msgs.getModelsArray)
  • In local db messages (internally used as Store.ConversationMsgs.loadEarlierMsgs)
  • External messages (internally called history sync on demmand): This is where the problem resides

Looking into what actually happens is as follows:

  • When the interface loads up a specific conversation, just 20 messages are pulled from local db and loaded into memory.
  • When user scrolls, and reaches the end, more messages are pulled from local db to memory.
  • When user reaches the LIMIT, no data is returned from loadEarlierMsgs and a button appears so the user can force a history sync from the device.

The thing is, this method is not whell documented, but I have figured out how to call it and force it to load more messages, and even, be able to change "how many messages" to fetch from device at once.

The steps are (assuming moduleRaid exists):

Step 0: (optional) set how many messages to force a history sync

mR.findModule('bulkCreateOrReplaceABPropConfigs')[0].bulkCreateOrReplaceABPropConfigs([{
    "configCode": 3811,
    "configValue": 1000, // Default value is 50
    "configExpoKey": null,
    "hasAccessed": false
}])

Step 1: Trigger a History sync on demand

const HISTORY_SYNC_ON_DEMAND = 3 // Operation code declared a few times in source code
await mR.findModule('sendPeerDataOperationRequest')[0].sendPeerDataOperationRequest(HISTORY_SYNC_ON_DEMAND, {
    "chatId": mR.findModule('createWid')[0].createWid('5511999999999@c.us') // The WID to Sync on demand goes here
})

Step 2: Use loadEarlierMsgs after finished (notice that this function is async, but doesn't wait until completion to return, need to investigate further)

Step 3: Read from getModelsArray as usual

This should be enough to force a on demand sync of a specific chat.

@hdem607
Copy link

hdem607 commented Aug 29, 2023

@ivanseidel, I didn't get your suggestion. Kind of hard to understand what to do to solve it, despite it is easy to understand the layers of data and how WA works behind the scenes.
Why don't you PR with the suggestion? Your idea of forcing fetch, as user do (clicking on the button), is awesome.

@alechkos alechkos marked this as a duplicate of #1325 Dec 2, 2023
@alechkos alechkos closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants