Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pfp change notification #323

Merged
merged 13 commits into from
Apr 28, 2024
4 changes: 2 additions & 2 deletions src/Socket/messages-recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const delPicture = getBinaryNodeChild(node, 'delete')

ev.emit('contacts.update', [{
id: from,
imgUrl: setPicture ? 'changed' : null
id: jidNormalizedUser(node?.attrs?.jid) || ((setPicture || delPicture)?.attrs?.hash) || '',
imgUrl: setPicture ? 'changed' : 'removed'
}])

if(isJidGroup(from)) {
Expand Down
33 changes: 27 additions & 6 deletions src/Store/make-in-memory-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type makeMDSocket from '../Socket'
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types'
import { Label } from '../Types/Label'
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 makeOrderedDictionary from './make-ordered-dictionary'
import { ObjectRepository } from './object-repository'
Expand All @@ -30,6 +30,7 @@ export type BaileysInMemoryStoreConfig = {
chatKey?: Comparable<Chat, string>
labelAssociationKey?: Comparable<LabelAssociation, string>
logger?: Logger
socket?: WASocket
}

const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID)
Expand Down Expand Up @@ -73,7 +74,7 @@ const predefinedLabels = Object.freeze<Record<string, Label>>({
})

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' })
chatKey = chatKey || waChatKey(true)
Expand Down Expand Up @@ -167,13 +168,31 @@ export default (
contactsUpsert(contacts)
})

ev.on('contacts.update', updates => {
ev.on('contacts.update', async updates => {
for(const update of updates) {
let contact: Contact
if(contacts[update.id!]) {
Object.assign(contacts[update.id!], update)
contact = contacts[update.id!]
} 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 => {
Expand Down Expand Up @@ -227,7 +246,9 @@ export default (
})
ev.on('chats.delete', deletions => {
for(const item of deletions) {
chats.deleteById(item)
if(chats.get(item)) {
chats.deleteById(item)
}
}
})
ev.on('messages.upsert', ({ messages: newMessages, type }) => {
Expand Down