Skip to content

Commit 3b87d74

Browse files
authored
Fix pfp change notification (WhiskeySockets#323)
* Crash if chat is already deleted * Crash if chat is already deleted (WhiskeySockets#321) (WhiskeySockets#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!
1 parent 9d2f9ed commit 3b87d74

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Socket/messages-recv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
378378
const delPicture = getBinaryNodeChild(node, 'delete')
379379

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

385385
if(isJidGroup(from)) {

src/Store/make-in-memory-store.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type makeMDSocket from '../Socket'
77
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types'
88
import { Label } from '../Types/Label'
99
import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
10-
import { toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils'
10+
import { md5, toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils'
1111
import { jidNormalizedUser } from '../WABinary'
1212
import makeOrderedDictionary from './make-ordered-dictionary'
1313
import { ObjectRepository } from './object-repository'
@@ -30,6 +30,7 @@ export type BaileysInMemoryStoreConfig = {
3030
chatKey?: Comparable<Chat, string>
3131
labelAssociationKey?: Comparable<LabelAssociation, string>
3232
logger?: Logger
33+
socket?: WASocket
3334
}
3435

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

7576
export default (
76-
{ logger: _logger, chatKey, labelAssociationKey }: BaileysInMemoryStoreConfig
77+
{ logger: _logger, chatKey, labelAssociationKey, socket }: BaileysInMemoryStoreConfig
7778
) => {
7879
// const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
7980
chatKey = chatKey || waChatKey(true)
@@ -167,13 +168,31 @@ export default (
167168
contactsUpsert(contacts)
168169
})
169170

170-
ev.on('contacts.update', updates => {
171+
ev.on('contacts.update', async updates => {
171172
for(const update of updates) {
173+
let contact: Contact
172174
if(contacts[update.id!]) {
173-
Object.assign(contacts[update.id!], update)
175+
contact = contacts[update.id!]
174176
} else {
175-
logger.debug({ update }, 'got update for non-existant contact')
177+
const contactHashes = await Promise.all(Object.keys(contacts).map(async a => {
178+
return (await md5(Buffer.from(a + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)
179+
}))
180+
contact = contacts[contactHashes.find(a => a === update.id) || '']
176181
}
182+
183+
if(update.imgUrl === 'changed' || update.imgUrl === 'removed') {
184+
if(contact) {
185+
if(update.imgUrl === 'changed') {
186+
contact.imgUrl = socket ? await socket?.profilePictureUrl(contact.id) : undefined
187+
} else {
188+
delete contact.imgUrl
189+
}
190+
} else {
191+
return logger.debug({ update }, 'got update for non-existant contact')
192+
}
193+
}
194+
195+
Object.assign(contacts[update.id!], contact)
177196
}
178197
})
179198
ev.on('chats.upsert', newChats => {

0 commit comments

Comments
 (0)