From 6cc5a910f7636ef6ac02d9f86055e1ef1b4f3e34 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 4 Jan 2021 14:19:44 +0530 Subject: [PATCH] Completely remove message-status-update & user-status-update --- Example/example.ts | 2 +- README.md | 20 +++++++++++--------- src/Tests/Tests.Messages.ts | 8 +++++--- src/Tests/Tests.Misc.ts | 4 ++-- src/WAConnection/4.Events.ts | 5 ----- src/WAConnection/5.User.ts | 3 --- src/WAConnection/6.MessagesSend.ts | 10 +++++++--- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Example/example.ts b/Example/example.ts index adcbab9..a6ecff2 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -50,7 +50,7 @@ async function example() { /** * The universal event for anything that happens - * New messages, updated messages, read & delivered messages + * New messages, updated messages, read & delivered messages, participants typing etc. */ conn.on('chat-update', async chat => { if (chat.presences) { // receive presence updates -- composing, available, etc. diff --git a/README.md b/README.md index 5beb4c0..5722976 100644 --- a/README.md +++ b/README.md @@ -193,26 +193,28 @@ on (event: 'credentials-updated', listener: (auth: AuthenticationCredentials) => on (event: 'qr', listener: (qr: string) => void): this /** when the connection to the phone changes */ on (event: 'connection-phone-change', listener: (state: {connected: boolean}) => void): this -/** when a user's status is updated */ -on (event: 'user-status-update', listener: (update: {jid: string, status?: string}) => void): this +/** when a contact is updated */ +on (event: 'contact-update', listener: (update: WAContactUpdate) => void): this /** when a new chat is added */ on (event: 'chat-new', listener: (chat: WAChat) => void): this /** when contacts are sent by WA */ -on (event: 'contacts-received', listener: () => void): this -/** when chats are sent by WA, and when all messages are received from WhatsApp */ -on (event: 'chats-received', listener: (update: {hasNewChats?: boolean, hasReceivedLastMessage?: boolean}) => void): this +on (event: 'contacts-received', listener: (u: { updatedContacts: Partial[] }) => void): this +/** when chats are sent by WA, and when all messages are received */ +on (event: 'chats-received', listener: (update: {hasNewChats?: boolean}) => void): this +/** when all initial messages are received from WA */ +on (event: 'initial-data-received', listener: (update: {chatsWithMissingMessages: { jid: string, count: number }[] }) => void): this /** when multiple chats are updated (new message, updated message, deleted, pinned, etc) */ on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this -/** when a chat is updated (new message, updated message, deleted, pinned, presence updated etc) */ -on (event: 'chat-update', listener: (chat: Partial & { hasNewMessage: boolean }) => void): this -/** when a message's status is updated (deleted, delivered, read, sent etc.) */ -on (event: 'message-status-update', listener: (message: WAMessageStatusUpdate) => void): this +/** when a chat is updated (new message, updated message, read message, deleted, pinned, presence updated etc) */ +on (event: 'chat-update', listener: (chat: WAChatUpdate) => void): this /** when participants are added to a group */ on (event: 'group-participants-update', listener: (update: {jid: string, participants: string[], actor?: string, action: WAParticipantAction}) => void): this /** when the group is updated */ on (event: 'group-update', listener: (update: Partial & {jid: string, actor?: string}) => void): this /** when WA sends back a pong */ on (event: 'received-pong', listener: () => void): this +/** when a user is blocked or unblockd */ +on (event: 'blocklist-update', listener: (update: BlocklistUpdate) => void): this ``` ## Sending Messages diff --git a/src/Tests/Tests.Messages.ts b/src/Tests/Tests.Messages.ts index ba2ec15..1444a99 100644 --- a/src/Tests/Tests.Messages.ts +++ b/src/Tests/Tests.Messages.ts @@ -12,9 +12,11 @@ WAConnectionTest('Messages', conn => { it('should send a pending message', async () => { const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text, { waitForAck: false }) - await new Promise(resolve => conn.once('message-status-update', update => { - if (update.ids.includes(message.key.id)) { - assert.strictEqual(update.type, WA_MESSAGE_STATUS_TYPE.SERVER_ACK) + await new Promise(resolve => conn.on('chat-update', update => { + if (update.jid === testJid && + update.messages && + update.messages.first.key.id === message.key.id && + update.messages.first.status === WA_MESSAGE_STATUS_TYPE.SERVER_ACK) { resolve(undefined) } })) diff --git a/src/Tests/Tests.Misc.ts b/src/Tests/Tests.Misc.ts index 635267e..bfa0cb1 100644 --- a/src/Tests/Tests.Misc.ts +++ b/src/Tests/Tests.Misc.ts @@ -21,10 +21,10 @@ WAConnectionTest('Misc', conn => { const newStatus = 'v cool status' const waitForEvent = new Promise (resolve => { - conn.on ('user-status-update', ({jid, status}) => { + conn.on ('contact-update', ({jid, status}) => { if (jid === conn.user.jid) { assert.strictEqual (status, newStatus) - conn.removeAllListeners ('user-status-update') + conn.removeAllListeners ('contact-update') resolve(undefined) } }) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index df67d96..c20f99a 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -625,11 +625,6 @@ export class WAConnection extends Base { on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this /** when a chat is updated (new message, updated message, read message, deleted, pinned, presence updated etc) */ on (event: 'chat-update', listener: (chat: WAChatUpdate) => void): this - /** - * when a message's status is updated (deleted, delivered, read, sent etc.) - * @deprecated will not be called anymore. Use `chat-update` - * */ - on (event: 'message-status-update', listener: (message: WAMessageStatusUpdate) => void): this /** when participants are added to a group */ on (event: 'group-participants-update', listener: (update: {jid: string, participants: string[], actor?: string, action: WAParticipantAction}) => void): this /** when the group is updated */ diff --git a/src/WAConnection/5.User.ts b/src/WAConnection/5.User.ts index e0f5963..1e3a206 100644 --- a/src/WAConnection/5.User.ts +++ b/src/WAConnection/5.User.ts @@ -78,9 +78,6 @@ export class WAConnection extends Base { ] ) this.emit ('contact-update', { jid: this.user.jid, status }) - - // emit deprecated - this.emit ('user-status-update', { jid: this.user.jid, status }) return response } async updateProfileName (name: string) { diff --git a/src/WAConnection/6.MessagesSend.ts b/src/WAConnection/6.MessagesSend.ts index c1b0249..05f0c2d 100644 --- a/src/WAConnection/6.MessagesSend.ts +++ b/src/WAConnection/6.MessagesSend.ts @@ -11,7 +11,7 @@ import { WATextMessage, WAMessageContent, WAMetric, WAFlag, WAMessage, BaileysError, WA_MESSAGE_STATUS_TYPE, WAMessageProto, MediaConnInfo, MessageTypeProto, URL_REGEX, WAUrlInfo, WA_DEFAULT_EPHEMERAL } from './Constants' -import { generateMessageID, sha256, hmacSign, aesEncrypWithIV, randomBytes, generateThumbnail, getMediaKeys, decodeMediaMessageBuffer, extensionForMediaMessage, whatsappID, unixTimestampSeconds, getAudioDuration } from './Utils' +import { generateMessageID, sha256, hmacSign, aesEncrypWithIV, randomBytes, generateThumbnail, getMediaKeys, decodeMediaMessageBuffer, extensionForMediaMessage, whatsappID, unixTimestampSeconds, getAudioDuration, newMessagesDB } from './Utils' import { Mutex } from './Mutex' export class WAConnection extends Base { @@ -288,9 +288,13 @@ export class WAConnection extends Base { if (waitForAck) { await promise } else { + const emitUpdate = (status: WA_MESSAGE_STATUS_TYPE) => { + message.status = status + this.emit('chat-update', { jid: message.key.remoteJid, messages: newMessagesDB([ message ]) }) + } promise - .then(() => this.emit('message-status-update', { ids: [ mID ], to: message.key.remoteJid, type: WA_MESSAGE_STATUS_TYPE.SERVER_ACK })) - .catch(() => this.emit('message-status-update', { ids: [ mID ], to: message.key.remoteJid, type: WA_MESSAGE_STATUS_TYPE.ERROR })) + .then(() => emitUpdate(WA_MESSAGE_STATUS_TYPE.SERVER_ACK)) + .catch(() => emitUpdate(WA_MESSAGE_STATUS_TYPE.ERROR)) } await this.chatAddMessageAppropriate (message) }