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
* 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.

View File

@@ -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<WAContact>[] }) => 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<WAChat> & { 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<WAGroupMetadata> & {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

View File

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

View File

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

View File

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

View File

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

View File

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