From 4fbb331015b7a90ee8047d9e10ede3df2a747fe4 Mon Sep 17 00:00:00 2001 From: devlikepro Date: Fri, 2 May 2025 13:31:08 +0700 Subject: [PATCH] [core] Copy-paste in-memory store Got removed from the upstream --- .../engines/noweb/store/NowebInMemoryStore.ts | 2 +- .../store/memory/make-in-memory-store.ts | 564 ++++++++++++++++++ .../store/memory/make-ordered-dictionary.ts | 86 +++ .../noweb/store/memory/object-repository.ts | 32 + 4 files changed, 683 insertions(+), 1 deletion(-) create mode 100644 src/core/engines/noweb/store/memory/make-in-memory-store.ts create mode 100644 src/core/engines/noweb/store/memory/make-ordered-dictionary.ts create mode 100644 src/core/engines/noweb/store/memory/object-repository.ts diff --git a/src/core/engines/noweb/store/NowebInMemoryStore.ts b/src/core/engines/noweb/store/NowebInMemoryStore.ts index 3ae061c6..7f8ab5ef 100644 --- a/src/core/engines/noweb/store/NowebInMemoryStore.ts +++ b/src/core/engines/noweb/store/NowebInMemoryStore.ts @@ -2,7 +2,6 @@ import makeWASocket, { Chat, Contact, GroupMetadata, - makeInMemoryStore, proto, } from '@adiwajshing/baileys'; import { Label } from '@adiwajshing/baileys/lib/Types/Label'; @@ -12,6 +11,7 @@ import { PaginationParams } from '@waha/structures/pagination.dto'; import { PaginatorInMemory } from '@waha/utils/Paginator'; import { INowebStore } from './INowebStore'; +import makeInMemoryStore from './memory/make-in-memory-store'; // eslint-disable-next-line @typescript-eslint/no-var-requires const logger = require('pino')(); diff --git a/src/core/engines/noweb/store/memory/make-in-memory-store.ts b/src/core/engines/noweb/store/memory/make-in-memory-store.ts new file mode 100644 index 00000000..0d6f9819 --- /dev/null +++ b/src/core/engines/noweb/store/memory/make-in-memory-store.ts @@ -0,0 +1,564 @@ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ + +import type { + BaileysEventEmitter, + Chat, + ConnectionState, + Contact, + GroupMetadata, + PresenceData, + WAMessage, + WAMessageCursor, + WAMessageKey, +} from '@adiwajshing/baileys/lib/Types'; +import { Label } from '@adiwajshing/baileys/lib/Types/Label'; +import { + LabelAssociation, + LabelAssociationType, + MessageLabelAssociation, +} from '@adiwajshing/baileys/lib/Types/LabelAssociation'; +import { + md5, + toNumber, + updateMessageWithReaction, + updateMessageWithReceipt, +} from '@adiwajshing/baileys/lib/Utils'; +import { + jidDecode, + jidNormalizedUser, +} from '@adiwajshing/baileys/lib/WABinary'; +import { proto } from '@adiwajshing/baileys/WAProto'; +import type { Comparable } from '@adiwajshing/keyed-db/lib/Types'; +import type { Logger } from 'pino'; + +import makeOrderedDictionary from './make-ordered-dictionary'; +import { ObjectRepository } from './object-repository'; + +// eslint-disable-next-line @typescript-eslint/no-var-requires +const KeyedDB = require('@adiwajshing/keyed-db').default; +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { writeFileSync } = require('fs'); +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { readFileSync, existsSync } = require('fs'); + +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; + socket?: WASocket; +}; + +const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID); + +export default (config: BaileysInMemoryStoreConfig) => { + const socket = config.socket; + const chatKey = config.chatKey || waChatKey(true); + const labelAssociationKey = + config.labelAssociationKey || waLabelAssociationKey; + const logger: Logger = config.logger; + + const chats = new KeyedDB(chatKey, (c) => c.id) as any; + const messages: { [_: string]: ReturnType } = + {}; + const contacts: { [_: string]: Contact } = {}; + const groupMetadata: { [_: string]: GroupMetadata } = {}; + let presences: { [id: string]: { [participant: string]: PresenceData } } = {}; + const state: ConnectionState = { connection: 'close' }; + const labels = new ObjectRepository