diff --git a/src/core/abc/DataStore.ts b/src/core/abc/DataStore.ts index feadadb6..44f9b327 100644 --- a/src/core/abc/DataStore.ts +++ b/src/core/abc/DataStore.ts @@ -1,3 +1,5 @@ export abstract class DataStore { abstract init(sessionName?: string): Promise; + + abstract close(): Promise; } diff --git a/src/core/abc/manager.abc.ts b/src/core/abc/manager.abc.ts index 24ab4c5a..209bcac1 100644 --- a/src/core/abc/manager.abc.ts +++ b/src/core/abc/manager.abc.ts @@ -2,6 +2,7 @@ import { BeforeApplicationShutdown, UnprocessableEntityException, } from '@nestjs/common'; +import { ISessionMeRepository } from '@waha/core/storage/ISessionMeRepository'; import { WAHAWebhook } from '@waha/structures/webhooks.dto'; import { waitUntil } from '@waha/utils/promiseTimeout'; import { VERSION } from '@waha/version'; @@ -27,6 +28,7 @@ export abstract class SessionManager implements BeforeApplicationShutdown { public store: any; public sessionAuthRepository: ISessionAuthRepository; public sessionConfigRepository: ISessionConfigRepository; + protected sessionMeRepository: ISessionMeRepository; public events: EventEmitter; WAIT_STATUS_INTERVAL = 500; WAIT_STATUS_TIMEOUT = 5_000; @@ -37,8 +39,6 @@ export abstract class SessionManager implements BeforeApplicationShutdown { protected abstract get WebhookConductorClass(): typeof WebhookConductor; - abstract beforeApplicationShutdown(signal?: string); - // // API Methods // @@ -110,4 +110,8 @@ export abstract class SessionManager implements BeforeApplicationShutdown { } return session; } + + async beforeApplicationShutdown(signal?: string) { + this.events.removeAllListeners(); + } } diff --git a/src/core/engines/noweb/store/Schema.ts b/src/core/engines/noweb/store/Schema.ts index 9b447c19..ef09952d 100644 --- a/src/core/engines/noweb/store/Schema.ts +++ b/src/core/engines/noweb/store/Schema.ts @@ -1,24 +1,4 @@ -export class Field { - constructor( - public fieldName: string, - public type: string, - ) {} -} - -export class Index { - constructor( - public name: string, - public columns: string[], - ) {} -} - -export class Schema { - constructor( - public name: string, - public columns: Field[], - public indexes: Index[], - ) {} -} +import { Field, Index, Schema } from '@waha/core/storage/sqlite3/Schema'; export const NOWEB_STORE_SCHEMA = [ new Schema( diff --git a/src/core/engines/noweb/store/sqlite3/NOWEBSqlite3KVRepository.ts b/src/core/engines/noweb/store/sqlite3/NOWEBSqlite3KVRepository.ts new file mode 100644 index 00000000..2d479933 --- /dev/null +++ b/src/core/engines/noweb/store/sqlite3/NOWEBSqlite3KVRepository.ts @@ -0,0 +1,28 @@ +import { BufferJSON } from '@adiwajshing/baileys/lib/Utils'; +import { + convertProtobufToPlainObject, + replaceLongsWithNumber, +} from '@waha/core/engines/noweb/utils'; +import { Sqlite3KVRepository } from '@waha/core/storage/sqlite3/Sqlite3KVRepository'; + +/** + * Key value repository with extra metadata + * Add support for converting protobuf to plain object + */ +export class NOWEBSqlite3KVRepository< + Entity, +> extends Sqlite3KVRepository { + protected stringify(data: any): string { + return JSON.stringify(data, BufferJSON.replacer); + } + + protected parse(row: any): any { + return JSON.parse(row.data, BufferJSON.reviver); + } + + protected dump(entity: Entity) { + const raw = convertProtobufToPlainObject(entity); + replaceLongsWithNumber(raw); + return super.dump(raw); + } +} diff --git a/src/core/engines/noweb/store/sqlite3/Sqlite3ChatRepository.ts b/src/core/engines/noweb/store/sqlite3/Sqlite3ChatRepository.ts index 909e4eca..76624c4c 100644 --- a/src/core/engines/noweb/store/sqlite3/Sqlite3ChatRepository.ts +++ b/src/core/engines/noweb/store/sqlite3/Sqlite3ChatRepository.ts @@ -1,10 +1,10 @@ import { Chat } from '@adiwajshing/baileys'; import { IChatRepository } from '../IChatRepository'; -import { Sqlite3KVRepository } from './Sqlite3KVRepository'; +import { NOWEBSqlite3KVRepository } from './NOWEBSqlite3KVRepository'; export class Sqlite3ChatRepository - extends Sqlite3KVRepository + extends NOWEBSqlite3KVRepository implements IChatRepository { async getAllWithMessages(limit?: number, offset?: number): Promise { diff --git a/src/core/engines/noweb/store/sqlite3/Sqlite3ContactRepository.ts b/src/core/engines/noweb/store/sqlite3/Sqlite3ContactRepository.ts index 9d6ec954..11db26aa 100644 --- a/src/core/engines/noweb/store/sqlite3/Sqlite3ContactRepository.ts +++ b/src/core/engines/noweb/store/sqlite3/Sqlite3ContactRepository.ts @@ -1,8 +1,8 @@ import { Contact } from '@adiwajshing/baileys'; import { IContactRepository } from '../IContactRepository'; -import { Sqlite3KVRepository } from './Sqlite3KVRepository'; +import { NOWEBSqlite3KVRepository } from './NOWEBSqlite3KVRepository'; export class Sqlite3ContactRepository - extends Sqlite3KVRepository + extends NOWEBSqlite3KVRepository implements IContactRepository {} diff --git a/src/core/engines/noweb/store/sqlite3/Sqlite3LabelAssociationsRepository.ts b/src/core/engines/noweb/store/sqlite3/Sqlite3LabelAssociationsRepository.ts index 56d2bc90..adcd30e5 100644 --- a/src/core/engines/noweb/store/sqlite3/Sqlite3LabelAssociationsRepository.ts +++ b/src/core/engines/noweb/store/sqlite3/Sqlite3LabelAssociationsRepository.ts @@ -4,10 +4,10 @@ import { } from '@adiwajshing/baileys/lib/Types/LabelAssociation'; import { ILabelAssociationRepository } from '@waha/core/engines/noweb/store/ILabelAssociationsRepository'; -import { Sqlite3KVRepository } from './Sqlite3KVRepository'; +import { NOWEBSqlite3KVRepository } from './NOWEBSqlite3KVRepository'; export class Sqlite3LabelAssociationsRepository - extends Sqlite3KVRepository + extends NOWEBSqlite3KVRepository implements ILabelAssociationRepository { async deleteOne(association: LabelAssociation): Promise { diff --git a/src/core/engines/noweb/store/sqlite3/Sqlite3LabelsRepository.ts b/src/core/engines/noweb/store/sqlite3/Sqlite3LabelsRepository.ts index 72e2d748..416a289a 100644 --- a/src/core/engines/noweb/store/sqlite3/Sqlite3LabelsRepository.ts +++ b/src/core/engines/noweb/store/sqlite3/Sqlite3LabelsRepository.ts @@ -1,8 +1,8 @@ import { Label } from '@adiwajshing/baileys/lib/Types/Label'; import { ILabelsRepository } from '@waha/core/engines/noweb/store/ILabelsRepository'; -import { Sqlite3KVRepository } from './Sqlite3KVRepository'; +import { NOWEBSqlite3KVRepository } from './NOWEBSqlite3KVRepository'; export class Sqlite3LabelsRepository - extends Sqlite3KVRepository