diff --git a/package.json b/package.json index 28e6602..d7cceb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adiwajshing/baileys", - "version": "3.2.3", + "version": "3.2.4", "description": "WhatsApp Web API", "homepage": "https://github.com/adiwajshing/Baileys", "main": "lib/WAConnection/WAConnection.js", diff --git a/src/Tests/Common.ts b/src/Tests/Common.ts index 82fb402..7cbba23 100644 --- a/src/Tests/Common.ts +++ b/src/Tests/Common.ts @@ -7,7 +7,7 @@ export const testJid = process.env.TEST_JID || '1234@s.whatsapp.net' // set TEST export const makeConnection = () => { const conn = new WAConnection() - conn.connectOptions.maxIdleTimeMs = 30_000 + conn.connectOptions.maxIdleTimeMs = 45_000 conn.logger.level = 'debug' let evCounts = {} diff --git a/src/Tests/Tests.Misc.ts b/src/Tests/Tests.Misc.ts index 333cade..698377c 100644 --- a/src/Tests/Tests.Misc.ts +++ b/src/Tests/Tests.Misc.ts @@ -71,21 +71,24 @@ WAConnectionTest('Misc', (conn) => { assert.ok(response) }) it('should change a chat read status', async () => { - const waitForEvent = new Promise (resolve => { - conn.on ('chat-update', ({jid, count}) => { - if (jid === testJid) { - assert.ok (count < 0) - conn.removeAllListeners ('chat-update') - resolve () - } + const jids = conn.chats.all ().map (c => c.jid) + for (let jid of jids.slice(0, 5)) { + console.log (`changing read status for ${jid}`) + const waitForEvent = new Promise (resolve => { + conn.once ('chat-update', ({jid: tJid, count}) => { + if (jid === tJid) { + assert.ok (count < 0) + resolve () + } + }) }) - }) - await conn.chatRead (testJid, 'unread') - await waitForEvent - - await delay (5000) - - await conn.chatRead (testJid, 'read') + await conn.chatRead (jid, 'unread') + await waitForEvent + + await delay (5000) + + await conn.chatRead (jid, 'read') + } }) it('should archive & unarchive', async () => { await conn.modifyChat (testJid, ChatModification.archive) diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index ed5dda1..7a013b0 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -43,7 +43,7 @@ export class WAConnection extends EventEmitter { state: WAConnectionState = 'close' connectOptions: WAConnectOptions = { regenerateQRIntervalMs: 30_000, - maxIdleTimeMs: 15_000, + maxIdleTimeMs: 40_000, waitOnlyForLastMessage: false, waitForChats: true, maxRetries: 10, @@ -219,6 +219,10 @@ export class WAConnection extends EventEmitter { tag = tag || this.generateMessageTag (longTag) const promise = this.waitForMessage(tag, json, 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) @@ -238,7 +242,7 @@ export class WAConnection extends EventEmitter { {query: json, message, status: response.status} ) } - if (startDebouncedTimeout) this.stopDebouncedTimeout () + if (startDebouncedTimeout) this.startDebouncedTimeout () return response } /** interval is started when a query takes too long to respond */ diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 140910c..1319ab8 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -44,10 +44,10 @@ export class WAConnection extends Base { jid = whatsappID (jid) const chat = this.assertChatGet (jid) - if (type === 'unread') await this.sendReadReceipt (jid, null, -2) - else if (chat.count !== 0) { - const {messages} = await this.loadMessages (jid, 1) - await this.sendReadReceipt (jid, messages[0].key, Math.abs(chat.count)) + const message = (await this.loadMessages(jid, 1)).messages[0] + const count = type === 'unread' ? -2 : Math.abs(chat.count) + if (type === 'unread' || chat.count !== 0) { + await this.sendReadReceipt (jid, message.key, count) } chat.count = type === 'unread' ? -1 : 0 this.emit ('chat-update', {jid, count: chat.count}) @@ -59,14 +59,15 @@ export class WAConnection extends Base { * @param messageKey the key of the message * @param count number of messages to read, set to < 0 to unread a message */ - async sendReadReceipt(jid: string, messageKey: { id?: string, fromMe?: boolean }, count: number) { + async sendReadReceipt(jid: string, messageKey: WAMessageKey, count: number) { const attributes = { - jid: jid, + jid, count: count.toString(), index: messageKey?.id, + participant: messageKey?.participant || undefined, owner: messageKey?.fromMe?.toString() } - const read = await this.setQuery ([['read', attributes, null]]) + const read = await this.setQuery ([['read', attributes, null]], [ WAMetric.read, WAFlag.ignore ]) return read } async fetchMessagesFromWA (jid: string, count: number, indexMessage?: { id?: string; fromMe?: boolean }, mostRecentFirst: boolean = true) {