possible convo wait fix

This commit is contained in:
Adhiraj Singh
2020-06-16 12:45:48 +05:30
parent b023546210
commit 19112755c3
2 changed files with 23 additions and 13 deletions

View File

@@ -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). If you require more functionality than provided, it'll super easy for you to write an extension (More on this at the end).
## Install ## Install
Create and cd to your NPM project directory and then in terminal, write: ``` npm install baileys ``` Create and cd to your NPM project directory and then in terminal, write: ``` npm install baileys ```
Then import in your code using: Then import in your code using:
``` javascript ``` javascript
const WhatsAppWeb = require('baileys') const WhatsAppWeb = require('baileys')
``` ```
## Connecting ## Connecting
``` javascript ``` javascript
const client = new WhatsAppWeb() const client = new WhatsAppWeb()

View File

@@ -121,10 +121,16 @@ module.exports = {
let unreadMessages = [] let unreadMessages = []
let unreadMap = {} let unreadMap = {}
let encounteredAddBefore = false
var convoResolve
this.log ("waiting for chats & contacts") // wait for the message with chats this.log ("waiting for chats & contacts") // wait for the message with chats
const waitForConvos = () => new Promise ((resolve, _) => { const waitForConvos = () => new Promise ((resolve, _) => {
convoResolve = resolve
const chatUpdate = (json) => { const chatUpdate = (json) => {
const isLast = json[1].last const isLast = json[1].last
encounteredAddBefore = json[1].add === "before" ? true : encounteredAddBefore
json = json[2] json = json[2]
if (json) { if (json) {
for (var k = json.length-1;k >= 0;k--) { 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:before"], chatUpdate)
this.registerCallback (["action", "add:unread"], chatUpdate) this.registerCallback (["action", "add:unread"], chatUpdate)
}) })
const waitForChats = this.registerCallbackOneTime (["response", "type:chat"]).then (json => { const waitForChats = this.registerCallbackOneTime (["response", "type:chat"])
chats = json[2] // chats data (log json to see what it looks like) .then (json => {
chats.forEach (chat => unreadMap [chat[1].jid] = chat[1].count) // store the number of unread messages for each sender chats = json[2] // chats data (log json to see what it looks like)
if (chats && chats.length > 0) { chats.forEach (chat => unreadMap [chat[1].jid] = chat[1].count) // store the number of unread messages for each sender
return waitForConvos () if (chats && chats.length > 0) return waitForConvos ()
} })
})
const waitForContacts = this.registerCallbackOneTime (["response", "type:contacts"]) 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 // wait for the chats & contacts to load
return Promise.all ([waitForChats, waitForContacts]).then (() => [chats, contacts, unreadMessages]) return Promise.all ([waitForChats, waitForContacts]).then (() => [chats, contacts, unreadMessages])
}, },