diff --git a/Example/example.ts b/Example/example.ts index 6f2c036..d07e208 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -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') diff --git a/src/Tests/Tests.Misc.ts b/src/Tests/Tests.Misc.ts index 9329791..ad5e7da 100644 --- a/src/Tests/Tests.Misc.ts +++ b/src/Tests/Tests.Misc.ts @@ -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) diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index f953f83..7c0e974 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -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 } } diff --git a/src/WAConnection/1.Validation.ts b/src/WAConnection/1.Validation.ts index 7c4ac16..3d29c25 100644 --- a/src/WAConnection/1.Validation.ts +++ b/src/WAConnection/1.Validation.ts @@ -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 ()