mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Fix pfp change notification (#323)
* Crash if chat is already deleted * Crash if chat is already deleted (#321) (#322) * Update messages-recv.ts * Update make-in-memory-store.ts * Update messages-recv.ts * Update make-in-memory-store.ts * Fix store errors * Fix hash issue * Fix eslint * Update messages-recv.ts * Update messages-recv.ts * ESLint final * no more eslint errors!
This commit is contained in:
@@ -378,8 +378,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
const delPicture = getBinaryNodeChild(node, 'delete')
|
const delPicture = getBinaryNodeChild(node, 'delete')
|
||||||
|
|
||||||
ev.emit('contacts.update', [{
|
ev.emit('contacts.update', [{
|
||||||
id: from,
|
id: jidNormalizedUser(node?.attrs?.jid) || ((setPicture || delPicture)?.attrs?.hash) || '',
|
||||||
imgUrl: setPicture ? 'changed' : null
|
imgUrl: setPicture ? 'changed' : 'removed'
|
||||||
}])
|
}])
|
||||||
|
|
||||||
if(isJidGroup(from)) {
|
if(isJidGroup(from)) {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type makeMDSocket from '../Socket'
|
|||||||
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types'
|
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types'
|
||||||
import { Label } from '../Types/Label'
|
import { Label } from '../Types/Label'
|
||||||
import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
|
import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
|
||||||
import { toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils'
|
import { md5, toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils'
|
||||||
import { jidNormalizedUser } from '../WABinary'
|
import { jidNormalizedUser } from '../WABinary'
|
||||||
import makeOrderedDictionary from './make-ordered-dictionary'
|
import makeOrderedDictionary from './make-ordered-dictionary'
|
||||||
import { ObjectRepository } from './object-repository'
|
import { ObjectRepository } from './object-repository'
|
||||||
@@ -30,6 +30,7 @@ export type BaileysInMemoryStoreConfig = {
|
|||||||
chatKey?: Comparable<Chat, string>
|
chatKey?: Comparable<Chat, string>
|
||||||
labelAssociationKey?: Comparable<LabelAssociation, string>
|
labelAssociationKey?: Comparable<LabelAssociation, string>
|
||||||
logger?: Logger
|
logger?: Logger
|
||||||
|
socket?: WASocket
|
||||||
}
|
}
|
||||||
|
|
||||||
const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID)
|
const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID)
|
||||||
@@ -73,7 +74,7 @@ const predefinedLabels = Object.freeze<Record<string, Label>>({
|
|||||||
})
|
})
|
||||||
|
|
||||||
export default (
|
export default (
|
||||||
{ logger: _logger, chatKey, labelAssociationKey }: BaileysInMemoryStoreConfig
|
{ logger: _logger, chatKey, labelAssociationKey, socket }: BaileysInMemoryStoreConfig
|
||||||
) => {
|
) => {
|
||||||
// const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
|
// const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
|
||||||
chatKey = chatKey || waChatKey(true)
|
chatKey = chatKey || waChatKey(true)
|
||||||
@@ -167,13 +168,31 @@ export default (
|
|||||||
contactsUpsert(contacts)
|
contactsUpsert(contacts)
|
||||||
})
|
})
|
||||||
|
|
||||||
ev.on('contacts.update', updates => {
|
ev.on('contacts.update', async updates => {
|
||||||
for(const update of updates) {
|
for(const update of updates) {
|
||||||
|
let contact: Contact
|
||||||
if(contacts[update.id!]) {
|
if(contacts[update.id!]) {
|
||||||
Object.assign(contacts[update.id!], update)
|
contact = contacts[update.id!]
|
||||||
} else {
|
} else {
|
||||||
logger.debug({ update }, 'got update for non-existant contact')
|
const contactHashes = await Promise.all(Object.keys(contacts).map(async a => {
|
||||||
|
return (await md5(Buffer.from(a + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)
|
||||||
|
}))
|
||||||
|
contact = contacts[contactHashes.find(a => a === update.id) || '']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(update.imgUrl === 'changed' || update.imgUrl === 'removed') {
|
||||||
|
if(contact) {
|
||||||
|
if(update.imgUrl === 'changed') {
|
||||||
|
contact.imgUrl = socket ? await socket?.profilePictureUrl(contact.id) : undefined
|
||||||
|
} else {
|
||||||
|
delete contact.imgUrl
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return logger.debug({ update }, 'got update for non-existant contact')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(contacts[update.id!], contact)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
ev.on('chats.upsert', newChats => {
|
ev.on('chats.upsert', newChats => {
|
||||||
|
|||||||
Reference in New Issue
Block a user