Add more contact-update + better phone check

This commit is contained in:
Adhiraj Singh
2020-12-30 11:26:46 +05:30
parent ebaf9280b3
commit 100c8fd96d
5 changed files with 34 additions and 21 deletions

View File

@@ -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']

View File

@@ -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 => {

View File

@@ -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 })

View File

@@ -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 })
})

View File

@@ -466,7 +466,6 @@ export type BaileysEvent =
'ws-close' |
'qr' |
'connection-phone-change' |
'user-status-update' |
'contacts-received' |
'chats-received' |
'chat-new' |