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
This commit is contained in:
Rajeh Taher
2024-05-06 17:41:43 +03:00
committed by GitHub
parent 2ef59bf924
commit 6fe7401337
2 changed files with 17 additions and 18 deletions

View File

@@ -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<LabelAssociation, string> = {
export type BaileysInMemoryStoreConfig = {
chatKey?: Comparable<Chat, string>
labelAssociationKey?: Comparable<LabelAssociation, string>
logger?: Logger
logger: Logger
socket?: WASocket
}
@@ -74,12 +74,12 @@ const predefinedLabels = Object.freeze<Record<string, Label>>({
})
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<Chat, string>
@@ -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 => {

View File

@@ -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')
}
}