Close before terminate + update presence on message receive

This commit is contained in:
Adhiraj Singh
2020-10-11 23:08:48 +05:30
parent 357069ec2c
commit 210b467b68
6 changed files with 54 additions and 15 deletions

View File

@@ -30,7 +30,7 @@ import { STATUS_CODES, Agent } from 'http'
export class WAConnection extends EventEmitter {
/** The version of WhatsApp Web we're telling the servers we are */
version: [number, number, number] = [2, 2039, 9]
version: [number, number, number] = [2, 2041, 6]
/** The Browser we're telling the WhatsApp Web servers we are */
browserDescription: [string, string, string] = Utils.Browsers.baileys ('Chrome')
/** Metadata like WhatsApp id, name set on WhatsApp etc. */
@@ -47,7 +47,7 @@ export class WAConnection extends EventEmitter {
waitOnlyForLastMessage: false,
waitForChats: true,
maxRetries: 5,
connectCooldownMs: 2250,
connectCooldownMs: 3000,
phoneResponseTime: 7500
}
/** When to auto-reconnect */
@@ -333,7 +333,6 @@ export class WAConnection extends EventEmitter {
this.log (`closed connection, reason ${reason}${isReconnecting ? ', reconnecting in a few seconds...' : ''}`, MessageLogLevel.info)
this.qrTimeout && clearTimeout (this.qrTimeout)
this.keepAliveReq && clearInterval(this.keepAliveReq)
this.debounceTimeout && clearTimeout (this.debounceTimeout)
this.state = 'close'
@@ -355,8 +354,14 @@ export class WAConnection extends EventEmitter {
this.conn?.removeAllListeners ('error')
this.conn?.removeAllListeners ('open')
this.conn?.removeAllListeners ('message')
this.conn?.terminate()
this.keepAliveReq && clearInterval(this.keepAliveReq)
try {
this.conn?.close()
this.conn?.terminate()
} catch {
}
this.conn = null
this.lastSeen = null
this.msgCount = 0

View File

@@ -115,7 +115,6 @@ export class WAConnection extends Base {
let credsChanged = false
// if we didn't get a secret, we don't need it, we're validated
if (json.clientToken && json.clientToken !== this.authInfo.clientToken) {
console.log (`change: ${this.authInfo.clientToken}, ${json.clientToken}`)
this.authInfo = { ...this.authInfo, clientToken: json.clientToken }
credsChanged = true
}

View File

@@ -52,7 +52,6 @@ export class WAConnection extends Base {
this.emit ('open', result)
this.releasePendingRequests ()
this.startKeepAliveRequest()
this.log ('opened connection to WhatsApp Web', MessageLogLevel.info)
@@ -114,6 +113,9 @@ export class WAConnection extends Base {
}
try {
await this.authenticate (startDebouncedTimeout, stopDebouncedTimeout, reconnectID)
this.startKeepAliveRequest()
this.conn
.removeAllListeners ('error')
.removeAllListeners ('close')
@@ -269,7 +271,6 @@ export class WAConnection extends Base {
resolveTask = resolve
cancelChats = () => reject (CancelledError())
})
console.log ('resolved task')
const oldChats = this.chats
const updatedChats: { [k: string]: Partial<WAChat> } = {}

View File

@@ -221,8 +221,14 @@ export class WAConnection extends Base {
}
protected chatAddMessage (message: WAMessage, chat: WAChat) {
// add to count if the message isn't from me & there exists a message
if (!message.key.fromMe && message.message) chat.count += 1
if (!message.key.fromMe && message.message) {
chat.count += 1
const contact = this.contacts[chat.jid]
if (contact && contact.lastKnownPresence === Presence.composing) { // update presence
contact.lastKnownPresence = Presence.available // emit change
this.emit ('user-presence-update', { id: chat.jid, presence: Presence.available, participant: message.participant })
}
}
const protocolMessage = message.message?.protocolMessage
// if it's a message to delete another message
if (protocolMessage) {