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

Problem when fetch Messages with searchOptions as undefined or with high limit #1471

Closed
1 task done
MatheusPerozini opened this issue May 24, 2022 · 0 comments
Closed
1 task done
Labels
enhancement New feature or request

Comments

@MatheusPerozini
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe.

Yes. The problem is if I call the method fetchMessages with searchOptions as undefined, in some chats, it will not return the messages. But, if I put the limit with low numbers, it will get the messages. On high numbers, it will stay in a loop.

Describe the solution you'd like.

I fixed by checking if the searchOption are undefined, if true it will defined the limit as 30. I also put || loadedMessages.length == 0, inside the if on while:
`
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.models.filter(msgFilter);

        if (!msgs || msgs.length == 0) {
            searchOptions = { limit: 30 };
        }

        if (searchOptions && searchOptions.limit > 0) {
            while (msgs.length < searchOptions.limit) {
                const loadedMessages = await chat.loadEarlierMsgs();
                if (!loadedMessages || loadedMessages.length == 0) 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));
}

`

But a better solution is a fix to get messages even if the searchOptions is undefined.

Describe an alternate solution.

No response

Additional context

No response

@MatheusPerozini MatheusPerozini added the enhancement New feature or request label May 24, 2022
@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
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants