Android chat read fix + debounceTimeout bug fix

This commit is contained in:
Adhiraj Singh
2020-11-03 13:24:56 +05:30
parent e0fd554aa0
commit 155df2bc4f
5 changed files with 33 additions and 25 deletions

View File

@@ -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",

View File

@@ -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 = {}

View File

@@ -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)

View File

@@ -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 */

View File

@@ -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) {