From 100c8fd96d2c05a4e9672ae898dc1cdba67b89d0 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 30 Dec 2020 11:26:46 +0530 Subject: [PATCH] Add more contact-update + better phone check --- src/BrowserMessageDecoding.ts | 7 +++---- src/Tests/Tests.Misc.ts | 2 +- src/WAConnection/0.Base.ts | 38 +++++++++++++++++++++++------------ src/WAConnection/4.Events.ts | 7 +++++-- src/WAConnection/Constants.ts | 1 - 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/BrowserMessageDecoding.ts b/src/BrowserMessageDecoding.ts index c9634f9..455679f 100644 --- a/src/BrowserMessageDecoding.ts +++ b/src/BrowserMessageDecoding.ts @@ -3,8 +3,7 @@ import { decryptWA } from './WAConnection/WAConnection' import Decoder from './Binary/Decoder' interface BrowserMessagesInfo { - encKey: string, - macKey: string, + bundle: { encKey: string, macKey: string } harFilePath: string } interface WSMessage { @@ -14,8 +13,8 @@ interface WSMessage { const file = fs.readFileSync ('./browser-messages.json', {encoding: 'utf-8'}) const json: BrowserMessagesInfo = JSON.parse (file) -const encKey = Buffer.from (json.encKey, 'base64') -const macKey = Buffer.from (json.macKey, 'base64') +const encKey = Buffer.from (json.bundle.encKey, 'base64') +const macKey = Buffer.from (json.bundle.macKey, 'base64') const harFile = JSON.parse ( fs.readFileSync( json.harFilePath , {encoding: 'utf-8'})) const entries = harFile['log']['entries'] diff --git a/src/Tests/Tests.Misc.ts b/src/Tests/Tests.Misc.ts index 61fcc3a..705b754 100644 --- a/src/Tests/Tests.Misc.ts +++ b/src/Tests/Tests.Misc.ts @@ -2,7 +2,7 @@ import { Presence, ChatModification, delay, newMessagesDB, WA_DEFAULT_EPHEMERAL, import { promises as fs } from 'fs' import * as assert from 'assert' import fetch from 'node-fetch' -import { WAConnectionTest, testJid, assertChatDBIntegrity, sendAndRetreiveMessage } from './Common' +import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common' WAConnectionTest('Misc', conn => { diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index 8348cb4..36abbb7 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -79,6 +79,7 @@ export class WAConnection extends EventEmitter { protected encoder = new Encoder() protected decoder = new Decoder() protected phoneCheckInterval = undefined + protected phoneCheckListeners = 0 protected referenceDate = new Date () // used for generating tags protected lastSeen: Date = null // last keep alive received @@ -171,7 +172,7 @@ export class WAConnection extends EventEmitter { * @param timeoutMs timeout after which the promise will reject */ async waitForMessage(tag: string, requiresPhoneConnection: boolean, timeoutMs?: number) { - if (!this.phoneCheckInterval && requiresPhoneConnection) { + if (requiresPhoneConnection) { this.startPhoneCheckInterval () } let onRecv: (json) => void @@ -187,7 +188,7 @@ export class WAConnection extends EventEmitter { ) return result as any } finally { - requiresPhoneConnection && this.clearPhoneCheckInterval () + requiresPhoneConnection && this.clearPhoneCheckInterval() this.off (`TAG:${tag}`, onRecv) this.off (`ws-close`, onErr) } @@ -242,20 +243,29 @@ export class WAConnection extends EventEmitter { } /** interval is started when a query takes too long to respond */ protected startPhoneCheckInterval () { - // if its been a long time and we haven't heard back from WA, send a ping - this.phoneCheckInterval = setInterval (() => { - if (!this.conn) return // if disconnected, then don't do anything + this.phoneCheckListeners += 1 + if (!this.phoneCheckInterval) { + // if its been a long time and we haven't heard back from WA, send a ping + this.phoneCheckInterval = setInterval (() => { + if (!this.conn) return // if disconnected, then don't do anything - this.logger.debug ('checking phone connection...') - this.sendAdminTest () - - this.phoneConnected = false - this.emit ('connection-phone-change', { connected: false }) - }, this.connectOptions.phoneResponseTime) + this.logger.info('checking phone connection...') + this.sendAdminTest () + + this.phoneConnected = false + this.emit ('connection-phone-change', { connected: false }) + }, this.connectOptions.phoneResponseTime) + } + } protected clearPhoneCheckInterval () { - this.phoneCheckInterval && clearInterval (this.phoneCheckInterval) - this.phoneCheckInterval = undefined + this.phoneCheckListeners -= 1 + if (this.phoneCheckListeners <= 0) { + this.phoneCheckInterval && clearInterval (this.phoneCheckInterval) + this.phoneCheckInterval = undefined + this.phoneCheckListeners = 0 + } + } /** checks for phone connection */ protected async sendAdminTest () { @@ -383,7 +393,9 @@ export class WAConnection extends EventEmitter { this.initTimeout && clearTimeout (this.initTimeout) this.debounceTimeout && clearTimeout (this.debounceTimeout) this.keepAliveReq && clearInterval(this.keepAliveReq) + this.phoneCheckListeners = 0 this.clearPhoneCheckInterval () + this.emit ('ws-close', { reason: DisconnectReason.close }) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 2456a67..257e402 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -235,6 +235,7 @@ export class WAConnection extends Base { user.jid = whatsappID(user.jid) this.contacts[user.jid] = user + this.emit('contact-update', user) const chat = this.chats.get (user.jid) if (chat) { @@ -289,7 +290,10 @@ export class WAConnection extends Base { const jid = whatsappID(json[1].jid) const imgUrl = await this.getProfilePicture(jid).catch(() => '') const contact = this.contacts[jid] - if (contact) contact.imgUrl = imgUrl + if (contact) { + contact.imgUrl = imgUrl + this.emit('contact-update', { jid, imgUrl }) + } const chat = this.chats.get(jid) if (chat) { @@ -301,7 +305,6 @@ export class WAConnection extends Base { this.on('CB:Status,status', async json => { const jid = whatsappID(json[1].id) this.emit ('contact-update', { jid, status: json[1].status }) - // emit deprecated this.emit ('user-status-update', { jid, status: json[1].status }) }) diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 63c922b..1b98a68 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -466,7 +466,6 @@ export type BaileysEvent = 'ws-close' | 'qr' | 'connection-phone-change' | - 'user-status-update' | 'contacts-received' | 'chats-received' | 'chat-new' |