mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
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:
@@ -8,7 +8,7 @@ import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata
|
|||||||
import { Label } from '../Types/Label'
|
import { Label } from '../Types/Label'
|
||||||
import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
|
import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
|
||||||
import { md5, toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils'
|
import { md5, toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils'
|
||||||
import { jidNormalizedUser } from '../WABinary'
|
import { jidDecode, jidNormalizedUser } from '../WABinary'
|
||||||
import makeOrderedDictionary from './make-ordered-dictionary'
|
import makeOrderedDictionary from './make-ordered-dictionary'
|
||||||
import { ObjectRepository } from './object-repository'
|
import { ObjectRepository } from './object-repository'
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ export const waLabelAssociationKey: Comparable<LabelAssociation, string> = {
|
|||||||
export type BaileysInMemoryStoreConfig = {
|
export type BaileysInMemoryStoreConfig = {
|
||||||
chatKey?: Comparable<Chat, string>
|
chatKey?: Comparable<Chat, string>
|
||||||
labelAssociationKey?: Comparable<LabelAssociation, string>
|
labelAssociationKey?: Comparable<LabelAssociation, string>
|
||||||
logger?: Logger
|
logger: Logger
|
||||||
socket?: WASocket
|
socket?: WASocket
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,12 +74,12 @@ const predefinedLabels = Object.freeze<Record<string, Label>>({
|
|||||||
})
|
})
|
||||||
|
|
||||||
export default (
|
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' })
|
// const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
|
||||||
chatKey = chatKey || waChatKey(true)
|
chatKey = chatKey || waChatKey(true)
|
||||||
labelAssociationKey = labelAssociationKey || waLabelAssociationKey
|
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 KeyedDB = require('@adiwajshing/keyed-db').default
|
||||||
|
|
||||||
const chats = new KeyedDB(chatKey, c => c.id) as KeyedDB<Chat, string>
|
const chats = new KeyedDB(chatKey, c => c.id) as KeyedDB<Chat, string>
|
||||||
@@ -174,25 +174,24 @@ export default (
|
|||||||
if(contacts[update.id!]) {
|
if(contacts[update.id!]) {
|
||||||
contact = contacts[update.id!]
|
contact = contacts[update.id!]
|
||||||
} else {
|
} else {
|
||||||
const contactHashes = await Promise.all(Object.keys(contacts).map(async a => {
|
const contactHashes = await Promise.all(Object.keys(contacts).map(async contactId => {
|
||||||
return (await md5(Buffer.from(a + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)
|
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(contact) {
|
if(update.imgUrl === 'changed') {
|
||||||
if(update.imgUrl === 'changed') {
|
contact.imgUrl = socket ? await socket?.profilePictureUrl(contact.id) : undefined
|
||||||
contact.imgUrl = socket ? await socket?.profilePictureUrl(contact.id) : undefined
|
} else if(update.imgUrl === 'removed') {
|
||||||
} else {
|
delete contact.imgUrl
|
||||||
delete contact.imgUrl
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return logger.debug({ update }, 'got update for non-existant contact')
|
|
||||||
}
|
}
|
||||||
|
} 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 => {
|
ev.on('chats.upsert', newChats => {
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ export const decryptMessageNode = (
|
|||||||
authorJid: author,
|
authorJid: author,
|
||||||
item: msg.senderKeyDistributionMessage
|
item: msg.senderKeyDistributionMessage
|
||||||
})
|
})
|
||||||
}catch (err) {
|
} catch(err) {
|
||||||
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
|
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user