Skip to content

Commit a7be7c3

Browse files
committed
fix: Adjust in store files
1 parent 5bd7dd3 commit a7be7c3

10 files changed

Lines changed: 75 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Fixed
88

99
* Adjusted set in webhook to go empty when enabled false
10+
* Adjust in store files
1011

1112
# 1.1.3 (2023-07-06 11:43)
1213

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {

src/config/path.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export const ROOT_DIR = process.cwd();
44
export const INSTANCE_DIR = join(ROOT_DIR, 'instances');
55
export const SRC_DIR = join(ROOT_DIR, 'src');
66
export const AUTH_DIR = join(ROOT_DIR, 'store', 'auth');
7+
export const STORE_DIR = join(ROOT_DIR, 'store');

src/whatsapp/abstract/abstract.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ROOT_DIR } from '../../config/path.config';
66
export type IInsert = { insertCount: number };
77

88
export interface IRepository {
9-
insert(data: any, saveDb?: boolean): Promise<IInsert>;
9+
insert(data: any, instanceName: string, saveDb?: boolean): Promise<IInsert>;
1010
find(query: any): Promise<any>;
1111
delete(query: any, force?: boolean): Promise<any>;
1212

@@ -45,7 +45,7 @@ export abstract class Repository implements IRepository {
4545
}
4646
};
4747

