From ce019dcf0f0e3c3f1a8c57b776aa5fe39854b598 Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Sun, 14 May 2023 12:50:45 -0500 Subject: [PATCH] feat(store/labels): add ability to store and query labels --- src/Store/make-in-memory-store.ts | 296 +++++++++++++++++++++--------- src/Store/object-repository.ts | 31 ++++ 2 files changed, 243 insertions(+), 84 deletions(-) create mode 100644 src/Store/object-repository.ts diff --git a/src/Store/make-in-memory-store.ts b/src/Store/make-in-memory-store.ts index f8d7a6b..5787b24 100644 --- a/src/Store/make-in-memory-store.ts +++ b/src/Store/make-in-memory-store.ts @@ -1,46 +1,97 @@ -import type KeyedDB from '@adiwajshing/keyed-db' +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) + 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) -export default ( - { logger: _logger, chatKey }: BaileysInMemoryStoreConfig -) => { - const logger = _logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' }) - chatKey = chatKey || waChatKey(true) - const KeyedDB = require('@adiwajshing/keyed-db').default as new (...args: any[]) => KeyedDB +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 + } +}) - 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 } } = { } +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