From 47021c34bf083e3d9acc629a37ed510225c2484b Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Wed, 19 Aug 2020 17:43:30 +0530 Subject: [PATCH] correct message updates --- Example/example.ts | 6 +++--- src/Tests/Tests.Connect.ts | 1 - src/WAConnection/3.Connect.ts | 6 +++++- src/WAConnection/4.Events.ts | 23 +++++++++++++++-------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Example/example.ts b/Example/example.ts index 92323ea..d484285 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -32,9 +32,9 @@ async function example() { /* Note: one can take this auth_info.json file and login again from any computer without having to scan the QR code, and get full access to one's WhatsApp. Despite the convenience, be careful with this file */ conn.on ('user-presence-update', json => console.log(json.id + ' presence is ' + json.type)) - conn.on ('message-update', message => { - //const participant = json.participant ? ' (' + json.participant + ')' : '' // participant exists when the message is from a group - //console.log(`${json.to}${participant} acknlowledged message(s) ${json.ids} as ${json.type}`) + conn.on ('message-update', json => { + const participant = json.participant ? ' (' + json.participant + ')' : '' // participant exists when the message is from a group + console.log(`${json.to}${participant} acknlowledged message(s) ${json.ids} as ${json.type}`) }) // set to false to NOT relay your own sent messages conn.on('message-new', async (m) => { diff --git a/src/Tests/Tests.Connect.ts b/src/Tests/Tests.Connect.ts index 8deb6fe..432ee47 100644 --- a/src/Tests/Tests.Connect.ts +++ b/src/Tests/Tests.Connect.ts @@ -116,7 +116,6 @@ describe ('Reconnects', () => { // let it fail reconnect a few times if (closes > 4) { - console.log ('here') conn.removeAllListeners ('closed') conn.removeAllListeners ('connecting') resolve () diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index dee6fec..9b9af95 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -1,6 +1,6 @@ import WS from 'ws' import * as Utils from './Utils' -import { WAMessage, WAChat, WAContact, MessageLogLevel, WANode, KEEP_ALIVE_INTERVAL_MS } from './Constants' +import { WAMessage, WAChat, WAContact, MessageLogLevel, WANode, KEEP_ALIVE_INTERVAL_MS, BaileysError } from './Constants' import {WAConnection as Base} from './1.Validation' import Decoder from '../Binary/Decoder' @@ -266,6 +266,10 @@ export class WAConnection extends Base { this.cancelReconnect = null break } catch (error) { + // don't continue reconnecting if error is 400 + if (error instanceof BaileysError && error.status >= 400) { + break + } this.log (`error in reconnecting: ${error}, reconnecting...`, MessageLogLevel.info) } } diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index a0c9697..c3f01f8 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -127,7 +127,8 @@ export class WAConnection extends Base { const chat = this.chats.get( whatsappID(update.to) ) if (!chat) return - + + this.emit ('message-update', update) this.chatUpdatedMessage (update.ids, update.type, chat) } this.registerCallback('Msg', func) @@ -183,11 +184,19 @@ export class WAConnection extends Base { case WAMessageProto.ProtocolMessage.PROTOCOL_MESSAGE_TYPE.REVOKE: const found = chat.messages.find(m => m.key.id === protocolMessage.key.id) if (found && found.message) { - //this.log ('deleting message: ' + protocolMessage.key.id + ' in chat: ' + protocolMessage.key.remoteJid) + + this.log ('deleting message: ' + protocolMessage.key.id + ' in chat: ' + protocolMessage.key.remoteJid, MessageLogLevel.info) + found.messageStubType = WA_MESSAGE_STUB_TYPE.REVOKE found.message = null - - this.emit ('message-update', found) + const update: MessageStatusUpdate = { + from: this.user.id, + to: message.key.remoteJid, + ids: [message.key.id], + timestamp: new Date(), + type: -1 + } + this.emit ('message-update', update) } break default: @@ -249,8 +258,6 @@ export class WAConnection extends Base { if (messageIDs.includes(msg.key.id)) { if (isGroupID(chat.jid)) msg.status = WA_MESSAGE_STATUS_TYPE.SERVER_ACK else msg.status = status - - this.emit ('message-update', msg) } } } @@ -294,8 +301,8 @@ export class WAConnection extends Base { on (event: 'chat-update', listener: (chat: Partial & { jid: string }) => void): this /** when a new message is relayed */ on (event: 'message-new', listener: (message: WAMessage) => void): this - /** when a message is updated (deleted, delivered, read) */ - on (event: 'message-update', listener: (message: WAMessage) => void): this + /** when a message is updated (deleted, delivered, read, sent etc.) */ + on (event: 'message-update', listener: (message: MessageStatusUpdate) => void): this /** when participants are added to a group */ on (event: 'group-participants-add', listener: (update: {jid: string, participants: string[], actor?: string}) => void): this /** when participants are removed or leave from a group */