Completely remove message-status-update & user-status-update

This commit is contained in:
Adhiraj Singh
2021-01-04 14:19:44 +05:30
parent ac4b4c7384
commit 6cc5a910f7
7 changed files with 26 additions and 26 deletions

View File

@@ -50,7 +50,7 @@ async function example() {
/** /**
* The universal event for anything that happens * 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 => { conn.on('chat-update', async chat => {
if (chat.presences) { // receive presence updates -- composing, available, etc. if (chat.presences) { // receive presence updates -- composing, available, etc.

View File

@@ -193,26 +193,28 @@ on (event: 'credentials-updated', listener: (auth: AuthenticationCredentials) =>
on (event: 'qr', listener: (qr: string) => void): this on (event: 'qr', listener: (qr: string) => void): this
/** when the connection to the phone changes */ /** when the connection to the phone changes */
on (event: 'connection-phone-change', listener: (state: {connected: boolean}) => void): this on (event: 'connection-phone-change', listener: (state: {connected: boolean}) => void): this
/** when a user's status is updated */ /** when a contact is updated */
on (event: 'user-status-update', listener: (update: {jid: string, status?: string}) => void): this on (event: 'contact-update', listener: (update: WAContactUpdate) => void): this
/** when a new chat is added */ /** when a new chat is added */
on (event: 'chat-new', listener: (chat: WAChat) => void): this on (event: 'chat-new', listener: (chat: WAChat) => void): this
/** when contacts are sent by WA */ /** when contacts are sent by WA */
on (event: 'contacts-received', listener: () => void): this on (event: 'contacts-received', listener: (u: { updatedContacts: Partial<WAContact>[] }) => void): this
/** when chats are sent by WA, and when all messages are received from WhatsApp */ /** when chats are sent by WA, and when all messages are received */
on (event: 'chats-received', listener: (update: {hasNewChats?: boolean, hasReceivedLastMessage?: boolean}) => void): this 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) */ /** when multiple chats are updated (new message, updated message, deleted, pinned, etc) */
on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this
/** when a chat is updated (new message, updated message, deleted, pinned, presence updated etc) */ /** when a chat is updated (new message, updated message, read message, deleted, pinned, presence updated etc) */
on (event: 'chat-update', listener: (chat: Partial<WAChat> & { hasNewMessage: boolean }) => void): this on (event: 'chat-update', listener: (chat: WAChatUpdate) => 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 participants are added to a group */ /** when participants are added to a group */
on (event: 'group-participants-update', listener: (update: {jid: string, participants: string[], actor?: string, action: WAParticipantAction}) => void): this on (event: 'group-participants-update', listener: (update: {jid: string, participants: string[], actor?: string, action: WAParticipantAction}) => void): this
/** when the group is updated */ /** when the group is updated */
on (event: 'group-update', listener: (update: Partial<WAGroupMetadata> & {jid: string, actor?: string}) => void): this on (event: 'group-update', listener: (update: Partial<WAGroupMetadata> & {jid: string, actor?: string}) => void): this
/** when WA sends back a pong */ /** when WA sends back a pong */
on (event: 'received-pong', listener: () => void): this 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 ## Sending Messages

View File

@@ -12,9 +12,11 @@ WAConnectionTest('Messages', conn => {
it('should send a pending message', async () => { it('should send a pending message', async () => {
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text, { waitForAck: false }) const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text, { waitForAck: false })
await new Promise(resolve => conn.once('message-status-update', update => { await new Promise(resolve => conn.on('chat-update', update => {
if (update.ids.includes(message.key.id)) { if (update.jid === testJid &&
assert.strictEqual(update.type, WA_MESSAGE_STATUS_TYPE.SERVER_ACK) update.messages &&
update.messages.first.key.id === message.key.id &&
update.messages.first.status === WA_MESSAGE_STATUS_TYPE.SERVER_ACK) {
resolve(undefined) resolve(undefined)
} }
})) }))

View File

@@ -21,10 +21,10 @@ WAConnectionTest('Misc', conn => {
const newStatus = 'v cool status' const newStatus = 'v cool status'
const waitForEvent = new Promise (resolve => { const waitForEvent = new Promise (resolve => {
conn.on ('user-status-update', ({jid, status}) => { conn.on ('contact-update', ({jid, status}) => {
if (jid === conn.user.jid) { if (jid === conn.user.jid) {
assert.strictEqual (status, newStatus) assert.strictEqual (status, newStatus)
conn.removeAllListeners ('user-status-update') conn.removeAllListeners ('contact-update')
resolve(undefined) resolve(undefined)
} }
}) })

View File

@@ -625,11 +625,6 @@ export class WAConnection extends Base {
on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this 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) */ /** 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 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 */ /** when participants are added to a group */
on (event: 'group-participants-update', listener: (update: {jid: string, participants: string[], actor?: string, action: WAParticipantAction}) => void): this on (event: 'group-participants-update', listener: (update: {jid: string, participants: string[], actor?: string, action: WAParticipantAction}) => void): this
/** when the group is updated */ /** when the group is updated */

View File

@@ -78,9 +78,6 @@ export class WAConnection extends Base {
] ]
) )
this.emit ('contact-update', { jid: this.user.jid, status }) this.emit ('contact-update', { jid: this.user.jid, status })
// emit deprecated
this.emit ('user-status-update', { jid: this.user.jid, status })
return response return response
} }
async updateProfileName (name: string) { async updateProfileName (name: string) {

View File

@@ -11,7 +11,7 @@ import {
WATextMessage, WATextMessage,
WAMessageContent, WAMetric, WAFlag, WAMessage, BaileysError, WA_MESSAGE_STATUS_TYPE, WAMessageProto, MediaConnInfo, MessageTypeProto, URL_REGEX, WAUrlInfo, WA_DEFAULT_EPHEMERAL WAMessageContent, WAMetric, WAFlag, WAMessage, BaileysError, WA_MESSAGE_STATUS_TYPE, WAMessageProto, MediaConnInfo, MessageTypeProto, URL_REGEX, WAUrlInfo, WA_DEFAULT_EPHEMERAL
} from './Constants' } 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' import { Mutex } from './Mutex'
export class WAConnection extends Base { export class WAConnection extends Base {
@@ -288,9 +288,13 @@ export class WAConnection extends Base {
if (waitForAck) { if (waitForAck) {
await promise await promise
} else { } else {
const emitUpdate = (status: WA_MESSAGE_STATUS_TYPE) => {
message.status = status
this.emit('chat-update', { jid: message.key.remoteJid, messages: newMessagesDB([ message ]) })
}
promise promise
.then(() => this.emit('message-status-update', { ids: [ mID ], to: message.key.remoteJid, type: WA_MESSAGE_STATUS_TYPE.SERVER_ACK })) .then(() => emitUpdate(WA_MESSAGE_STATUS_TYPE.SERVER_ACK))
.catch(() => this.emit('message-status-update', { ids: [ mID ], to: message.key.remoteJid, type: WA_MESSAGE_STATUS_TYPE.ERROR })) .catch(() => emitUpdate(WA_MESSAGE_STATUS_TYPE.ERROR))
} }
await this.chatAddMessageAppropriate (message) await this.chatAddMessageAppropriate (message)
} }