Skip to content

Commit

Permalink
Fix pfp change notification (#323)
Browse files Browse the repository at this point in the history
* 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!
  • Loading branch information
codebossdev authored Apr 28, 2024
1 parent 9d2f9ed commit 3b87d74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Socket/messages-recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,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
29 changes: 24 additions & 5 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

0 comments on commit 3b87d74

Please sign in to comment.