import KeyedDB from '@adiwajshing/keyed-db' import type { Comparable } from '@adiwajshing/keyed-db/lib/Types' import type { Logger } from 'pino' import { proto } from '../../WAProto' import { DEFAULT_CONNECTION_CONFIG } from '../Defaults' import type makeMDSocket from '../Socket' import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types' import { Label } from '../Types/Label' import { LabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation' import { toNumber, updateMessageWithReaction, updateMessageWithReceipt } from '../Utils' import { jidNormalizedUser } from '../WABinary' import makeOrderedDictionary from './make-ordered-dictionary' import { ObjectRepository } from './object-repository' type WASocket = ReturnType export const waChatKey = (pin: boolean) => ({ key: (c: Chat) => (pin ? (c.pinned ? '1' : '0') : '') + (c.archived ? '0' : '1') + (c.conversationTimestamp ? c.conversationTimestamp.toString(16).padStart(8, '0') : '') + c.id, compare: (k1: string, k2: string) => k2.localeCompare(k1) }) export const waMessageID = (m: WAMessage) => m.key.id || '' export const waLabelAssociationKey: Comparable = { key: (la: LabelAssociation) => (la.type === LabelAssociationType.Chat ? la.chatId + la.labelId : la.chatId + la.messageId + la.labelId), compare: (k1: string, k2: string) => k2.localeCompare(k1) } export type BaileysInMemoryStoreConfig = { chatKey?: Comparable labelAssociationKey?: Comparable logger?: Logger } const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID) const predefinedLabels = Object.freeze>({ '0': { id: '0', name: 'New customer', predefinedId: '0', color: 0, deleted: false }, '1': { id: '1', name: 'New order', predefinedId: '1', color: 1, deleted: false }, '2': { id: '2', name: 'Pending payment', predefinedId: '2', color: 2, deleted: false }, '3': { id: '3', name: 'Paid', predefinedId: '3', color: 3, deleted: false }, '4': { id: '4', name: 'Order completed', predefinedId: '4', color: 4, deleted: false } }) export default ( { logger: _logger, chatKey, labelAssociationKey }: 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' }) // const KeyedDB = require('@adiwajshing/keyed-db').default as new (...args: any[]) => KeyedDB const chats = new KeyedDB(chatKey, c => c.id) const messages: { [_: string]: ReturnType } = {} const contacts: { [_: string]: Contact } = {} const groupMetadata: { [_: string]: GroupMetadata } = {} const presences: { [id: string]: { [participant: string]: PresenceData } } = {} const state: ConnectionState = { connection: 'close' } const labels = new ObjectRepository