Fix ping not happening more than once

This commit is contained in:
Adhiraj Singh
2021-03-11 13:52:52 +05:30
parent e670ff003e
commit 4c2f954601
4 changed files with 32 additions and 35 deletions

View File

@@ -94,7 +94,9 @@ export class WAConnection extends EventEmitter {
1000, 1000,
() => this.state === 'connecting' && this.endConnection(DisconnectReason.timedOut) () => this.state === 'connecting' && this.endConnection(DisconnectReason.timedOut)
) )
// timeout to know when we're done recieving messages
protected messagesDebounceTimeout = Utils.debouncedTimeout(2000) protected messagesDebounceTimeout = Utils.debouncedTimeout(2000)
// ping chats till recieved
protected chatsDebounceTimeout = Utils.debouncedTimeout(10_000) protected chatsDebounceTimeout = Utils.debouncedTimeout(10_000)
/** /**
* Connect to WhatsAppWeb * Connect to WhatsAppWeb

View File

@@ -96,37 +96,8 @@ export class WAConnection extends Base {
this.user = result.user this.user = result.user
this.logger.info('validated connection successfully') this.logger.info('validated connection successfully')
this.sendPostConnectQueries ()
this.logger.debug('sent init queries')
return result
}
/**
* Send the same queries WA Web sends after connect
*/
sendPostConnectQueries () {
this.sendBinary (['query', {type: 'contacts', epoch: '1'}, null], [ WAMetric.queryContact, WAFlag.ignore ])
this.sendBinary (['query', {type: 'status', epoch: '1'}, null], [ WAMetric.queryStatus, WAFlag.ignore ])
this.sendBinary (['query', {type: 'quick_reply', epoch: '1'}, null], [ WAMetric.queryQuickReply, WAFlag.ignore ])
this.sendBinary (['query', {type: 'label', epoch: '1'}, null], [ WAMetric.queryLabel, WAFlag.ignore ])
this.sendBinary (['query', {type: 'emoji', epoch: '1'}, null], [ WAMetric.queryEmoji, WAFlag.ignore ])
this.sendBinary (['action', {type: 'set', epoch: '1'}, [['presence', {type: Presence.available}, null]] ], [ WAMetric.presence, WAFlag.available ])
if(this.connectOptions.queryChatsTillReceived) { return result
this.chatsDebounceTimeout.start(
undefined,
() => {
this.logger.debug('pinging with chats query')
this.sendChatsQuery(this.msgCount)
}
)
} else {
this.sendChatsQuery(1)
}
}
protected sendChatsQuery(epoch: number) {
return this.sendBinary(['query', {type: 'chat', epoch: epoch.toString()}, null], [ WAMetric.queryChat, WAFlag.ignore ])
} }
/** /**
* Refresh QR Code * Refresh QR Code

View File

@@ -1,6 +1,6 @@
import * as QR from 'qrcode-terminal' import * as QR from 'qrcode-terminal'
import { WAConnection as Base } from './3.Connect' import { WAConnection as Base } from './3.Connect'
import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate, BlocklistUpdate, WAContactUpdate } from './Constants' import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate, BlocklistUpdate, WAContactUpdate, WAMetric, WAFlag } from './Constants'
import { whatsappID, unixTimestampSeconds, isGroupID, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils' import { whatsappID, unixTimestampSeconds, isGroupID, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils'
import KeyedDB from '@adiwajshing/keyed-db' import KeyedDB from '@adiwajshing/keyed-db'
import { Mutex } from './Mutex' import { Mutex } from './Mutex'
@@ -10,6 +10,29 @@ export class WAConnection extends Base {
constructor () { constructor () {
super () super ()
this.setMaxListeners (30) this.setMaxListeners (30)
this.chatsDebounceTimeout.setTask(() => {
this.logger.debug('pinging with chats query')
this.sendChatsQuery(this.msgCount)
this.chatsDebounceTimeout.start()
})
this.on('open', () => {
// send queries WA Web expects
this.sendBinary (['query', {type: 'contacts', epoch: '1'}, null], [ WAMetric.queryContact, WAFlag.ignore ])
this.sendBinary (['query', {type: 'status', epoch: '1'}, null], [ WAMetric.queryStatus, WAFlag.ignore ])
this.sendBinary (['query', {type: 'quick_reply', epoch: '1'}, null], [ WAMetric.queryQuickReply, WAFlag.ignore ])
this.sendBinary (['query', {type: 'label', epoch: '1'}, null], [ WAMetric.queryLabel, WAFlag.ignore ])
this.sendBinary (['query', {type: 'emoji', epoch: '1'}, null], [ WAMetric.queryEmoji, WAFlag.ignore ])
this.sendBinary (['action', {type: 'set', epoch: '1'}, [['presence', {type: Presence.available}, null]] ], [ WAMetric.presence, WAFlag.available ])
if(this.connectOptions.queryChatsTillReceived) {
this.chatsDebounceTimeout.start()
} else {
this.sendChatsQuery(1)
}
this.logger.debug('sent init queries')
})
// on disconnects // on disconnects
this.on('CB:Cmd,type:disconnect', json => ( this.on('CB:Cmd,type:disconnect', json => (
this.state === 'open' && this.unexpectedDisconnect(json[1].kind || 'unknown') this.state === 'open' && this.unexpectedDisconnect(json[1].kind || 'unknown')
@@ -379,6 +402,9 @@ export class WAConnection extends Base {
this.emit('blocklist-update', update) this.emit('blocklist-update', update)
}) })
} }
protected sendChatsQuery(epoch: number) {
return this.sendBinary(['query', {type: 'chat', epoch: epoch.toString()}, null], [ WAMetric.queryChat, WAFlag.ignore ])
}
/** Get the URL to download the profile picture of a person/group */ /** Get the URL to download the profile picture of a person/group */
@Mutex (jid => jid) @Mutex (jid => jid)
async getProfilePicture(jid: string | null) { async getProfilePicture(jid: string | null) {

View File

@@ -172,10 +172,8 @@ export class WAConnection extends Base {
// if there are still more messages // if there are still more messages
if (messages.length >= chunkSize) { if (messages.length >= chunkSize) {
offsetID = lastMessage.key // get the last message offsetID = lastMessage.key // get the last message
return new Promise((resolve, reject) => { await delay(200)
// send query after 200 ms return loadMessage()
setTimeout(() => loadMessage().then(resolve).catch(reject), 200)
})
} }
} }
return loadMessage() as Promise<void> return loadMessage() as Promise<void>