fire contact update if pushname provided

This commit is contained in:
Adhiraj Singh
2021-09-27 16:48:46 +05:30
parent 07561bec53
commit 723d1d6712
2 changed files with 16 additions and 0 deletions

View File

@@ -52,6 +52,7 @@ import makeWASocket, { WASocket, AuthenticationState, DisconnectReason, AnyMessa
sock.ev.on('messages.update', m => console.log(m)) sock.ev.on('messages.update', m => console.log(m))
sock.ev.on('presence.update', m => console.log(m)) sock.ev.on('presence.update', m => console.log(m))
sock.ev.on('chats.update', m => console.log(m)) sock.ev.on('chats.update', m => console.log(m))
sock.ev.on('contacts.update', m => console.log(m))
return sock return sock
} }

View File

@@ -423,7 +423,17 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
ev.on('messages.upsert', async({ messages }) => { ev.on('messages.upsert', async({ messages }) => {
const chat: Partial<Chat> = { id: messages[0].key.remoteJid } const chat: Partial<Chat> = { id: messages[0].key.remoteJid }
const contactNameUpdates: { [_: string]: string } = { }
for(const msg of messages) { for(const msg of messages) {
if(!!msg.pushName) {
contactNameUpdates[msg.key.remoteJid!] = msg.pushName
// update our pushname too
if(msg.key.fromMe && authState.creds.me?.name !== msg.pushName) {
authState.creds.me!.name = msg.pushName
ev.emit('auth-state.update', authState)
}
}
await processMessage(msg, chat) await processMessage(msg, chat)
if(!!msg.message && !msg.message!.protocolMessage) { if(!!msg.message && !msg.message!.protocolMessage) {
chat.conversationTimestamp = toNumber(msg.messageTimestamp) chat.conversationTimestamp = toNumber(msg.messageTimestamp)
@@ -435,6 +445,11 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
if(Object.keys(chat).length > 1) { if(Object.keys(chat).length > 1) {
ev.emit('chats.update', [ chat ]) ev.emit('chats.update', [ chat ])
} }
if(Object.keys(contactNameUpdates).length) {
ev.emit('contacts.update', Object.keys(contactNameUpdates).map(
id => ({ id, notify: contactNameUpdates[id] })
))
}
}) })
return sock return sock