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' import Decoder from './Binary/Decoder'
interface BrowserMessagesInfo { interface BrowserMessagesInfo {
encKey: string, bundle: { encKey: string, macKey: string }
macKey: string,
harFilePath: string harFilePath: string
} }
interface WSMessage { interface WSMessage {
@@ -14,8 +13,8 @@ interface WSMessage {
const file = fs.readFileSync ('./browser-messages.json', {encoding: 'utf-8'}) const file = fs.readFileSync ('./browser-messages.json', {encoding: 'utf-8'})
const json: BrowserMessagesInfo = JSON.parse (file) const json: BrowserMessagesInfo = JSON.parse (file)
const encKey = Buffer.from (json.encKey, 'base64') const encKey = Buffer.from (json.bundle.encKey, 'base64')
const macKey = Buffer.from (json.macKey, 'base64') const macKey = Buffer.from (json.bundle.macKey, 'base64')
const harFile = JSON.parse ( fs.readFileSync( json.harFilePath , {encoding: 'utf-8'})) const harFile = JSON.parse ( fs.readFileSync( json.harFilePath , {encoding: 'utf-8'}))
const entries = harFile['log']['entries'] 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 { promises as fs } from 'fs'
import * as assert from 'assert' import * as assert from 'assert'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { WAConnectionTest, testJid, assertChatDBIntegrity, sendAndRetreiveMessage } from './Common' import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
WAConnectionTest('Misc', conn => { WAConnectionTest('Misc', conn => {

View File

@@ -79,6 +79,7 @@ export class WAConnection extends EventEmitter {
protected encoder = new Encoder() protected encoder = new Encoder()
protected decoder = new Decoder() protected decoder = new Decoder()
protected phoneCheckInterval = undefined protected phoneCheckInterval = undefined
protected phoneCheckListeners = 0
protected referenceDate = new Date () // used for generating tags protected referenceDate = new Date () // used for generating tags
protected lastSeen: Date = null // last keep alive received 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 * @param timeoutMs timeout after which the promise will reject
*/ */
async waitForMessage(tag: string, requiresPhoneConnection: boolean, timeoutMs?: number) { async waitForMessage(tag: string, requiresPhoneConnection: boolean, timeoutMs?: number) {
if (!this.phoneCheckInterval && requiresPhoneConnection) { if (requiresPhoneConnection) {
this.startPhoneCheckInterval () this.startPhoneCheckInterval ()
} }
let onRecv: (json) => void let onRecv: (json) => void
@@ -187,7 +188,7 @@ export class WAConnection extends EventEmitter {
) )
return result as any return result as any
} finally { } finally {
requiresPhoneConnection && this.clearPhoneCheckInterval () requiresPhoneConnection && this.clearPhoneCheckInterval()
this.off (`TAG:${tag}`, onRecv) this.off (`TAG:${tag}`, onRecv)
this.off (`ws-close`, onErr) 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 */ /** interval is started when a query takes too long to respond */
protected startPhoneCheckInterval () { protected startPhoneCheckInterval () {
// if its been a long time and we haven't heard back from WA, send a ping this.phoneCheckListeners += 1
this.phoneCheckInterval = setInterval (() => { if (!this.phoneCheckInterval) {
if (!this.conn) return // if disconnected, then don't do anything // 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.logger.info('checking phone connection...')
this.sendAdminTest () this.sendAdminTest ()
this.phoneConnected = false this.phoneConnected = false
this.emit ('connection-phone-change', { connected: false }) this.emit ('connection-phone-change', { connected: false })
}, this.connectOptions.phoneResponseTime) }, this.connectOptions.phoneResponseTime)
}
} }
protected clearPhoneCheckInterval () { protected clearPhoneCheckInterval () {
this.phoneCheckInterval && clearInterval (this.phoneCheckInterval) this.phoneCheckListeners -= 1
this.phoneCheckInterval = undefined if (this.phoneCheckListeners <= 0) {
this.phoneCheckInterval && clearInterval (this.phoneCheckInterval)
this.phoneCheckInterval = undefined
this.phoneCheckListeners = 0
}
} }
/** checks for phone connection */ /** checks for phone connection */
protected async sendAdminTest () { protected async sendAdminTest () {
@@ -383,7 +393,9 @@ export class WAConnection extends EventEmitter {
this.initTimeout && clearTimeout (this.initTimeout) this.initTimeout && clearTimeout (this.initTimeout)
this.debounceTimeout && clearTimeout (this.debounceTimeout) this.debounceTimeout && clearTimeout (this.debounceTimeout)
this.keepAliveReq && clearInterval(this.keepAliveReq) this.keepAliveReq && clearInterval(this.keepAliveReq)
this.phoneCheckListeners = 0
this.clearPhoneCheckInterval () this.clearPhoneCheckInterval ()
this.emit ('ws-close', { reason: DisconnectReason.close }) this.emit ('ws-close', { reason: DisconnectReason.close })

View File

@@ -235,6 +235,7 @@ export class WAConnection extends Base {
user.jid = whatsappID(user.jid) user.jid = whatsappID(user.jid)
this.contacts[user.jid] = user this.contacts[user.jid] = user
this.emit('contact-update', user)
const chat = this.chats.get (user.jid) const chat = this.chats.get (user.jid)
if (chat) { if (chat) {
@@ -289,7 +290,10 @@ export class WAConnection extends Base {
const jid = whatsappID(json[1].jid) const jid = whatsappID(json[1].jid)
const imgUrl = await this.getProfilePicture(jid).catch(() => '') const imgUrl = await this.getProfilePicture(jid).catch(() => '')
const contact = this.contacts[jid] 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) const chat = this.chats.get(jid)
if (chat) { if (chat) {
@@ -301,7 +305,6 @@ export class WAConnection extends Base {
this.on('CB:Status,status', async json => { this.on('CB:Status,status', async json => {
const jid = whatsappID(json[1].id) const jid = whatsappID(json[1].id)
this.emit ('contact-update', { jid, status: json[1].status }) this.emit ('contact-update', { jid, status: json[1].status })
// emit deprecated // emit deprecated
this.emit ('user-status-update', { jid, status: json[1].status }) this.emit ('user-status-update', { jid, status: json[1].status })
}) })

View File

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