48-
public insert(data: any, saveDb = false): Promise<IInsert> {
48+
public insert(data: any, instanceName: string, saveDb = false): Promise<IInsert> {
4949
throw new Error('Method not implemented.');
5050
}
5151
public find(query: any): Promise<any> {

src/whatsapp/repository/chat.repository.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export class ChatRepository extends Repository {
1616
super(configService);
1717
}
1818

19-
public async insert(data: ChatRaw[], saveDb = false): Promise<IInsert> {
19+
public async insert(
20+
data: ChatRaw[],
21+
instanceName: string,
22+
saveDb = false,
23+
): Promise<IInsert> {
2024
if (data.length === 0) {
2125
return;
2226
}
@@ -32,7 +36,7 @@ export class ChatRepository extends Repository {
3236
if (store.CHATS) {
3337
data.forEach((chat) => {
3438
this.writeStore<ChatRaw>({
35-
path: join(this.storePath, 'chats', chat.owner),
39+
path: join(this.storePath, 'chats', instanceName),
3640
fileName: chat.id,
3741
data: chat,
3842
});

src/whatsapp/repository/contact.repository.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export class ContactRepository extends Repository {
1616
super(configService);
1717
}
1818

19-
public async insert(data: ContactRaw[], saveDb = false): Promise<IInsert> {
19+
public async insert(
20+
data: ContactRaw[],
21+
instanceName: string,
22+
saveDb = false,
23+
): Promise<IInsert> {
2024
if (data.length === 0) {
2125
return;
2226
}
@@ -32,7 +36,7 @@ export class ContactRepository extends Repository {
3236
if (store.CONTACTS) {
3337
data.forEach((contact) => {
3438
this.writeStore({
35-
path: join(this.storePath, 'contacts', contact.owner),
39+
path: join(this.storePath, 'contacts', instanceName),
3640
fileName: contact.id,
3741
data: contact,
3842
});

src/whatsapp/repository/message.repository.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ export class MessageRepository extends Repository {
1717
super(configService);
1818
}
1919

20-
public async insert(data: MessageRaw[], saveDb = false): Promise<IInsert> {
20+
public async insert(
21+
data: MessageRaw[],
22+
instanceName: string,
23+
saveDb = false,
24+
): Promise<IInsert> {
2125
if (!Array.isArray(data) || data.length === 0) {
2226
return;
2327
}
@@ -52,7 +56,7 @@ export class MessageRepository extends Repository {
5256
if (store.MESSAGES) {
5357
data.forEach((msg) =>
5458
this.writeStore<MessageRaw>({
55-
path: join(this.storePath, 'messages', msg.owner),
59+
path: join(this.storePath, 'messages', instanceName),
5660
fileName: msg.key.id,
5761
data: msg,
5862
}),

src/whatsapp/repository/messageUp.repository.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ export class MessageUpRepository extends Repository {
1717
super(configService);
1818
}
1919

20-
public async insert(data: MessageUpdateRaw[], saveDb?: boolean): Promise<IInsert> {
20+
public async insert(
21+
data: MessageUpdateRaw[],
22+
instanceName: string,
23+
saveDb?: boolean,
24+
): Promise<IInsert> {
2125
if (data.length === 0) {
2226
return;
2327
}
@@ -33,7 +37,7 @@ export class MessageUpRepository extends Repository {
3337
if (store.MESSAGE_UP) {
3438
data.forEach((update) => {
3539
this.writeStore<MessageUpdateRaw>({
36-
path: join(this.storePath, 'message-up', update.owner),
40+
path: join(this.storePath, 'message-up', instanceName),
3741
fileName: update.id,
3842
data: update,
3943
});

src/whatsapp/services/monitor.service.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { opendirSync, readdirSync, rmSync } from 'fs';
22
import { WAStartupService } from './whatsapp.service';
3-
import { INSTANCE_DIR } from '../../config/path.config';
3+
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
44
import EventEmitter2 from 'eventemitter2';
55
import { join } from 'path';
66
import { Logger } from '../../config/logger.config';
@@ -16,6 +16,7 @@ import { NotFoundException } from '../../exceptions';
1616
import { Db } from 'mongodb';
1717
import { initInstance } from '../whatsapp.module';
1818
import { RedisCache } from '../../db/redis.client';
19+
import { execSync } from 'child_process';
1920

2021
export class WAMonitoringService {
2122
constructor(
@@ -216,7 +217,23 @@ export class WAMonitoringService {
216217
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
217218
}
218219

219-
public async clearStoreFiles(instanceName: string) {}
220+
public async cleaningStoreFiles(instanceName: string) {
221+
this.logger.verbose('cleaning store files instance: ' + instanceName);
222+
223+
if (!this.db.ENABLED) {
224+
const instance = this.waInstances[instanceName];
225+
226+
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
227+
228+
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
229+
execSync(`rm -rf ${join(STORE_DIR, 'contacts', instanceName)}`);
230+
execSync(`rm -rf ${join(STORE_DIR, 'message-up', instanceName)}`);
231+
execSync(`rm -rf ${join(STORE_DIR, 'messages', instanceName)}`);
232+
233+
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
234+
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
235+
}
236+
}
220237

221238
public async loadInstance() {
222239
this.logger.verbose('load instances');
@@ -302,7 +319,7 @@ export class WAMonitoringService {
302319
try {
303320
this.logger.verbose('request cleaning up instance: ' + instanceName);
304321
this.cleaningUp(instanceName);
305-
this.clearStoreFiles(instanceName);
322+
this.cleaningStoreFiles(instanceName);
306323
} finally {
307324
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
308325
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import makeWASocket, {
3030
WAMessageUpdate,
3131
WASocket,
3232
getAggregateVotesInPollMessage,
33+
Browsers,
3334
} from '@whiskeysockets/baileys';
3435
import {
3536
Auth,
@@ -554,14 +555,14 @@ export class WAStartupService {
554555
`rm -rf ${join(
555556
this.storePath,
556557
key.toLowerCase().replace('_', '-'),
557-
this.instance.wuid,
558+
this.instance.name,
558559
)}/*.json`,
559560
);
560561
this.logger.verbose(
561562
`Cleaned ${join(
562563
this.storePath,
563564
key.toLowerCase().replace('_', '-'),
564-
this.instance.wuid,
565+
this.instance.name,
565566
)}/*.json`,
566567
);
567568
}
@@ -601,7 +602,8 @@ export class WAStartupService {
601602
const { version } = await fetchLatestBaileysVersion();
602603
this.logger.verbose('Baileys version: ' + version);
603604
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
604-
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
605+
// const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
606+
const browser: WABrowserDescription = Browsers.appropriate(session.CLIENT);
605607
this.logger.verbose('Browser: ' + JSON.stringify(browser));
606608

607609
const socketConfig: UserFacingSocketConfig = {
@@ -692,7 +694,11 @@ export class WAStartupService {
692694
await this.sendDataWebhook(Events.CHATS_UPSERT, chatsRaw);
693695

694696
this.logger.verbose('Inserting chats in database');
695-
await this.repository.chat.insert(chatsRaw, database.SAVE_DATA.CHATS);
697+
await this.repository.chat.insert(
698+
chatsRaw,
699+
this.instance.name,
700+
database.SAVE_DATA.CHATS,
701+
);
696702
},
697703

698704
'chats.update': async (
@@ -757,7 +763,11 @@ export class WAStartupService {
757763
await this.sendDataWebhook(Events.CONTACTS_UPSERT, contactsRaw);
758764

759765
this.logger.verbose('Inserting contacts in database');
760-
await this.repository.contact.insert(contactsRaw, database.SAVE_DATA.CONTACTS);
766+
await this.repository.contact.insert(
767+
contactsRaw,
768+
this.instance.name,
769+
database.SAVE_DATA.CONTACTS,
770+
);
761771
},
762772

763773
'contacts.update': async (contacts: Partial<Contact>[]) => {
@@ -808,7 +818,11 @@ export class WAStartupService {
808818
await this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
809819

810820
this.logger.verbose('Inserting chats in database');
811-
await this.repository.chat.insert(chatsRaw, database.SAVE_DATA.CHATS);
821+
await this.repository.chat.insert(
822+
chatsRaw,
823+
this.instance.name,
824+
database.SAVE_DATA.CHATS,
825+
);
812826
}
813827

814828
const messagesRaw: MessageRaw[] = [];
@@ -890,7 +904,11 @@ export class WAStartupService {
890904
await this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
891905

892906
this.logger.verbose('Inserting message in database');
893-
await this.repository.message.insert([messageRaw], database.SAVE_DATA.NEW_MESSAGE);
907+
await this.repository.message.insert(
908+
[messageRaw],
909+
this.instance.name,
910+
database.SAVE_DATA.NEW_MESSAGE,
911+
);
894912
},
895913

896914
'messages.update': async (args: WAMessageUpdate[], database: Database) => {
@@ -934,6 +952,7 @@ export class WAStartupService {
934952
this.logger.verbose('Inserting message in database');
935953
await this.repository.messageUpdate.insert(
936954
[message],
955+
this.instance.name,
937956
database.SAVE_DATA.MESSAGE_UPDATE,
938957
);
939958
}

0 commit comments

Comments
 (0)