From 6fe74013373dd596562acd5bacca12edcbe50239 Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Mon, 6 May 2024 17:41:43 +0300 Subject: [PATCH] Update make-in-memory-store.ts (#755) * Update make-in-memory-store.ts * fix logger error (#754) * Update make-in-memory-store.ts * Update make-in-memory-store.ts * fix linting * will we ever fix linting? * final change * test * simplified * final fix * comments * linting --- src/Store/make-in-memory-store.ts | 33 +++++++++++++++---------------- src/Utils/decode-wa-message.ts | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Store/make-in-memory-store.ts b/src/Store/make-in-memory-store.ts index 96dc749..99d50af 100644 --- a/src/Store/make-in-memory-store.ts +++ b/src/Store/make-in-memory-store.ts @@ -8,7 +8,7 @@ import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata import { Label } from '../Types/Label' import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation' import { md5, toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils' -import { jidNormalizedUser } from '../WABinary' +import { jidDecode, jidNormalizedUser } from '../WABinary' import makeOrderedDictionary from './make-ordered-dictionary' import { ObjectRepository } from './object-repository' @@ -29,7 +29,7 @@ export const waLabelAssociationKey: Comparable = { export type BaileysInMemoryStoreConfig = { chatKey?: Comparable labelAssociationKey?: Comparable - logger?: Logger + logger: Logger socket?: WASocket } @@ -74,12 +74,12 @@ const predefinedLabels = Object.freeze>({ }) export default ( - { logger: _logger, chatKey, labelAssociationKey, socket }: BaileysInMemoryStoreConfig + { logger, chatKey, labelAssociationKey, socket }: BaileysInMemoryStoreConfig ) => { // const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' }) chatKey = chatKey || waChatKey(true) labelAssociationKey = labelAssociationKey || waLabelAssociationKey - const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' }) + logger = logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' }) const KeyedDB = require('@adiwajshing/keyed-db').default const chats = new KeyedDB(chatKey, c => c.id) as KeyedDB @@ -174,25 +174,24 @@ export default ( if(contacts[update.id!]) { contact = contacts[update.id!] } else { - 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) + const contactHashes = await Promise.all(Object.keys(contacts).map(async contactId => { + const { user } = jidDecode(contactId)! + return [contactId, (await md5(Buffer.from(user + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)] })) - contact = contacts[contactHashes.find(a => a === update.id) || ''] + contact = contacts[contactHashes.find(([, b]) => b === update.id)?.[0] || ''] // find contact by attrs.hash, when user is not saved as a contact } - 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') + if(contact) { + if(update.imgUrl === 'changed') { + contact.imgUrl = socket ? await socket?.profilePictureUrl(contact.id) : undefined + } else if(update.imgUrl === 'removed') { + delete contact.imgUrl } + } else { + return logger.debug({ update }, 'got update for non-existant contact') } - Object.assign(contacts[update.id!], contact) + Object.assign(contacts[contact.id], contact) } }) ev.on('chats.upsert', newChats => { diff --git a/src/Utils/decode-wa-message.ts b/src/Utils/decode-wa-message.ts index 9d5b609..3760865 100644 --- a/src/Utils/decode-wa-message.ts +++ b/src/Utils/decode-wa-message.ts @@ -175,7 +175,7 @@ export const decryptMessageNode = ( authorJid: author, item: msg.senderKeyDistributionMessage }) - }catch (err) { + } catch(err) { logger.error({ key: fullMessage.key, err }, 'failed to decrypt message') } }