Move msgCount increment to generateMessageTag

Prevents duplicate tags from being sent
This commit is contained in:
Adhiraj Singh
2021-01-24 17:25:49 +05:30
parent e1952ff349
commit 63dd136afa
4 changed files with 28 additions and 14 deletions

View File

@@ -15,7 +15,7 @@ import * as fs from 'fs'
async function example() {
const conn = new WAConnection() // instantiate
conn.autoReconnect = ReconnectMode.onConnectionLost // only automatically reconnect when the connection breaks
conn.logger.level = 'trace' // set to 'debug' to see what kind of stuff you can implement
conn.logger.level = 'debug' // set to 'debug' to see what kind of stuff you can implement
// attempt to reconnect at most 10 times in a row
conn.connectOptions.maxRetries = 10
conn.chatOrderingKey = waChatKey(true) // order chats such that pinned chats are on top
@@ -38,7 +38,6 @@ async function example() {
fs.writeFileSync('./auth_info.json', JSON.stringify(authInfo, null, '\t')) // save this info to a file
console.log('oh hello ' + conn.user.name + ' (' + conn.user.jid + ')')
// uncomment to load all unread messages
//const unread = await conn.loadAllUnreadMessages ()
//console.log ('you have ' + unread.length + ' unread messages')

View File

@@ -75,6 +75,26 @@ WAConnectionTest('Misc', conn => {
it('should return the stories', async () => {
await conn.getStories()
})
it('should return the profile picture correctly', async () => {
// wait for chats
await new Promise(resolve => (
conn.once('initial-data-received', resolve)
))
const pictures = await Promise.all(
conn.chats.all().slice(0, 15).map(({ jid }) => (
conn.getProfilePicture(jid)
.catch(err => '')
))
)
// pictures should return correctly
const truePictures = pictures.filter(pp => !!pp)
assert.strictEqual(
new Set(truePictures).size,
truePictures.length
)
})
it('should change the profile picture', async () => {
await delay (5000)
@@ -88,11 +108,6 @@ WAConnectionTest('Misc', conn => {
await conn.updateProfilePicture (conn.user.jid, oldPP) // revert back
})
it('should return the profile picture', async () => {
const response = await conn.getProfilePicture(testJid)
assert.ok(response)
assert.rejects(conn.getProfilePicture('abcd@s.whatsapp.net'))
})
it('should send typing indicator', async () => {
const response = await conn.updatePresence(testJid, Presence.composing)
assert.ok(response)

View File

@@ -223,17 +223,17 @@ export class WAConnection extends EventEmitter {
requiresPhoneConnection = requiresPhoneConnection !== false
waitForOpen = waitForOpen !== false
let triesLeft = maxRetries || 2
tag = tag || this.generateMessageTag (longTag)
tag = tag || this.generateMessageTag(longTag)
while (triesLeft >= 0) {
if (waitForOpen) await this.waitForConnection()
const promise = this.waitForMessage(tag, requiresPhoneConnection, timeoutMs)
if (this.logger.level === 'trace') {
this.logger.trace ({ fromMe: true },`${tag},${JSON.stringify(json)}`)
}
if (binaryTags) tag = await this.sendBinary(json as WANode, binaryTags, tag)
else tag = await this.sendJSON(json, tag)
@@ -369,7 +369,6 @@ export class WAConnection extends EventEmitter {
}
/** Send some message to the WhatsApp servers */
protected async send(m) {
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
this.conn.send(m)
}
protected async waitForConnection () {
@@ -477,6 +476,8 @@ export class WAConnection extends EventEmitter {
)
generateMessageTag (longTag: boolean = false) {
const seconds = Utils.unixTimestampSeconds(this.referenceDate)
return `${longTag ? seconds : (seconds%1000)}.--${this.msgCount}`
const tag = `${longTag ? seconds : (seconds%1000)}.--${this.msgCount}`
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
return tag
}
}

View File

@@ -14,7 +14,6 @@ export class WAConnection extends Base {
}
const canLogin = this.canLogin()
this.referenceDate = new Date () // refresh reference date
let isNewUser = false
this.startDebouncedTimeout ()