Adjust to repository from session worker

This commit is contained in:
Davidson Gomes
2024-06-01 19:20:59 -03:00
parent f48f331d43
commit 9354af3bc7
5 changed files with 53 additions and 24 deletions

View File

@@ -146,7 +146,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.instance.qrcode = { count: 0 };
this.mobile = false;
this.recoveringMessages();
this.authStateProvider = new AuthStateProvider(this.configService, this.providerFiles);
this.authStateProvider = new AuthStateProvider(this.providerFiles);
}
private authStateProvider: AuthStateProvider;
@@ -1486,7 +1486,7 @@ export class BaileysStartupService extends ChannelStartupService {
});
const chat = chats.find((c) => c.id === data.association.chatId);
if (chat) {
let labels = [...chat.labels];
let labels = [...chat?.labels];
if (data.type === 'remove') {
labels = labels.filter((label) => label !== data.association.labelId);
} else if (data.type === 'add') {

View File

@@ -5,7 +5,15 @@ import { Db } from 'mongodb';
import { Collection } from 'mongoose';
import { join } from 'path';
import { Auth, CacheConf, ConfigService, Database, DelInstance, HttpServer } from '../../config/env.config';
import {
Auth,
CacheConf,
ConfigService,
Database,
DelInstance,
HttpServer,
ProviderSession,
} from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
import { NotFoundException } from '../../exceptions';
@@ -60,6 +68,8 @@ export class WAMonitoringService {
private readonly logger = new Logger(WAMonitoringService.name);
public readonly waInstances: Record<string, BaileysStartupService | BusinessStartupService> = {};
private readonly providerSession = Object.freeze(this.configService.get<ProviderSession>('PROVIDER'));
public delInstanceTime(instance: string) {
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
if (typeof time === 'number' && time > 0) {
@@ -259,13 +269,21 @@ export class WAMonitoringService {
}
this.logger.verbose('cleaning up instance in files: ' + instanceName);
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
if (this.providerSession?.ENABLED) {
await this.providerFiles.removeSession(instanceName);
} else {
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
}
}
public async cleaningStoreFiles(instanceName: string) {
if (!this.db.ENABLED) {
this.logger.verbose('cleaning store files instance: ' + instanceName);
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
if (this.providerSession?.ENABLED) {
await this.providerFiles.removeSession(instanceName);
} else {
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
}
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
execSync(`rm -rf ${join(STORE_DIR, 'contacts', instanceName)}`);
@@ -307,7 +325,9 @@ export class WAMonitoringService {
this.logger.verbose('Loading instances');
try {
if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
if (this.providerSession.ENABLED) {
await this.loadInstancesFromProvider();
} else if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
await this.loadInstancesFromRedis();
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
await this.loadInstancesFromDatabase();
@@ -405,6 +425,18 @@ export class WAMonitoringService {
}
}
private async loadInstancesFromProvider() {
this.logger.verbose('Provider in files enabled');
const [instances] = await this.providerFiles.allInstances();
if (!instances?.data) {
this.logger.verbose('No instances found');
return;
}
await Promise.all(instances?.data?.map(async (instanceName: string) => this.setInstance(instanceName)));
}
private async loadInstancesFromFiles() {
this.logger.verbose('Store in files enabled');
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });