From 19112755c3c9a2c6347ce5f38eaf8730615d3fe6 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 16 Jun 2020 12:45:48 +0530 Subject: [PATCH] possible convo wait fix --- README.md | 10 +++++----- WhatsAppWeb.Session.js | 26 ++++++++++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 388d6d3..0e1ccdc 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,11 @@ If you require more functionality than provided, it'll super easy for you to write an extension (More on this at the end). ## Install - Create and cd to your NPM project directory and then in terminal, write: ``` npm install baileys ``` - Then import in your code using: - ``` javascript - const WhatsAppWeb = require('baileys') - ``` +Create and cd to your NPM project directory and then in terminal, write: ``` npm install baileys ``` +Then import in your code using: +``` javascript + const WhatsAppWeb = require('baileys') +``` ## Connecting ``` javascript const client = new WhatsAppWeb() diff --git a/WhatsAppWeb.Session.js b/WhatsAppWeb.Session.js index ba2d327..6347997 100644 --- a/WhatsAppWeb.Session.js +++ b/WhatsAppWeb.Session.js @@ -121,10 +121,16 @@ module.exports = { let unreadMessages = [] let unreadMap = {} + let encounteredAddBefore = false + var convoResolve + this.log ("waiting for chats & contacts") // wait for the message with chats const waitForConvos = () => new Promise ((resolve, _) => { + convoResolve = resolve const chatUpdate = (json) => { const isLast = json[1].last + encounteredAddBefore = json[1].add === "before" ? true : encounteredAddBefore + json = json[2] if (json) { for (var k = json.length-1;k >= 0;k--) { @@ -149,15 +155,19 @@ module.exports = { this.registerCallback (["action", "add:before"], chatUpdate) this.registerCallback (["action", "add:unread"], chatUpdate) }) - const waitForChats = this.registerCallbackOneTime (["response", "type:chat"]).then (json => { - chats = json[2] // chats data (log json to see what it looks like) - chats.forEach (chat => unreadMap [chat[1].jid] = chat[1].count) // store the number of unread messages for each sender - if (chats && chats.length > 0) { - return waitForConvos () - } - }) + const waitForChats = this.registerCallbackOneTime (["response", "type:chat"]) + .then (json => { + chats = json[2] // chats data (log json to see what it looks like) + chats.forEach (chat => unreadMap [chat[1].jid] = chat[1].count) // store the number of unread messages for each sender + if (chats && chats.length > 0) return waitForConvos () + }) const waitForContacts = this.registerCallbackOneTime (["response", "type:contacts"]) - .then (json => contacts = json[2]) + .then (json => { + contacts = json[2] + // if no add:before messages are sent, and you receive contacts + // should probably resolve the promise + if (!encounteredAddBefore && convoResolve) convoResolve () + }) // wait for the chats & contacts to load return Promise.all ([waitForChats, waitForContacts]).then (() => [chats, contacts, unreadMessages]) },