@@ -7,7 +7,7 @@ import type makeMDSocket from '../Socket'
77import type { BaileysEventEmitter , Chat , ConnectionState , Contact , GroupMetadata , PresenceData , WAMessage , WAMessageCursor , WAMessageKey } from '../Types'
88import { Label } from '../Types/Label'
99import { LabelAssociation , LabelAssociationType , MessageLabelAssociation } from '../Types/LabelAssociation'
10- import { toNumber , updateMessageWithReaction , updateMessageWithReceipt } from '../Utils'
10+ import { md5 , toNumber , updateMessageWithReaction , updateMessageWithReceipt } from '../Utils'
1111import { jidNormalizedUser } from '../WABinary'
1212import makeOrderedDictionary from './make-ordered-dictionary'
1313import { 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
3536const makeMessagesDictionary = ( ) => makeOrderedDictionary ( waMessageID )
@@ -73,7 +74,7 @@ const predefinedLabels = Object.freeze<Record<string, Label>>({
7374} )
7475
7576export 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