separated out connect & receiving chats

This commit is contained in:
Adhiraj Singh
2020-06-13 12:43:00 +05:30
parent 7d9fa7a6d1
commit f04fe71530
3 changed files with 70 additions and 58 deletions

View File

@@ -9,9 +9,20 @@ module.exports = {
* Connect to WhatsAppWeb * Connect to WhatsAppWeb
* @param {Object} [authInfo] credentials to log back in * @param {Object} [authInfo] credentials to log back in
* @param {number} [timeoutMs] timeout after which the connect will fail, set to null for an infinite timeout * @param {number} [timeoutMs] timeout after which the connect will fail, set to null for an infinite timeout
* @return {promise<[object, any[], any[], any[]]>} returns [userMetaData, chats, contacts, unreadMessages] * @return {[object, object[], object[], object[]]} returns [userMetaData, chats, contacts, unreadMessages]
*/ */
connect: function (authInfo, timeoutMs) { connect: async function (authInfo, timeoutMs) {
const userInfo = await this.connectSlim (authInfo, timeoutMs)
const chats = await this.receiveChatsAndContacts (authInfo, timeoutMs)
return [userInfo, ...chats]
},
/**
* Connect to WhatsAppWeb, resolves without waiting for chats & contacts
* @param {Object} [authInfo] credentials to log back in
* @param {number} [timeoutMs] timeout after which the connect will fail, set to null for an infinite timeout
* @return {Promise<Object>} returns [userMetaData, chats, contacts, unreadMessages]
*/
connectSlim: function (authInfo, timeoutMs) {
// set authentication credentials if required // set authentication credentials if required
if (authInfo) { if (authInfo) {
this.authInfo = Object.assign ({}, authInfo) // copy credentials this.authInfo = Object.assign ({}, authInfo) // copy credentials
@@ -26,7 +37,7 @@ module.exports = {
let promise = new Promise ( (resolve, reject) => { let promise = new Promise ( (resolve, reject) => {
this.conn.on('open', () => { this.conn.on('open', () => {
this.conn.on('message', (m) => this.onMessageRecieved(m)) // in WhatsAppWeb.Recv.js this.conn.on('message', m => this.onMessageRecieved(m)) // in WhatsAppWeb.Recv.js
this.beginAuthentication ().then (resolve).catch (reject) this.beginAuthentication ().then (resolve).catch (reject)
}) })
this.conn.on('error', error => { // if there was an error in the WebSocket this.conn.on('error', error => { // if there was an error in the WebSocket
@@ -35,12 +46,10 @@ module.exports = {
}) })
}) })
promise = timeoutMs ? Utils.promiseTimeout (timeoutMs, promise) : promise promise = timeoutMs ? Utils.promiseTimeout (timeoutMs, promise) : promise
return promise.catch (err => { return promise.catch (err => {this.close (); throw err;})
this.close ()
throw err
})
}, },
/** once a connection has been successfully established /**
* Once a connection has been successfully established
* @private * @private
* @return {promise<object[]>} * @return {promise<object[]>}
*/ */
@@ -51,11 +60,6 @@ module.exports = {
this.authInfo = { clientID: Utils.generateClientID() } // generate a client ID this.authInfo = { clientID: Utils.generateClientID() } // generate a client ID
} }
let chats = []
let contacts = []
let unreadMessages = []
let unreadMap = {}
const data = ["admin", "init", this.version, this.browserDescriptions, this.authInfo.clientID, true] const data = ["admin", "init", this.version, this.browserDescriptions, this.authInfo.clientID, true]
return this.query(data) return this.query(data)
.then (([json, _]) => { .then (([json, _]) => {
@@ -102,6 +106,21 @@ module.exports = {
this.startKeepAliveRequest() // start sending keep alive requests (keeps the WebSocket alive & updates our last seen) this.startKeepAliveRequest() // start sending keep alive requests (keeps the WebSocket alive & updates our last seen)
}) // validate the connection }) // validate the connection
.then (() => { .then (() => {
this.log("connected successfully")
return this.userMetaData
})
},
/**
* Sets up callbacks to receive chats, contacts & unread messages.
* Must be called immediately after connect
* @returns {[ object[], object[], object[] ]} - [chats, contacts, unreadMessages]
*/
receiveChatsAndContacts: function () {
let chats = []
let contacts = []
let unreadMessages = []
let unreadMap = {}
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, _) => {
const chatUpdate = (json) => { const chatUpdate = (json) => {
@@ -140,14 +159,7 @@ module.exports = {
const waitForContacts = this.registerCallbackOneTime (["response", "type:contacts"]) const waitForContacts = this.registerCallbackOneTime (["response", "type:contacts"])
.then (json => contacts = json[2]) .then (json => contacts = json[2])
// wait for the chats & contacts to load // wait for the chats & contacts to load
return Promise.all ([waitForChats, waitForContacts]) return Promise.all ([waitForChats, waitForContacts]).then (() => [chats, contacts, unreadMessages])
})
.then (() => {
// now we're successfully connected
this.log("connected successfully")
// resolve the promise
return [this.userMetaData, chats, contacts, unreadMessages]
})
}, },
/** /**
* Once the QR code is scanned and we can validate our connection, or we resolved the challenge when logging back in * Once the QR code is scanned and we can validate our connection, or we resolved the challenge when logging back in

View File

@@ -178,9 +178,7 @@ class WhatsAppWeb {
QR.generate(str, {small: true}) QR.generate(str, {small: true})
} }
log (text) { log (text) { console.log (`[Baileys] ${text}`) }
console.log (`[Baileys] ${text}"`)
}
} }
/* Import the rest of the code */ /* Import the rest of the code */
@@ -203,6 +201,8 @@ WhatsAppWeb.prototype.decodeMediaMessage = recv.decodeMediaMessage
const session = require("./WhatsAppWeb.Session") const session = require("./WhatsAppWeb.Session")
WhatsAppWeb.prototype.connect = session.connect WhatsAppWeb.prototype.connect = session.connect
WhatsAppWeb.prototype.connectSlim = session.connectSlim
WhatsAppWeb.prototype.receiveChatsAndContacts = session.receiveChatsAndContacts
WhatsAppWeb.prototype.beginAuthentication = session.beginAuthentication WhatsAppWeb.prototype.beginAuthentication = session.beginAuthentication
WhatsAppWeb.prototype.validateNewConnection = session.validateNewConnection WhatsAppWeb.prototype.validateNewConnection = session.validateNewConnection
WhatsAppWeb.prototype.respondToChallenge = session.respondToChallenge WhatsAppWeb.prototype.respondToChallenge = session.respondToChallenge

View File

@@ -11,7 +11,7 @@ try {
authInfo = JSON.parse(file) authInfo = JSON.parse(file)
} catch { } } catch { }
client.connect (authInfo, 30*1000) // connect or timeout in 30 seconds client.connect (authInfo, 20*1000) // connect or timeout in 20 seconds
.then (([user, chats, contacts, unread]) => { .then (([user, chats, contacts, unread]) => {
console.log ("oh hello " + user.name + " (" + user.id + ")") console.log ("oh hello " + user.name + " (" + user.id + ")")
console.log ("you have " + unread.length + " unread messages") console.log ("you have " + unread.length + " unread messages")